winmm: Improve MCI's Sysinfo string command.
[wine/multimedia.git] / dlls / comctl32 / imagelist.c
blobf4f0b45c349ed3343a22dbd55ddfab2e7de208e0
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, UINT width);
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, UINT cx, SIZE *sz )
112 sz->cx = 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, INT cx, INT cy)
159 HDC hdcBitmap;
160 HBITMAP hbmNewBitmap, hbmNull;
161 INT nNewCount;
162 SIZE sz;
164 if ((himl->cCurImage + nImageCount <= himl->cMaxImage)
165 && (himl->cy >= cy))
166 return;
168 if (cx == 0) cx = himl->cx;
169 nNewCount = himl->cCurImage + nImageCount + himl->cGrow;
171 imagelist_get_bitmap_size(himl, nNewCount, cx, &sz);
173 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, cy, nNewCount);
174 hdcBitmap = CreateCompatibleDC (0);
176 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount, cx);
178 if (hbmNewBitmap == 0)
179 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, 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 nImageCount = bmp.bmWidth / himl->cx;
250 IMAGELIST_InternalExpandBitmaps (himl, nImageCount, bmp.bmWidth, bmp.bmHeight);
252 hdcBitmap = CreateCompatibleDC(0);
254 hOldBitmap = SelectObject(hdcBitmap, hbmImage);
256 for (i=0; i<nImageCount; i++)
258 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
260 /* Copy result to the imagelist
262 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
263 hdcBitmap, i*himl->cx, 0, SRCCOPY );
265 if (!himl->hbmMask)
266 continue;
268 hdcTemp = CreateCompatibleDC(0);
269 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
271 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
272 hdcTemp, i*himl->cx, 0, SRCCOPY );
274 SelectObject(hdcTemp, hOldBitmapTemp);
275 DeleteDC(hdcTemp);
277 /* Remove the background from the image
279 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
280 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
283 SelectObject(hdcBitmap, hOldBitmap);
284 DeleteDC(hdcBitmap);
286 nFirstIndex = himl->cCurImage;
287 himl->cCurImage += nImageCount;
289 return nFirstIndex;
293 /*************************************************************************
294 * ImageList_AddIcon [COMCTL32.@]
296 * Adds an icon to an image list.
298 * PARAMS
299 * himl [I] handle to image list
300 * hIcon [I] handle to icon
302 * RETURNS
303 * Success: index of the new image
304 * Failure: -1
306 #undef ImageList_AddIcon
307 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
309 return ImageList_ReplaceIcon (himl, -1, hIcon);
313 /*************************************************************************
314 * ImageList_AddMasked [COMCTL32.@]
316 * Adds an image or images to an image list and creates a mask from the
317 * specified bitmap using the mask color.
319 * PARAMS
320 * himl [I] handle to image list.
321 * hBitmap [I] handle to bitmap
322 * clrMask [I] mask color.
324 * RETURNS
325 * Success: Index of the first new image.
326 * Failure: -1
329 INT WINAPI
330 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
332 HDC hdcMask, hdcBitmap;
333 INT i, nIndex, nImageCount;
334 BITMAP bmp;
335 HBITMAP hOldBitmap;
336 HBITMAP hMaskBitmap=0;
337 COLORREF bkColor;
338 POINT pt;
340 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
341 if (!is_valid(himl))
342 return -1;
344 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
345 return -1;
347 if (himl->cx > 0)
348 nImageCount = bmp.bmWidth / himl->cx;
349 else
350 nImageCount = 0;
352 IMAGELIST_InternalExpandBitmaps (himl, nImageCount, bmp.bmWidth, bmp.bmHeight);
354 nIndex = himl->cCurImage;
355 himl->cCurImage += nImageCount;
357 hdcBitmap = CreateCompatibleDC(0);
358 hOldBitmap = SelectObject(hdcBitmap, hBitmap);
360 /* Create a temp Mask so we can remove the background of the Image */
361 hdcMask = CreateCompatibleDC(0);
362 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
363 SelectObject(hdcMask, hMaskBitmap);
365 /* create monochrome image to the mask bitmap */
366 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
367 SetBkColor (hdcBitmap, bkColor);
368 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
370 SetBkColor(hdcBitmap, RGB(255,255,255));
373 * Remove the background from the image
375 * WINDOWS BUG ALERT!!!!!!
376 * The statement below should not be done in common practice
377 * but this is how ImageList_AddMasked works in Windows.
378 * It overwrites the original bitmap passed, this was discovered
379 * by using the same bitmap to iterate the different styles
380 * on windows where it failed (BUT ImageList_Add is OK)
381 * This is here in case some apps rely on this bug
383 * Blt mode 0x220326 is NOTSRCAND
385 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
387 /* Copy result to the imagelist */
388 for (i=0; i<nImageCount; i++)
390 imagelist_point_from_index( himl, nIndex + i, &pt );
391 BitBlt(himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
392 hdcBitmap, i*himl->cx, 0, SRCCOPY);
393 BitBlt(himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
394 hdcMask, i*himl->cx, 0, SRCCOPY);
397 /* Clean up */
398 SelectObject(hdcBitmap, hOldBitmap);
399 DeleteDC(hdcBitmap);
400 DeleteObject(hMaskBitmap);
401 DeleteDC(hdcMask);
403 return nIndex;
407 /*************************************************************************
408 * ImageList_BeginDrag [COMCTL32.@]
410 * Creates a temporary image list that contains one image. It will be used
411 * as a drag image.
413 * PARAMS
414 * himlTrack [I] handle to the source image list
415 * iTrack [I] index of the drag image in the source image list
416 * dxHotspot [I] X position of the hot spot of the drag image
417 * dyHotspot [I] Y position of the hot spot of the drag image
419 * RETURNS
420 * Success: TRUE
421 * Failure: FALSE
424 BOOL WINAPI
425 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
426 INT dxHotspot, INT dyHotspot)
428 INT cx, cy;
430 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
431 dxHotspot, dyHotspot);
433 if (!is_valid(himlTrack))
434 return FALSE;
436 if (InternalDrag.himl)
437 ImageList_EndDrag ();
439 cx = himlTrack->cx;
440 cy = himlTrack->cy;
442 InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
443 if (InternalDrag.himl == NULL) {
444 WARN("Error creating drag image list!\n");
445 return FALSE;
448 InternalDrag.dxHotspot = dxHotspot;
449 InternalDrag.dyHotspot = dyHotspot;
451 /* copy image */
452 BitBlt (InternalDrag.himl->hdcImage, 0, 0, cx, cy, himlTrack->hdcImage, iTrack * cx, 0, SRCCOPY);
454 /* copy mask */
455 BitBlt (InternalDrag.himl->hdcMask, 0, 0, cx, cy, himlTrack->hdcMask, iTrack * cx, 0, SRCCOPY);
457 InternalDrag.himl->cCurImage = 1;
459 return TRUE;
463 /*************************************************************************
464 * ImageList_Copy [COMCTL32.@]
466 * Copies an image of the source image list to an image of the
467 * destination image list. Images can be copied or swapped.
469 * PARAMS
470 * himlDst [I] handle to the destination image list
471 * iDst [I] destination image index.
472 * himlSrc [I] handle to the source image list
473 * iSrc [I] source image index
474 * uFlags [I] flags for the copy operation
476 * RETURNS
477 * Success: TRUE
478 * Failure: FALSE
480 * NOTES
481 * Copying from one image list to another is possible. The original
482 * implementation just copies or swaps within one image list.
483 * Could this feature become a bug??? ;-)
486 BOOL WINAPI
487 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
488 INT iSrc, UINT uFlags)
490 POINT ptSrc, ptDst;
492 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
494 if (!is_valid(himlSrc) || !is_valid(himlDst))
495 return FALSE;
496 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
497 return FALSE;
498 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
499 return FALSE;
501 imagelist_point_from_index( himlDst, iDst, &ptDst );
502 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
504 if (uFlags & ILCF_SWAP) {
505 /* swap */
506 HDC hdcBmp;
507 HBITMAP hbmTempImage, hbmTempMask;
509 hdcBmp = CreateCompatibleDC (0);
511 /* create temporary bitmaps */
512 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
513 himlSrc->uBitsPixel, NULL);
514 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
515 1, NULL);
517 /* copy (and stretch) destination to temporary bitmaps.(save) */
518 /* image */
519 SelectObject (hdcBmp, hbmTempImage);
520 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
521 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
522 SRCCOPY);
523 /* mask */
524 SelectObject (hdcBmp, hbmTempMask);
525 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
526 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
527 SRCCOPY);
529 /* copy (and stretch) source to destination */
530 /* image */
531 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
532 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
533 SRCCOPY);
534 /* mask */
535 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
536 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
537 SRCCOPY);
539 /* copy (without stretching) temporary bitmaps to source (restore) */
540 /* mask */
541 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
542 hdcBmp, 0, 0, SRCCOPY);
544 /* image */
545 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
546 hdcBmp, 0, 0, SRCCOPY);
547 /* delete temporary bitmaps */
548 DeleteObject (hbmTempMask);
549 DeleteObject (hbmTempImage);
550 DeleteDC(hdcBmp);
552 else {
553 /* copy image */
554 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
555 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
556 SRCCOPY);
558 /* copy mask */
559 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
560 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
561 SRCCOPY);
564 return TRUE;
568 /*************************************************************************
569 * ImageList_Create [COMCTL32.@]
571 * Creates a new image list.
573 * PARAMS
574 * cx [I] image height
575 * cy [I] image width
576 * flags [I] creation flags
577 * cInitial [I] initial number of images in the image list
578 * cGrow [I] number of images by which image list grows
580 * RETURNS
581 * Success: Handle to the created image list
582 * Failure: NULL
584 HIMAGELIST WINAPI
585 ImageList_Create (INT cx, INT cy, UINT flags,
586 INT cInitial, INT cGrow)
588 HIMAGELIST himl;
589 INT nCount;
590 HBITMAP hbmTemp;
591 UINT ilc = (flags & 0xFE);
592 static const WORD aBitBlend25[] =
593 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
595 static const WORD aBitBlend50[] =
596 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
598 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
600 /* Create the IImageList interface for the image list */
601 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
602 return NULL;
604 cGrow = (cGrow < 4) ? 4 : (cGrow + 3) & ~3;
606 himl->cx = cx;
607 himl->cy = cy;
608 himl->flags = flags;
609 himl->cMaxImage = cInitial + 1;
610 himl->cInitial = cInitial;
611 himl->cGrow = cGrow;
612 himl->clrFg = CLR_DEFAULT;
613 himl->clrBk = CLR_NONE;
615 /* initialize overlay mask indices */
616 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
617 himl->nOvlIdx[nCount] = -1;
619 /* Create Image & Mask DCs */
620 himl->hdcImage = CreateCompatibleDC (0);
621 if (!himl->hdcImage)
622 goto cleanup;
623 if (himl->flags & ILC_MASK){
624 himl->hdcMask = CreateCompatibleDC(0);
625 if (!himl->hdcMask)
626 goto cleanup;
629 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
630 if (ilc == ILC_COLOR)
631 ilc = ILC_COLOR4;
633 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
634 himl->uBitsPixel = ilc;
635 else
636 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
638 if (himl->cMaxImage > 0) {
639 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage, cx);
640 SelectObject(himl->hdcImage, himl->hbmImage);
641 } else
642 himl->hbmImage = 0;
644 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
645 SIZE sz;
647 imagelist_get_bitmap_size(himl, himl->cMaxImage, himl->cx, &sz);
648 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
649 if (himl->hbmMask == 0) {
650 ERR("Error creating mask bitmap!\n");
651 goto cleanup;
653 SelectObject(himl->hdcMask, himl->hbmMask);
655 else
656 himl->hbmMask = 0;
658 /* create blending brushes */
659 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
660 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
661 DeleteObject (hbmTemp);
663 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
664 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
665 DeleteObject (hbmTemp);
667 TRACE("created imagelist %p\n", himl);
668 return himl;
670 cleanup:
671 ImageList_Destroy(himl);
672 return NULL;
676 /*************************************************************************
677 * ImageList_Destroy [COMCTL32.@]
679 * Destroys an image list.
681 * PARAMS
682 * himl [I] handle to image list
684 * RETURNS
685 * Success: TRUE
686 * Failure: FALSE
689 BOOL WINAPI
690 ImageList_Destroy (HIMAGELIST himl)
692 if (!is_valid(himl))
693 return FALSE;
695 IImageList_Release((IImageList *) himl);
696 return TRUE;
700 /*************************************************************************
701 * ImageList_DragEnter [COMCTL32.@]
703 * Locks window update and displays the drag image at the given position.
705 * PARAMS
706 * hwndLock [I] handle of the window that owns the drag image.
707 * x [I] X position of the drag image.
708 * y [I] Y position of the drag image.
710 * RETURNS
711 * Success: TRUE
712 * Failure: FALSE
714 * NOTES
715 * The position of the drag image is relative to the window, not
716 * the client area.
719 BOOL WINAPI
720 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
722 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
724 if (!is_valid(InternalDrag.himl))
725 return FALSE;
727 if (hwndLock)
728 InternalDrag.hwnd = hwndLock;
729 else
730 InternalDrag.hwnd = GetDesktopWindow ();
732 InternalDrag.x = x;
733 InternalDrag.y = y;
735 /* draw the drag image and save the background */
736 if (!ImageList_DragShowNolock(TRUE)) {
737 return FALSE;
740 return TRUE;
744 /*************************************************************************
745 * ImageList_DragLeave [COMCTL32.@]
747 * Unlocks window update and hides the drag image.
749 * PARAMS
750 * hwndLock [I] handle of the window that owns the drag image.
752 * RETURNS
753 * Success: TRUE
754 * Failure: FALSE
757 BOOL WINAPI
758 ImageList_DragLeave (HWND hwndLock)
760 /* As we don't save drag info in the window this can lead to problems if
761 an app does not supply the same window as DragEnter */
762 /* if (hwndLock)
763 InternalDrag.hwnd = hwndLock;
764 else
765 InternalDrag.hwnd = GetDesktopWindow (); */
766 if(!hwndLock)
767 hwndLock = GetDesktopWindow();
768 if(InternalDrag.hwnd != hwndLock)
769 FIXME("DragLeave hWnd != DragEnter hWnd\n");
771 ImageList_DragShowNolock (FALSE);
773 return TRUE;
777 /*************************************************************************
778 * ImageList_InternalDragDraw [Internal]
780 * Draws the drag image.
782 * PARAMS
783 * hdc [I] device context to draw into.
784 * x [I] X position of the drag image.
785 * y [I] Y position of the drag image.
787 * RETURNS
788 * Success: TRUE
789 * Failure: FALSE
791 * NOTES
792 * The position of the drag image is relative to the window, not
793 * the client area.
797 static inline void
798 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
800 IMAGELISTDRAWPARAMS imldp;
802 ZeroMemory (&imldp, sizeof(imldp));
803 imldp.cbSize = sizeof(imldp);
804 imldp.himl = InternalDrag.himl;
805 imldp.i = 0;
806 imldp.hdcDst = hdc,
807 imldp.x = x;
808 imldp.y = y;
809 imldp.rgbBk = CLR_DEFAULT;
810 imldp.rgbFg = CLR_DEFAULT;
811 imldp.fStyle = ILD_NORMAL;
812 imldp.fState = ILS_ALPHA;
813 imldp.Frame = 128;
815 /* FIXME: instead of using the alpha blending, we should
816 * create a 50% mask, and draw it semitransparantly that way */
817 ImageList_DrawIndirect (&imldp);
820 /*************************************************************************
821 * ImageList_DragMove [COMCTL32.@]
823 * Moves the drag image.
825 * PARAMS
826 * x [I] X position of the drag image.
827 * y [I] Y position of the drag image.
829 * RETURNS
830 * Success: TRUE
831 * Failure: FALSE
833 * NOTES
834 * The position of the drag image is relative to the window, not
835 * the client area.
837 * BUGS
838 * The drag image should be drawn semitransparent.
841 BOOL WINAPI
842 ImageList_DragMove (INT x, INT y)
844 TRACE("(x=%d y=%d)\n", x, y);
846 if (!is_valid(InternalDrag.himl))
847 return FALSE;
849 /* draw/update the drag image */
850 if (InternalDrag.bShow) {
851 HDC hdcDrag;
852 HDC hdcOffScreen;
853 HDC hdcBg;
854 HBITMAP hbmOffScreen;
855 INT origNewX, origNewY;
856 INT origOldX, origOldY;
857 INT origRegX, origRegY;
858 INT sizeRegX, sizeRegY;
861 /* calculate the update region */
862 origNewX = x - InternalDrag.dxHotspot;
863 origNewY = y - InternalDrag.dyHotspot;
864 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
865 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
866 origRegX = min(origNewX, origOldX);
867 origRegY = min(origNewY, origOldY);
868 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
869 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
871 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
872 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
873 hdcOffScreen = CreateCompatibleDC(hdcDrag);
874 hdcBg = CreateCompatibleDC(hdcDrag);
876 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
877 SelectObject(hdcOffScreen, hbmOffScreen);
878 SelectObject(hdcBg, InternalDrag.hbmBg);
880 /* get the actual background of the update region */
881 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
882 origRegX, origRegY, SRCCOPY);
883 /* erase the old image */
884 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
885 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
886 SRCCOPY);
887 /* save the background */
888 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
889 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
890 /* draw the image */
891 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
892 origNewY - origRegY);
893 /* draw the update region to the screen */
894 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
895 hdcOffScreen, 0, 0, SRCCOPY);
897 DeleteDC(hdcBg);
898 DeleteDC(hdcOffScreen);
899 DeleteObject(hbmOffScreen);
900 ReleaseDC(InternalDrag.hwnd, hdcDrag);
903 /* update the image position */
904 InternalDrag.x = x;
905 InternalDrag.y = y;
907 return TRUE;
911 /*************************************************************************
912 * ImageList_DragShowNolock [COMCTL32.@]
914 * Shows or hides the drag image.
916 * PARAMS
917 * bShow [I] TRUE shows the drag image, FALSE hides it.
919 * RETURNS
920 * Success: TRUE
921 * Failure: FALSE
923 * BUGS
924 * The drag image should be drawn semitransparent.
927 BOOL WINAPI
928 ImageList_DragShowNolock (BOOL bShow)
930 HDC hdcDrag;
931 HDC hdcBg;
932 INT x, y;
934 if (!is_valid(InternalDrag.himl))
935 return FALSE;
937 TRACE("bShow=0x%X!\n", bShow);
939 /* DragImage is already visible/hidden */
940 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
941 return FALSE;
944 /* position of the origin of the DragImage */
945 x = InternalDrag.x - InternalDrag.dxHotspot;
946 y = InternalDrag.y - InternalDrag.dyHotspot;
948 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
949 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
950 if (!hdcDrag) {
951 return FALSE;
954 hdcBg = CreateCompatibleDC(hdcDrag);
955 if (!InternalDrag.hbmBg) {
956 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
957 InternalDrag.himl->cx, InternalDrag.himl->cy);
959 SelectObject(hdcBg, InternalDrag.hbmBg);
961 if (bShow) {
962 /* save the background */
963 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
964 hdcDrag, x, y, SRCCOPY);
965 /* show the image */
966 ImageList_InternalDragDraw(hdcDrag, x, y);
967 } else {
968 /* hide the image */
969 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
970 hdcBg, 0, 0, SRCCOPY);
973 InternalDrag.bShow = !InternalDrag.bShow;
975 DeleteDC(hdcBg);
976 ReleaseDC (InternalDrag.hwnd, hdcDrag);
977 return TRUE;
981 /*************************************************************************
982 * ImageList_Draw [COMCTL32.@]
984 * Draws an image.
986 * PARAMS
987 * himl [I] handle to image list
988 * i [I] image index
989 * hdc [I] handle to device context
990 * x [I] x position
991 * y [I] y position
992 * fStyle [I] drawing flags
994 * RETURNS
995 * Success: TRUE
996 * Failure: FALSE
998 * SEE
999 * ImageList_DrawEx.
1002 BOOL WINAPI
1003 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1005 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1006 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1010 /*************************************************************************
1011 * ImageList_DrawEx [COMCTL32.@]
1013 * Draws an image and allows to use extended drawing features.
1015 * PARAMS
1016 * himl [I] handle to image list
1017 * i [I] image index
1018 * hdc [I] handle to device context
1019 * x [I] X position
1020 * y [I] Y position
1021 * dx [I] X offset
1022 * dy [I] Y offset
1023 * rgbBk [I] background color
1024 * rgbFg [I] foreground color
1025 * fStyle [I] drawing flags
1027 * RETURNS
1028 * Success: TRUE
1029 * Failure: FALSE
1031 * NOTES
1032 * Calls ImageList_DrawIndirect.
1034 * SEE
1035 * ImageList_DrawIndirect.
1038 BOOL WINAPI
1039 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1040 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1041 UINT fStyle)
1043 IMAGELISTDRAWPARAMS imldp;
1045 ZeroMemory (&imldp, sizeof(imldp));
1046 imldp.cbSize = sizeof(imldp);
1047 imldp.himl = himl;
1048 imldp.i = i;
1049 imldp.hdcDst = hdc,
1050 imldp.x = x;
1051 imldp.y = y;
1052 imldp.cx = dx;
1053 imldp.cy = dy;
1054 imldp.rgbBk = rgbBk;
1055 imldp.rgbFg = rgbFg;
1056 imldp.fStyle = fStyle;
1058 return ImageList_DrawIndirect (&imldp);
1062 /*************************************************************************
1063 * ImageList_DrawIndirect [COMCTL32.@]
1065 * Draws an image using various parameters specified in pimldp.
1067 * PARAMS
1068 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1070 * RETURNS
1071 * Success: TRUE
1072 * Failure: FALSE
1075 BOOL WINAPI
1076 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1078 INT cx, cy, nOvlIdx;
1079 DWORD fState, dwRop;
1080 UINT fStyle;
1081 COLORREF oldImageBk, oldImageFg;
1082 HDC hImageDC, hImageListDC, hMaskListDC;
1083 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1084 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1085 HIMAGELIST himl;
1086 POINT pt;
1088 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1089 if (!is_valid(himl)) return FALSE;
1090 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1092 imagelist_point_from_index( himl, pimldp->i, &pt );
1093 pt.x += pimldp->xBitmap;
1094 pt.y += pimldp->yBitmap;
1096 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1097 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1098 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1099 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1101 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1102 if( pimldp->rgbBk == CLR_NONE )
1103 bIsTransparent = TRUE;
1104 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1105 bIsTransparent = TRUE;
1106 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1107 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1109 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1110 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1112 /* we will use these DCs to access the images and masks in the ImageList */
1113 hImageListDC = himl->hdcImage;
1114 hMaskListDC = himl->hdcMask;
1116 /* these will accumulate the image and mask for the image we're drawing */
1117 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1118 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1119 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1121 /* Create a compatible DC. */
1122 if (!hImageListDC || !hImageDC || !hImageBmp ||
1123 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1124 goto cleanup;
1126 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1129 * To obtain a transparent look, background color should be set
1130 * to white and foreground color to black when blting the
1131 * monochrome mask.
1133 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1134 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1137 * Draw the initial image
1139 if( bMask ) {
1140 if (himl->hbmMask) {
1141 HBRUSH hOldBrush;
1142 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1143 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1144 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1145 DeleteObject (SelectObject (hImageDC, hOldBrush));
1146 if( bIsTransparent )
1148 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1149 bResult = TRUE;
1150 goto end;
1152 } else {
1153 HBRUSH hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1154 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1155 SelectObject(hImageDC, hOldBrush);
1157 } else {
1158 /* blend the image with the needed solid background */
1159 COLORREF colour = RGB(0,0,0);
1160 HBRUSH hOldBrush;
1162 if( !bIsTransparent )
1164 colour = pimldp->rgbBk;
1165 if( colour == CLR_DEFAULT )
1166 colour = himl->clrBk;
1167 if( colour == CLR_NONE )
1168 colour = GetBkColor(pimldp->hdcDst);
1171 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1172 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1173 if (himl->hbmMask)
1175 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1176 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1178 else
1179 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1180 DeleteObject (SelectObject (hImageDC, hOldBrush));
1183 /* Time for blending, if required */
1184 if (bBlend) {
1185 HBRUSH hBlendBrush, hOldBrush;
1186 COLORREF clrBlend = pimldp->rgbFg;
1187 HDC hBlendMaskDC = hImageListDC;
1188 HBITMAP hOldBitmap;
1190 /* Create the blend Mask */
1191 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1192 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1193 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1194 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1195 SelectObject(hBlendMaskDC, hOldBrush);
1197 /* Modify the blend mask if an Image Mask exist */
1198 if(himl->hbmMask) {
1199 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1200 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1203 /* now apply blend to the current image given the BlendMask */
1204 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1205 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1206 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1207 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1208 DeleteObject(SelectObject(hImageDC, hOldBrush));
1209 SelectObject(hBlendMaskDC, hOldBitmap);
1212 /* Now do the overlay image, if any */
1213 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1214 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1215 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1216 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1217 POINT ptOvl;
1218 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1219 ptOvl.x += pimldp->xBitmap;
1220 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1221 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1222 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1226 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1227 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1228 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1229 if (fState & ILS_ALPHA) FIXME("ILS_ALPHA: unimplemented!\n");
1231 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1232 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1233 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1235 /* now copy the image to the screen */
1236 dwRop = SRCCOPY;
1237 if (himl->hbmMask && bIsTransparent ) {
1238 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1239 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1240 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1241 SetBkColor(pimldp->hdcDst, oldDstBk);
1242 SetTextColor(pimldp->hdcDst, oldDstFg);
1243 dwRop = SRCPAINT;
1245 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1246 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1248 bResult = TRUE;
1249 end:
1250 /* cleanup the mess */
1251 SetBkColor(hImageDC, oldImageBk);
1252 SetTextColor(hImageDC, oldImageFg);
1253 SelectObject(hImageDC, hOldImageBmp);
1254 cleanup:
1255 DeleteObject(hBlendMaskBmp);
1256 DeleteObject(hImageBmp);
1257 DeleteDC(hImageDC);
1259 return bResult;
1263 /*************************************************************************
1264 * ImageList_Duplicate [COMCTL32.@]
1266 * Duplicates an image list.
1268 * PARAMS
1269 * himlSrc [I] source image list handle
1271 * RETURNS
1272 * Success: Handle of duplicated image list.
1273 * Failure: NULL
1276 HIMAGELIST WINAPI
1277 ImageList_Duplicate (HIMAGELIST himlSrc)
1279 HIMAGELIST himlDst;
1281 if (!is_valid(himlSrc)) {
1282 ERR("Invalid image list handle!\n");
1283 return NULL;
1286 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1287 himlSrc->cInitial, himlSrc->cGrow);
1289 if (himlDst)
1291 SIZE sz;
1293 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, himlSrc->cx, &sz);
1294 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1295 himlSrc->hdcImage, 0, 0, SRCCOPY);
1297 if (himlDst->hbmMask)
1298 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1299 himlSrc->hdcMask, 0, 0, SRCCOPY);
1301 himlDst->cCurImage = himlSrc->cCurImage;
1302 himlDst->cMaxImage = himlSrc->cMaxImage;
1304 return himlDst;
1308 /*************************************************************************
1309 * ImageList_EndDrag [COMCTL32.@]
1311 * Finishes a drag operation.
1313 * PARAMS
1314 * no Parameters
1316 * RETURNS
1317 * Success: TRUE
1318 * Failure: FALSE
1321 VOID WINAPI
1322 ImageList_EndDrag (void)
1324 /* cleanup the InternalDrag struct */
1325 InternalDrag.hwnd = 0;
1326 ImageList_Destroy (InternalDrag.himl);
1327 InternalDrag.himl = 0;
1328 InternalDrag.x= 0;
1329 InternalDrag.y= 0;
1330 InternalDrag.dxHotspot = 0;
1331 InternalDrag.dyHotspot = 0;
1332 InternalDrag.bShow = FALSE;
1333 DeleteObject(InternalDrag.hbmBg);
1334 InternalDrag.hbmBg = 0;
1338 /*************************************************************************
1339 * ImageList_GetBkColor [COMCTL32.@]
1341 * Returns the background color of an image list.
1343 * PARAMS
1344 * himl [I] Image list handle.
1346 * RETURNS
1347 * Success: background color
1348 * Failure: CLR_NONE
1351 COLORREF WINAPI
1352 ImageList_GetBkColor (HIMAGELIST himl)
1354 return himl ? himl->clrBk : CLR_NONE;
1358 /*************************************************************************
1359 * ImageList_GetDragImage [COMCTL32.@]
1361 * Returns the handle to the internal drag image list.
1363 * PARAMS
1364 * ppt [O] Pointer to the drag position. Can be NULL.
1365 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1367 * RETURNS
1368 * Success: Handle of the drag image list.
1369 * Failure: NULL.
1372 HIMAGELIST WINAPI
1373 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1375 if (is_valid(InternalDrag.himl)) {
1376 if (ppt) {
1377 ppt->x = InternalDrag.x;
1378 ppt->y = InternalDrag.y;
1380 if (pptHotspot) {
1381 pptHotspot->x = InternalDrag.dxHotspot;
1382 pptHotspot->y = InternalDrag.dyHotspot;
1384 return (InternalDrag.himl);
1387 return NULL;
1391 /*************************************************************************
1392 * ImageList_GetFlags [COMCTL32.@]
1394 * Gets the flags of the specified image list.
1396 * PARAMS
1397 * himl [I] Handle to image list
1399 * RETURNS
1400 * Image list flags.
1402 * BUGS
1403 * Stub.
1406 DWORD WINAPI
1407 ImageList_GetFlags(HIMAGELIST himl)
1409 TRACE("%p\n", himl);
1411 return is_valid(himl) ? himl->flags : 0;
1415 /*************************************************************************
1416 * ImageList_GetIcon [COMCTL32.@]
1418 * Creates an icon from a masked image of an image list.
1420 * PARAMS
1421 * himl [I] handle to image list
1422 * i [I] image index
1423 * flags [I] drawing style flags
1425 * RETURNS
1426 * Success: icon handle
1427 * Failure: NULL
1430 HICON WINAPI
1431 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1433 ICONINFO ii;
1434 HICON hIcon;
1435 HBITMAP hOldDstBitmap;
1436 HDC hdcDst;
1437 POINT pt;
1439 TRACE("%p %d %d\n", himl, i, fStyle);
1440 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1442 ii.fIcon = TRUE;
1443 ii.xHotspot = 0;
1444 ii.yHotspot = 0;
1446 /* create colour bitmap */
1447 hdcDst = GetDC(0);
1448 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1449 ReleaseDC(0, hdcDst);
1451 hdcDst = CreateCompatibleDC(0);
1453 imagelist_point_from_index( himl, i, &pt );
1455 /* draw mask*/
1456 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1457 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1458 if (himl->hbmMask) {
1459 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1460 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1462 else
1463 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1465 /* draw image*/
1466 SelectObject (hdcDst, ii.hbmColor);
1467 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1468 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1471 * CreateIconIndirect requires us to deselect the bitmaps from
1472 * the DCs before calling
1474 SelectObject(hdcDst, hOldDstBitmap);
1476 hIcon = CreateIconIndirect (&ii);
1478 DeleteObject (ii.hbmMask);
1479 DeleteObject (ii.hbmColor);
1480 DeleteDC (hdcDst);
1482 return hIcon;
1486 /*************************************************************************
1487 * ImageList_GetIconSize [COMCTL32.@]
1489 * Retrieves the size of an image in an image list.
1491 * PARAMS
1492 * himl [I] handle to image list
1493 * cx [O] pointer to the image width.
1494 * cy [O] pointer to the image height.
1496 * RETURNS
1497 * Success: TRUE
1498 * Failure: FALSE
1500 * NOTES
1501 * All images in an image list have the same size.
1504 BOOL WINAPI
1505 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1507 if (!is_valid(himl))
1508 return FALSE;
1509 if ((himl->cx <= 0) || (himl->cy <= 0))
1510 return FALSE;
1512 if (cx)
1513 *cx = himl->cx;
1514 if (cy)
1515 *cy = himl->cy;
1517 return TRUE;
1521 /*************************************************************************
1522 * ImageList_GetImageCount [COMCTL32.@]
1524 * Returns the number of images in an image list.
1526 * PARAMS
1527 * himl [I] handle to image list
1529 * RETURNS
1530 * Success: Number of images.
1531 * Failure: 0
1534 INT WINAPI
1535 ImageList_GetImageCount (HIMAGELIST himl)
1537 if (!is_valid(himl))
1538 return 0;
1540 return himl->cCurImage;
1544 /*************************************************************************
1545 * ImageList_GetImageInfo [COMCTL32.@]
1547 * Returns information about an image in an image list.
1549 * PARAMS
1550 * himl [I] handle to image list
1551 * i [I] image index
1552 * pImageInfo [O] pointer to the image information
1554 * RETURNS
1555 * Success: TRUE
1556 * Failure: FALSE
1559 BOOL WINAPI
1560 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1562 POINT pt;
1564 if (!is_valid(himl) || (pImageInfo == NULL))
1565 return FALSE;
1566 if ((i < 0) || (i >= himl->cCurImage))
1567 return FALSE;
1569 pImageInfo->hbmImage = himl->hbmImage;
1570 pImageInfo->hbmMask = himl->hbmMask;
1572 imagelist_point_from_index( himl, i, &pt );
1573 pImageInfo->rcImage.top = pt.y;
1574 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1575 pImageInfo->rcImage.left = pt.x;
1576 pImageInfo->rcImage.right = pt.x + himl->cx;
1578 return TRUE;
1582 /*************************************************************************
1583 * ImageList_GetImageRect [COMCTL32.@]
1585 * Retrieves the rectangle of the specified image in an image list.
1587 * PARAMS
1588 * himl [I] handle to image list
1589 * i [I] image index
1590 * lpRect [O] pointer to the image rectangle
1592 * RETURNS
1593 * Success: TRUE
1594 * Failure: FALSE
1596 * NOTES
1597 * This is an UNDOCUMENTED function!!!
1600 BOOL WINAPI
1601 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1603 POINT pt;
1605 if (!is_valid(himl) || (lpRect == NULL))
1606 return FALSE;
1607 if ((i < 0) || (i >= himl->cCurImage))
1608 return FALSE;
1610 imagelist_point_from_index( himl, i, &pt );
1611 lpRect->left = pt.x;
1612 lpRect->top = pt.y;
1613 lpRect->right = pt.x + himl->cx;
1614 lpRect->bottom = pt.y + himl->cy;
1616 return TRUE;
1620 /*************************************************************************
1621 * ImageList_LoadImage [COMCTL32.@]
1622 * ImageList_LoadImageA [COMCTL32.@]
1624 * Creates an image list from a bitmap, icon or cursor.
1626 * See ImageList_LoadImageW.
1629 HIMAGELIST WINAPI
1630 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1631 COLORREF clrMask, UINT uType, UINT uFlags)
1633 HIMAGELIST himl;
1634 LPWSTR lpbmpW;
1635 DWORD len;
1637 if (IS_INTRESOURCE(lpbmp))
1638 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1639 uType, uFlags);
1641 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1642 lpbmpW = Alloc(len * sizeof(WCHAR));
1643 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1645 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1646 Free (lpbmpW);
1647 return himl;
1651 /*************************************************************************
1652 * ImageList_LoadImageW [COMCTL32.@]
1654 * Creates an image list from a bitmap, icon or cursor.
1656 * PARAMS
1657 * hi [I] instance handle
1658 * lpbmp [I] name or id of the image
1659 * cx [I] width of each image
1660 * cGrow [I] number of images to expand
1661 * clrMask [I] mask color
1662 * uType [I] type of image to load
1663 * uFlags [I] loading flags
1665 * RETURNS
1666 * Success: handle to the loaded image list
1667 * Failure: NULL
1669 * SEE
1670 * LoadImage ()
1673 HIMAGELIST WINAPI
1674 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1675 COLORREF clrMask, UINT uType, UINT uFlags)
1677 HIMAGELIST himl = NULL;
1678 HANDLE handle;
1679 INT nImageCount;
1681 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
1682 if (!handle) {
1683 WARN("Couldn't load image\n");
1684 return NULL;
1687 if (uType == IMAGE_BITMAP) {
1688 BITMAP bmp;
1689 GetObjectW (handle, sizeof(BITMAP), &bmp);
1691 /* To match windows behavior, if cx is set to zero and
1692 the flag DI_DEFAULTSIZE is specified, cx becomes the
1693 system metric value for icons. If the flag is not specified
1694 the function sets the size to the height of the bitmap */
1695 if (cx == 0)
1697 if (uFlags & DI_DEFAULTSIZE)
1698 cx = GetSystemMetrics (SM_CXICON);
1699 else
1700 cx = bmp.bmHeight;
1703 nImageCount = bmp.bmWidth / cx;
1705 himl = ImageList_Create (cx, bmp.bmHeight, ILC_MASK | ILC_COLOR,
1706 nImageCount, cGrow);
1707 if (!himl) {
1708 DeleteObject (handle);
1709 return NULL;
1711 ImageList_AddMasked (himl, handle, clrMask);
1713 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
1714 ICONINFO ii;
1715 BITMAP bmp;
1717 GetIconInfo (handle, &ii);
1718 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
1719 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
1720 ILC_MASK | ILC_COLOR, 1, cGrow);
1721 if (!himl) {
1722 DeleteObject (ii.hbmColor);
1723 DeleteObject (ii.hbmMask);
1724 DeleteObject (handle);
1725 return NULL;
1727 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
1728 DeleteObject (ii.hbmColor);
1729 DeleteObject (ii.hbmMask);
1732 DeleteObject (handle);
1734 return himl;
1738 /*************************************************************************
1739 * ImageList_Merge [COMCTL32.@]
1741 * Create an image list containing a merged image from two image lists.
1743 * PARAMS
1744 * himl1 [I] handle to first image list
1745 * i1 [I] first image index
1746 * himl2 [I] handle to second image list
1747 * i2 [I] second image index
1748 * dx [I] X offset of the second image relative to the first.
1749 * dy [I] Y offset of the second image relative to the first.
1751 * RETURNS
1752 * Success: The newly created image list. It contains a single image
1753 * consisting of the second image merged with the first.
1754 * Failure: NULL, if either himl1 or himl2 are invalid.
1756 * NOTES
1757 * - The returned image list should be deleted by the caller using
1758 * ImageList_Destroy() when it is no longer required.
1759 * - If either i1 or i2 are not valid image indices they will be treated
1760 * as a blank image.
1762 HIMAGELIST WINAPI
1763 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
1764 INT dx, INT dy)
1766 HIMAGELIST himlDst = NULL;
1767 INT cxDst, cyDst;
1768 INT xOff1, yOff1, xOff2, yOff2;
1769 POINT pt1, pt2;
1771 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
1772 i2, dx, dy);
1774 if (!is_valid(himl1) || !is_valid(himl2))
1775 return NULL;
1777 if (dx > 0) {
1778 cxDst = max (himl1->cx, dx + himl2->cx);
1779 xOff1 = 0;
1780 xOff2 = dx;
1782 else if (dx < 0) {
1783 cxDst = max (himl2->cx, himl1->cx - dx);
1784 xOff1 = -dx;
1785 xOff2 = 0;
1787 else {
1788 cxDst = max (himl1->cx, himl2->cx);
1789 xOff1 = 0;
1790 xOff2 = 0;
1793 if (dy > 0) {
1794 cyDst = max (himl1->cy, dy + himl2->cy);
1795 yOff1 = 0;
1796 yOff2 = dy;
1798 else if (dy < 0) {
1799 cyDst = max (himl2->cy, himl1->cy - dy);
1800 yOff1 = -dy;
1801 yOff2 = 0;
1803 else {
1804 cyDst = max (himl1->cy, himl2->cy);
1805 yOff1 = 0;
1806 yOff2 = 0;
1809 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | ILC_COLOR, 1, 1);
1811 if (himlDst)
1813 imagelist_point_from_index( himl1, i1, &pt1 );
1814 imagelist_point_from_index( himl1, i2, &pt2 );
1816 /* copy image */
1817 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
1818 if (i1 >= 0 && i1 < himl1->cCurImage)
1819 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
1820 if (i2 >= 0 && i2 < himl2->cCurImage)
1822 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
1823 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
1826 /* copy mask */
1827 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
1828 if (i1 >= 0 && i1 < himl1->cCurImage)
1829 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
1830 if (i2 >= 0 && i2 < himl2->cCurImage)
1831 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
1833 himlDst->cCurImage = 1;
1836 return himlDst;
1840 /***********************************************************************
1841 * DIB_GetDIBWidthBytes
1843 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
1845 static int DIB_GetDIBWidthBytes( int width, int depth )
1847 int words;
1849 switch(depth)
1851 case 1: words = (width + 31) / 32; break;
1852 case 4: words = (width + 7) / 8; break;
1853 case 8: words = (width + 3) / 4; break;
1854 case 15:
1855 case 16: words = (width + 1) / 2; break;
1856 case 24: words = (width * 3 + 3)/4; break;
1858 default:
1859 WARN("(%d): Unsupported depth\n", depth );
1860 /* fall through */
1861 case 32:
1862 words = width;
1863 break;
1865 return 4 * words;
1868 /***********************************************************************
1869 * DIB_GetDIBImageBytes
1871 * Return the number of bytes used to hold the image in a DIB bitmap.
1873 static int DIB_GetDIBImageBytes( int width, int height, int depth )
1875 return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
1879 /* helper for ImageList_Read, see comments below */
1880 static BOOL _read_bitmap(HDC hdcIml, LPSTREAM pstm)
1882 BITMAPFILEHEADER bmfh;
1883 int bitsperpixel, palspace;
1884 char bmi_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
1885 LPBITMAPINFO bmi = (LPBITMAPINFO)bmi_buf;
1886 int result = FALSE;
1887 LPBYTE bits = NULL;
1889 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
1890 return FALSE;
1892 if (bmfh.bfType != (('M'<<8)|'B'))
1893 return FALSE;
1895 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
1896 return FALSE;
1898 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
1899 return FALSE;
1901 TRACE("width %u, height %u, planes %u, bpp %u\n",
1902 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
1903 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
1905 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
1906 if (bitsperpixel<=8)
1907 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
1908 else
1909 palspace = 0;
1911 bmi->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bitsperpixel);
1913 /* read the palette right after the end of the bitmapinfoheader */
1914 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
1915 goto error;
1917 bits = Alloc(bmi->bmiHeader.biSizeImage);
1918 if (!bits)
1919 goto error;
1920 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
1921 goto error;
1923 if (!StretchDIBits(hdcIml, 0, 0, bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
1924 0, 0, bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
1925 bits, bmi, DIB_RGB_COLORS, SRCCOPY))
1926 goto error;
1927 result = TRUE;
1929 error:
1930 Free(bits);
1931 return result;
1934 /*************************************************************************
1935 * ImageList_Read [COMCTL32.@]
1937 * Reads an image list from a stream.
1939 * PARAMS
1940 * pstm [I] pointer to a stream
1942 * RETURNS
1943 * Success: handle to image list
1944 * Failure: NULL
1946 * The format is like this:
1947 * ILHEAD ilheadstruct;
1949 * for the color image part:
1950 * BITMAPFILEHEADER bmfh;
1951 * BITMAPINFOHEADER bmih;
1952 * only if it has a palette:
1953 * RGBQUAD rgbs[nr_of_paletted_colors];
1955 * BYTE colorbits[imagesize];
1957 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
1958 * BITMAPFILEHEADER bmfh_mask;
1959 * BITMAPINFOHEADER bmih_mask;
1960 * only if it has a palette (it usually does not):
1961 * RGBQUAD rgbs[nr_of_paletted_colors];
1963 * BYTE maskbits[imagesize];
1965 HIMAGELIST WINAPI ImageList_Read (LPSTREAM pstm)
1967 ILHEAD ilHead;
1968 HIMAGELIST himl;
1969 int i;
1971 TRACE("%p\n", pstm);
1973 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
1974 return NULL;
1975 if (ilHead.usMagic != (('L' << 8) | 'I'))
1976 return NULL;
1977 if (ilHead.usVersion != 0x101) /* probably version? */
1978 return NULL;
1980 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
1981 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
1983 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
1984 if (!himl)
1985 return NULL;
1987 if (!_read_bitmap(himl->hdcImage, pstm))
1989 WARN("failed to read bitmap from stream\n");
1990 return NULL;
1992 if (ilHead.flags & ILC_MASK)
1994 if (!_read_bitmap(himl->hdcMask, pstm))
1996 WARN("failed to read mask bitmap from stream\n");
1997 return NULL;
2001 himl->cCurImage = ilHead.cCurImage;
2002 himl->cMaxImage = ilHead.cMaxImage;
2004 ImageList_SetBkColor(himl,ilHead.bkcolor);
2005 for (i=0;i<4;i++)
2006 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2007 return himl;
2011 /*************************************************************************
2012 * ImageList_Remove [COMCTL32.@]
2014 * Removes an image from an image list
2016 * PARAMS
2017 * himl [I] image list handle
2018 * i [I] image index
2020 * RETURNS
2021 * Success: TRUE
2022 * Failure: FALSE
2024 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2025 * images without creating a new bitmap.
2027 BOOL WINAPI
2028 ImageList_Remove (HIMAGELIST himl, INT i)
2030 HBITMAP hbmNewImage, hbmNewMask;
2031 HDC hdcBmp;
2032 SIZE sz;
2034 TRACE("(himl=%p i=%d)\n", himl, i);
2036 if (!is_valid(himl)) {
2037 ERR("Invalid image list handle!\n");
2038 return FALSE;
2041 if ((i < -1) || (i >= himl->cCurImage)) {
2042 TRACE("index out of range! %d\n", i);
2043 return FALSE;
2046 if (i == -1) {
2047 INT nCount;
2049 /* remove all */
2050 if (himl->cCurImage == 0) {
2051 /* remove all on empty ImageList is allowed */
2052 TRACE("remove all on empty ImageList!\n");
2053 return TRUE;
2056 himl->cMaxImage = himl->cInitial + himl->cGrow - 1;
2057 himl->cCurImage = 0;
2058 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2059 himl->nOvlIdx[nCount] = -1;
2061 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage, himl->cx);
2062 SelectObject (himl->hdcImage, hbmNewImage);
2063 DeleteObject (himl->hbmImage);
2064 himl->hbmImage = hbmNewImage;
2066 if (himl->hbmMask) {
2068 imagelist_get_bitmap_size(himl, himl->cMaxImage, himl->cx, &sz);
2069 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2070 SelectObject (himl->hdcMask, hbmNewMask);
2071 DeleteObject (himl->hbmMask);
2072 himl->hbmMask = hbmNewMask;
2075 else {
2076 /* delete one image */
2077 TRACE("Remove single image! %d\n", i);
2079 /* create new bitmap(s) */
2080 TRACE(" - Number of images: %d / %d (Old/New)\n",
2081 himl->cCurImage, himl->cCurImage - 1);
2083 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage, himl->cx);
2085 imagelist_get_bitmap_size(himl, himl->cMaxImage, himl->cx, &sz );
2086 if (himl->hbmMask)
2087 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2088 else
2089 hbmNewMask = 0; /* Just to keep compiler happy! */
2091 hdcBmp = CreateCompatibleDC (0);
2093 /* copy all images and masks prior to the "removed" image */
2094 if (i > 0) {
2095 TRACE("Pre image copy: Copy %d images\n", i);
2097 SelectObject (hdcBmp, hbmNewImage);
2098 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2100 if (himl->hbmMask) {
2101 SelectObject (hdcBmp, hbmNewMask);
2102 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2106 /* copy all images and masks behind the removed image */
2107 if (i < himl->cCurImage - 1) {
2108 TRACE("Post image copy!\n");
2110 SelectObject (hdcBmp, hbmNewImage);
2111 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2112 (himl->cCurImage - i), i );
2114 if (himl->hbmMask) {
2115 SelectObject (hdcBmp, hbmNewMask);
2116 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2117 (himl->cCurImage - i), i );
2121 DeleteDC (hdcBmp);
2123 /* delete old images and insert new ones */
2124 SelectObject (himl->hdcImage, hbmNewImage);
2125 DeleteObject (himl->hbmImage);
2126 himl->hbmImage = hbmNewImage;
2127 if (himl->hbmMask) {
2128 SelectObject (himl->hdcMask, hbmNewMask);
2129 DeleteObject (himl->hbmMask);
2130 himl->hbmMask = hbmNewMask;
2133 himl->cCurImage--;
2136 return TRUE;
2140 /*************************************************************************
2141 * ImageList_Replace [COMCTL32.@]
2143 * Replaces an image in an image list with a new image.
2145 * PARAMS
2146 * himl [I] handle to image list
2147 * i [I] image index
2148 * hbmImage [I] handle to image bitmap
2149 * hbmMask [I] handle to mask bitmap. Can be NULL.
2151 * RETURNS
2152 * Success: TRUE
2153 * Failure: FALSE
2156 BOOL WINAPI
2157 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2158 HBITMAP hbmMask)
2160 HDC hdcImage;
2161 BITMAP bmp;
2162 HBITMAP hOldBitmap;
2163 POINT pt;
2165 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2167 if (!is_valid(himl)) {
2168 ERR("Invalid image list handle!\n");
2169 return FALSE;
2172 if ((i >= himl->cMaxImage) || (i < 0)) {
2173 ERR("Invalid image index!\n");
2174 return FALSE;
2177 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2178 return FALSE;
2180 hdcImage = CreateCompatibleDC (0);
2182 /* Replace Image */
2183 hOldBitmap = SelectObject (hdcImage, hbmImage);
2185 imagelist_point_from_index(himl, i, &pt);
2186 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2187 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2189 if (himl->hbmMask)
2191 HDC hdcTemp;
2192 HBITMAP hOldBitmapTemp;
2194 hdcTemp = CreateCompatibleDC(0);
2195 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2197 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2198 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2199 SelectObject(hdcTemp, hOldBitmapTemp);
2200 DeleteDC(hdcTemp);
2202 /* Remove the background from the image
2204 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2205 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2208 SelectObject (hdcImage, hOldBitmap);
2209 DeleteDC (hdcImage);
2211 return TRUE;
2215 /*************************************************************************
2216 * ImageList_ReplaceIcon [COMCTL32.@]
2218 * Replaces an image in an image list using an icon.
2220 * PARAMS
2221 * himl [I] handle to image list
2222 * i [I] image index
2223 * hIcon [I] handle to icon
2225 * RETURNS
2226 * Success: index of the replaced image
2227 * Failure: -1
2230 INT WINAPI
2231 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2233 HDC hdcImage;
2234 HICON hBestFitIcon;
2235 HBITMAP hbmOldSrc;
2236 ICONINFO ii;
2237 BITMAP bmp;
2238 BOOL ret;
2239 POINT pt;
2241 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2243 if (!is_valid(himl)) {
2244 ERR("invalid image list\n");
2245 return -1;
2247 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2248 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2249 return -1;
2252 hBestFitIcon = CopyImage(
2253 hIcon, IMAGE_ICON,
2254 himl->cx, himl->cy,
2255 LR_COPYFROMRESOURCE);
2256 /* the above will fail if the icon wasn't loaded from a resource, so try
2257 * again without LR_COPYFROMRESOURCE flag */
2258 if (!hBestFitIcon)
2259 hBestFitIcon = CopyImage(
2260 hIcon, IMAGE_ICON,
2261 himl->cx, himl->cy,
2263 if (!hBestFitIcon)
2264 return -1;
2266 ret = GetIconInfo (hBestFitIcon, &ii);
2267 if (!ret) {
2268 DestroyIcon(hBestFitIcon);
2269 return -1;
2272 ret = GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2273 if (!ret) {
2274 ERR("couldn't get mask bitmap info\n");
2275 if (ii.hbmColor)
2276 DeleteObject (ii.hbmColor);
2277 if (ii.hbmMask)
2278 DeleteObject (ii.hbmMask);
2279 DestroyIcon(hBestFitIcon);
2280 return -1;
2283 if (nIndex == -1) {
2284 if (himl->cCurImage + 1 > himl->cMaxImage)
2285 IMAGELIST_InternalExpandBitmaps (himl, 1, 0, 0);
2287 nIndex = himl->cCurImage;
2288 himl->cCurImage++;
2291 hdcImage = CreateCompatibleDC (0);
2292 TRACE("hdcImage=%p\n", hdcImage);
2293 if (hdcImage == 0)
2294 ERR("invalid hdcImage!\n");
2296 imagelist_point_from_index(himl, nIndex, &pt);
2298 SetTextColor(himl->hdcImage, RGB(0,0,0));
2299 SetBkColor (himl->hdcImage, RGB(255,255,255));
2301 if (ii.hbmColor)
2303 hbmOldSrc = SelectObject (hdcImage, ii.hbmColor);
2304 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2305 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2306 if (himl->hbmMask)
2308 SelectObject (hdcImage, ii.hbmMask);
2309 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2310 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2313 else
2315 UINT height = bmp.bmHeight / 2;
2316 hbmOldSrc = SelectObject (hdcImage, ii.hbmMask);
2317 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2318 hdcImage, 0, height, bmp.bmWidth, height, SRCCOPY);
2319 if (himl->hbmMask)
2320 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2321 hdcImage, 0, 0, bmp.bmWidth, height, SRCCOPY);
2324 SelectObject (hdcImage, hbmOldSrc);
2326 DestroyIcon(hBestFitIcon);
2327 if (hdcImage)
2328 DeleteDC (hdcImage);
2329 if (ii.hbmColor)
2330 DeleteObject (ii.hbmColor);
2331 if (ii.hbmMask)
2332 DeleteObject (ii.hbmMask);
2334 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2335 return nIndex;
2339 /*************************************************************************
2340 * ImageList_SetBkColor [COMCTL32.@]
2342 * Sets the background color of an image list.
2344 * PARAMS
2345 * himl [I] handle to image list
2346 * clrBk [I] background color
2348 * RETURNS
2349 * Success: previous background color
2350 * Failure: CLR_NONE
2353 COLORREF WINAPI
2354 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2356 COLORREF clrOldBk;
2358 if (!is_valid(himl))
2359 return CLR_NONE;
2361 clrOldBk = himl->clrBk;
2362 himl->clrBk = clrBk;
2363 return clrOldBk;
2367 /*************************************************************************
2368 * ImageList_SetDragCursorImage [COMCTL32.@]
2370 * Combines the specified image with the current drag image
2372 * PARAMS
2373 * himlDrag [I] handle to drag image list
2374 * iDrag [I] drag image index
2375 * dxHotspot [I] X position of the hot spot
2376 * dyHotspot [I] Y position of the hot spot
2378 * RETURNS
2379 * Success: TRUE
2380 * Failure: FALSE
2382 * NOTES
2383 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2384 * to do with a hotspot but are only the offset of the origin of the new
2385 * image relative to the origin of the old image.
2387 * - When this function is called and the drag image is visible, a
2388 * short flickering occurs but this matches the Win9x behavior. It is
2389 * possible to fix the flickering using code like in ImageList_DragMove.
2392 BOOL WINAPI
2393 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2394 INT dxHotspot, INT dyHotspot)
2396 HIMAGELIST himlTemp;
2397 BOOL visible;
2399 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2400 return FALSE;
2402 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2403 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2405 visible = InternalDrag.bShow;
2407 himlTemp = ImageList_Merge (InternalDrag.himl, 0, himlDrag, iDrag,
2408 dxHotspot, dyHotspot);
2410 if (visible) {
2411 /* hide the drag image */
2412 ImageList_DragShowNolock(FALSE);
2414 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2415 (InternalDrag.himl->cy != himlTemp->cy)) {
2416 /* the size of the drag image changed, invalidate the buffer */
2417 DeleteObject(InternalDrag.hbmBg);
2418 InternalDrag.hbmBg = 0;
2421 ImageList_Destroy (InternalDrag.himl);
2422 InternalDrag.himl = himlTemp;
2424 if (visible) {
2425 /* show the drag image */
2426 ImageList_DragShowNolock(TRUE);
2429 return TRUE;
2433 /*************************************************************************
2434 * ImageList_SetFilter [COMCTL32.@]
2436 * Sets a filter (or does something completely different)!!???
2437 * It removes 12 Bytes from the stack (3 Parameters).
2439 * PARAMS
2440 * himl [I] SHOULD be a handle to image list
2441 * i [I] COULD be an index?
2442 * dwFilter [I] ???
2444 * RETURNS
2445 * Success: TRUE ???
2446 * Failure: FALSE ???
2448 * BUGS
2449 * This is an UNDOCUMENTED function!!!!
2450 * empty stub.
2453 BOOL WINAPI
2454 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2456 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2458 return FALSE;
2462 /*************************************************************************
2463 * ImageList_SetFlags [COMCTL32.@]
2465 * Sets the image list flags.
2467 * PARAMS
2468 * himl [I] Handle to image list
2469 * flags [I] Flags to set
2471 * RETURNS
2472 * Old flags?
2474 * BUGS
2475 * Stub.
2478 DWORD WINAPI
2479 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2481 FIXME("(%p %08x):empty stub\n", himl, flags);
2482 return 0;
2486 /*************************************************************************
2487 * ImageList_SetIconSize [COMCTL32.@]
2489 * Sets the image size of the bitmap and deletes all images.
2491 * PARAMS
2492 * himl [I] handle to image list
2493 * cx [I] image width
2494 * cy [I] image height
2496 * RETURNS
2497 * Success: TRUE
2498 * Failure: FALSE
2501 BOOL WINAPI
2502 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2504 INT nCount;
2505 HBITMAP hbmNew;
2507 if (!is_valid(himl))
2508 return FALSE;
2510 /* remove all images */
2511 himl->cMaxImage = himl->cInitial + 1;
2512 himl->cCurImage = 0;
2513 himl->cx = cx;
2514 himl->cy = cy;
2516 /* initialize overlay mask indices */
2517 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2518 himl->nOvlIdx[nCount] = -1;
2520 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage, himl->cx);
2521 SelectObject (himl->hdcImage, hbmNew);
2522 DeleteObject (himl->hbmImage);
2523 himl->hbmImage = hbmNew;
2525 if (himl->hbmMask) {
2526 SIZE sz;
2527 imagelist_get_bitmap_size(himl, himl->cMaxImage, himl->cx, &sz);
2528 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2529 SelectObject (himl->hdcMask, hbmNew);
2530 DeleteObject (himl->hbmMask);
2531 himl->hbmMask = hbmNew;
2534 return TRUE;
2538 /*************************************************************************
2539 * ImageList_SetImageCount [COMCTL32.@]
2541 * Resizes an image list to the specified number of images.
2543 * PARAMS
2544 * himl [I] handle to image list
2545 * iImageCount [I] number of images in the image list
2547 * RETURNS
2548 * Success: TRUE
2549 * Failure: FALSE
2552 BOOL WINAPI
2553 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2555 HDC hdcBitmap;
2556 HBITMAP hbmNewBitmap, hbmOld;
2557 INT nNewCount, nCopyCount;
2559 TRACE("%p %d\n",himl,iImageCount);
2561 if (!is_valid(himl))
2562 return FALSE;
2563 if (himl->cMaxImage > iImageCount)
2565 himl->cCurImage = iImageCount;
2566 /* TODO: shrink the bitmap when cMaxImage-cCurImage>cGrow ? */
2567 return TRUE;
2570 nNewCount = iImageCount + himl->cGrow;
2571 nCopyCount = min(himl->cCurImage, iImageCount);
2573 hdcBitmap = CreateCompatibleDC (0);
2575 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount, himl->cx);
2577 if (hbmNewBitmap != 0)
2579 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2580 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2581 SelectObject (hdcBitmap, hbmOld);
2583 /* FIXME: delete 'empty' image space? */
2585 SelectObject (himl->hdcImage, hbmNewBitmap);
2586 DeleteObject (himl->hbmImage);
2587 himl->hbmImage = hbmNewBitmap;
2589 else
2590 ERR("Could not create new image bitmap !\n");
2592 if (himl->hbmMask)
2594 SIZE sz;
2595 imagelist_get_bitmap_size( himl, nNewCount, himl->cx, &sz );
2596 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2597 if (hbmNewBitmap != 0)
2599 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2600 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2601 SelectObject (hdcBitmap, hbmOld);
2603 /* FIXME: delete 'empty' image space? */
2605 SelectObject (himl->hdcMask, hbmNewBitmap);
2606 DeleteObject (himl->hbmMask);
2607 himl->hbmMask = hbmNewBitmap;
2609 else
2610 ERR("Could not create new mask bitmap!\n");
2613 DeleteDC (hdcBitmap);
2615 /* Update max image count and current image count */
2616 himl->cMaxImage = nNewCount;
2617 himl->cCurImage = iImageCount;
2619 return TRUE;
2623 /*************************************************************************
2624 * ImageList_SetOverlayImage [COMCTL32.@]
2626 * Assigns an overlay mask index to an existing image in an image list.
2628 * PARAMS
2629 * himl [I] handle to image list
2630 * iImage [I] image index
2631 * iOverlay [I] overlay mask index
2633 * RETURNS
2634 * Success: TRUE
2635 * Failure: FALSE
2638 BOOL WINAPI
2639 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2641 if (!is_valid(himl))
2642 return FALSE;
2643 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2644 return FALSE;
2645 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2646 return FALSE;
2647 himl->nOvlIdx[iOverlay - 1] = iImage;
2648 return TRUE;
2653 /* helper for ImageList_Write - write bitmap to pstm
2654 * currently everything is written as 24 bit RGB, except masks
2656 static BOOL
2657 _write_bitmap(HBITMAP hBitmap, LPSTREAM pstm)
2659 LPBITMAPFILEHEADER bmfh;
2660 LPBITMAPINFOHEADER bmih;
2661 LPBYTE data = NULL, lpBits;
2662 BITMAP bm;
2663 INT bitCount, sizeImage, offBits, totalSize;
2664 HDC xdc;
2665 BOOL result = FALSE;
2667 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2668 return FALSE;
2670 bitCount = bm.bmBitsPixel == 1 ? 1 : 24;
2671 sizeImage = DIB_GetDIBImageBytes(bm.bmWidth, bm.bmHeight, bitCount);
2673 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
2674 if(bitCount != 24)
2675 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
2676 offBits = totalSize;
2677 totalSize += sizeImage;
2679 data = Alloc(totalSize);
2680 bmfh = (LPBITMAPFILEHEADER)data;
2681 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
2682 lpBits = data + offBits;
2684 /* setup BITMAPFILEHEADER */
2685 bmfh->bfType = (('M' << 8) | 'B');
2686 bmfh->bfSize = offBits;
2687 bmfh->bfReserved1 = 0;
2688 bmfh->bfReserved2 = 0;
2689 bmfh->bfOffBits = offBits;
2691 /* setup BITMAPINFOHEADER */
2692 bmih->biSize = sizeof(BITMAPINFOHEADER);
2693 bmih->biWidth = bm.bmWidth;
2694 bmih->biHeight = bm.bmHeight;
2695 bmih->biPlanes = 1;
2696 bmih->biBitCount = bitCount;
2697 bmih->biCompression = BI_RGB;
2698 bmih->biSizeImage = sizeImage;
2699 bmih->biXPelsPerMeter = 0;
2700 bmih->biYPelsPerMeter = 0;
2701 bmih->biClrUsed = 0;
2702 bmih->biClrImportant = 0;
2704 xdc = GetDC(0);
2705 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
2706 ReleaseDC(0, xdc);
2707 if (!result)
2708 goto failed;
2710 TRACE("width %u, height %u, planes %u, bpp %u\n",
2711 bmih->biWidth, bmih->biHeight,
2712 bmih->biPlanes, bmih->biBitCount);
2714 if(bitCount == 1) {
2715 /* Hack. */
2716 LPBITMAPINFO inf = (LPBITMAPINFO)bmih;
2717 inf->bmiColors[0].rgbRed = inf->bmiColors[0].rgbGreen = inf->bmiColors[0].rgbBlue = 0;
2718 inf->bmiColors[1].rgbRed = inf->bmiColors[1].rgbGreen = inf->bmiColors[1].rgbBlue = 0xff;
2721 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
2722 goto failed;
2724 result = TRUE;
2726 failed:
2727 Free(data);
2729 return result;
2733 /*************************************************************************
2734 * ImageList_Write [COMCTL32.@]
2736 * Writes an image list to a stream.
2738 * PARAMS
2739 * himl [I] handle to image list
2740 * pstm [O] Pointer to a stream.
2742 * RETURNS
2743 * Success: TRUE
2744 * Failure: FALSE
2746 * BUGS
2747 * probably.
2750 BOOL WINAPI
2751 ImageList_Write (HIMAGELIST himl, LPSTREAM pstm)
2753 ILHEAD ilHead;
2754 int i;
2756 TRACE("%p %p\n", himl, pstm);
2758 if (!is_valid(himl))
2759 return FALSE;
2761 ilHead.usMagic = (('L' << 8) | 'I');
2762 ilHead.usVersion = 0x101;
2763 ilHead.cCurImage = himl->cCurImage;
2764 ilHead.cMaxImage = himl->cMaxImage;
2765 ilHead.cGrow = himl->cGrow;
2766 ilHead.cx = himl->cx;
2767 ilHead.cy = himl->cy;
2768 ilHead.bkcolor = himl->clrBk;
2769 ilHead.flags = himl->flags;
2770 for(i = 0; i < 4; i++) {
2771 ilHead.ovls[i] = himl->nOvlIdx[i];
2774 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
2775 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2777 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
2778 return FALSE;
2780 /* write the bitmap */
2781 if(!_write_bitmap(himl->hbmImage, pstm))
2782 return FALSE;
2784 /* write the mask if we have one */
2785 if(himl->flags & ILC_MASK) {
2786 if(!_write_bitmap(himl->hbmMask, pstm))
2787 return FALSE;
2790 return TRUE;
2794 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count, UINT width)
2796 HBITMAP hbmNewBitmap;
2797 UINT ilc = (himl->flags & 0xFE);
2798 SIZE sz;
2800 imagelist_get_bitmap_size( himl, count, width, &sz );
2802 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
2804 VOID* bits;
2805 BITMAPINFO *bmi;
2807 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
2808 sz.cx, sz.cy, himl->uBitsPixel);
2810 if (himl->uBitsPixel <= ILC_COLOR8)
2812 LPPALETTEENTRY pal;
2813 ULONG i, colors;
2814 BYTE temp;
2816 colors = 1 << himl->uBitsPixel;
2817 bmi = Alloc(sizeof(BITMAPINFOHEADER) +
2818 sizeof(PALETTEENTRY) * colors);
2820 pal = (LPPALETTEENTRY)bmi->bmiColors;
2821 GetPaletteEntries(GetStockObject(DEFAULT_PALETTE), 0, colors, pal);
2823 /* Swap colors returned by GetPaletteEntries so we can use them for
2824 * CreateDIBSection call. */
2825 for (i = 0; i < colors; i++)
2827 temp = pal[i].peBlue;
2828 bmi->bmiColors[i].rgbRed = pal[i].peRed;
2829 bmi->bmiColors[i].rgbBlue = temp;
2832 else
2834 bmi = Alloc(sizeof(BITMAPINFOHEADER));
2837 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2838 bmi->bmiHeader.biWidth = sz.cx;
2839 bmi->bmiHeader.biHeight = sz.cy;
2840 bmi->bmiHeader.biPlanes = 1;
2841 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
2842 bmi->bmiHeader.biCompression = BI_RGB;
2843 bmi->bmiHeader.biSizeImage = 0;
2844 bmi->bmiHeader.biXPelsPerMeter = 0;
2845 bmi->bmiHeader.biYPelsPerMeter = 0;
2846 bmi->bmiHeader.biClrUsed = 0;
2847 bmi->bmiHeader.biClrImportant = 0;
2849 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, &bits, 0, 0);
2851 Free (bmi);
2853 else /*if (ilc == ILC_COLORDDB)*/
2855 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
2857 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
2859 TRACE("returning %p\n", hbmNewBitmap);
2860 return hbmNewBitmap;
2863 /*************************************************************************
2864 * ImageList_SetColorTable [COMCTL32.@]
2866 * Sets the color table of an image list.
2868 * PARAMS
2869 * himl [I] Handle to the image list.
2870 * uStartIndex [I] The first index to set.
2871 * cEntries [I] Number of entries to set.
2872 * prgb [I] New color information for color table for the image list.
2874 * RETURNS
2875 * Success: Number of entries in the table that were set.
2876 * Failure: Zero.
2878 * SEE
2879 * ImageList_Create(), SetDIBColorTable()
2882 UINT WINAPI
2883 ImageList_SetColorTable (HIMAGELIST himl, UINT uStartIndex, UINT cEntries, CONST RGBQUAD * prgb)
2885 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
2888 /*************************************************************************
2889 * ImageList_CoCreateInstance [COMCTL32.@]
2891 * Creates a new imagelist instance and returns an interface pointer to it.
2893 * PARAMS
2894 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
2895 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
2896 * riid [I] Identifier of the requested interface.
2897 * ppv [O] Returns the address of the pointer requested, or NULL.
2899 * RETURNS
2900 * Success: S_OK.
2901 * Failure: Error value.
2903 HRESULT WINAPI
2904 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
2906 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
2908 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
2909 return E_NOINTERFACE;
2911 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
2915 /*************************************************************************
2916 * IImageList implementation
2919 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList *iface,
2920 REFIID iid, void **ppv)
2922 HIMAGELIST This = (HIMAGELIST) iface;
2923 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
2925 if (!ppv) return E_INVALIDARG;
2927 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IImageList, iid))
2928 *ppv = This;
2929 else
2931 *ppv = NULL;
2932 return E_NOINTERFACE;
2935 IUnknown_AddRef((IUnknown*)*ppv);
2936 return S_OK;
2939 static ULONG WINAPI ImageListImpl_AddRef(IImageList *iface)
2941 HIMAGELIST This = (HIMAGELIST) iface;
2942 ULONG ref = InterlockedIncrement(&This->ref);
2944 TRACE("(%p) refcount=%u\n", iface, ref);
2945 return ref;
2948 static ULONG WINAPI ImageListImpl_Release(IImageList *iface)
2950 HIMAGELIST This = (HIMAGELIST) iface;
2951 ULONG ref = InterlockedDecrement(&This->ref);
2953 TRACE("(%p) refcount=%u\n", iface, ref);
2955 if (ref == 0)
2957 /* delete image bitmaps */
2958 if (This->hbmImage) DeleteObject (This->hbmImage);
2959 if (This->hbmMask) DeleteObject (This->hbmMask);
2961 /* delete image & mask DCs */
2962 if (This->hdcImage) DeleteDC (This->hdcImage);
2963 if (This->hdcMask) DeleteDC (This->hdcMask);
2965 /* delete blending brushes */
2966 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
2967 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
2969 This->lpVtbl = NULL;
2970 HeapFree(GetProcessHeap(), 0, This);
2973 return ref;
2976 static HRESULT WINAPI ImageListImpl_Add(IImageList *iface, HBITMAP hbmImage,
2977 HBITMAP hbmMask, int *pi)
2979 HIMAGELIST This = (HIMAGELIST) iface;
2980 int ret;
2982 if (!pi)
2983 return E_FAIL;
2985 ret = ImageList_Add(This, hbmImage, hbmMask);
2987 if (ret == -1)
2988 return E_FAIL;
2990 *pi = ret;
2991 return S_OK;
2994 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList *iface, int i,
2995 HICON hicon, int *pi)
2997 HIMAGELIST This = (HIMAGELIST) iface;
2998 int ret;
3000 if (!pi)
3001 return E_FAIL;
3003 ret = ImageList_ReplaceIcon(This, i, hicon);
3005 if (ret == -1)
3006 return E_FAIL;
3008 *pi = ret;
3009 return S_OK;
3012 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList *iface,
3013 int iImage, int iOverlay)
3015 return ImageList_SetOverlayImage((HIMAGELIST) iface, iImage, iOverlay)
3016 ? S_OK : E_FAIL;
3019 static HRESULT WINAPI ImageListImpl_Replace(IImageList *iface, int i,
3020 HBITMAP hbmImage, HBITMAP hbmMask)
3022 return ImageList_Replace((HIMAGELIST) iface, i, hbmImage, hbmMask) ? S_OK :
3023 E_FAIL;
3026 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList *iface, HBITMAP hbmImage,
3027 COLORREF crMask, int *pi)
3029 HIMAGELIST This = (HIMAGELIST) iface;
3030 int ret;
3032 if (!pi)
3033 return E_FAIL;
3035 ret = ImageList_AddMasked(This, hbmImage, crMask);
3037 if (ret == -1)
3038 return E_FAIL;
3040 *pi = ret;
3041 return S_OK;
3044 static HRESULT WINAPI ImageListImpl_Draw(IImageList *iface,
3045 IMAGELISTDRAWPARAMS *pimldp)
3047 HIMAGELIST This = (HIMAGELIST) iface;
3048 HIMAGELIST old_himl = 0;
3049 int ret = 0;
3051 if (!pimldp)
3052 return E_FAIL;
3054 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3055 so we shall simulate the same */
3056 old_himl = pimldp->himl;
3057 pimldp->himl = This;
3059 ret = ImageList_DrawIndirect(pimldp);
3061 pimldp->himl = old_himl;
3062 return ret ? S_OK : E_FAIL;
3065 static HRESULT WINAPI ImageListImpl_Remove(IImageList *iface, int i)
3067 return (ImageList_Remove((HIMAGELIST) iface, i) == 0) ? E_FAIL : S_OK;
3070 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList *iface, int i, UINT flags,
3071 HICON *picon)
3073 HICON hIcon;
3075 if (!picon)
3076 return E_FAIL;
3078 hIcon = ImageList_GetIcon((HIMAGELIST) iface, i, flags);
3080 if (hIcon == NULL)
3081 return E_FAIL;
3083 *picon = hIcon;
3084 return S_OK;
3087 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList *iface, int i,
3088 IMAGEINFO *pImageInfo)
3090 return ImageList_GetImageInfo((HIMAGELIST) iface, i, pImageInfo) ? S_OK : E_FAIL;
3093 static HRESULT WINAPI ImageListImpl_Copy(IImageList *iface, int iDst,
3094 IUnknown *punkSrc, int iSrc, UINT uFlags)
3096 HIMAGELIST This = (HIMAGELIST) iface;
3097 IImageList *src = NULL;
3098 HRESULT ret;
3100 if (!punkSrc)
3101 return E_FAIL;
3103 /* TODO: Add test for IID_ImageList2 too */
3104 if (FAILED(IImageList_QueryInterface(punkSrc, &IID_IImageList,
3105 (void **) &src)))
3106 return E_FAIL;
3108 if (ImageList_Copy(This, iDst, (HIMAGELIST) src, iSrc, uFlags))
3109 ret = S_OK;
3110 else
3111 ret = E_FAIL;
3113 IImageList_Release(src);
3114 return ret;
3117 static HRESULT WINAPI ImageListImpl_Merge(IImageList *iface, int i1,
3118 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, PVOID *ppv)
3120 HIMAGELIST This = (HIMAGELIST) iface;
3121 IImageList *iml2 = NULL;
3122 HIMAGELIST hNew;
3123 HRESULT ret = E_FAIL;
3125 if (!punk2 || !ppv)
3126 return E_FAIL;
3128 /* TODO: Add test for IID_ImageList2 too */
3129 if (FAILED(IImageList_QueryInterface(punk2, &IID_IImageList,
3130 (void **) &iml2)))
3131 return E_FAIL;
3133 hNew = ImageList_Merge(This, i1, (HIMAGELIST) iml2, i2, dx, dy);
3135 /* Get the interface for the new image list */
3136 if (hNew)
3137 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3139 IImageList_Release(iml2);
3140 return ret;
3143 static HRESULT WINAPI ImageListImpl_Clone(IImageList *iface, REFIID riid,
3144 PVOID *ppv)
3146 HIMAGELIST This = (HIMAGELIST) iface;
3147 HIMAGELIST hNew;
3148 HRESULT ret = E_FAIL;
3150 if (!ppv)
3151 return E_FAIL;
3153 hNew = ImageList_Duplicate(This);
3155 /* Get the interface for the new image list */
3156 if (hNew)
3157 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3159 return ret;
3162 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList *iface, int i,
3163 RECT *prc)
3165 HIMAGELIST This = (HIMAGELIST) iface;
3166 IMAGEINFO info;
3168 if (!prc)
3169 return E_FAIL;
3171 if (!ImageList_GetImageInfo(This, i, &info))
3172 return E_FAIL;
3174 return CopyRect(prc, &info.rcImage) ? S_OK : E_FAIL;
3177 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList *iface, int *cx,
3178 int *cy)
3180 HIMAGELIST This = (HIMAGELIST) iface;
3182 return ImageList_GetIconSize(This, cx, cy) ? S_OK : E_FAIL;
3185 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList *iface, int cx,
3186 int cy)
3188 return ImageList_SetIconSize((HIMAGELIST) iface, cx, cy) ? S_OK : E_FAIL;
3191 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList *iface, int *pi)
3193 if (!pi)
3194 return E_FAIL;
3196 *pi = ImageList_GetImageCount((HIMAGELIST) iface);
3197 return S_OK;
3200 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList *iface,
3201 UINT uNewCount)
3203 return ImageList_SetImageCount((HIMAGELIST) iface, uNewCount) ? S_OK : E_FAIL;
3206 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList *iface, COLORREF clrBk,
3207 COLORREF *pclr)
3209 if (!pclr)
3210 return E_FAIL;
3212 *pclr = ImageList_SetBkColor((HIMAGELIST) iface, clrBk);
3213 return *pclr == CLR_NONE ? E_FAIL : S_OK;
3216 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList *iface, COLORREF *pclr)
3218 if (!pclr)
3219 return E_FAIL;
3221 *pclr = ImageList_GetBkColor((HIMAGELIST) iface);
3222 return S_OK;
3225 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList *iface, int iTrack,
3226 int dxHotspot, int dyHotspot)
3228 return ImageList_BeginDrag((HIMAGELIST) iface, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3231 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList *iface)
3233 ImageList_EndDrag();
3234 return S_OK;
3237 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList *iface, HWND hwndLock,
3238 int x, int y)
3240 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3243 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList *iface, HWND hwndLock)
3245 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3248 static HRESULT WINAPI ImageListImpl_DragMove(IImageList *iface, int x, int y)
3250 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3253 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList *iface,
3254 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3256 IImageList *iml2 = NULL;
3257 HRESULT ret;
3259 if (!punk)
3260 return E_FAIL;
3262 /* TODO: Add test for IID_ImageList2 too */
3263 if (FAILED(IImageList_QueryInterface(punk, &IID_IImageList,
3264 (void **) &iml2)))
3265 return E_FAIL;
3267 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3268 dyHotspot);
3270 IImageList_Release(iml2);
3272 return ret ? S_OK : E_FAIL;
3275 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList *iface, BOOL fShow)
3277 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3280 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList *iface, POINT *ppt,
3281 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3283 HRESULT ret = E_FAIL;
3284 HIMAGELIST hNew;
3286 if (!ppv)
3287 return E_FAIL;
3289 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3291 /* Get the interface for the new image list */
3292 if (hNew)
3293 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3295 return ret;
3298 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList *iface, int i,
3299 DWORD *dwFlags)
3301 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3302 return E_NOTIMPL;
3305 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList *iface, int iOverlay,
3306 int *piIndex)
3308 HIMAGELIST This = (HIMAGELIST) iface;
3309 int i;
3311 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3312 return E_FAIL;
3314 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3316 if (This->nOvlIdx[i] == iOverlay)
3318 *piIndex = i + 1;
3319 return S_OK;
3323 return E_FAIL;
3327 static const IImageListVtbl ImageListImpl_Vtbl = {
3328 ImageListImpl_QueryInterface,
3329 ImageListImpl_AddRef,
3330 ImageListImpl_Release,
3331 ImageListImpl_Add,
3332 ImageListImpl_ReplaceIcon,
3333 ImageListImpl_SetOverlayImage,
3334 ImageListImpl_Replace,
3335 ImageListImpl_AddMasked,
3336 ImageListImpl_Draw,
3337 ImageListImpl_Remove,
3338 ImageListImpl_GetIcon,
3339 ImageListImpl_GetImageInfo,
3340 ImageListImpl_Copy,
3341 ImageListImpl_Merge,
3342 ImageListImpl_Clone,
3343 ImageListImpl_GetImageRect,
3344 ImageListImpl_GetIconSize,
3345 ImageListImpl_SetIconSize,
3346 ImageListImpl_GetImageCount,
3347 ImageListImpl_SetImageCount,
3348 ImageListImpl_SetBkColor,
3349 ImageListImpl_GetBkColor,
3350 ImageListImpl_BeginDrag,
3351 ImageListImpl_EndDrag,
3352 ImageListImpl_DragEnter,
3353 ImageListImpl_DragLeave,
3354 ImageListImpl_DragMove,
3355 ImageListImpl_SetDragCursorImage,
3356 ImageListImpl_DragShowNolock,
3357 ImageListImpl_GetDragImage,
3358 ImageListImpl_GetItemFlags,
3359 ImageListImpl_GetOverlayImage
3362 static inline BOOL is_valid(HIMAGELIST himl)
3364 return himl && himl->lpVtbl == &ImageListImpl_Vtbl;
3367 /*************************************************************************
3368 * HIMAGELIST_QueryInterface [COMCTL32.@]
3370 * Returns a pointer to an IImageList or IImageList2 object for the given
3371 * HIMAGELIST.
3373 * PARAMS
3374 * himl [I] Image list handle.
3375 * riid [I] Identifier of the requested interface.
3376 * ppv [O] Returns the address of the pointer requested, or NULL.
3378 * RETURNS
3379 * Success: S_OK.
3380 * Failure: Error value.
3382 HRESULT WINAPI
3383 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3385 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3386 return IImageList_QueryInterface((IImageList *) himl, riid, ppv);
3389 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3391 HIMAGELIST This;
3392 HRESULT ret;
3394 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3396 *ppv = NULL;
3398 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3400 This = HeapAlloc(GetProcessHeap(), 0, sizeof(struct _IMAGELIST));
3401 if (!This) return E_OUTOFMEMORY;
3403 ZeroMemory(This, sizeof(struct _IMAGELIST));
3405 This->lpVtbl = &ImageListImpl_Vtbl;
3406 This->ref = 1;
3408 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
3409 IUnknown_Release((IUnknown*)This);
3411 return ret;