gdiplus: Add test for GdipGetImageThumbnail.
[wine/hacks.git] / dlls / comctl32 / imagelist.c
blob63f0ea0425629f3f8d33ed400f78911ea582369c
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, ILS_ALPHA
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"
58 WINE_DEFAULT_DEBUG_CHANNEL(imagelist);
61 #define MAX_OVERLAYIMAGE 15
63 /* internal image list data used for Drag & Drop operations */
64 typedef struct
66 HWND hwnd;
67 HIMAGELIST himl;
68 /* position of the drag image relative to the window */
69 INT x;
70 INT y;
71 /* offset of the hotspot relative to the origin of the image */
72 INT dxHotspot;
73 INT dyHotspot;
74 /* is the drag image visible */
75 BOOL bShow;
76 /* saved background */
77 HBITMAP hbmBg;
78 } INTERNALDRAG;
80 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, FALSE, 0 };
82 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
83 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
84 static inline BOOL is_valid(HIMAGELIST himl);
87 * An imagelist with N images is tiled like this:
89 * N/4 ->
91 * 4 048C..
92 * 159D..
93 * | 26AE.N
94 * V 37BF.
97 #define TILE_COUNT 4
99 static inline UINT imagelist_height( UINT count )
101 return ((count + TILE_COUNT - 1)/TILE_COUNT);
104 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
106 pt->x = (index%TILE_COUNT) * himl->cx;
107 pt->y = (index/TILE_COUNT) * himl->cy;
110 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
112 sz->cx = himl->cx * TILE_COUNT;
113 sz->cy = imagelist_height( count ) * himl->cy;
117 * imagelist_copy_images()
119 * Copies a block of count images from offset src in the list to offset dest.
120 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
122 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
123 UINT src, UINT count, UINT dest )
125 POINT ptSrc, ptDest;
126 SIZE sz;
127 UINT i;
129 for ( i=0; i<TILE_COUNT; i++ )
131 imagelist_point_from_index( himl, src+i, &ptSrc );
132 imagelist_point_from_index( himl, dest+i, &ptDest );
133 sz.cx = himl->cx;
134 sz.cy = himl->cy * imagelist_height( count - i );
136 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
137 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
141 /*************************************************************************
142 * IMAGELIST_InternalExpandBitmaps [Internal]
144 * Expands the bitmaps of an image list by the given number of images.
146 * PARAMS
147 * himl [I] handle to image list
148 * nImageCount [I] number of images to add
150 * RETURNS
151 * nothing
153 * NOTES
154 * This function CANNOT be used to reduce the number of images.
156 static void
157 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
159 HDC hdcBitmap;
160 HBITMAP hbmNewBitmap, hbmNull;
161 INT nNewCount;
162 SIZE sz;
164 TRACE("%p has %d allocated %d images\n", himl, himl->cCurImage, himl->cMaxImage);
166 if (himl->cCurImage + nImageCount <= himl->cMaxImage)
167 return;
169 nNewCount = himl->cCurImage + max(nImageCount, himl->cGrow) + 1;
171 imagelist_get_bitmap_size(himl, nNewCount, &sz);
173 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
174 hdcBitmap = CreateCompatibleDC (0);
176 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
178 if (hbmNewBitmap == 0)
179 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
181 if (himl->cCurImage)
183 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
184 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
185 himl->hdcImage, 0, 0, SRCCOPY);
186 SelectObject (hdcBitmap, hbmNull);
188 SelectObject (himl->hdcImage, hbmNewBitmap);
189 DeleteObject (himl->hbmImage);
190 himl->hbmImage = hbmNewBitmap;
192 if (himl->flags & ILC_MASK)
194 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
196 if (hbmNewBitmap == 0)
197 ERR("creating new mask bitmap!\n");
199 if(himl->cCurImage)
201 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
202 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
203 himl->hdcMask, 0, 0, SRCCOPY);
204 SelectObject (hdcBitmap, hbmNull);
206 SelectObject (himl->hdcMask, hbmNewBitmap);
207 DeleteObject (himl->hbmMask);
208 himl->hbmMask = hbmNewBitmap;
211 himl->cMaxImage = nNewCount;
213 DeleteDC (hdcBitmap);
217 /*************************************************************************
218 * ImageList_Add [COMCTL32.@]
220 * Add an image or images to an image list.
222 * PARAMS
223 * himl [I] handle to image list
224 * hbmImage [I] handle to image bitmap
225 * hbmMask [I] handle to mask bitmap
227 * RETURNS
228 * Success: Index of the first new image.
229 * Failure: -1
232 INT WINAPI
233 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
235 HDC hdcBitmap, hdcTemp;
236 INT nFirstIndex, nImageCount, i;
237 BITMAP bmp;
238 HBITMAP hOldBitmap, hOldBitmapTemp;
239 POINT pt;
241 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
242 if (!is_valid(himl))
243 return -1;
245 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
246 return -1;
248 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
249 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
251 nImageCount = bmp.bmWidth / himl->cx;
253 TRACE("%p has %d images (%d x %d)\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight);
255 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
257 hdcBitmap = CreateCompatibleDC(0);
259 hOldBitmap = SelectObject(hdcBitmap, hbmImage);
261 for (i=0; i<nImageCount; i++)
263 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
265 /* Copy result to the imagelist
267 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
268 hdcBitmap, i*himl->cx, 0, SRCCOPY );
270 if (!himl->hbmMask)
271 continue;
273 hdcTemp = CreateCompatibleDC(0);
274 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
276 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
277 hdcTemp, i*himl->cx, 0, SRCCOPY );
279 SelectObject(hdcTemp, hOldBitmapTemp);
280 DeleteDC(hdcTemp);
282 /* Remove the background from the image
284 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
285 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
288 SelectObject(hdcBitmap, hOldBitmap);
289 DeleteDC(hdcBitmap);
291 nFirstIndex = himl->cCurImage;
292 himl->cCurImage += nImageCount;
294 return nFirstIndex;
298 /*************************************************************************
299 * ImageList_AddIcon [COMCTL32.@]
301 * Adds an icon to an image list.
303 * PARAMS
304 * himl [I] handle to image list
305 * hIcon [I] handle to icon
307 * RETURNS
308 * Success: index of the new image
309 * Failure: -1
311 #undef ImageList_AddIcon
312 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
314 return ImageList_ReplaceIcon (himl, -1, hIcon);
318 /*************************************************************************
319 * ImageList_AddMasked [COMCTL32.@]
321 * Adds an image or images to an image list and creates a mask from the
322 * specified bitmap using the mask color.
324 * PARAMS
325 * himl [I] handle to image list.
326 * hBitmap [I] handle to bitmap
327 * clrMask [I] mask color.
329 * RETURNS
330 * Success: Index of the first new image.
331 * Failure: -1
334 INT WINAPI
335 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
337 HDC hdcMask, hdcBitmap;
338 INT i, nIndex, nImageCount;
339 BITMAP bmp;
340 HBITMAP hOldBitmap;
341 HBITMAP hMaskBitmap=0;
342 COLORREF bkColor;
343 POINT pt;
345 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
346 if (!is_valid(himl))
347 return -1;
349 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
350 return -1;
352 if (himl->cx > 0)
353 nImageCount = bmp.bmWidth / himl->cx;
354 else
355 nImageCount = 0;
357 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
359 nIndex = himl->cCurImage;
360 himl->cCurImage += nImageCount;
362 hdcBitmap = CreateCompatibleDC(0);
363 hOldBitmap = SelectObject(hdcBitmap, hBitmap);
365 /* Create a temp Mask so we can remove the background of the Image */
366 hdcMask = CreateCompatibleDC(0);
367 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
368 SelectObject(hdcMask, hMaskBitmap);
370 /* create monochrome image to the mask bitmap */
371 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
372 SetBkColor (hdcBitmap, bkColor);
373 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
375 SetBkColor(hdcBitmap, RGB(255,255,255));
378 * Remove the background from the image
380 * WINDOWS BUG ALERT!!!!!!
381 * The statement below should not be done in common practice
382 * but this is how ImageList_AddMasked works in Windows.
383 * It overwrites the original bitmap passed, this was discovered
384 * by using the same bitmap to iterate the different styles
385 * on windows where it failed (BUT ImageList_Add is OK)
386 * This is here in case some apps rely on this bug
388 * Blt mode 0x220326 is NOTSRCAND
390 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
392 /* Copy result to the imagelist */
393 for (i=0; i<nImageCount; i++)
395 imagelist_point_from_index( himl, nIndex + i, &pt );
396 BitBlt(himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
397 hdcBitmap, i*himl->cx, 0, SRCCOPY);
398 BitBlt(himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
399 hdcMask, i*himl->cx, 0, SRCCOPY);
402 /* Clean up */
403 SelectObject(hdcBitmap, hOldBitmap);
404 DeleteDC(hdcBitmap);
405 DeleteObject(hMaskBitmap);
406 DeleteDC(hdcMask);
408 return nIndex;
412 /*************************************************************************
413 * ImageList_BeginDrag [COMCTL32.@]
415 * Creates a temporary image list that contains one image. It will be used
416 * as a drag image.
418 * PARAMS
419 * himlTrack [I] handle to the source image list
420 * iTrack [I] index of the drag image in the source image list
421 * dxHotspot [I] X position of the hot spot of the drag image
422 * dyHotspot [I] Y position of the hot spot of the drag image
424 * RETURNS
425 * Success: TRUE
426 * Failure: FALSE
429 BOOL WINAPI
430 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
431 INT dxHotspot, INT dyHotspot)
433 INT cx, cy;
435 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
436 dxHotspot, dyHotspot);
438 if (!is_valid(himlTrack))
439 return FALSE;
441 if (InternalDrag.himl)
442 ImageList_EndDrag ();
444 cx = himlTrack->cx;
445 cy = himlTrack->cy;
447 InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
448 if (InternalDrag.himl == NULL) {
449 WARN("Error creating drag image list!\n");
450 return FALSE;
453 InternalDrag.dxHotspot = dxHotspot;
454 InternalDrag.dyHotspot = dyHotspot;
456 /* copy image */
457 BitBlt (InternalDrag.himl->hdcImage, 0, 0, cx, cy, himlTrack->hdcImage, iTrack * cx, 0, SRCCOPY);
459 /* copy mask */
460 BitBlt (InternalDrag.himl->hdcMask, 0, 0, cx, cy, himlTrack->hdcMask, iTrack * cx, 0, SRCCOPY);
462 InternalDrag.himl->cCurImage = 1;
464 return TRUE;
468 /*************************************************************************
469 * ImageList_Copy [COMCTL32.@]
471 * Copies an image of the source image list to an image of the
472 * destination image list. Images can be copied or swapped.
474 * PARAMS
475 * himlDst [I] handle to the destination image list
476 * iDst [I] destination image index.
477 * himlSrc [I] handle to the source image list
478 * iSrc [I] source image index
479 * uFlags [I] flags for the copy operation
481 * RETURNS
482 * Success: TRUE
483 * Failure: FALSE
485 * NOTES
486 * Copying from one image list to another is possible. The original
487 * implementation just copies or swaps within one image list.
488 * Could this feature become a bug??? ;-)
491 BOOL WINAPI
492 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
493 INT iSrc, UINT uFlags)
495 POINT ptSrc, ptDst;
497 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
499 if (!is_valid(himlSrc) || !is_valid(himlDst))
500 return FALSE;
501 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
502 return FALSE;
503 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
504 return FALSE;
506 imagelist_point_from_index( himlDst, iDst, &ptDst );
507 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
509 if (uFlags & ILCF_SWAP) {
510 /* swap */
511 HDC hdcBmp;
512 HBITMAP hbmTempImage, hbmTempMask;
514 hdcBmp = CreateCompatibleDC (0);
516 /* create temporary bitmaps */
517 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
518 himlSrc->uBitsPixel, NULL);
519 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
520 1, NULL);
522 /* copy (and stretch) destination to temporary bitmaps.(save) */
523 /* image */
524 SelectObject (hdcBmp, hbmTempImage);
525 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
526 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
527 SRCCOPY);
528 /* mask */
529 SelectObject (hdcBmp, hbmTempMask);
530 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
531 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
532 SRCCOPY);
534 /* copy (and stretch) source to destination */
535 /* image */
536 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
537 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
538 SRCCOPY);
539 /* mask */
540 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
541 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
542 SRCCOPY);
544 /* copy (without stretching) temporary bitmaps to source (restore) */
545 /* mask */
546 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
547 hdcBmp, 0, 0, SRCCOPY);
549 /* image */
550 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
551 hdcBmp, 0, 0, SRCCOPY);
552 /* delete temporary bitmaps */
553 DeleteObject (hbmTempMask);
554 DeleteObject (hbmTempImage);
555 DeleteDC(hdcBmp);
557 else {
558 /* copy image */
559 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
560 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
561 SRCCOPY);
563 /* copy mask */
564 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
565 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
566 SRCCOPY);
569 return TRUE;
573 /*************************************************************************
574 * ImageList_Create [COMCTL32.@]
576 * Creates a new image list.
578 * PARAMS
579 * cx [I] image height
580 * cy [I] image width
581 * flags [I] creation flags
582 * cInitial [I] initial number of images in the image list
583 * cGrow [I] number of images by which image list grows
585 * RETURNS
586 * Success: Handle to the created image list
587 * Failure: NULL
589 HIMAGELIST WINAPI
590 ImageList_Create (INT cx, INT cy, UINT flags,
591 INT cInitial, INT cGrow)
593 HIMAGELIST himl;
594 INT nCount;
595 HBITMAP hbmTemp;
596 UINT ilc = (flags & 0xFE);
597 static const WORD aBitBlend25[] =
598 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
600 static const WORD aBitBlend50[] =
601 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
603 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
605 /* Create the IImageList interface for the image list */
606 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
607 return NULL;
609 cGrow = (cGrow < 4) ? 4 : (cGrow + 3) & ~3;
611 himl->cx = cx;
612 himl->cy = cy;
613 himl->flags = flags;
614 himl->cMaxImage = cInitial + 1;
615 himl->cInitial = cInitial;
616 himl->cGrow = cGrow;
617 himl->clrFg = CLR_DEFAULT;
618 himl->clrBk = CLR_NONE;
620 /* initialize overlay mask indices */
621 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
622 himl->nOvlIdx[nCount] = -1;
624 /* Create Image & Mask DCs */
625 himl->hdcImage = CreateCompatibleDC (0);
626 if (!himl->hdcImage)
627 goto cleanup;
628 if (himl->flags & ILC_MASK){
629 himl->hdcMask = CreateCompatibleDC(0);
630 if (!himl->hdcMask)
631 goto cleanup;
634 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
635 if (ilc == ILC_COLOR)
636 ilc = ILC_COLOR4;
638 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
639 himl->uBitsPixel = ilc;
640 else
641 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
643 if (himl->cMaxImage > 0) {
644 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
645 SelectObject(himl->hdcImage, himl->hbmImage);
646 } else
647 himl->hbmImage = 0;
649 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
650 SIZE sz;
652 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
653 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
654 if (himl->hbmMask == 0) {
655 ERR("Error creating mask bitmap!\n");
656 goto cleanup;
658 SelectObject(himl->hdcMask, himl->hbmMask);
660 else
661 himl->hbmMask = 0;
663 /* create blending brushes */
664 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
665 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
666 DeleteObject (hbmTemp);
668 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
669 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
670 DeleteObject (hbmTemp);
672 TRACE("created imagelist %p\n", himl);
673 return himl;
675 cleanup:
676 ImageList_Destroy(himl);
677 return NULL;
681 /*************************************************************************
682 * ImageList_Destroy [COMCTL32.@]
684 * Destroys an image list.
686 * PARAMS
687 * himl [I] handle to image list
689 * RETURNS
690 * Success: TRUE
691 * Failure: FALSE
694 BOOL WINAPI
695 ImageList_Destroy (HIMAGELIST himl)
697 if (!is_valid(himl))
698 return FALSE;
700 IImageList_Release((IImageList *) himl);
701 return TRUE;
705 /*************************************************************************
706 * ImageList_DragEnter [COMCTL32.@]
708 * Locks window update and displays the drag image at the given position.
710 * PARAMS
711 * hwndLock [I] handle of the window that owns the drag image.
712 * x [I] X position of the drag image.
713 * y [I] Y position of the drag image.
715 * RETURNS
716 * Success: TRUE
717 * Failure: FALSE
719 * NOTES
720 * The position of the drag image is relative to the window, not
721 * the client area.
724 BOOL WINAPI
725 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
727 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
729 if (!is_valid(InternalDrag.himl))
730 return FALSE;
732 if (hwndLock)
733 InternalDrag.hwnd = hwndLock;
734 else
735 InternalDrag.hwnd = GetDesktopWindow ();
737 InternalDrag.x = x;
738 InternalDrag.y = y;
740 /* draw the drag image and save the background */
741 if (!ImageList_DragShowNolock(TRUE)) {
742 return FALSE;
745 return TRUE;
749 /*************************************************************************
750 * ImageList_DragLeave [COMCTL32.@]
752 * Unlocks window update and hides the drag image.
754 * PARAMS
755 * hwndLock [I] handle of the window that owns the drag image.
757 * RETURNS
758 * Success: TRUE
759 * Failure: FALSE
762 BOOL WINAPI
763 ImageList_DragLeave (HWND hwndLock)
765 /* As we don't save drag info in the window this can lead to problems if
766 an app does not supply the same window as DragEnter */
767 /* if (hwndLock)
768 InternalDrag.hwnd = hwndLock;
769 else
770 InternalDrag.hwnd = GetDesktopWindow (); */
771 if(!hwndLock)
772 hwndLock = GetDesktopWindow();
773 if(InternalDrag.hwnd != hwndLock)
774 FIXME("DragLeave hWnd != DragEnter hWnd\n");
776 ImageList_DragShowNolock (FALSE);
778 return TRUE;
782 /*************************************************************************
783 * ImageList_InternalDragDraw [Internal]
785 * Draws the drag image.
787 * PARAMS
788 * hdc [I] device context to draw into.
789 * x [I] X position of the drag image.
790 * y [I] Y position of the drag image.
792 * RETURNS
793 * Success: TRUE
794 * Failure: FALSE
796 * NOTES
797 * The position of the drag image is relative to the window, not
798 * the client area.
802 static inline void
803 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
805 IMAGELISTDRAWPARAMS imldp;
807 ZeroMemory (&imldp, sizeof(imldp));
808 imldp.cbSize = sizeof(imldp);
809 imldp.himl = InternalDrag.himl;
810 imldp.i = 0;
811 imldp.hdcDst = hdc,
812 imldp.x = x;
813 imldp.y = y;
814 imldp.rgbBk = CLR_DEFAULT;
815 imldp.rgbFg = CLR_DEFAULT;
816 imldp.fStyle = ILD_NORMAL;
817 imldp.fState = ILS_ALPHA;
818 imldp.Frame = 128;
820 /* FIXME: instead of using the alpha blending, we should
821 * create a 50% mask, and draw it semitransparantly that way */
822 ImageList_DrawIndirect (&imldp);
825 /*************************************************************************
826 * ImageList_DragMove [COMCTL32.@]
828 * Moves the drag image.
830 * PARAMS
831 * x [I] X position of the drag image.
832 * y [I] Y position of the drag image.
834 * RETURNS
835 * Success: TRUE
836 * Failure: FALSE
838 * NOTES
839 * The position of the drag image is relative to the window, not
840 * the client area.
842 * BUGS
843 * The drag image should be drawn semitransparent.
846 BOOL WINAPI
847 ImageList_DragMove (INT x, INT y)
849 TRACE("(x=%d y=%d)\n", x, y);
851 if (!is_valid(InternalDrag.himl))
852 return FALSE;
854 /* draw/update the drag image */
855 if (InternalDrag.bShow) {
856 HDC hdcDrag;
857 HDC hdcOffScreen;
858 HDC hdcBg;
859 HBITMAP hbmOffScreen;
860 INT origNewX, origNewY;
861 INT origOldX, origOldY;
862 INT origRegX, origRegY;
863 INT sizeRegX, sizeRegY;
866 /* calculate the update region */
867 origNewX = x - InternalDrag.dxHotspot;
868 origNewY = y - InternalDrag.dyHotspot;
869 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
870 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
871 origRegX = min(origNewX, origOldX);
872 origRegY = min(origNewY, origOldY);
873 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
874 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
876 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
877 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
878 hdcOffScreen = CreateCompatibleDC(hdcDrag);
879 hdcBg = CreateCompatibleDC(hdcDrag);
881 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
882 SelectObject(hdcOffScreen, hbmOffScreen);
883 SelectObject(hdcBg, InternalDrag.hbmBg);
885 /* get the actual background of the update region */
886 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
887 origRegX, origRegY, SRCCOPY);
888 /* erase the old image */
889 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
890 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
891 SRCCOPY);
892 /* save the background */
893 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
894 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
895 /* draw the image */
896 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
897 origNewY - origRegY);
898 /* draw the update region to the screen */
899 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
900 hdcOffScreen, 0, 0, SRCCOPY);
902 DeleteDC(hdcBg);
903 DeleteDC(hdcOffScreen);
904 DeleteObject(hbmOffScreen);
905 ReleaseDC(InternalDrag.hwnd, hdcDrag);
908 /* update the image position */
909 InternalDrag.x = x;
910 InternalDrag.y = y;
912 return TRUE;
916 /*************************************************************************
917 * ImageList_DragShowNolock [COMCTL32.@]
919 * Shows or hides the drag image.
921 * PARAMS
922 * bShow [I] TRUE shows the drag image, FALSE hides it.
924 * RETURNS
925 * Success: TRUE
926 * Failure: FALSE
928 * BUGS
929 * The drag image should be drawn semitransparent.
932 BOOL WINAPI
933 ImageList_DragShowNolock (BOOL bShow)
935 HDC hdcDrag;
936 HDC hdcBg;
937 INT x, y;
939 if (!is_valid(InternalDrag.himl))
940 return FALSE;
942 TRACE("bShow=0x%X!\n", bShow);
944 /* DragImage is already visible/hidden */
945 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
946 return FALSE;
949 /* position of the origin of the DragImage */
950 x = InternalDrag.x - InternalDrag.dxHotspot;
951 y = InternalDrag.y - InternalDrag.dyHotspot;
953 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
954 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
955 if (!hdcDrag) {
956 return FALSE;
959 hdcBg = CreateCompatibleDC(hdcDrag);
960 if (!InternalDrag.hbmBg) {
961 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
962 InternalDrag.himl->cx, InternalDrag.himl->cy);
964 SelectObject(hdcBg, InternalDrag.hbmBg);
966 if (bShow) {
967 /* save the background */
968 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
969 hdcDrag, x, y, SRCCOPY);
970 /* show the image */
971 ImageList_InternalDragDraw(hdcDrag, x, y);
972 } else {
973 /* hide the image */
974 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
975 hdcBg, 0, 0, SRCCOPY);
978 InternalDrag.bShow = !InternalDrag.bShow;
980 DeleteDC(hdcBg);
981 ReleaseDC (InternalDrag.hwnd, hdcDrag);
982 return TRUE;
986 /*************************************************************************
987 * ImageList_Draw [COMCTL32.@]
989 * Draws an image.
991 * PARAMS
992 * himl [I] handle to image list
993 * i [I] image index
994 * hdc [I] handle to device context
995 * x [I] x position
996 * y [I] y position
997 * fStyle [I] drawing flags
999 * RETURNS
1000 * Success: TRUE
1001 * Failure: FALSE
1003 * SEE
1004 * ImageList_DrawEx.
1007 BOOL WINAPI
1008 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1010 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1011 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1015 /*************************************************************************
1016 * ImageList_DrawEx [COMCTL32.@]
1018 * Draws an image and allows to use extended drawing features.
1020 * PARAMS
1021 * himl [I] handle to image list
1022 * i [I] image index
1023 * hdc [I] handle to device context
1024 * x [I] X position
1025 * y [I] Y position
1026 * dx [I] X offset
1027 * dy [I] Y offset
1028 * rgbBk [I] background color
1029 * rgbFg [I] foreground color
1030 * fStyle [I] drawing flags
1032 * RETURNS
1033 * Success: TRUE
1034 * Failure: FALSE
1036 * NOTES
1037 * Calls ImageList_DrawIndirect.
1039 * SEE
1040 * ImageList_DrawIndirect.
1043 BOOL WINAPI
1044 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1045 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1046 UINT fStyle)
1048 IMAGELISTDRAWPARAMS imldp;
1050 ZeroMemory (&imldp, sizeof(imldp));
1051 imldp.cbSize = sizeof(imldp);
1052 imldp.himl = himl;
1053 imldp.i = i;
1054 imldp.hdcDst = hdc,
1055 imldp.x = x;
1056 imldp.y = y;
1057 imldp.cx = dx;
1058 imldp.cy = dy;
1059 imldp.rgbBk = rgbBk;
1060 imldp.rgbFg = rgbFg;
1061 imldp.fStyle = fStyle;
1063 return ImageList_DrawIndirect (&imldp);
1067 /*************************************************************************
1068 * ImageList_DrawIndirect [COMCTL32.@]
1070 * Draws an image using various parameters specified in pimldp.
1072 * PARAMS
1073 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1075 * RETURNS
1076 * Success: TRUE
1077 * Failure: FALSE
1080 BOOL WINAPI
1081 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1083 INT cx, cy, nOvlIdx;
1084 DWORD fState, dwRop;
1085 UINT fStyle;
1086 COLORREF oldImageBk, oldImageFg;
1087 HDC hImageDC, hImageListDC, hMaskListDC;
1088 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1089 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1090 HIMAGELIST himl;
1091 HBRUSH hOldBrush;
1092 POINT pt;
1094 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1095 if (!is_valid(himl)) return FALSE;
1096 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1098 imagelist_point_from_index( himl, pimldp->i, &pt );
1099 pt.x += pimldp->xBitmap;
1100 pt.y += pimldp->yBitmap;
1102 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1103 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1104 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1105 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1107 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1108 if( pimldp->rgbBk == CLR_NONE )
1109 bIsTransparent = TRUE;
1110 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1111 bIsTransparent = TRUE;
1112 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1113 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1115 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1116 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1118 /* we will use these DCs to access the images and masks in the ImageList */
1119 hImageListDC = himl->hdcImage;
1120 hMaskListDC = himl->hdcMask;
1122 /* these will accumulate the image and mask for the image we're drawing */
1123 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1124 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1125 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1127 /* Create a compatible DC. */
1128 if (!hImageListDC || !hImageDC || !hImageBmp ||
1129 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1130 goto cleanup;
1132 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1135 * To obtain a transparent look, background color should be set
1136 * to white and foreground color to black when blting the
1137 * monochrome mask.
1139 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1140 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1142 if (fState & ILS_ALPHA)
1144 BLENDFUNCTION func;
1145 COLORREF colour;
1147 func.BlendOp = AC_SRC_OVER;
1148 func.BlendFlags = 0;
1149 func.SourceConstantAlpha = pimldp->Frame;
1150 func.AlphaFormat = AC_SRC_ALPHA;
1151 if (bIsTransparent)
1153 bResult = GdiAlphaBlend( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy,
1154 hImageListDC, pt.x, pt.y, cx, cy, func );
1155 goto end;
1157 colour = pimldp->rgbBk;
1158 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1159 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1161 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1162 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1163 GdiAlphaBlend( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, cx, cy, func );
1164 DeleteObject (SelectObject (hImageDC, hOldBrush));
1165 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1166 goto end;
1170 * Draw the initial image
1172 if( bMask ) {
1173 if (himl->hbmMask) {
1174 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1175 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1176 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1177 DeleteObject (SelectObject (hImageDC, hOldBrush));
1178 if( bIsTransparent )
1180 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1181 bResult = TRUE;
1182 goto end;
1184 } else {
1185 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1186 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1187 SelectObject(hImageDC, hOldBrush);
1189 } else {
1190 /* blend the image with the needed solid background */
1191 COLORREF colour = RGB(0,0,0);
1193 if( !bIsTransparent )
1195 colour = pimldp->rgbBk;
1196 if( colour == CLR_DEFAULT )
1197 colour = himl->clrBk;
1198 if( colour == CLR_NONE )
1199 colour = GetBkColor(pimldp->hdcDst);
1202 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1203 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1204 if (himl->hbmMask)
1206 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1207 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1209 else
1210 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1211 DeleteObject (SelectObject (hImageDC, hOldBrush));
1214 /* Time for blending, if required */
1215 if (bBlend) {
1216 HBRUSH hBlendBrush;
1217 COLORREF clrBlend = pimldp->rgbFg;
1218 HDC hBlendMaskDC = hImageListDC;
1219 HBITMAP hOldBitmap;
1221 /* Create the blend Mask */
1222 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1223 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1224 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1225 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1226 SelectObject(hBlendMaskDC, hOldBrush);
1228 /* Modify the blend mask if an Image Mask exist */
1229 if(himl->hbmMask) {
1230 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1231 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1234 /* now apply blend to the current image given the BlendMask */
1235 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1236 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1237 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1238 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1239 DeleteObject(SelectObject(hImageDC, hOldBrush));
1240 SelectObject(hBlendMaskDC, hOldBitmap);
1243 /* Now do the overlay image, if any */
1244 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1245 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1246 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1247 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1248 POINT ptOvl;
1249 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1250 ptOvl.x += pimldp->xBitmap;
1251 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1252 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1253 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1257 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1258 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1259 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1261 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1262 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1263 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1265 /* now copy the image to the screen */
1266 dwRop = SRCCOPY;
1267 if (himl->hbmMask && bIsTransparent ) {
1268 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1269 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1270 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1271 SetBkColor(pimldp->hdcDst, oldDstBk);
1272 SetTextColor(pimldp->hdcDst, oldDstFg);
1273 dwRop = SRCPAINT;
1275 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1276 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1278 bResult = TRUE;
1279 end:
1280 /* cleanup the mess */
1281 SetBkColor(hImageDC, oldImageBk);
1282 SetTextColor(hImageDC, oldImageFg);
1283 SelectObject(hImageDC, hOldImageBmp);
1284 cleanup:
1285 DeleteObject(hBlendMaskBmp);
1286 DeleteObject(hImageBmp);
1287 DeleteDC(hImageDC);
1289 return bResult;
1293 /*************************************************************************
1294 * ImageList_Duplicate [COMCTL32.@]
1296 * Duplicates an image list.
1298 * PARAMS
1299 * himlSrc [I] source image list handle
1301 * RETURNS
1302 * Success: Handle of duplicated image list.
1303 * Failure: NULL
1306 HIMAGELIST WINAPI
1307 ImageList_Duplicate (HIMAGELIST himlSrc)
1309 HIMAGELIST himlDst;
1311 if (!is_valid(himlSrc)) {
1312 ERR("Invalid image list handle!\n");
1313 return NULL;
1316 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1317 himlSrc->cInitial, himlSrc->cGrow);
1319 if (himlDst)
1321 SIZE sz;
1323 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1324 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1325 himlSrc->hdcImage, 0, 0, SRCCOPY);
1327 if (himlDst->hbmMask)
1328 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1329 himlSrc->hdcMask, 0, 0, SRCCOPY);
1331 himlDst->cCurImage = himlSrc->cCurImage;
1332 himlDst->cMaxImage = himlSrc->cMaxImage;
1334 return himlDst;
1338 /*************************************************************************
1339 * ImageList_EndDrag [COMCTL32.@]
1341 * Finishes a drag operation.
1343 * PARAMS
1344 * no Parameters
1346 * RETURNS
1347 * Success: TRUE
1348 * Failure: FALSE
1351 VOID WINAPI
1352 ImageList_EndDrag (void)
1354 /* cleanup the InternalDrag struct */
1355 InternalDrag.hwnd = 0;
1356 ImageList_Destroy (InternalDrag.himl);
1357 InternalDrag.himl = 0;
1358 InternalDrag.x= 0;
1359 InternalDrag.y= 0;
1360 InternalDrag.dxHotspot = 0;
1361 InternalDrag.dyHotspot = 0;
1362 InternalDrag.bShow = FALSE;
1363 DeleteObject(InternalDrag.hbmBg);
1364 InternalDrag.hbmBg = 0;
1368 /*************************************************************************
1369 * ImageList_GetBkColor [COMCTL32.@]
1371 * Returns the background color of an image list.
1373 * PARAMS
1374 * himl [I] Image list handle.
1376 * RETURNS
1377 * Success: background color
1378 * Failure: CLR_NONE
1381 COLORREF WINAPI
1382 ImageList_GetBkColor (HIMAGELIST himl)
1384 return himl ? himl->clrBk : CLR_NONE;
1388 /*************************************************************************
1389 * ImageList_GetDragImage [COMCTL32.@]
1391 * Returns the handle to the internal drag image list.
1393 * PARAMS
1394 * ppt [O] Pointer to the drag position. Can be NULL.
1395 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1397 * RETURNS
1398 * Success: Handle of the drag image list.
1399 * Failure: NULL.
1402 HIMAGELIST WINAPI
1403 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1405 if (is_valid(InternalDrag.himl)) {
1406 if (ppt) {
1407 ppt->x = InternalDrag.x;
1408 ppt->y = InternalDrag.y;
1410 if (pptHotspot) {
1411 pptHotspot->x = InternalDrag.dxHotspot;
1412 pptHotspot->y = InternalDrag.dyHotspot;
1414 return (InternalDrag.himl);
1417 return NULL;
1421 /*************************************************************************
1422 * ImageList_GetFlags [COMCTL32.@]
1424 * Gets the flags of the specified image list.
1426 * PARAMS
1427 * himl [I] Handle to image list
1429 * RETURNS
1430 * Image list flags.
1432 * BUGS
1433 * Stub.
1436 DWORD WINAPI
1437 ImageList_GetFlags(HIMAGELIST himl)
1439 TRACE("%p\n", himl);
1441 return is_valid(himl) ? himl->flags : 0;
1445 /*************************************************************************
1446 * ImageList_GetIcon [COMCTL32.@]
1448 * Creates an icon from a masked image of an image list.
1450 * PARAMS
1451 * himl [I] handle to image list
1452 * i [I] image index
1453 * flags [I] drawing style flags
1455 * RETURNS
1456 * Success: icon handle
1457 * Failure: NULL
1460 HICON WINAPI
1461 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1463 ICONINFO ii;
1464 HICON hIcon;
1465 HBITMAP hOldDstBitmap;
1466 HDC hdcDst;
1467 POINT pt;
1469 TRACE("%p %d %d\n", himl, i, fStyle);
1470 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1472 ii.fIcon = TRUE;
1473 ii.xHotspot = 0;
1474 ii.yHotspot = 0;
1476 /* create colour bitmap */
1477 hdcDst = GetDC(0);
1478 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1479 ReleaseDC(0, hdcDst);
1481 hdcDst = CreateCompatibleDC(0);
1483 imagelist_point_from_index( himl, i, &pt );
1485 /* draw mask*/
1486 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1487 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1488 if (himl->hbmMask) {
1489 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1490 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1492 else
1493 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1495 /* draw image*/
1496 SelectObject (hdcDst, ii.hbmColor);
1497 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1498 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1501 * CreateIconIndirect requires us to deselect the bitmaps from
1502 * the DCs before calling
1504 SelectObject(hdcDst, hOldDstBitmap);
1506 hIcon = CreateIconIndirect (&ii);
1508 DeleteObject (ii.hbmMask);
1509 DeleteObject (ii.hbmColor);
1510 DeleteDC (hdcDst);
1512 return hIcon;
1516 /*************************************************************************
1517 * ImageList_GetIconSize [COMCTL32.@]
1519 * Retrieves the size of an image in an image list.
1521 * PARAMS
1522 * himl [I] handle to image list
1523 * cx [O] pointer to the image width.
1524 * cy [O] pointer to the image height.
1526 * RETURNS
1527 * Success: TRUE
1528 * Failure: FALSE
1530 * NOTES
1531 * All images in an image list have the same size.
1534 BOOL WINAPI
1535 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1537 if (!is_valid(himl))
1538 return FALSE;
1539 if ((himl->cx <= 0) || (himl->cy <= 0))
1540 return FALSE;
1542 if (cx)
1543 *cx = himl->cx;
1544 if (cy)
1545 *cy = himl->cy;
1547 return TRUE;
1551 /*************************************************************************
1552 * ImageList_GetImageCount [COMCTL32.@]
1554 * Returns the number of images in an image list.
1556 * PARAMS
1557 * himl [I] handle to image list
1559 * RETURNS
1560 * Success: Number of images.
1561 * Failure: 0
1564 INT WINAPI
1565 ImageList_GetImageCount (HIMAGELIST himl)
1567 if (!is_valid(himl))
1568 return 0;
1570 return himl->cCurImage;
1574 /*************************************************************************
1575 * ImageList_GetImageInfo [COMCTL32.@]
1577 * Returns information about an image in an image list.
1579 * PARAMS
1580 * himl [I] handle to image list
1581 * i [I] image index
1582 * pImageInfo [O] pointer to the image information
1584 * RETURNS
1585 * Success: TRUE
1586 * Failure: FALSE
1589 BOOL WINAPI
1590 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1592 POINT pt;
1594 if (!is_valid(himl) || (pImageInfo == NULL))
1595 return FALSE;
1596 if ((i < 0) || (i >= himl->cCurImage))
1597 return FALSE;
1599 pImageInfo->hbmImage = himl->hbmImage;
1600 pImageInfo->hbmMask = himl->hbmMask;
1602 imagelist_point_from_index( himl, i, &pt );
1603 pImageInfo->rcImage.top = pt.y;
1604 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1605 pImageInfo->rcImage.left = pt.x;
1606 pImageInfo->rcImage.right = pt.x + himl->cx;
1608 return TRUE;
1612 /*************************************************************************
1613 * ImageList_GetImageRect [COMCTL32.@]
1615 * Retrieves the rectangle of the specified image in an image list.
1617 * PARAMS
1618 * himl [I] handle to image list
1619 * i [I] image index
1620 * lpRect [O] pointer to the image rectangle
1622 * RETURNS
1623 * Success: TRUE
1624 * Failure: FALSE
1626 * NOTES
1627 * This is an UNDOCUMENTED function!!!
1630 BOOL WINAPI
1631 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1633 POINT pt;
1635 if (!is_valid(himl) || (lpRect == NULL))
1636 return FALSE;
1637 if ((i < 0) || (i >= himl->cCurImage))
1638 return FALSE;
1640 imagelist_point_from_index( himl, i, &pt );
1641 lpRect->left = pt.x;
1642 lpRect->top = pt.y;
1643 lpRect->right = pt.x + himl->cx;
1644 lpRect->bottom = pt.y + himl->cy;
1646 return TRUE;
1650 /*************************************************************************
1651 * ImageList_LoadImage [COMCTL32.@]
1652 * ImageList_LoadImageA [COMCTL32.@]
1654 * Creates an image list from a bitmap, icon or cursor.
1656 * See ImageList_LoadImageW.
1659 HIMAGELIST WINAPI
1660 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1661 COLORREF clrMask, UINT uType, UINT uFlags)
1663 HIMAGELIST himl;
1664 LPWSTR lpbmpW;
1665 DWORD len;
1667 if (IS_INTRESOURCE(lpbmp))
1668 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1669 uType, uFlags);
1671 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1672 lpbmpW = Alloc(len * sizeof(WCHAR));
1673 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1675 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1676 Free (lpbmpW);
1677 return himl;
1681 /*************************************************************************
1682 * ImageList_LoadImageW [COMCTL32.@]
1684 * Creates an image list from a bitmap, icon or cursor.
1686 * PARAMS
1687 * hi [I] instance handle
1688 * lpbmp [I] name or id of the image
1689 * cx [I] width of each image
1690 * cGrow [I] number of images to expand
1691 * clrMask [I] mask color
1692 * uType [I] type of image to load
1693 * uFlags [I] loading flags
1695 * RETURNS
1696 * Success: handle to the loaded image list
1697 * Failure: NULL
1699 * SEE
1700 * LoadImage ()
1703 HIMAGELIST WINAPI
1704 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1705 COLORREF clrMask, UINT uType, UINT uFlags)
1707 HIMAGELIST himl = NULL;
1708 HANDLE handle;
1709 INT nImageCount;
1711 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
1712 if (!handle) {
1713 WARN("Couldn't load image\n");
1714 return NULL;
1717 if (uType == IMAGE_BITMAP) {
1718 BITMAP bmp;
1719 GetObjectW (handle, sizeof(BITMAP), &bmp);
1721 /* To match windows behavior, if cx is set to zero and
1722 the flag DI_DEFAULTSIZE is specified, cx becomes the
1723 system metric value for icons. If the flag is not specified
1724 the function sets the size to the height of the bitmap */
1725 if (cx == 0)
1727 if (uFlags & DI_DEFAULTSIZE)
1728 cx = GetSystemMetrics (SM_CXICON);
1729 else
1730 cx = bmp.bmHeight;
1733 nImageCount = bmp.bmWidth / cx;
1735 himl = ImageList_Create (cx, bmp.bmHeight, ILC_MASK | ILC_COLOR,
1736 nImageCount, cGrow);
1737 if (!himl) {
1738 DeleteObject (handle);
1739 return NULL;
1741 ImageList_AddMasked (himl, handle, clrMask);
1743 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
1744 ICONINFO ii;
1745 BITMAP bmp;
1747 GetIconInfo (handle, &ii);
1748 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
1749 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
1750 ILC_MASK | ILC_COLOR, 1, cGrow);
1751 if (!himl) {
1752 DeleteObject (ii.hbmColor);
1753 DeleteObject (ii.hbmMask);
1754 DeleteObject (handle);
1755 return NULL;
1757 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
1758 DeleteObject (ii.hbmColor);
1759 DeleteObject (ii.hbmMask);
1762 DeleteObject (handle);
1764 return himl;
1768 /*************************************************************************
1769 * ImageList_Merge [COMCTL32.@]
1771 * Create an image list containing a merged image from two image lists.
1773 * PARAMS
1774 * himl1 [I] handle to first image list
1775 * i1 [I] first image index
1776 * himl2 [I] handle to second image list
1777 * i2 [I] second image index
1778 * dx [I] X offset of the second image relative to the first.
1779 * dy [I] Y offset of the second image relative to the first.
1781 * RETURNS
1782 * Success: The newly created image list. It contains a single image
1783 * consisting of the second image merged with the first.
1784 * Failure: NULL, if either himl1 or himl2 are invalid.
1786 * NOTES
1787 * - The returned image list should be deleted by the caller using
1788 * ImageList_Destroy() when it is no longer required.
1789 * - If either i1 or i2 are not valid image indices they will be treated
1790 * as a blank image.
1792 HIMAGELIST WINAPI
1793 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
1794 INT dx, INT dy)
1796 HIMAGELIST himlDst = NULL;
1797 INT cxDst, cyDst;
1798 INT xOff1, yOff1, xOff2, yOff2;
1799 POINT pt1, pt2;
1801 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
1802 i2, dx, dy);
1804 if (!is_valid(himl1) || !is_valid(himl2))
1805 return NULL;
1807 if (dx > 0) {
1808 cxDst = max (himl1->cx, dx + himl2->cx);
1809 xOff1 = 0;
1810 xOff2 = dx;
1812 else if (dx < 0) {
1813 cxDst = max (himl2->cx, himl1->cx - dx);
1814 xOff1 = -dx;
1815 xOff2 = 0;
1817 else {
1818 cxDst = max (himl1->cx, himl2->cx);
1819 xOff1 = 0;
1820 xOff2 = 0;
1823 if (dy > 0) {
1824 cyDst = max (himl1->cy, dy + himl2->cy);
1825 yOff1 = 0;
1826 yOff2 = dy;
1828 else if (dy < 0) {
1829 cyDst = max (himl2->cy, himl1->cy - dy);
1830 yOff1 = -dy;
1831 yOff2 = 0;
1833 else {
1834 cyDst = max (himl1->cy, himl2->cy);
1835 yOff1 = 0;
1836 yOff2 = 0;
1839 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | ILC_COLOR, 1, 1);
1841 if (himlDst)
1843 imagelist_point_from_index( himl1, i1, &pt1 );
1844 imagelist_point_from_index( himl1, i2, &pt2 );
1846 /* copy image */
1847 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
1848 if (i1 >= 0 && i1 < himl1->cCurImage)
1849 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
1850 if (i2 >= 0 && i2 < himl2->cCurImage)
1852 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
1853 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
1856 /* copy mask */
1857 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
1858 if (i1 >= 0 && i1 < himl1->cCurImage)
1859 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
1860 if (i2 >= 0 && i2 < himl2->cCurImage)
1861 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
1863 himlDst->cCurImage = 1;
1866 return himlDst;
1870 /***********************************************************************
1871 * DIB_GetDIBWidthBytes
1873 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
1875 static int DIB_GetDIBWidthBytes( int width, int depth )
1877 int words;
1879 switch(depth)
1881 case 1: words = (width + 31) / 32; break;
1882 case 4: words = (width + 7) / 8; break;
1883 case 8: words = (width + 3) / 4; break;
1884 case 15:
1885 case 16: words = (width + 1) / 2; break;
1886 case 24: words = (width * 3 + 3)/4; break;
1888 default:
1889 WARN("(%d): Unsupported depth\n", depth );
1890 /* fall through */
1891 case 32:
1892 words = width;
1893 break;
1895 return 4 * words;
1898 /***********************************************************************
1899 * DIB_GetDIBImageBytes
1901 * Return the number of bytes used to hold the image in a DIB bitmap.
1903 static int DIB_GetDIBImageBytes( int width, int height, int depth )
1905 return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
1909 /* helper for ImageList_Read, see comments below */
1910 static BOOL _read_bitmap(HDC hdcIml, LPSTREAM pstm)
1912 BITMAPFILEHEADER bmfh;
1913 int bitsperpixel, palspace;
1914 char bmi_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
1915 LPBITMAPINFO bmi = (LPBITMAPINFO)bmi_buf;
1916 int result = FALSE;
1917 LPBYTE bits = NULL;
1919 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
1920 return FALSE;
1922 if (bmfh.bfType != (('M'<<8)|'B'))
1923 return FALSE;
1925 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
1926 return FALSE;
1928 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
1929 return FALSE;
1931 TRACE("width %u, height %u, planes %u, bpp %u\n",
1932 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
1933 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
1935 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
1936 if (bitsperpixel<=8)
1937 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
1938 else
1939 palspace = 0;
1941 bmi->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bitsperpixel);
1943 /* read the palette right after the end of the bitmapinfoheader */
1944 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
1945 goto error;
1947 bits = Alloc(bmi->bmiHeader.biSizeImage);
1948 if (!bits)
1949 goto error;
1950 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
1951 goto error;
1953 if (!StretchDIBits(hdcIml, 0, 0, bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
1954 0, 0, bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
1955 bits, bmi, DIB_RGB_COLORS, SRCCOPY))
1956 goto error;
1957 result = TRUE;
1959 error:
1960 Free(bits);
1961 return result;
1964 /*************************************************************************
1965 * ImageList_Read [COMCTL32.@]
1967 * Reads an image list from a stream.
1969 * PARAMS
1970 * pstm [I] pointer to a stream
1972 * RETURNS
1973 * Success: handle to image list
1974 * Failure: NULL
1976 * The format is like this:
1977 * ILHEAD ilheadstruct;
1979 * for the color image part:
1980 * BITMAPFILEHEADER bmfh;
1981 * BITMAPINFOHEADER bmih;
1982 * only if it has a palette:
1983 * RGBQUAD rgbs[nr_of_paletted_colors];
1985 * BYTE colorbits[imagesize];
1987 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
1988 * BITMAPFILEHEADER bmfh_mask;
1989 * BITMAPINFOHEADER bmih_mask;
1990 * only if it has a palette (it usually does not):
1991 * RGBQUAD rgbs[nr_of_paletted_colors];
1993 * BYTE maskbits[imagesize];
1995 HIMAGELIST WINAPI ImageList_Read (LPSTREAM pstm)
1997 ILHEAD ilHead;
1998 HIMAGELIST himl;
1999 int i;
2001 TRACE("%p\n", pstm);
2003 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2004 return NULL;
2005 if (ilHead.usMagic != (('L' << 8) | 'I'))
2006 return NULL;
2007 if (ilHead.usVersion != 0x101) /* probably version? */
2008 return NULL;
2010 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2011 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2013 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2014 if (!himl)
2015 return NULL;
2017 if (!_read_bitmap(himl->hdcImage, pstm))
2019 WARN("failed to read bitmap from stream\n");
2020 return NULL;
2022 if (ilHead.flags & ILC_MASK)
2024 if (!_read_bitmap(himl->hdcMask, pstm))
2026 WARN("failed to read mask bitmap from stream\n");
2027 return NULL;
2031 himl->cCurImage = ilHead.cCurImage;
2032 himl->cMaxImage = ilHead.cMaxImage;
2034 ImageList_SetBkColor(himl,ilHead.bkcolor);
2035 for (i=0;i<4;i++)
2036 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2037 return himl;
2041 /*************************************************************************
2042 * ImageList_Remove [COMCTL32.@]
2044 * Removes an image from an image list
2046 * PARAMS
2047 * himl [I] image list handle
2048 * i [I] image index
2050 * RETURNS
2051 * Success: TRUE
2052 * Failure: FALSE
2054 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2055 * images without creating a new bitmap.
2057 BOOL WINAPI
2058 ImageList_Remove (HIMAGELIST himl, INT i)
2060 HBITMAP hbmNewImage, hbmNewMask;
2061 HDC hdcBmp;
2062 SIZE sz;
2064 TRACE("(himl=%p i=%d)\n", himl, i);
2066 if (!is_valid(himl)) {
2067 ERR("Invalid image list handle!\n");
2068 return FALSE;
2071 if ((i < -1) || (i >= himl->cCurImage)) {
2072 TRACE("index out of range! %d\n", i);
2073 return FALSE;
2076 if (i == -1) {
2077 INT nCount;
2079 /* remove all */
2080 if (himl->cCurImage == 0) {
2081 /* remove all on empty ImageList is allowed */
2082 TRACE("remove all on empty ImageList!\n");
2083 return TRUE;
2086 himl->cMaxImage = himl->cInitial + himl->cGrow - 1;
2087 himl->cCurImage = 0;
2088 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2089 himl->nOvlIdx[nCount] = -1;
2091 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2092 SelectObject (himl->hdcImage, hbmNewImage);
2093 DeleteObject (himl->hbmImage);
2094 himl->hbmImage = hbmNewImage;
2096 if (himl->hbmMask) {
2098 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2099 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2100 SelectObject (himl->hdcMask, hbmNewMask);
2101 DeleteObject (himl->hbmMask);
2102 himl->hbmMask = hbmNewMask;
2105 else {
2106 /* delete one image */
2107 TRACE("Remove single image! %d\n", i);
2109 /* create new bitmap(s) */
2110 TRACE(" - Number of images: %d / %d (Old/New)\n",
2111 himl->cCurImage, himl->cCurImage - 1);
2113 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2115 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2116 if (himl->hbmMask)
2117 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2118 else
2119 hbmNewMask = 0; /* Just to keep compiler happy! */
2121 hdcBmp = CreateCompatibleDC (0);
2123 /* copy all images and masks prior to the "removed" image */
2124 if (i > 0) {
2125 TRACE("Pre image copy: Copy %d images\n", i);
2127 SelectObject (hdcBmp, hbmNewImage);
2128 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2130 if (himl->hbmMask) {
2131 SelectObject (hdcBmp, hbmNewMask);
2132 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2136 /* copy all images and masks behind the removed image */
2137 if (i < himl->cCurImage - 1) {
2138 TRACE("Post image copy!\n");
2140 SelectObject (hdcBmp, hbmNewImage);
2141 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2142 (himl->cCurImage - i), i );
2144 if (himl->hbmMask) {
2145 SelectObject (hdcBmp, hbmNewMask);
2146 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2147 (himl->cCurImage - i), i );
2151 DeleteDC (hdcBmp);
2153 /* delete old images and insert new ones */
2154 SelectObject (himl->hdcImage, hbmNewImage);
2155 DeleteObject (himl->hbmImage);
2156 himl->hbmImage = hbmNewImage;
2157 if (himl->hbmMask) {
2158 SelectObject (himl->hdcMask, hbmNewMask);
2159 DeleteObject (himl->hbmMask);
2160 himl->hbmMask = hbmNewMask;
2163 himl->cCurImage--;
2166 return TRUE;
2170 /*************************************************************************
2171 * ImageList_Replace [COMCTL32.@]
2173 * Replaces an image in an image list with a new image.
2175 * PARAMS
2176 * himl [I] handle to image list
2177 * i [I] image index
2178 * hbmImage [I] handle to image bitmap
2179 * hbmMask [I] handle to mask bitmap. Can be NULL.
2181 * RETURNS
2182 * Success: TRUE
2183 * Failure: FALSE
2186 BOOL WINAPI
2187 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2188 HBITMAP hbmMask)
2190 HDC hdcImage;
2191 BITMAP bmp;
2192 HBITMAP hOldBitmap;
2193 POINT pt;
2195 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2197 if (!is_valid(himl)) {
2198 ERR("Invalid image list handle!\n");
2199 return FALSE;
2202 if ((i >= himl->cMaxImage) || (i < 0)) {
2203 ERR("Invalid image index!\n");
2204 return FALSE;
2207 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2208 return FALSE;
2210 hdcImage = CreateCompatibleDC (0);
2212 /* Replace Image */
2213 hOldBitmap = SelectObject (hdcImage, hbmImage);
2215 imagelist_point_from_index(himl, i, &pt);
2216 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2217 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2219 if (himl->hbmMask)
2221 HDC hdcTemp;
2222 HBITMAP hOldBitmapTemp;
2224 hdcTemp = CreateCompatibleDC(0);
2225 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2227 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2228 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2229 SelectObject(hdcTemp, hOldBitmapTemp);
2230 DeleteDC(hdcTemp);
2232 /* Remove the background from the image
2234 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2235 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2238 SelectObject (hdcImage, hOldBitmap);
2239 DeleteDC (hdcImage);
2241 return TRUE;
2245 /*************************************************************************
2246 * ImageList_ReplaceIcon [COMCTL32.@]
2248 * Replaces an image in an image list using an icon.
2250 * PARAMS
2251 * himl [I] handle to image list
2252 * i [I] image index
2253 * hIcon [I] handle to icon
2255 * RETURNS
2256 * Success: index of the replaced image
2257 * Failure: -1
2260 INT WINAPI
2261 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2263 HDC hdcImage;
2264 HICON hBestFitIcon;
2265 HBITMAP hbmOldSrc;
2266 ICONINFO ii;
2267 BITMAP bmp;
2268 BOOL ret;
2269 POINT pt;
2271 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2273 if (!is_valid(himl)) {
2274 ERR("invalid image list\n");
2275 return -1;
2277 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2278 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2279 return -1;
2282 hBestFitIcon = CopyImage(
2283 hIcon, IMAGE_ICON,
2284 himl->cx, himl->cy,
2285 LR_COPYFROMRESOURCE);
2286 /* the above will fail if the icon wasn't loaded from a resource, so try
2287 * again without LR_COPYFROMRESOURCE flag */
2288 if (!hBestFitIcon)
2289 hBestFitIcon = CopyImage(
2290 hIcon, IMAGE_ICON,
2291 himl->cx, himl->cy,
2293 if (!hBestFitIcon)
2294 return -1;
2296 ret = GetIconInfo (hBestFitIcon, &ii);
2297 if (!ret) {
2298 DestroyIcon(hBestFitIcon);
2299 return -1;
2302 ret = GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2303 if (!ret) {
2304 ERR("couldn't get mask bitmap info\n");
2305 if (ii.hbmColor)
2306 DeleteObject (ii.hbmColor);
2307 if (ii.hbmMask)
2308 DeleteObject (ii.hbmMask);
2309 DestroyIcon(hBestFitIcon);
2310 return -1;
2313 if (nIndex == -1) {
2314 if (himl->cCurImage + 1 > himl->cMaxImage)
2315 IMAGELIST_InternalExpandBitmaps(himl, 1);
2317 nIndex = himl->cCurImage;
2318 himl->cCurImage++;
2321 hdcImage = CreateCompatibleDC (0);
2322 TRACE("hdcImage=%p\n", hdcImage);
2323 if (hdcImage == 0)
2324 ERR("invalid hdcImage!\n");
2326 imagelist_point_from_index(himl, nIndex, &pt);
2328 SetTextColor(himl->hdcImage, RGB(0,0,0));
2329 SetBkColor (himl->hdcImage, RGB(255,255,255));
2331 if (ii.hbmColor)
2333 hbmOldSrc = SelectObject (hdcImage, ii.hbmColor);
2334 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2335 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2336 if (himl->hbmMask)
2338 SelectObject (hdcImage, ii.hbmMask);
2339 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2340 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2343 else
2345 UINT height = bmp.bmHeight / 2;
2346 hbmOldSrc = SelectObject (hdcImage, ii.hbmMask);
2347 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2348 hdcImage, 0, height, bmp.bmWidth, height, SRCCOPY);
2349 if (himl->hbmMask)
2350 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2351 hdcImage, 0, 0, bmp.bmWidth, height, SRCCOPY);
2354 SelectObject (hdcImage, hbmOldSrc);
2356 DestroyIcon(hBestFitIcon);
2357 if (hdcImage)
2358 DeleteDC (hdcImage);
2359 if (ii.hbmColor)
2360 DeleteObject (ii.hbmColor);
2361 if (ii.hbmMask)
2362 DeleteObject (ii.hbmMask);
2364 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2365 return nIndex;
2369 /*************************************************************************
2370 * ImageList_SetBkColor [COMCTL32.@]
2372 * Sets the background color of an image list.
2374 * PARAMS
2375 * himl [I] handle to image list
2376 * clrBk [I] background color
2378 * RETURNS
2379 * Success: previous background color
2380 * Failure: CLR_NONE
2383 COLORREF WINAPI
2384 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2386 COLORREF clrOldBk;
2388 if (!is_valid(himl))
2389 return CLR_NONE;
2391 clrOldBk = himl->clrBk;
2392 himl->clrBk = clrBk;
2393 return clrOldBk;
2397 /*************************************************************************
2398 * ImageList_SetDragCursorImage [COMCTL32.@]
2400 * Combines the specified image with the current drag image
2402 * PARAMS
2403 * himlDrag [I] handle to drag image list
2404 * iDrag [I] drag image index
2405 * dxHotspot [I] X position of the hot spot
2406 * dyHotspot [I] Y position of the hot spot
2408 * RETURNS
2409 * Success: TRUE
2410 * Failure: FALSE
2412 * NOTES
2413 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2414 * to do with a hotspot but are only the offset of the origin of the new
2415 * image relative to the origin of the old image.
2417 * - When this function is called and the drag image is visible, a
2418 * short flickering occurs but this matches the Win9x behavior. It is
2419 * possible to fix the flickering using code like in ImageList_DragMove.
2422 BOOL WINAPI
2423 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2424 INT dxHotspot, INT dyHotspot)
2426 HIMAGELIST himlTemp;
2427 BOOL visible;
2429 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2430 return FALSE;
2432 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2433 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2435 visible = InternalDrag.bShow;
2437 himlTemp = ImageList_Merge (InternalDrag.himl, 0, himlDrag, iDrag,
2438 dxHotspot, dyHotspot);
2440 if (visible) {
2441 /* hide the drag image */
2442 ImageList_DragShowNolock(FALSE);
2444 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2445 (InternalDrag.himl->cy != himlTemp->cy)) {
2446 /* the size of the drag image changed, invalidate the buffer */
2447 DeleteObject(InternalDrag.hbmBg);
2448 InternalDrag.hbmBg = 0;
2451 ImageList_Destroy (InternalDrag.himl);
2452 InternalDrag.himl = himlTemp;
2454 if (visible) {
2455 /* show the drag image */
2456 ImageList_DragShowNolock(TRUE);
2459 return TRUE;
2463 /*************************************************************************
2464 * ImageList_SetFilter [COMCTL32.@]
2466 * Sets a filter (or does something completely different)!!???
2467 * It removes 12 Bytes from the stack (3 Parameters).
2469 * PARAMS
2470 * himl [I] SHOULD be a handle to image list
2471 * i [I] COULD be an index?
2472 * dwFilter [I] ???
2474 * RETURNS
2475 * Success: TRUE ???
2476 * Failure: FALSE ???
2478 * BUGS
2479 * This is an UNDOCUMENTED function!!!!
2480 * empty stub.
2483 BOOL WINAPI
2484 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2486 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2488 return FALSE;
2492 /*************************************************************************
2493 * ImageList_SetFlags [COMCTL32.@]
2495 * Sets the image list flags.
2497 * PARAMS
2498 * himl [I] Handle to image list
2499 * flags [I] Flags to set
2501 * RETURNS
2502 * Old flags?
2504 * BUGS
2505 * Stub.
2508 DWORD WINAPI
2509 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2511 FIXME("(%p %08x):empty stub\n", himl, flags);
2512 return 0;
2516 /*************************************************************************
2517 * ImageList_SetIconSize [COMCTL32.@]
2519 * Sets the image size of the bitmap and deletes all images.
2521 * PARAMS
2522 * himl [I] handle to image list
2523 * cx [I] image width
2524 * cy [I] image height
2526 * RETURNS
2527 * Success: TRUE
2528 * Failure: FALSE
2531 BOOL WINAPI
2532 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2534 INT nCount;
2535 HBITMAP hbmNew;
2537 if (!is_valid(himl))
2538 return FALSE;
2540 /* remove all images */
2541 himl->cMaxImage = himl->cInitial + 1;
2542 himl->cCurImage = 0;
2543 himl->cx = cx;
2544 himl->cy = cy;
2546 /* initialize overlay mask indices */
2547 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2548 himl->nOvlIdx[nCount] = -1;
2550 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2551 SelectObject (himl->hdcImage, hbmNew);
2552 DeleteObject (himl->hbmImage);
2553 himl->hbmImage = hbmNew;
2555 if (himl->hbmMask) {
2556 SIZE sz;
2557 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2558 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2559 SelectObject (himl->hdcMask, hbmNew);
2560 DeleteObject (himl->hbmMask);
2561 himl->hbmMask = hbmNew;
2564 return TRUE;
2568 /*************************************************************************
2569 * ImageList_SetImageCount [COMCTL32.@]
2571 * Resizes an image list to the specified number of images.
2573 * PARAMS
2574 * himl [I] handle to image list
2575 * iImageCount [I] number of images in the image list
2577 * RETURNS
2578 * Success: TRUE
2579 * Failure: FALSE
2582 BOOL WINAPI
2583 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2585 HDC hdcBitmap;
2586 HBITMAP hbmNewBitmap, hbmOld;
2587 INT nNewCount, nCopyCount;
2589 TRACE("%p %d\n",himl,iImageCount);
2591 if (!is_valid(himl))
2592 return FALSE;
2593 if (himl->cMaxImage > iImageCount)
2595 himl->cCurImage = iImageCount;
2596 /* TODO: shrink the bitmap when cMaxImage-cCurImage>cGrow ? */
2597 return TRUE;
2600 nNewCount = iImageCount + himl->cGrow;
2601 nCopyCount = min(himl->cCurImage, iImageCount);
2603 hdcBitmap = CreateCompatibleDC (0);
2605 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2607 if (hbmNewBitmap != 0)
2609 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2610 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2611 SelectObject (hdcBitmap, hbmOld);
2613 /* FIXME: delete 'empty' image space? */
2615 SelectObject (himl->hdcImage, hbmNewBitmap);
2616 DeleteObject (himl->hbmImage);
2617 himl->hbmImage = hbmNewBitmap;
2619 else
2620 ERR("Could not create new image bitmap !\n");
2622 if (himl->hbmMask)
2624 SIZE sz;
2625 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2626 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2627 if (hbmNewBitmap != 0)
2629 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2630 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2631 SelectObject (hdcBitmap, hbmOld);
2633 /* FIXME: delete 'empty' image space? */
2635 SelectObject (himl->hdcMask, hbmNewBitmap);
2636 DeleteObject (himl->hbmMask);
2637 himl->hbmMask = hbmNewBitmap;
2639 else
2640 ERR("Could not create new mask bitmap!\n");
2643 DeleteDC (hdcBitmap);
2645 /* Update max image count and current image count */
2646 himl->cMaxImage = nNewCount;
2647 himl->cCurImage = iImageCount;
2649 return TRUE;
2653 /*************************************************************************
2654 * ImageList_SetOverlayImage [COMCTL32.@]
2656 * Assigns an overlay mask index to an existing image in an image list.
2658 * PARAMS
2659 * himl [I] handle to image list
2660 * iImage [I] image index
2661 * iOverlay [I] overlay mask index
2663 * RETURNS
2664 * Success: TRUE
2665 * Failure: FALSE
2668 BOOL WINAPI
2669 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2671 if (!is_valid(himl))
2672 return FALSE;
2673 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2674 return FALSE;
2675 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2676 return FALSE;
2677 himl->nOvlIdx[iOverlay - 1] = iImage;
2678 return TRUE;
2683 /* helper for ImageList_Write - write bitmap to pstm
2684 * currently everything is written as 24 bit RGB, except masks
2686 static BOOL
2687 _write_bitmap(HBITMAP hBitmap, LPSTREAM pstm)
2689 LPBITMAPFILEHEADER bmfh;
2690 LPBITMAPINFOHEADER bmih;
2691 LPBYTE data = NULL, lpBits;
2692 BITMAP bm;
2693 INT bitCount, sizeImage, offBits, totalSize;
2694 HDC xdc;
2695 BOOL result = FALSE;
2697 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2698 return FALSE;
2700 bitCount = bm.bmBitsPixel == 1 ? 1 : 24;
2701 sizeImage = DIB_GetDIBImageBytes(bm.bmWidth, bm.bmHeight, bitCount);
2703 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
2704 if(bitCount != 24)
2705 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
2706 offBits = totalSize;
2707 totalSize += sizeImage;
2709 data = Alloc(totalSize);
2710 bmfh = (LPBITMAPFILEHEADER)data;
2711 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
2712 lpBits = data + offBits;
2714 /* setup BITMAPFILEHEADER */
2715 bmfh->bfType = (('M' << 8) | 'B');
2716 bmfh->bfSize = offBits;
2717 bmfh->bfReserved1 = 0;
2718 bmfh->bfReserved2 = 0;
2719 bmfh->bfOffBits = offBits;
2721 /* setup BITMAPINFOHEADER */
2722 bmih->biSize = sizeof(BITMAPINFOHEADER);
2723 bmih->biWidth = bm.bmWidth;
2724 bmih->biHeight = bm.bmHeight;
2725 bmih->biPlanes = 1;
2726 bmih->biBitCount = bitCount;
2727 bmih->biCompression = BI_RGB;
2728 bmih->biSizeImage = sizeImage;
2729 bmih->biXPelsPerMeter = 0;
2730 bmih->biYPelsPerMeter = 0;
2731 bmih->biClrUsed = 0;
2732 bmih->biClrImportant = 0;
2734 xdc = GetDC(0);
2735 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
2736 ReleaseDC(0, xdc);
2737 if (!result)
2738 goto failed;
2740 TRACE("width %u, height %u, planes %u, bpp %u\n",
2741 bmih->biWidth, bmih->biHeight,
2742 bmih->biPlanes, bmih->biBitCount);
2744 if(bitCount == 1) {
2745 /* Hack. */
2746 LPBITMAPINFO inf = (LPBITMAPINFO)bmih;
2747 inf->bmiColors[0].rgbRed = inf->bmiColors[0].rgbGreen = inf->bmiColors[0].rgbBlue = 0;
2748 inf->bmiColors[1].rgbRed = inf->bmiColors[1].rgbGreen = inf->bmiColors[1].rgbBlue = 0xff;
2751 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
2752 goto failed;
2754 result = TRUE;
2756 failed:
2757 Free(data);
2759 return result;
2763 /*************************************************************************
2764 * ImageList_Write [COMCTL32.@]
2766 * Writes an image list to a stream.
2768 * PARAMS
2769 * himl [I] handle to image list
2770 * pstm [O] Pointer to a stream.
2772 * RETURNS
2773 * Success: TRUE
2774 * Failure: FALSE
2776 * BUGS
2777 * probably.
2780 BOOL WINAPI
2781 ImageList_Write (HIMAGELIST himl, LPSTREAM pstm)
2783 ILHEAD ilHead;
2784 int i;
2786 TRACE("%p %p\n", himl, pstm);
2788 if (!is_valid(himl))
2789 return FALSE;
2791 ilHead.usMagic = (('L' << 8) | 'I');
2792 ilHead.usVersion = 0x101;
2793 ilHead.cCurImage = himl->cCurImage;
2794 ilHead.cMaxImage = himl->cMaxImage;
2795 ilHead.cGrow = himl->cGrow;
2796 ilHead.cx = himl->cx;
2797 ilHead.cy = himl->cy;
2798 ilHead.bkcolor = himl->clrBk;
2799 ilHead.flags = himl->flags;
2800 for(i = 0; i < 4; i++) {
2801 ilHead.ovls[i] = himl->nOvlIdx[i];
2804 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
2805 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2807 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
2808 return FALSE;
2810 /* write the bitmap */
2811 if(!_write_bitmap(himl->hbmImage, pstm))
2812 return FALSE;
2814 /* write the mask if we have one */
2815 if(himl->flags & ILC_MASK) {
2816 if(!_write_bitmap(himl->hbmMask, pstm))
2817 return FALSE;
2820 return TRUE;
2824 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
2826 HBITMAP hbmNewBitmap;
2827 UINT ilc = (himl->flags & 0xFE);
2828 SIZE sz;
2830 imagelist_get_bitmap_size( himl, count, &sz );
2832 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
2834 VOID* bits;
2835 BITMAPINFO *bmi;
2837 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
2838 sz.cx, sz.cy, himl->uBitsPixel);
2840 if (himl->uBitsPixel <= ILC_COLOR8)
2842 LPPALETTEENTRY pal;
2843 ULONG i, colors;
2844 BYTE temp;
2846 colors = 1 << himl->uBitsPixel;
2847 bmi = Alloc(sizeof(BITMAPINFOHEADER) +
2848 sizeof(PALETTEENTRY) * colors);
2850 pal = (LPPALETTEENTRY)bmi->bmiColors;
2851 GetPaletteEntries(GetStockObject(DEFAULT_PALETTE), 0, colors, pal);
2853 /* Swap colors returned by GetPaletteEntries so we can use them for
2854 * CreateDIBSection call. */
2855 for (i = 0; i < colors; i++)
2857 temp = pal[i].peBlue;
2858 bmi->bmiColors[i].rgbRed = pal[i].peRed;
2859 bmi->bmiColors[i].rgbBlue = temp;
2862 else
2864 bmi = Alloc(sizeof(BITMAPINFOHEADER));
2867 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2868 bmi->bmiHeader.biWidth = sz.cx;
2869 bmi->bmiHeader.biHeight = sz.cy;
2870 bmi->bmiHeader.biPlanes = 1;
2871 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
2872 bmi->bmiHeader.biCompression = BI_RGB;
2873 bmi->bmiHeader.biSizeImage = 0;
2874 bmi->bmiHeader.biXPelsPerMeter = 0;
2875 bmi->bmiHeader.biYPelsPerMeter = 0;
2876 bmi->bmiHeader.biClrUsed = 0;
2877 bmi->bmiHeader.biClrImportant = 0;
2879 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, &bits, 0, 0);
2881 Free (bmi);
2883 else /*if (ilc == ILC_COLORDDB)*/
2885 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
2887 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
2889 TRACE("returning %p\n", hbmNewBitmap);
2890 return hbmNewBitmap;
2893 /*************************************************************************
2894 * ImageList_SetColorTable [COMCTL32.@]
2896 * Sets the color table of an image list.
2898 * PARAMS
2899 * himl [I] Handle to the image list.
2900 * uStartIndex [I] The first index to set.
2901 * cEntries [I] Number of entries to set.
2902 * prgb [I] New color information for color table for the image list.
2904 * RETURNS
2905 * Success: Number of entries in the table that were set.
2906 * Failure: Zero.
2908 * SEE
2909 * ImageList_Create(), SetDIBColorTable()
2912 UINT WINAPI
2913 ImageList_SetColorTable (HIMAGELIST himl, UINT uStartIndex, UINT cEntries, CONST RGBQUAD * prgb)
2915 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
2918 /*************************************************************************
2919 * ImageList_CoCreateInstance [COMCTL32.@]
2921 * Creates a new imagelist instance and returns an interface pointer to it.
2923 * PARAMS
2924 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
2925 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
2926 * riid [I] Identifier of the requested interface.
2927 * ppv [O] Returns the address of the pointer requested, or NULL.
2929 * RETURNS
2930 * Success: S_OK.
2931 * Failure: Error value.
2933 HRESULT WINAPI
2934 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
2936 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
2938 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
2939 return E_NOINTERFACE;
2941 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
2945 /*************************************************************************
2946 * IImageList implementation
2949 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList *iface,
2950 REFIID iid, void **ppv)
2952 HIMAGELIST This = (HIMAGELIST) iface;
2953 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
2955 if (!ppv) return E_INVALIDARG;
2957 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IImageList, iid))
2958 *ppv = This;
2959 else
2961 *ppv = NULL;
2962 return E_NOINTERFACE;
2965 IUnknown_AddRef((IUnknown*)*ppv);
2966 return S_OK;
2969 static ULONG WINAPI ImageListImpl_AddRef(IImageList *iface)
2971 HIMAGELIST This = (HIMAGELIST) iface;
2972 ULONG ref = InterlockedIncrement(&This->ref);
2974 TRACE("(%p) refcount=%u\n", iface, ref);
2975 return ref;
2978 static ULONG WINAPI ImageListImpl_Release(IImageList *iface)
2980 HIMAGELIST This = (HIMAGELIST) iface;
2981 ULONG ref = InterlockedDecrement(&This->ref);
2983 TRACE("(%p) refcount=%u\n", iface, ref);
2985 if (ref == 0)
2987 /* delete image bitmaps */
2988 if (This->hbmImage) DeleteObject (This->hbmImage);
2989 if (This->hbmMask) DeleteObject (This->hbmMask);
2991 /* delete image & mask DCs */
2992 if (This->hdcImage) DeleteDC (This->hdcImage);
2993 if (This->hdcMask) DeleteDC (This->hdcMask);
2995 /* delete blending brushes */
2996 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
2997 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
2999 This->lpVtbl = NULL;
3000 HeapFree(GetProcessHeap(), 0, This);
3003 return ref;
3006 static HRESULT WINAPI ImageListImpl_Add(IImageList *iface, HBITMAP hbmImage,
3007 HBITMAP hbmMask, int *pi)
3009 HIMAGELIST This = (HIMAGELIST) iface;
3010 int ret;
3012 if (!pi)
3013 return E_FAIL;
3015 ret = ImageList_Add(This, hbmImage, hbmMask);
3017 if (ret == -1)
3018 return E_FAIL;
3020 *pi = ret;
3021 return S_OK;
3024 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList *iface, int i,
3025 HICON hicon, int *pi)
3027 HIMAGELIST This = (HIMAGELIST) iface;
3028 int ret;
3030 if (!pi)
3031 return E_FAIL;
3033 ret = ImageList_ReplaceIcon(This, i, hicon);
3035 if (ret == -1)
3036 return E_FAIL;
3038 *pi = ret;
3039 return S_OK;
3042 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList *iface,
3043 int iImage, int iOverlay)
3045 return ImageList_SetOverlayImage((HIMAGELIST) iface, iImage, iOverlay)
3046 ? S_OK : E_FAIL;
3049 static HRESULT WINAPI ImageListImpl_Replace(IImageList *iface, int i,
3050 HBITMAP hbmImage, HBITMAP hbmMask)
3052 return ImageList_Replace((HIMAGELIST) iface, i, hbmImage, hbmMask) ? S_OK :
3053 E_FAIL;
3056 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList *iface, HBITMAP hbmImage,
3057 COLORREF crMask, int *pi)
3059 HIMAGELIST This = (HIMAGELIST) iface;
3060 int ret;
3062 if (!pi)
3063 return E_FAIL;
3065 ret = ImageList_AddMasked(This, hbmImage, crMask);
3067 if (ret == -1)
3068 return E_FAIL;
3070 *pi = ret;
3071 return S_OK;
3074 static HRESULT WINAPI ImageListImpl_Draw(IImageList *iface,
3075 IMAGELISTDRAWPARAMS *pimldp)
3077 HIMAGELIST This = (HIMAGELIST) iface;
3078 HIMAGELIST old_himl = 0;
3079 int ret = 0;
3081 if (!pimldp)
3082 return E_FAIL;
3084 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3085 so we shall simulate the same */
3086 old_himl = pimldp->himl;
3087 pimldp->himl = This;
3089 ret = ImageList_DrawIndirect(pimldp);
3091 pimldp->himl = old_himl;
3092 return ret ? S_OK : E_FAIL;
3095 static HRESULT WINAPI ImageListImpl_Remove(IImageList *iface, int i)
3097 return (ImageList_Remove((HIMAGELIST) iface, i) == 0) ? E_FAIL : S_OK;
3100 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList *iface, int i, UINT flags,
3101 HICON *picon)
3103 HICON hIcon;
3105 if (!picon)
3106 return E_FAIL;
3108 hIcon = ImageList_GetIcon((HIMAGELIST) iface, i, flags);
3110 if (hIcon == NULL)
3111 return E_FAIL;
3113 *picon = hIcon;
3114 return S_OK;
3117 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList *iface, int i,
3118 IMAGEINFO *pImageInfo)
3120 return ImageList_GetImageInfo((HIMAGELIST) iface, i, pImageInfo) ? S_OK : E_FAIL;
3123 static HRESULT WINAPI ImageListImpl_Copy(IImageList *iface, int iDst,
3124 IUnknown *punkSrc, int iSrc, UINT uFlags)
3126 HIMAGELIST This = (HIMAGELIST) iface;
3127 IImageList *src = NULL;
3128 HRESULT ret;
3130 if (!punkSrc)
3131 return E_FAIL;
3133 /* TODO: Add test for IID_ImageList2 too */
3134 if (FAILED(IImageList_QueryInterface(punkSrc, &IID_IImageList,
3135 (void **) &src)))
3136 return E_FAIL;
3138 if (ImageList_Copy(This, iDst, (HIMAGELIST) src, iSrc, uFlags))
3139 ret = S_OK;
3140 else
3141 ret = E_FAIL;
3143 IImageList_Release(src);
3144 return ret;
3147 static HRESULT WINAPI ImageListImpl_Merge(IImageList *iface, int i1,
3148 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, PVOID *ppv)
3150 HIMAGELIST This = (HIMAGELIST) iface;
3151 IImageList *iml2 = NULL;
3152 HIMAGELIST hNew;
3153 HRESULT ret = E_FAIL;
3155 if (!punk2 || !ppv)
3156 return E_FAIL;
3158 /* TODO: Add test for IID_ImageList2 too */
3159 if (FAILED(IImageList_QueryInterface(punk2, &IID_IImageList,
3160 (void **) &iml2)))
3161 return E_FAIL;
3163 hNew = ImageList_Merge(This, i1, (HIMAGELIST) iml2, i2, dx, dy);
3165 /* Get the interface for the new image list */
3166 if (hNew)
3167 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3169 IImageList_Release(iml2);
3170 return ret;
3173 static HRESULT WINAPI ImageListImpl_Clone(IImageList *iface, REFIID riid,
3174 PVOID *ppv)
3176 HIMAGELIST This = (HIMAGELIST) iface;
3177 HIMAGELIST hNew;
3178 HRESULT ret = E_FAIL;
3180 if (!ppv)
3181 return E_FAIL;
3183 hNew = ImageList_Duplicate(This);
3185 /* Get the interface for the new image list */
3186 if (hNew)
3187 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3189 return ret;
3192 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList *iface, int i,
3193 RECT *prc)
3195 HIMAGELIST This = (HIMAGELIST) iface;
3196 IMAGEINFO info;
3198 if (!prc)
3199 return E_FAIL;
3201 if (!ImageList_GetImageInfo(This, i, &info))
3202 return E_FAIL;
3204 return CopyRect(prc, &info.rcImage) ? S_OK : E_FAIL;
3207 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList *iface, int *cx,
3208 int *cy)
3210 HIMAGELIST This = (HIMAGELIST) iface;
3212 return ImageList_GetIconSize(This, cx, cy) ? S_OK : E_FAIL;
3215 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList *iface, int cx,
3216 int cy)
3218 return ImageList_SetIconSize((HIMAGELIST) iface, cx, cy) ? S_OK : E_FAIL;
3221 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList *iface, int *pi)
3223 if (!pi)
3224 return E_FAIL;
3226 *pi = ImageList_GetImageCount((HIMAGELIST) iface);
3227 return S_OK;
3230 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList *iface,
3231 UINT uNewCount)
3233 return ImageList_SetImageCount((HIMAGELIST) iface, uNewCount) ? S_OK : E_FAIL;
3236 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList *iface, COLORREF clrBk,
3237 COLORREF *pclr)
3239 if (!pclr)
3240 return E_FAIL;
3242 *pclr = ImageList_SetBkColor((HIMAGELIST) iface, clrBk);
3243 return *pclr == CLR_NONE ? E_FAIL : S_OK;
3246 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList *iface, COLORREF *pclr)
3248 if (!pclr)
3249 return E_FAIL;
3251 *pclr = ImageList_GetBkColor((HIMAGELIST) iface);
3252 return S_OK;
3255 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList *iface, int iTrack,
3256 int dxHotspot, int dyHotspot)
3258 return ImageList_BeginDrag((HIMAGELIST) iface, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3261 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList *iface)
3263 ImageList_EndDrag();
3264 return S_OK;
3267 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList *iface, HWND hwndLock,
3268 int x, int y)
3270 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3273 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList *iface, HWND hwndLock)
3275 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3278 static HRESULT WINAPI ImageListImpl_DragMove(IImageList *iface, int x, int y)
3280 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3283 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList *iface,
3284 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3286 IImageList *iml2 = NULL;
3287 HRESULT ret;
3289 if (!punk)
3290 return E_FAIL;
3292 /* TODO: Add test for IID_ImageList2 too */
3293 if (FAILED(IImageList_QueryInterface(punk, &IID_IImageList,
3294 (void **) &iml2)))
3295 return E_FAIL;
3297 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3298 dyHotspot);
3300 IImageList_Release(iml2);
3302 return ret ? S_OK : E_FAIL;
3305 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList *iface, BOOL fShow)
3307 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3310 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList *iface, POINT *ppt,
3311 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3313 HRESULT ret = E_FAIL;
3314 HIMAGELIST hNew;
3316 if (!ppv)
3317 return E_FAIL;
3319 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3321 /* Get the interface for the new image list */
3322 if (hNew)
3323 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3325 return ret;
3328 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList *iface, int i,
3329 DWORD *dwFlags)
3331 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3332 return E_NOTIMPL;
3335 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList *iface, int iOverlay,
3336 int *piIndex)
3338 HIMAGELIST This = (HIMAGELIST) iface;
3339 int i;
3341 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3342 return E_FAIL;
3344 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3346 if (This->nOvlIdx[i] == iOverlay)
3348 *piIndex = i + 1;
3349 return S_OK;
3353 return E_FAIL;
3357 static const IImageListVtbl ImageListImpl_Vtbl = {
3358 ImageListImpl_QueryInterface,
3359 ImageListImpl_AddRef,
3360 ImageListImpl_Release,
3361 ImageListImpl_Add,
3362 ImageListImpl_ReplaceIcon,
3363 ImageListImpl_SetOverlayImage,
3364 ImageListImpl_Replace,
3365 ImageListImpl_AddMasked,
3366 ImageListImpl_Draw,
3367 ImageListImpl_Remove,
3368 ImageListImpl_GetIcon,
3369 ImageListImpl_GetImageInfo,
3370 ImageListImpl_Copy,
3371 ImageListImpl_Merge,
3372 ImageListImpl_Clone,
3373 ImageListImpl_GetImageRect,
3374 ImageListImpl_GetIconSize,
3375 ImageListImpl_SetIconSize,
3376 ImageListImpl_GetImageCount,
3377 ImageListImpl_SetImageCount,
3378 ImageListImpl_SetBkColor,
3379 ImageListImpl_GetBkColor,
3380 ImageListImpl_BeginDrag,
3381 ImageListImpl_EndDrag,
3382 ImageListImpl_DragEnter,
3383 ImageListImpl_DragLeave,
3384 ImageListImpl_DragMove,
3385 ImageListImpl_SetDragCursorImage,
3386 ImageListImpl_DragShowNolock,
3387 ImageListImpl_GetDragImage,
3388 ImageListImpl_GetItemFlags,
3389 ImageListImpl_GetOverlayImage
3392 static inline BOOL is_valid(HIMAGELIST himl)
3394 return himl && himl->lpVtbl == &ImageListImpl_Vtbl;
3397 /*************************************************************************
3398 * HIMAGELIST_QueryInterface [COMCTL32.@]
3400 * Returns a pointer to an IImageList or IImageList2 object for the given
3401 * HIMAGELIST.
3403 * PARAMS
3404 * himl [I] Image list handle.
3405 * riid [I] Identifier of the requested interface.
3406 * ppv [O] Returns the address of the pointer requested, or NULL.
3408 * RETURNS
3409 * Success: S_OK.
3410 * Failure: Error value.
3412 HRESULT WINAPI
3413 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3415 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3416 return IImageList_QueryInterface((IImageList *) himl, riid, ppv);
3419 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3421 HIMAGELIST This;
3422 HRESULT ret;
3424 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3426 *ppv = NULL;
3428 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3430 This = HeapAlloc(GetProcessHeap(), 0, sizeof(struct _IMAGELIST));
3431 if (!This) return E_OUTOFMEMORY;
3433 ZeroMemory(This, sizeof(struct _IMAGELIST));
3435 This->lpVtbl = &ImageListImpl_Vtbl;
3436 This->ref = 1;
3438 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
3439 IUnknown_Release((IUnknown*)This);
3441 return ret;