* Sync up to trunk HEAD (r62975).
[reactos.git] / dll / win32 / comctl32 / imagelist.c
1 /*
2 * ImageList implementation
3 *
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
10 *
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.
15 *
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.
20 *
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
24 *
25 * NOTE
26 *
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.
29 *
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.
33 *
34 * TODO:
35 * - Add support for ILD_PRESERVEALPHA, ILD_SCALE, ILD_DPISCALE
36 * - Add support for ILS_GLOW, ILS_SHADOW, ILS_SATURATE
37 * - Thread-safe locking
38 */
39
40 #include "comctl32.h"
41
42 #include <commoncontrols.h>
43 #include <wine/exception.h>
44
45 WINE_DEFAULT_DEBUG_CHANNEL(imagelist);
46
47 #define MAX_OVERLAYIMAGE 15
48
49 struct _IMAGELIST
50 {
51 const struct IImageListVtbl *lpVtbl; /* 00: IImageList vtable */
52
53 INT cCurImage; /* 04: ImageCount */
54 INT cMaxImage; /* 08: maximages */
55 INT cGrow; /* 0C: cGrow */
56 INT cx; /* 10: cx */
57 INT cy; /* 14: cy */
58 DWORD x4;
59 UINT flags; /* 1C: flags */
60 COLORREF clrFg; /* 20: foreground color */
61 COLORREF clrBk; /* 24: background color */
62
63
64 HBITMAP hbmImage; /* 28: images Bitmap */
65 HBITMAP hbmMask; /* 2C: masks Bitmap */
66 HDC hdcImage; /* 30: images MemDC */
67 HDC hdcMask; /* 34: masks MemDC */
68 INT nOvlIdx[MAX_OVERLAYIMAGE]; /* 38: overlay images index */
69
70 /* not yet found out */
71 HBRUSH hbrBlend25;
72 HBRUSH hbrBlend50;
73 INT cInitial;
74 UINT uBitsPixel;
75 char *has_alpha;
76
77 LONG ref; /* reference count */
78 };
79
80 #define IMAGELIST_MAGIC 0x53414D58
81
82 /* Header used by ImageList_Read() and ImageList_Write() */
83 #include "pshpack2.h"
84 typedef struct _ILHEAD
85 {
86 USHORT usMagic;
87 USHORT usVersion;
88 WORD cCurImage;
89 WORD cMaxImage;
90 WORD cGrow;
91 WORD cx;
92 WORD cy;
93 COLORREF bkcolor;
94 WORD flags;
95 SHORT ovls[4];
96 } ILHEAD;
97 #include "poppack.h"
98
99 /* internal image list data used for Drag & Drop operations */
100 typedef struct
101 {
102 HWND hwnd;
103 HIMAGELIST himl;
104 HIMAGELIST himlNoCursor;
105 /* position of the drag image relative to the window */
106 INT x;
107 INT y;
108 /* offset of the hotspot relative to the origin of the image */
109 INT dxHotspot;
110 INT dyHotspot;
111 /* is the drag image visible */
112 BOOL bShow;
113 /* saved background */
114 HBITMAP hbmBg;
115 } INTERNALDRAG;
116
117 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, 0, FALSE, 0 };
118
119 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
120 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
121 static inline BOOL is_valid(HIMAGELIST himl);
122
123 /*
124 * An imagelist with N images is tiled like this:
125 *
126 * N/4 ->
127 *
128 * 4 048C..
129 * 159D..
130 * | 26AE.N
131 * V 37BF.
132 */
133
134 #define TILE_COUNT 4
135
136 static inline UINT imagelist_height( UINT count )
137 {
138 return ((count + TILE_COUNT - 1)/TILE_COUNT);
139 }
140
141 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
142 {
143 pt->x = (index%TILE_COUNT) * himl->cx;
144 pt->y = (index/TILE_COUNT) * himl->cy;
145 }
146
147 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
148 {
149 sz->cx = himl->cx * TILE_COUNT;
150 sz->cy = imagelist_height( count ) * himl->cy;
151 }
152
153 static inline int get_dib_stride( int width, int bpp )
154 {
155 return ((width * bpp + 31) >> 3) & ~3;
156 }
157
158 static inline int get_dib_image_size( const BITMAPINFO *info )
159 {
160 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
161 * abs( info->bmiHeader.biHeight );
162 }
163
164 /*
165 * imagelist_copy_images()
166 *
167 * Copies a block of count images from offset src in the list to offset dest.
168 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
169 */
170 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
171 UINT src, UINT count, UINT dest )
172 {
173 POINT ptSrc, ptDest;
174 SIZE sz;
175 UINT i;
176
177 for ( i=0; i<TILE_COUNT; i++ )
178 {
179 imagelist_point_from_index( himl, src+i, &ptSrc );
180 imagelist_point_from_index( himl, dest+i, &ptDest );
181 sz.cx = himl->cx;
182 sz.cy = himl->cy * imagelist_height( count - i );
183
184 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
185 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
186 }
187 }
188
189 static void add_dib_bits( HIMAGELIST himl, int pos, int count, int width, int height,
190 BITMAPINFO *info, BITMAPINFO *mask_info, DWORD *bits, BYTE *mask_bits )
191 {
192 int i, j, n;
193 POINT pt;
194 int stride = info->bmiHeader.biWidth;
195 int mask_stride = (info->bmiHeader.biWidth + 31) / 32 * 4;
196
197 for (n = 0; n < count; n++)
198 {
199 BOOL has_alpha = FALSE;
200
201 imagelist_point_from_index( himl, pos + n, &pt );
202
203 /* check if bitmap has an alpha channel */
204 for (i = 0; i < height && !has_alpha; i++)
205 for (j = n * width; j < (n + 1) * width; j++)
206 if ((has_alpha = ((bits[i * stride + j] & 0xff000000) != 0))) break;
207
208 if (!has_alpha) /* generate alpha channel from the mask */
209 {
210 for (i = 0; i < height; i++)
211 for (j = n * width; j < (n + 1) * width; j++)
212 if (!mask_info || !((mask_bits[i * mask_stride + j / 8] << (j % 8)) & 0x80))
213 bits[i * stride + j] |= 0xff000000;
214 else
215 bits[i * stride + j] = 0;
216 }
217 else
218 {
219 himl->has_alpha[pos + n] = 1;
220
221 if (mask_info && himl->hbmMask) /* generate the mask from the alpha channel */
222 {
223 for (i = 0; i < height; i++)
224 for (j = n * width; j < (n + 1) * width; j++)
225 if ((bits[i * stride + j] >> 24) > 25) /* more than 10% alpha */
226 mask_bits[i * mask_stride + j / 8] &= ~(0x80 >> (j % 8));
227 else
228 mask_bits[i * mask_stride + j / 8] |= 0x80 >> (j % 8);
229 }
230 }
231 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
232 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
233 if (mask_info)
234 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
235 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
236 }
237 }
238
239 /* add images with an alpha channel when the image list is 32 bpp */
240 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
241 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
242 {
243 BOOL ret = FALSE;
244 BITMAP bm;
245 BITMAPINFO *info, *mask_info = NULL;
246 DWORD *bits = NULL;
247 BYTE *mask_bits = NULL;
248 DWORD mask_width;
249
250 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
251
252 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
253 if (!himl->has_alpha) return FALSE;
254 if (bm.bmBitsPixel != 32) return FALSE;
255
256 SelectObject( hdc, hbmImage );
257 mask_width = (bm.bmWidth + 31) / 32 * 4;
258
259 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
260 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
261 info->bmiHeader.biWidth = bm.bmWidth;
262 info->bmiHeader.biHeight = -height;
263 info->bmiHeader.biPlanes = 1;
264 info->bmiHeader.biBitCount = 32;
265 info->bmiHeader.biCompression = BI_RGB;
266 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
267 info->bmiHeader.biXPelsPerMeter = 0;
268 info->bmiHeader.biYPelsPerMeter = 0;
269 info->bmiHeader.biClrUsed = 0;
270 info->bmiHeader.biClrImportant = 0;
271 if (!(bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
272 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
273
274 if (hbmMask)
275 {
276 if (!(mask_info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
277 goto done;
278 mask_info->bmiHeader = info->bmiHeader;
279 mask_info->bmiHeader.biBitCount = 1;
280 mask_info->bmiHeader.biSizeImage = mask_width * height;
281 if (!(mask_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage )))
282 goto done;
283 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
284 }
285
286 add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
287 ret = TRUE;
288
289 done:
290 HeapFree( GetProcessHeap(), 0, info );
291 HeapFree( GetProcessHeap(), 0, mask_info );
292 HeapFree( GetProcessHeap(), 0, bits );
293 HeapFree( GetProcessHeap(), 0, mask_bits );
294 return ret;
295 }
296
297 /*************************************************************************
298 * IMAGELIST_InternalExpandBitmaps [Internal]
299 *
300 * Expands the bitmaps of an image list by the given number of images.
301 *
302 * PARAMS
303 * himl [I] handle to image list
304 * nImageCount [I] number of images to add
305 *
306 * RETURNS
307 * nothing
308 *
309 * NOTES
310 * This function CANNOT be used to reduce the number of images.
311 */
312 static void
313 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
314 {
315 HDC hdcBitmap;
316 HBITMAP hbmNewBitmap, hbmNull;
317 INT nNewCount;
318 SIZE sz;
319
320 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
321
322 if (himl->cCurImage + nImageCount < himl->cMaxImage)
323 return;
324
325 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
326
327 imagelist_get_bitmap_size(himl, nNewCount, &sz);
328
329 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
330 hdcBitmap = CreateCompatibleDC (0);
331
332 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
333
334 if (hbmNewBitmap == 0)
335 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
336
337 if (himl->cCurImage)
338 {
339 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
340 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
341 himl->hdcImage, 0, 0, SRCCOPY);
342 SelectObject (hdcBitmap, hbmNull);
343 }
344 SelectObject (himl->hdcImage, hbmNewBitmap);
345 DeleteObject (himl->hbmImage);
346 himl->hbmImage = hbmNewBitmap;
347
348 if (himl->flags & ILC_MASK)
349 {
350 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
351
352 if (hbmNewBitmap == 0)
353 ERR("creating new mask bitmap!\n");
354
355 if(himl->cCurImage)
356 {
357 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
358 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
359 himl->hdcMask, 0, 0, SRCCOPY);
360 SelectObject (hdcBitmap, hbmNull);
361 }
362 SelectObject (himl->hdcMask, hbmNewBitmap);
363 DeleteObject (himl->hbmMask);
364 himl->hbmMask = hbmNewBitmap;
365 }
366
367 if (himl->has_alpha)
368 {
369 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
370 if (new_alpha) himl->has_alpha = new_alpha;
371 else
372 {
373 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
374 himl->has_alpha = NULL;
375 }
376 }
377
378 himl->cMaxImage = nNewCount;
379
380 DeleteDC (hdcBitmap);
381 }
382
383
384 /*************************************************************************
385 * ImageList_Add [COMCTL32.@]
386 *
387 * Add an image or images to an image list.
388 *
389 * PARAMS
390 * himl [I] handle to image list
391 * hbmImage [I] handle to image bitmap
392 * hbmMask [I] handle to mask bitmap
393 *
394 * RETURNS
395 * Success: Index of the first new image.
396 * Failure: -1
397 */
398
399 INT WINAPI
400 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
401 {
402 HDC hdcBitmap, hdcTemp = 0;
403 INT nFirstIndex, nImageCount, i;
404 BITMAP bmp;
405 POINT pt;
406
407 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
408 if (!is_valid(himl))
409 return -1;
410
411 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
412 return -1;
413
414 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
415 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
416
417 nImageCount = bmp.bmWidth / himl->cx;
418
419 TRACE("%p has %d images (%d x %d)\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight);
420
421 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
422
423 hdcBitmap = CreateCompatibleDC(0);
424
425 SelectObject(hdcBitmap, hbmImage);
426
427 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
428 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
429 goto done;
430
431 if (himl->hbmMask)
432 {
433 hdcTemp = CreateCompatibleDC(0);
434 SelectObject(hdcTemp, hbmMask);
435 }
436
437 for (i=0; i<nImageCount; i++)
438 {
439 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
440
441 /* Copy result to the imagelist
442 */
443 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
444 hdcBitmap, i*himl->cx, 0, SRCCOPY );
445
446 if (!himl->hbmMask)
447 continue;
448
449 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
450 hdcTemp, i*himl->cx, 0, SRCCOPY );
451
452 /* Remove the background from the image
453 */
454 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
455 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
456 }
457 if (hdcTemp) DeleteDC(hdcTemp);
458
459 done:
460 DeleteDC(hdcBitmap);
461
462 nFirstIndex = himl->cCurImage;
463 himl->cCurImage += nImageCount;
464
465 return nFirstIndex;
466 }
467
468
469 /*************************************************************************
470 * ImageList_AddIcon [COMCTL32.@]
471 *
472 * Adds an icon to an image list.
473 *
474 * PARAMS
475 * himl [I] handle to image list
476 * hIcon [I] handle to icon
477 *
478 * RETURNS
479 * Success: index of the new image
480 * Failure: -1
481 */
482 #undef ImageList_AddIcon
483 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
484 {
485 return ImageList_ReplaceIcon (himl, -1, hIcon);
486 }
487
488
489 /*************************************************************************
490 * ImageList_AddMasked [COMCTL32.@]
491 *
492 * Adds an image or images to an image list and creates a mask from the
493 * specified bitmap using the mask color.
494 *
495 * PARAMS
496 * himl [I] handle to image list.
497 * hBitmap [I] handle to bitmap
498 * clrMask [I] mask color.
499 *
500 * RETURNS
501 * Success: Index of the first new image.
502 * Failure: -1
503 */
504
505 INT WINAPI
506 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
507 {
508 HDC hdcMask, hdcBitmap;
509 INT ret;
510 BITMAP bmp;
511 HBITMAP hMaskBitmap;
512 COLORREF bkColor;
513
514 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
515 if (!is_valid(himl))
516 return -1;
517
518 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
519 return -1;
520
521 hdcBitmap = CreateCompatibleDC(0);
522 SelectObject(hdcBitmap, hBitmap);
523
524 /* Create a temp Mask so we can remove the background of the Image */
525 hdcMask = CreateCompatibleDC(0);
526 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
527 SelectObject(hdcMask, hMaskBitmap);
528
529 /* create monochrome image to the mask bitmap */
530 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
531 SetBkColor (hdcBitmap, bkColor);
532 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
533
534 /*
535 * Remove the background from the image
536 *
537 * WINDOWS BUG ALERT!!!!!!
538 * The statement below should not be done in common practice
539 * but this is how ImageList_AddMasked works in Windows.
540 * It overwrites the original bitmap passed, this was discovered
541 * by using the same bitmap to iterate the different styles
542 * on windows where it failed (BUT ImageList_Add is OK)
543 * This is here in case some apps rely on this bug
544 *
545 * Blt mode 0x220326 is NOTSRCAND
546 */
547 if (bmp.bmBitsPixel > 8) /* NOTSRCAND can't work with palettes */
548 {
549 SetBkColor(hdcBitmap, RGB(255,255,255));
550 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
551 }
552
553 DeleteDC(hdcBitmap);
554 DeleteDC(hdcMask);
555
556 ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
557
558 DeleteObject(hMaskBitmap);
559 return ret;
560 }
561
562
563 /*************************************************************************
564 * ImageList_BeginDrag [COMCTL32.@]
565 *
566 * Creates a temporary image list that contains one image. It will be used
567 * as a drag image.
568 *
569 * PARAMS
570 * himlTrack [I] handle to the source image list
571 * iTrack [I] index of the drag image in the source image list
572 * dxHotspot [I] X position of the hot spot of the drag image
573 * dyHotspot [I] Y position of the hot spot of the drag image
574 *
575 * RETURNS
576 * Success: TRUE
577 * Failure: FALSE
578 */
579
580 BOOL WINAPI
581 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
582 INT dxHotspot, INT dyHotspot)
583 {
584 INT cx, cy;
585
586 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
587 dxHotspot, dyHotspot);
588
589 if (!is_valid(himlTrack))
590 return FALSE;
591
592 if (InternalDrag.himl)
593 ImageList_EndDrag ();
594
595 cx = himlTrack->cx;
596 cy = himlTrack->cy;
597
598 InternalDrag.himlNoCursor = InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
599 if (InternalDrag.himl == NULL) {
600 WARN("Error creating drag image list!\n");
601 return FALSE;
602 }
603
604 InternalDrag.dxHotspot = dxHotspot;
605 InternalDrag.dyHotspot = dyHotspot;
606
607 /* copy image */
608 BitBlt (InternalDrag.himl->hdcImage, 0, 0, cx, cy, himlTrack->hdcImage, iTrack * cx, 0, SRCCOPY);
609
610 /* copy mask */
611 BitBlt (InternalDrag.himl->hdcMask, 0, 0, cx, cy, himlTrack->hdcMask, iTrack * cx, 0, SRCCOPY);
612
613 InternalDrag.himl->cCurImage = 1;
614
615 return TRUE;
616 }
617
618
619 /*************************************************************************
620 * ImageList_Copy [COMCTL32.@]
621 *
622 * Copies an image of the source image list to an image of the
623 * destination image list. Images can be copied or swapped.
624 *
625 * PARAMS
626 * himlDst [I] handle to the destination image list
627 * iDst [I] destination image index.
628 * himlSrc [I] handle to the source image list
629 * iSrc [I] source image index
630 * uFlags [I] flags for the copy operation
631 *
632 * RETURNS
633 * Success: TRUE
634 * Failure: FALSE
635 *
636 * NOTES
637 * Copying from one image list to another is possible. The original
638 * implementation just copies or swaps within one image list.
639 * Could this feature become a bug??? ;-)
640 */
641
642 BOOL WINAPI
643 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
644 INT iSrc, UINT uFlags)
645 {
646 POINT ptSrc, ptDst;
647
648 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
649
650 if (!is_valid(himlSrc) || !is_valid(himlDst))
651 return FALSE;
652 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
653 return FALSE;
654 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
655 return FALSE;
656
657 imagelist_point_from_index( himlDst, iDst, &ptDst );
658 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
659
660 if (uFlags & ILCF_SWAP) {
661 /* swap */
662 HDC hdcBmp;
663 HBITMAP hbmTempImage, hbmTempMask;
664
665 hdcBmp = CreateCompatibleDC (0);
666
667 /* create temporary bitmaps */
668 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
669 himlSrc->uBitsPixel, NULL);
670 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
671 1, NULL);
672
673 /* copy (and stretch) destination to temporary bitmaps.(save) */
674 /* image */
675 SelectObject (hdcBmp, hbmTempImage);
676 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
677 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
678 SRCCOPY);
679 /* mask */
680 SelectObject (hdcBmp, hbmTempMask);
681 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
682 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
683 SRCCOPY);
684
685 /* copy (and stretch) source to destination */
686 /* image */
687 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
688 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
689 SRCCOPY);
690 /* mask */
691 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
692 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
693 SRCCOPY);
694
695 /* copy (without stretching) temporary bitmaps to source (restore) */
696 /* mask */
697 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
698 hdcBmp, 0, 0, SRCCOPY);
699
700 /* image */
701 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
702 hdcBmp, 0, 0, SRCCOPY);
703 /* delete temporary bitmaps */
704 DeleteObject (hbmTempMask);
705 DeleteObject (hbmTempImage);
706 DeleteDC(hdcBmp);
707 }
708 else {
709 /* copy image */
710 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
711 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
712 SRCCOPY);
713
714 /* copy mask */
715 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
716 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
717 SRCCOPY);
718 }
719
720 return TRUE;
721 }
722
723
724 /*************************************************************************
725 * ImageList_Create [COMCTL32.@]
726 *
727 * Creates a new image list.
728 *
729 * PARAMS
730 * cx [I] image height
731 * cy [I] image width
732 * flags [I] creation flags
733 * cInitial [I] initial number of images in the image list
734 * cGrow [I] number of images by which image list grows
735 *
736 * RETURNS
737 * Success: Handle to the created image list
738 * Failure: NULL
739 */
740 HIMAGELIST WINAPI
741 ImageList_Create (INT cx, INT cy, UINT flags,
742 INT cInitial, INT cGrow)
743 {
744 HIMAGELIST himl;
745 INT nCount;
746 HBITMAP hbmTemp;
747 UINT ilc = (flags & 0xFE);
748 static const WORD aBitBlend25[] =
749 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
750
751 static const WORD aBitBlend50[] =
752 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
753
754 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
755
756 if (cx <= 0 || cy <= 0) return NULL;
757
758 /* Create the IImageList interface for the image list */
759 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
760 return NULL;
761
762 cGrow = (WORD)((max( cGrow, 1 ) + 3) & ~3);
763
764 if (cGrow > 256)
765 {
766 /* Windows doesn't limit the size here, but X11 doesn't let us allocate such huge bitmaps */
767 WARN( "grow %d too large, limiting to 256\n", cGrow );
768 cGrow = 256;
769 }
770
771 himl->cx = cx;
772 himl->cy = cy;
773 himl->flags = flags;
774 himl->cMaxImage = cInitial + 1;
775 himl->cInitial = cInitial;
776 himl->cGrow = cGrow;
777 himl->clrFg = CLR_DEFAULT;
778 himl->clrBk = CLR_NONE;
779
780 /* initialize overlay mask indices */
781 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
782 himl->nOvlIdx[nCount] = -1;
783
784 /* Create Image & Mask DCs */
785 himl->hdcImage = CreateCompatibleDC (0);
786 if (!himl->hdcImage)
787 goto cleanup;
788 if (himl->flags & ILC_MASK){
789 himl->hdcMask = CreateCompatibleDC(0);
790 if (!himl->hdcMask)
791 goto cleanup;
792 }
793
794 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
795 if (ilc == ILC_COLOR)
796 {
797 ilc = ILC_COLOR4;
798 himl->flags |= ILC_COLOR4;
799 }
800
801 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
802 himl->uBitsPixel = ilc;
803 else
804 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
805
806 if (himl->cMaxImage > 0) {
807 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
808 SelectObject(himl->hdcImage, himl->hbmImage);
809 } else
810 himl->hbmImage = 0;
811
812 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
813 SIZE sz;
814
815 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
816 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
817 if (himl->hbmMask == 0) {
818 ERR("Error creating mask bitmap!\n");
819 goto cleanup;
820 }
821 SelectObject(himl->hdcMask, himl->hbmMask);
822 }
823 else
824 himl->hbmMask = 0;
825
826 if (ilc == ILC_COLOR32)
827 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
828 else
829 himl->has_alpha = NULL;
830
831 /* create blending brushes */
832 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
833 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
834 DeleteObject (hbmTemp);
835
836 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
837 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
838 DeleteObject (hbmTemp);
839
840 TRACE("created imagelist %p\n", himl);
841 return himl;
842
843 cleanup:
844 ImageList_Destroy(himl);
845 return NULL;
846 }
847
848
849 /*************************************************************************
850 * ImageList_Destroy [COMCTL32.@]
851 *
852 * Destroys an image list.
853 *
854 * PARAMS
855 * himl [I] handle to image list
856 *
857 * RETURNS
858 * Success: TRUE
859 * Failure: FALSE
860 */
861
862 BOOL WINAPI
863 ImageList_Destroy (HIMAGELIST himl)
864 {
865 if (!is_valid(himl))
866 return FALSE;
867
868 IImageList_Release((IImageList *) himl);
869 return TRUE;
870 }
871
872
873 /*************************************************************************
874 * ImageList_DragEnter [COMCTL32.@]
875 *
876 * Locks window update and displays the drag image at the given position.
877 *
878 * PARAMS
879 * hwndLock [I] handle of the window that owns the drag image.
880 * x [I] X position of the drag image.
881 * y [I] Y position of the drag image.
882 *
883 * RETURNS
884 * Success: TRUE
885 * Failure: FALSE
886 *
887 * NOTES
888 * The position of the drag image is relative to the window, not
889 * the client area.
890 */
891
892 BOOL WINAPI
893 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
894 {
895 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
896
897 if (!is_valid(InternalDrag.himl))
898 return FALSE;
899
900 if (hwndLock)
901 InternalDrag.hwnd = hwndLock;
902 else
903 InternalDrag.hwnd = GetDesktopWindow ();
904
905 InternalDrag.x = x;
906 InternalDrag.y = y;
907
908 /* draw the drag image and save the background */
909 if (!ImageList_DragShowNolock(TRUE)) {
910 return FALSE;
911 }
912
913 return TRUE;
914 }
915
916
917 /*************************************************************************
918 * ImageList_DragLeave [COMCTL32.@]
919 *
920 * Unlocks window update and hides the drag image.
921 *
922 * PARAMS
923 * hwndLock [I] handle of the window that owns the drag image.
924 *
925 * RETURNS
926 * Success: TRUE
927 * Failure: FALSE
928 */
929
930 BOOL WINAPI
931 ImageList_DragLeave (HWND hwndLock)
932 {
933 /* As we don't save drag info in the window this can lead to problems if
934 an app does not supply the same window as DragEnter */
935 /* if (hwndLock)
936 InternalDrag.hwnd = hwndLock;
937 else
938 InternalDrag.hwnd = GetDesktopWindow (); */
939 if(!hwndLock)
940 hwndLock = GetDesktopWindow();
941 if(InternalDrag.hwnd != hwndLock)
942 FIXME("DragLeave hWnd != DragEnter hWnd\n");
943
944 ImageList_DragShowNolock (FALSE);
945
946 return TRUE;
947 }
948
949
950 /*************************************************************************
951 * ImageList_InternalDragDraw [Internal]
952 *
953 * Draws the drag image.
954 *
955 * PARAMS
956 * hdc [I] device context to draw into.
957 * x [I] X position of the drag image.
958 * y [I] Y position of the drag image.
959 *
960 * RETURNS
961 * Success: TRUE
962 * Failure: FALSE
963 *
964 * NOTES
965 * The position of the drag image is relative to the window, not
966 * the client area.
967 *
968 */
969
970 static inline void
971 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
972 {
973 IMAGELISTDRAWPARAMS imldp;
974
975 ZeroMemory (&imldp, sizeof(imldp));
976 imldp.cbSize = sizeof(imldp);
977 imldp.himl = InternalDrag.himl;
978 imldp.i = 0;
979 imldp.hdcDst = hdc,
980 imldp.x = x;
981 imldp.y = y;
982 imldp.rgbBk = CLR_DEFAULT;
983 imldp.rgbFg = CLR_DEFAULT;
984 imldp.fStyle = ILD_NORMAL;
985 imldp.fState = ILS_ALPHA;
986 imldp.Frame = 192;
987 ImageList_DrawIndirect (&imldp);
988 }
989
990 /*************************************************************************
991 * ImageList_DragMove [COMCTL32.@]
992 *
993 * Moves the drag image.
994 *
995 * PARAMS
996 * x [I] X position of the drag image.
997 * y [I] Y position of the drag image.
998 *
999 * RETURNS
1000 * Success: TRUE
1001 * Failure: FALSE
1002 *
1003 * NOTES
1004 * The position of the drag image is relative to the window, not
1005 * the client area.
1006 */
1007
1008 BOOL WINAPI
1009 ImageList_DragMove (INT x, INT y)
1010 {
1011 TRACE("(x=%d y=%d)\n", x, y);
1012
1013 if (!is_valid(InternalDrag.himl))
1014 return FALSE;
1015
1016 /* draw/update the drag image */
1017 if (InternalDrag.bShow) {
1018 HDC hdcDrag;
1019 HDC hdcOffScreen;
1020 HDC hdcBg;
1021 HBITMAP hbmOffScreen;
1022 INT origNewX, origNewY;
1023 INT origOldX, origOldY;
1024 INT origRegX, origRegY;
1025 INT sizeRegX, sizeRegY;
1026
1027
1028 /* calculate the update region */
1029 origNewX = x - InternalDrag.dxHotspot;
1030 origNewY = y - InternalDrag.dyHotspot;
1031 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
1032 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
1033 origRegX = min(origNewX, origOldX);
1034 origRegY = min(origNewY, origOldY);
1035 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
1036 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
1037
1038 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
1039 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1040 hdcOffScreen = CreateCompatibleDC(hdcDrag);
1041 hdcBg = CreateCompatibleDC(hdcDrag);
1042
1043 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
1044 SelectObject(hdcOffScreen, hbmOffScreen);
1045 SelectObject(hdcBg, InternalDrag.hbmBg);
1046
1047 /* get the actual background of the update region */
1048 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1049 origRegX, origRegY, SRCCOPY);
1050 /* erase the old image */
1051 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1052 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1053 SRCCOPY);
1054 /* save the background */
1055 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1056 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1057 /* draw the image */
1058 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
1059 origNewY - origRegY);
1060 /* draw the update region to the screen */
1061 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1062 hdcOffScreen, 0, 0, SRCCOPY);
1063
1064 DeleteDC(hdcBg);
1065 DeleteDC(hdcOffScreen);
1066 DeleteObject(hbmOffScreen);
1067 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1068 }
1069
1070 /* update the image position */
1071 InternalDrag.x = x;
1072 InternalDrag.y = y;
1073
1074 return TRUE;
1075 }
1076
1077
1078 /*************************************************************************
1079 * ImageList_DragShowNolock [COMCTL32.@]
1080 *
1081 * Shows or hides the drag image.
1082 *
1083 * PARAMS
1084 * bShow [I] TRUE shows the drag image, FALSE hides it.
1085 *
1086 * RETURNS
1087 * Success: TRUE
1088 * Failure: FALSE
1089 */
1090
1091 BOOL WINAPI
1092 ImageList_DragShowNolock (BOOL bShow)
1093 {
1094 HDC hdcDrag;
1095 HDC hdcBg;
1096 INT x, y;
1097
1098 if (!is_valid(InternalDrag.himl))
1099 return FALSE;
1100
1101 TRACE("bShow=0x%X!\n", bShow);
1102
1103 /* DragImage is already visible/hidden */
1104 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1105 return FALSE;
1106 }
1107
1108 /* position of the origin of the DragImage */
1109 x = InternalDrag.x - InternalDrag.dxHotspot;
1110 y = InternalDrag.y - InternalDrag.dyHotspot;
1111
1112 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1113 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1114 if (!hdcDrag) {
1115 return FALSE;
1116 }
1117
1118 hdcBg = CreateCompatibleDC(hdcDrag);
1119 if (!InternalDrag.hbmBg) {
1120 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1121 InternalDrag.himl->cx, InternalDrag.himl->cy);
1122 }
1123 SelectObject(hdcBg, InternalDrag.hbmBg);
1124
1125 if (bShow) {
1126 /* save the background */
1127 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1128 hdcDrag, x, y, SRCCOPY);
1129 /* show the image */
1130 ImageList_InternalDragDraw(hdcDrag, x, y);
1131 } else {
1132 /* hide the image */
1133 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1134 hdcBg, 0, 0, SRCCOPY);
1135 }
1136
1137 InternalDrag.bShow = !InternalDrag.bShow;
1138
1139 DeleteDC(hdcBg);
1140 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1141 return TRUE;
1142 }
1143
1144
1145 /*************************************************************************
1146 * ImageList_Draw [COMCTL32.@]
1147 *
1148 * Draws an image.
1149 *
1150 * PARAMS
1151 * himl [I] handle to image list
1152 * i [I] image index
1153 * hdc [I] handle to device context
1154 * x [I] x position
1155 * y [I] y position
1156 * fStyle [I] drawing flags
1157 *
1158 * RETURNS
1159 * Success: TRUE
1160 * Failure: FALSE
1161 *
1162 * SEE
1163 * ImageList_DrawEx.
1164 */
1165
1166 BOOL WINAPI
1167 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1168 {
1169 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1170 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1171 }
1172
1173
1174 /*************************************************************************
1175 * ImageList_DrawEx [COMCTL32.@]
1176 *
1177 * Draws an image and allows using extended drawing features.
1178 *
1179 * PARAMS
1180 * himl [I] handle to image list
1181 * i [I] image index
1182 * hdc [I] handle to device context
1183 * x [I] X position
1184 * y [I] Y position
1185 * dx [I] X offset
1186 * dy [I] Y offset
1187 * rgbBk [I] background color
1188 * rgbFg [I] foreground color
1189 * fStyle [I] drawing flags
1190 *
1191 * RETURNS
1192 * Success: TRUE
1193 * Failure: FALSE
1194 *
1195 * NOTES
1196 * Calls ImageList_DrawIndirect.
1197 *
1198 * SEE
1199 * ImageList_DrawIndirect.
1200 */
1201
1202 BOOL WINAPI
1203 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1204 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1205 UINT fStyle)
1206 {
1207 IMAGELISTDRAWPARAMS imldp;
1208
1209 ZeroMemory (&imldp, sizeof(imldp));
1210 imldp.cbSize = sizeof(imldp);
1211 imldp.himl = himl;
1212 imldp.i = i;
1213 imldp.hdcDst = hdc,
1214 imldp.x = x;
1215 imldp.y = y;
1216 imldp.cx = dx;
1217 imldp.cy = dy;
1218 imldp.rgbBk = rgbBk;
1219 imldp.rgbFg = rgbFg;
1220 imldp.fStyle = fStyle;
1221
1222 return ImageList_DrawIndirect (&imldp);
1223 }
1224
1225
1226 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1227 int src_x, int src_y, int cx, int cy, BLENDFUNCTION func,
1228 UINT style, COLORREF blend_col )
1229 {
1230 BOOL ret = FALSE;
1231 HDC hdc;
1232 HBITMAP bmp = 0, mask = 0;
1233 BITMAPINFO *info;
1234 void *bits, *mask_bits;
1235 unsigned int *ptr;
1236 int i, j;
1237
1238 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1239 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1240 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1241 info->bmiHeader.biWidth = cx;
1242 info->bmiHeader.biHeight = cy;
1243 info->bmiHeader.biPlanes = 1;
1244 info->bmiHeader.biBitCount = 32;
1245 info->bmiHeader.biCompression = BI_RGB;
1246 info->bmiHeader.biSizeImage = cx * cy * 4;
1247 info->bmiHeader.biXPelsPerMeter = 0;
1248 info->bmiHeader.biYPelsPerMeter = 0;
1249 info->bmiHeader.biClrUsed = 0;
1250 info->bmiHeader.biClrImportant = 0;
1251 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1252 SelectObject( hdc, bmp );
1253 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1254
1255 if (blend_col != CLR_NONE)
1256 {
1257 BYTE r = GetRValue( blend_col );
1258 BYTE g = GetGValue( blend_col );
1259 BYTE b = GetBValue( blend_col );
1260
1261 if (style & ILD_BLEND25)
1262 {
1263 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1264 *ptr = ((*ptr & 0xff000000) |
1265 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1266 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1267 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1268 }
1269 else if (style & ILD_BLEND50)
1270 {
1271 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1272 *ptr = ((*ptr & 0xff000000) |
1273 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1274 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1275 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1276 }
1277 }
1278
1279 if (himl->has_alpha) /* we already have an alpha channel in this case */
1280 {
1281 /* pre-multiply by the alpha channel */
1282 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1283 {
1284 DWORD alpha = *ptr >> 24;
1285 *ptr = ((*ptr & 0xff000000) |
1286 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1287 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1288 (((*ptr & 0x000000ff) * alpha / 255)));
1289 }
1290 }
1291 else if (himl->hbmMask)
1292 {
1293 unsigned int width_bytes = (cx + 31) / 32 * 4;
1294 /* generate alpha channel from the mask */
1295 info->bmiHeader.biBitCount = 1;
1296 info->bmiHeader.biSizeImage = width_bytes * cy;
1297 info->bmiColors[0].rgbRed = 0;
1298 info->bmiColors[0].rgbGreen = 0;
1299 info->bmiColors[0].rgbBlue = 0;
1300 info->bmiColors[0].rgbReserved = 0;
1301 info->bmiColors[1].rgbRed = 0xff;
1302 info->bmiColors[1].rgbGreen = 0xff;
1303 info->bmiColors[1].rgbBlue = 0xff;
1304 info->bmiColors[1].rgbReserved = 0;
1305 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1306 goto done;
1307 SelectObject( hdc, mask );
1308 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1309 SelectObject( hdc, bmp );
1310 for (i = 0, ptr = bits; i < cy; i++)
1311 for (j = 0; j < cx; j++, ptr++)
1312 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1313 else *ptr |= 0xff000000;
1314 }
1315
1316 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1317
1318 done:
1319 DeleteDC( hdc );
1320 if (bmp) DeleteObject( bmp );
1321 if (mask) DeleteObject( mask );
1322 HeapFree( GetProcessHeap(), 0, info );
1323 return ret;
1324 }
1325
1326 /*************************************************************************
1327 * ImageList_DrawIndirect [COMCTL32.@]
1328 *
1329 * Draws an image using various parameters specified in pimldp.
1330 *
1331 * PARAMS
1332 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1333 *
1334 * RETURNS
1335 * Success: TRUE
1336 * Failure: FALSE
1337 */
1338
1339 BOOL WINAPI
1340 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1341 {
1342 INT cx, cy, nOvlIdx;
1343 DWORD fState, dwRop;
1344 UINT fStyle;
1345 COLORREF oldImageBk, oldImageFg;
1346 HDC hImageDC, hImageListDC, hMaskListDC;
1347 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1348 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1349 HIMAGELIST himl;
1350 HBRUSH hOldBrush;
1351 POINT pt;
1352 BOOL has_alpha;
1353
1354 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1355 if (!is_valid(himl)) return FALSE;
1356 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1357
1358 imagelist_point_from_index( himl, pimldp->i, &pt );
1359 pt.x += pimldp->xBitmap;
1360 pt.y += pimldp->yBitmap;
1361
1362 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1363 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1364 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1365 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1366
1367 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1368 if( pimldp->rgbBk == CLR_NONE )
1369 bIsTransparent = TRUE;
1370 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1371 bIsTransparent = TRUE;
1372 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1373 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1374
1375 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1376 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1377
1378 /* we will use these DCs to access the images and masks in the ImageList */
1379 hImageListDC = himl->hdcImage;
1380 hMaskListDC = himl->hdcMask;
1381
1382 /* these will accumulate the image and mask for the image we're drawing */
1383 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1384 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1385 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1386
1387 /* Create a compatible DC. */
1388 if (!hImageListDC || !hImageDC || !hImageBmp ||
1389 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1390 goto cleanup;
1391
1392 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1393
1394 /*
1395 * To obtain a transparent look, background color should be set
1396 * to white and foreground color to black when blitting the
1397 * monochrome mask.
1398 */
1399 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1400 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1401
1402 has_alpha = (himl->has_alpha && himl->has_alpha[pimldp->i]);
1403 if (!bMask && (has_alpha || (fState & ILS_ALPHA)))
1404 {
1405 COLORREF colour, blend_col = CLR_NONE;
1406 BLENDFUNCTION func;
1407
1408 if (bBlend)
1409 {
1410 blend_col = pimldp->rgbFg;
1411 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1412 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1413 }
1414
1415 func.BlendOp = AC_SRC_OVER;
1416 func.BlendFlags = 0;
1417 func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1418 func.AlphaFormat = AC_SRC_ALPHA;
1419
1420 if (bIsTransparent)
1421 {
1422 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y,
1423 pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1424 goto end;
1425 }
1426 colour = pimldp->rgbBk;
1427 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1428 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1429
1430 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1431 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1432 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1433 DeleteObject (SelectObject (hImageDC, hOldBrush));
1434 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1435 goto end;
1436 }
1437
1438 /*
1439 * Draw the initial image
1440 */
1441 if( bMask ) {
1442 if (himl->hbmMask) {
1443 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1444 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1445 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1446 DeleteObject (SelectObject (hImageDC, hOldBrush));
1447 if( bIsTransparent )
1448 {
1449 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1450 bResult = TRUE;
1451 goto end;
1452 }
1453 } else {
1454 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1455 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1456 SelectObject(hImageDC, hOldBrush);
1457 }
1458 } else {
1459 /* blend the image with the needed solid background */
1460 COLORREF colour = RGB(0,0,0);
1461
1462 if( !bIsTransparent )
1463 {
1464 colour = pimldp->rgbBk;
1465 if( colour == CLR_DEFAULT )
1466 colour = himl->clrBk;
1467 if( colour == CLR_NONE )
1468 colour = GetBkColor(pimldp->hdcDst);
1469 }
1470
1471 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1472 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1473 if (himl->hbmMask)
1474 {
1475 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1476 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1477 }
1478 else
1479 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1480 DeleteObject (SelectObject (hImageDC, hOldBrush));
1481 }
1482
1483 /* Time for blending, if required */
1484 if (bBlend) {
1485 HBRUSH hBlendBrush;
1486 COLORREF clrBlend = pimldp->rgbFg;
1487 HDC hBlendMaskDC = hImageListDC;
1488 HBITMAP hOldBitmap;
1489
1490 /* Create the blend Mask */
1491 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1492 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1493 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1494 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1495 SelectObject(hBlendMaskDC, hOldBrush);
1496
1497 /* Modify the blend mask if an Image Mask exist */
1498 if(himl->hbmMask) {
1499 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1500 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1501 }
1502
1503 /* now apply blend to the current image given the BlendMask */
1504 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1505 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1506 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1507 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1508 DeleteObject(SelectObject(hImageDC, hOldBrush));
1509 SelectObject(hBlendMaskDC, hOldBitmap);
1510 }
1511
1512 /* Now do the overlay image, if any */
1513 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1514 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1515 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1516 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1517 POINT ptOvl;
1518 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1519 ptOvl.x += pimldp->xBitmap;
1520 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1521 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1522 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1523 }
1524 }
1525
1526 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1527 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1528 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1529
1530 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1531 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1532 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1533
1534 /* now copy the image to the screen */
1535 dwRop = SRCCOPY;
1536 if (himl->hbmMask && bIsTransparent ) {
1537 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1538 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1539 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1540 SetBkColor(pimldp->hdcDst, oldDstBk);
1541 SetTextColor(pimldp->hdcDst, oldDstFg);
1542 dwRop = SRCPAINT;
1543 }
1544 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1545 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1546
1547 bResult = TRUE;
1548 end:
1549 /* cleanup the mess */
1550 SetBkColor(hImageDC, oldImageBk);
1551 SetTextColor(hImageDC, oldImageFg);
1552 SelectObject(hImageDC, hOldImageBmp);
1553 cleanup:
1554 DeleteObject(hBlendMaskBmp);
1555 DeleteObject(hImageBmp);
1556 DeleteDC(hImageDC);
1557
1558 return bResult;
1559 }
1560
1561
1562 /*************************************************************************
1563 * ImageList_Duplicate [COMCTL32.@]
1564 *
1565 * Duplicates an image list.
1566 *
1567 * PARAMS
1568 * himlSrc [I] source image list handle
1569 *
1570 * RETURNS
1571 * Success: Handle of duplicated image list.
1572 * Failure: NULL
1573 */
1574
1575 HIMAGELIST WINAPI
1576 ImageList_Duplicate (HIMAGELIST himlSrc)
1577 {
1578 HIMAGELIST himlDst;
1579
1580 if (!is_valid(himlSrc)) {
1581 ERR("Invalid image list handle!\n");
1582 return NULL;
1583 }
1584
1585 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1586 himlSrc->cCurImage, himlSrc->cGrow);
1587
1588 if (himlDst)
1589 {
1590 SIZE sz;
1591
1592 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1593 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1594 himlSrc->hdcImage, 0, 0, SRCCOPY);
1595
1596 if (himlDst->hbmMask)
1597 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1598 himlSrc->hdcMask, 0, 0, SRCCOPY);
1599
1600 himlDst->cCurImage = himlSrc->cCurImage;
1601 if (himlSrc->has_alpha && himlDst->has_alpha)
1602 memcpy( himlDst->has_alpha, himlSrc->has_alpha, himlDst->cCurImage );
1603 }
1604 return himlDst;
1605 }
1606
1607
1608 /*************************************************************************
1609 * ImageList_EndDrag [COMCTL32.@]
1610 *
1611 * Finishes a drag operation.
1612 *
1613 * PARAMS
1614 * no Parameters
1615 *
1616 * RETURNS
1617 * Success: TRUE
1618 * Failure: FALSE
1619 */
1620
1621 VOID WINAPI
1622 ImageList_EndDrag (void)
1623 {
1624 /* cleanup the InternalDrag struct */
1625 InternalDrag.hwnd = 0;
1626 if (InternalDrag.himl != InternalDrag.himlNoCursor)
1627 ImageList_Destroy (InternalDrag.himlNoCursor);
1628 ImageList_Destroy (InternalDrag.himl);
1629 InternalDrag.himlNoCursor = InternalDrag.himl = 0;
1630 InternalDrag.x= 0;
1631 InternalDrag.y= 0;
1632 InternalDrag.dxHotspot = 0;
1633 InternalDrag.dyHotspot = 0;
1634 InternalDrag.bShow = FALSE;
1635 DeleteObject(InternalDrag.hbmBg);
1636 InternalDrag.hbmBg = 0;
1637 }
1638
1639
1640 /*************************************************************************
1641 * ImageList_GetBkColor [COMCTL32.@]
1642 *
1643 * Returns the background color of an image list.
1644 *
1645 * PARAMS
1646 * himl [I] Image list handle.
1647 *
1648 * RETURNS
1649 * Success: background color
1650 * Failure: CLR_NONE
1651 */
1652
1653 COLORREF WINAPI
1654 ImageList_GetBkColor (HIMAGELIST himl)
1655 {
1656 return himl ? himl->clrBk : CLR_NONE;
1657 }
1658
1659
1660 /*************************************************************************
1661 * ImageList_GetDragImage [COMCTL32.@]
1662 *
1663 * Returns the handle to the internal drag image list.
1664 *
1665 * PARAMS
1666 * ppt [O] Pointer to the drag position. Can be NULL.
1667 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1668 *
1669 * RETURNS
1670 * Success: Handle of the drag image list.
1671 * Failure: NULL.
1672 */
1673
1674 HIMAGELIST WINAPI
1675 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1676 {
1677 if (is_valid(InternalDrag.himl)) {
1678 if (ppt) {
1679 ppt->x = InternalDrag.x;
1680 ppt->y = InternalDrag.y;
1681 }
1682 if (pptHotspot) {
1683 pptHotspot->x = InternalDrag.dxHotspot;
1684 pptHotspot->y = InternalDrag.dyHotspot;
1685 }
1686 return (InternalDrag.himl);
1687 }
1688
1689 return NULL;
1690 }
1691
1692
1693 /*************************************************************************
1694 * ImageList_GetFlags [COMCTL32.@]
1695 *
1696 * Gets the flags of the specified image list.
1697 *
1698 * PARAMS
1699 * himl [I] Handle to image list
1700 *
1701 * RETURNS
1702 * Image list flags.
1703 *
1704 * BUGS
1705 * Stub.
1706 */
1707
1708 DWORD WINAPI
1709 ImageList_GetFlags(HIMAGELIST himl)
1710 {
1711 TRACE("%p\n", himl);
1712
1713 return is_valid(himl) ? himl->flags : 0;
1714 }
1715
1716
1717 /*************************************************************************
1718 * ImageList_GetIcon [COMCTL32.@]
1719 *
1720 * Creates an icon from a masked image of an image list.
1721 *
1722 * PARAMS
1723 * himl [I] handle to image list
1724 * i [I] image index
1725 * flags [I] drawing style flags
1726 *
1727 * RETURNS
1728 * Success: icon handle
1729 * Failure: NULL
1730 */
1731
1732 HICON WINAPI
1733 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1734 {
1735 ICONINFO ii;
1736 HICON hIcon;
1737 HBITMAP hOldDstBitmap;
1738 HDC hdcDst;
1739 POINT pt;
1740
1741 TRACE("%p %d %d\n", himl, i, fStyle);
1742 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1743
1744 ii.fIcon = TRUE;
1745 ii.xHotspot = 0;
1746 ii.yHotspot = 0;
1747
1748 /* create colour bitmap */
1749 hdcDst = GetDC(0);
1750 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1751 ReleaseDC(0, hdcDst);
1752
1753 hdcDst = CreateCompatibleDC(0);
1754
1755 imagelist_point_from_index( himl, i, &pt );
1756
1757 /* draw mask*/
1758 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1759 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1760 if (himl->hbmMask) {
1761 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1762 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1763 }
1764 else
1765 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1766
1767 /* draw image*/
1768 SelectObject (hdcDst, ii.hbmColor);
1769 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1770 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1771
1772 /*
1773 * CreateIconIndirect requires us to deselect the bitmaps from
1774 * the DCs before calling
1775 */
1776 SelectObject(hdcDst, hOldDstBitmap);
1777
1778 hIcon = CreateIconIndirect (&ii);
1779
1780 DeleteObject (ii.hbmMask);
1781 DeleteObject (ii.hbmColor);
1782 DeleteDC (hdcDst);
1783
1784 return hIcon;
1785 }
1786
1787
1788 /*************************************************************************
1789 * ImageList_GetIconSize [COMCTL32.@]
1790 *
1791 * Retrieves the size of an image in an image list.
1792 *
1793 * PARAMS
1794 * himl [I] handle to image list
1795 * cx [O] pointer to the image width.
1796 * cy [O] pointer to the image height.
1797 *
1798 * RETURNS
1799 * Success: TRUE
1800 * Failure: FALSE
1801 *
1802 * NOTES
1803 * All images in an image list have the same size.
1804 */
1805
1806 BOOL WINAPI
1807 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1808 {
1809 if (!is_valid(himl) || !cx || !cy)
1810 return FALSE;
1811 if ((himl->cx <= 0) || (himl->cy <= 0))
1812 return FALSE;
1813
1814 *cx = himl->cx;
1815 *cy = himl->cy;
1816
1817 return TRUE;
1818 }
1819
1820
1821 /*************************************************************************
1822 * ImageList_GetImageCount [COMCTL32.@]
1823 *
1824 * Returns the number of images in an image list.
1825 *
1826 * PARAMS
1827 * himl [I] handle to image list
1828 *
1829 * RETURNS
1830 * Success: Number of images.
1831 * Failure: 0
1832 */
1833
1834 INT WINAPI
1835 ImageList_GetImageCount (HIMAGELIST himl)
1836 {
1837 if (!is_valid(himl))
1838 return 0;
1839
1840 return himl->cCurImage;
1841 }
1842
1843
1844 /*************************************************************************
1845 * ImageList_GetImageInfo [COMCTL32.@]
1846 *
1847 * Returns information about an image in an image list.
1848 *
1849 * PARAMS
1850 * himl [I] handle to image list
1851 * i [I] image index
1852 * pImageInfo [O] pointer to the image information
1853 *
1854 * RETURNS
1855 * Success: TRUE
1856 * Failure: FALSE
1857 */
1858
1859 BOOL WINAPI
1860 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1861 {
1862 POINT pt;
1863
1864 if (!is_valid(himl) || (pImageInfo == NULL))
1865 return FALSE;
1866 if ((i < 0) || (i >= himl->cCurImage))
1867 return FALSE;
1868
1869 pImageInfo->hbmImage = himl->hbmImage;
1870 pImageInfo->hbmMask = himl->hbmMask;
1871
1872 imagelist_point_from_index( himl, i, &pt );
1873 pImageInfo->rcImage.top = pt.y;
1874 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1875 pImageInfo->rcImage.left = pt.x;
1876 pImageInfo->rcImage.right = pt.x + himl->cx;
1877
1878 return TRUE;
1879 }
1880
1881
1882 /*************************************************************************
1883 * ImageList_GetImageRect [COMCTL32.@]
1884 *
1885 * Retrieves the rectangle of the specified image in an image list.
1886 *
1887 * PARAMS
1888 * himl [I] handle to image list
1889 * i [I] image index
1890 * lpRect [O] pointer to the image rectangle
1891 *
1892 * RETURNS
1893 * Success: TRUE
1894 * Failure: FALSE
1895 *
1896 * NOTES
1897 * This is an UNDOCUMENTED function!!!
1898 */
1899
1900 BOOL WINAPI
1901 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1902 {
1903 POINT pt;
1904
1905 if (!is_valid(himl) || (lpRect == NULL))
1906 return FALSE;
1907 if ((i < 0) || (i >= himl->cCurImage))
1908 return FALSE;
1909
1910 imagelist_point_from_index( himl, i, &pt );
1911 lpRect->left = pt.x;
1912 lpRect->top = pt.y;
1913 lpRect->right = pt.x + himl->cx;
1914 lpRect->bottom = pt.y + himl->cy;
1915
1916 return TRUE;
1917 }
1918
1919
1920 /*************************************************************************
1921 * ImageList_LoadImage [COMCTL32.@]
1922 * ImageList_LoadImageA [COMCTL32.@]
1923 *
1924 * Creates an image list from a bitmap, icon or cursor.
1925 *
1926 * See ImageList_LoadImageW.
1927 */
1928
1929 HIMAGELIST WINAPI
1930 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1931 COLORREF clrMask, UINT uType, UINT uFlags)
1932 {
1933 HIMAGELIST himl;
1934 LPWSTR lpbmpW;
1935 DWORD len;
1936
1937 if (IS_INTRESOURCE(lpbmp))
1938 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1939 uType, uFlags);
1940
1941 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1942 lpbmpW = Alloc(len * sizeof(WCHAR));
1943 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1944
1945 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1946 Free (lpbmpW);
1947 return himl;
1948 }
1949
1950
1951 /*************************************************************************
1952 * ImageList_LoadImageW [COMCTL32.@]
1953 *
1954 * Creates an image list from a bitmap, icon or cursor.
1955 *
1956 * PARAMS
1957 * hi [I] instance handle
1958 * lpbmp [I] name or id of the image
1959 * cx [I] width of each image
1960 * cGrow [I] number of images to expand
1961 * clrMask [I] mask color
1962 * uType [I] type of image to load
1963 * uFlags [I] loading flags
1964 *
1965 * RETURNS
1966 * Success: handle to the loaded image list
1967 * Failure: NULL
1968 *
1969 * SEE
1970 * LoadImage ()
1971 */
1972
1973 HIMAGELIST WINAPI
1974 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1975 COLORREF clrMask, UINT uType, UINT uFlags)
1976 {
1977 HIMAGELIST himl = NULL;
1978 HANDLE handle;
1979 INT nImageCount;
1980
1981 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
1982 if (!handle) {
1983 WARN("Couldn't load image\n");
1984 return NULL;
1985 }
1986
1987 if (uType == IMAGE_BITMAP) {
1988 DIBSECTION dib;
1989 UINT color;
1990
1991 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
1992 else color = dib.dsBm.bmBitsPixel;
1993
1994 /* To match windows behavior, if cx is set to zero and
1995 the flag DI_DEFAULTSIZE is specified, cx becomes the
1996 system metric value for icons. If the flag is not specified
1997 the function sets the size to the height of the bitmap */
1998 if (cx == 0)
1999 {
2000 if (uFlags & DI_DEFAULTSIZE)
2001 cx = GetSystemMetrics (SM_CXICON);
2002 else
2003 cx = dib.dsBm.bmHeight;
2004 }
2005
2006 nImageCount = dib.dsBm.bmWidth / cx;
2007
2008 himl = ImageList_Create (cx, dib.dsBm.bmHeight, ILC_MASK | color, nImageCount, cGrow);
2009 if (!himl) {
2010 DeleteObject (handle);
2011 return NULL;
2012 }
2013 ImageList_AddMasked (himl, handle, clrMask);
2014 }
2015 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
2016 ICONINFO ii;
2017 BITMAP bmp;
2018
2019 GetIconInfo (handle, &ii);
2020 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
2021 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
2022 ILC_MASK | ILC_COLOR, 1, cGrow);
2023 if (!himl) {
2024 DeleteObject (ii.hbmColor);
2025 DeleteObject (ii.hbmMask);
2026 DeleteObject (handle);
2027 return NULL;
2028 }
2029 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
2030 DeleteObject (ii.hbmColor);
2031 DeleteObject (ii.hbmMask);
2032 }
2033
2034 DeleteObject (handle);
2035
2036 return himl;
2037 }
2038
2039
2040 /*************************************************************************
2041 * ImageList_Merge [COMCTL32.@]
2042 *
2043 * Create an image list containing a merged image from two image lists.
2044 *
2045 * PARAMS
2046 * himl1 [I] handle to first image list
2047 * i1 [I] first image index
2048 * himl2 [I] handle to second image list
2049 * i2 [I] second image index
2050 * dx [I] X offset of the second image relative to the first.
2051 * dy [I] Y offset of the second image relative to the first.
2052 *
2053 * RETURNS
2054 * Success: The newly created image list. It contains a single image
2055 * consisting of the second image merged with the first.
2056 * Failure: NULL, if either himl1 or himl2 are invalid.
2057 *
2058 * NOTES
2059 * - The returned image list should be deleted by the caller using
2060 * ImageList_Destroy() when it is no longer required.
2061 * - If either i1 or i2 are not valid image indices they will be treated
2062 * as a blank image.
2063 */
2064 HIMAGELIST WINAPI
2065 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2066 INT dx, INT dy)
2067 {
2068 HIMAGELIST himlDst = NULL;
2069 INT cxDst, cyDst;
2070 INT xOff1, yOff1, xOff2, yOff2;
2071 POINT pt1, pt2;
2072 INT newFlags;
2073
2074 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2075 i2, dx, dy);
2076
2077 if (!is_valid(himl1) || !is_valid(himl2))
2078 return NULL;
2079
2080 if (dx > 0) {
2081 cxDst = max (himl1->cx, dx + himl2->cx);
2082 xOff1 = 0;
2083 xOff2 = dx;
2084 }
2085 else if (dx < 0) {
2086 cxDst = max (himl2->cx, himl1->cx - dx);
2087 xOff1 = -dx;
2088 xOff2 = 0;
2089 }
2090 else {
2091 cxDst = max (himl1->cx, himl2->cx);
2092 xOff1 = 0;
2093 xOff2 = 0;
2094 }
2095
2096 if (dy > 0) {
2097 cyDst = max (himl1->cy, dy + himl2->cy);
2098 yOff1 = 0;
2099 yOff2 = dy;
2100 }
2101 else if (dy < 0) {
2102 cyDst = max (himl2->cy, himl1->cy - dy);
2103 yOff1 = -dy;
2104 yOff2 = 0;
2105 }
2106 else {
2107 cyDst = max (himl1->cy, himl2->cy);
2108 yOff1 = 0;
2109 yOff2 = 0;
2110 }
2111
2112 newFlags = (himl1->flags > himl2->flags ? himl1->flags : himl2->flags) & ILC_COLORDDB;
2113 if (newFlags == ILC_COLORDDB && (himl1->flags & ILC_COLORDDB) == ILC_COLOR16)
2114 newFlags = ILC_COLOR16; /* this is what native (at least v5) does, don't know why */
2115 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | newFlags, 1, 1);
2116
2117 if (himlDst)
2118 {
2119 imagelist_point_from_index( himl1, i1, &pt1 );
2120 imagelist_point_from_index( himl2, i2, &pt2 );
2121
2122 /* copy image */
2123 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2124 if (i1 >= 0 && i1 < himl1->cCurImage)
2125 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2126 if (i2 >= 0 && i2 < himl2->cCurImage)
2127 {
2128 if (himl2->flags & ILC_MASK)
2129 {
2130 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2131 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2132 }
2133 else
2134 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCCOPY);
2135 }
2136
2137 /* copy mask */
2138 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2139 if (i1 >= 0 && i1 < himl1->cCurImage)
2140 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2141 if (i2 >= 0 && i2 < himl2->cCurImage)
2142 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2143
2144 himlDst->cCurImage = 1;
2145 }
2146
2147 return himlDst;
2148 }
2149
2150
2151 /* helper for ImageList_Read, see comments below */
2152 static void *read_bitmap(LPSTREAM pstm, BITMAPINFO *bmi)
2153 {
2154 BITMAPFILEHEADER bmfh;
2155 int bitsperpixel, palspace;
2156 void *bits;
2157
2158 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2159 return NULL;
2160
2161 if (bmfh.bfType != (('M'<<8)|'B'))
2162 return NULL;
2163
2164 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2165 return NULL;
2166
2167 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2168 return NULL;
2169
2170 TRACE("width %u, height %u, planes %u, bpp %u\n",
2171 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2172 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2173
2174 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2175 if (bitsperpixel<=8)
2176 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2177 else
2178 palspace = 0;
2179
2180 bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2181
2182 /* read the palette right after the end of the bitmapinfoheader */
2183 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2184 return NULL;
2185
2186 bits = Alloc(bmi->bmiHeader.biSizeImage);
2187 if (!bits) return NULL;
2188
2189 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2190 {
2191 Free(bits);
2192 return NULL;
2193 }
2194 return bits;
2195 }
2196
2197 /*************************************************************************
2198 * ImageList_Read [COMCTL32.@]
2199 *
2200 * Reads an image list from a stream.
2201 *
2202 * PARAMS
2203 * pstm [I] pointer to a stream
2204 *
2205 * RETURNS
2206 * Success: handle to image list
2207 * Failure: NULL
2208 *
2209 * The format is like this:
2210 * ILHEAD ilheadstruct;
2211 *
2212 * for the color image part:
2213 * BITMAPFILEHEADER bmfh;
2214 * BITMAPINFOHEADER bmih;
2215 * only if it has a palette:
2216 * RGBQUAD rgbs[nr_of_paletted_colors];
2217 *
2218 * BYTE colorbits[imagesize];
2219 *
2220 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2221 * BITMAPFILEHEADER bmfh_mask;
2222 * BITMAPINFOHEADER bmih_mask;
2223 * only if it has a palette (it usually does not):
2224 * RGBQUAD rgbs[nr_of_paletted_colors];
2225 *
2226 * BYTE maskbits[imagesize];
2227 */
2228 HIMAGELIST WINAPI ImageList_Read (LPSTREAM pstm)
2229 {
2230 char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2231 char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2232 BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2233 BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2234 void *image_bits, *mask_bits = NULL;
2235 ILHEAD ilHead;
2236 HIMAGELIST himl;
2237 unsigned int i;
2238
2239 TRACE("%p\n", pstm);
2240
2241 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2242 return NULL;
2243 if (ilHead.usMagic != (('L' << 8) | 'I'))
2244 return NULL;
2245 if (ilHead.usVersion != 0x101) /* probably version? */
2246 return NULL;
2247
2248 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2249 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2250
2251 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2252 if (!himl)
2253 return NULL;
2254
2255 if (!(image_bits = read_bitmap(pstm, image_info)))
2256 {
2257 WARN("failed to read bitmap from stream\n");
2258 return NULL;
2259 }
2260 if (ilHead.flags & ILC_MASK)
2261 {
2262 if (!(mask_bits = read_bitmap(pstm, mask_info)))
2263 {
2264 WARN("failed to read mask bitmap from stream\n");
2265 return NULL;
2266 }
2267 }
2268 else mask_info = NULL;
2269
2270 if (himl->has_alpha && image_info->bmiHeader.biBitCount == 32)
2271 {
2272 DWORD *ptr = image_bits;
2273 BYTE *mask_ptr = mask_bits;
2274 int stride = himl->cy * image_info->bmiHeader.biWidth;
2275
2276 if (image_info->bmiHeader.biHeight > 0) /* bottom-up */
2277 {
2278 ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2279 mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2280 stride = -stride;
2281 image_info->bmiHeader.biHeight = himl->cy;
2282 }
2283 else image_info->bmiHeader.biHeight = -himl->cy;
2284
2285 for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2286 {
2287 add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2288 himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2289 ptr += stride;
2290 mask_ptr += stride / 8;
2291 }
2292 }
2293 else
2294 {
2295 StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2296 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2297 image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2298 if (mask_info)
2299 StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2300 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2301 mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2302 }
2303 Free( image_bits );
2304 Free( mask_bits );
2305
2306 himl->cCurImage = ilHead.cCurImage;
2307 himl->cMaxImage = ilHead.cMaxImage;
2308
2309 ImageList_SetBkColor(himl,ilHead.bkcolor);
2310 for (i=0;i<4;i++)
2311 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2312 return himl;
2313 }
2314
2315
2316 /*************************************************************************
2317 * ImageList_Remove [COMCTL32.@]
2318 *
2319 * Removes an image from an image list
2320 *
2321 * PARAMS
2322 * himl [I] image list handle
2323 * i [I] image index
2324 *
2325 * RETURNS
2326 * Success: TRUE
2327 * Failure: FALSE
2328 *
2329 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2330 * images without creating a new bitmap.
2331 */
2332 BOOL WINAPI
2333 ImageList_Remove (HIMAGELIST himl, INT i)
2334 {
2335 HBITMAP hbmNewImage, hbmNewMask;
2336 HDC hdcBmp;
2337 SIZE sz;
2338
2339 TRACE("(himl=%p i=%d)\n", himl, i);
2340
2341 if (!is_valid(himl)) {
2342 ERR("Invalid image list handle!\n");
2343 return FALSE;
2344 }
2345
2346 if ((i < -1) || (i >= himl->cCurImage)) {
2347 TRACE("index out of range! %d\n", i);
2348 return FALSE;
2349 }
2350
2351 if (i == -1) {
2352 INT nCount;
2353
2354 /* remove all */
2355 if (himl->cCurImage == 0) {
2356 /* remove all on empty ImageList is allowed */
2357 TRACE("remove all on empty ImageList!\n");
2358 return TRUE;
2359 }
2360
2361 himl->cMaxImage = himl->cGrow;
2362 himl->cCurImage = 0;
2363 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2364 himl->nOvlIdx[nCount] = -1;
2365
2366 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2367 SelectObject (himl->hdcImage, hbmNewImage);
2368 DeleteObject (himl->hbmImage);
2369 himl->hbmImage = hbmNewImage;
2370
2371 if (himl->hbmMask) {
2372
2373 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2374 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2375 SelectObject (himl->hdcMask, hbmNewMask);
2376 DeleteObject (himl->hbmMask);
2377 himl->hbmMask = hbmNewMask;
2378 }
2379 }
2380 else {
2381 /* delete one image */
2382 TRACE("Remove single image! %d\n", i);
2383
2384 /* create new bitmap(s) */
2385 TRACE(" - Number of images: %d / %d (Old/New)\n",
2386 himl->cCurImage, himl->cCurImage - 1);
2387
2388 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2389
2390 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2391 if (himl->hbmMask)
2392 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2393 else
2394 hbmNewMask = 0; /* Just to keep compiler happy! */
2395
2396 hdcBmp = CreateCompatibleDC (0);
2397
2398 /* copy all images and masks prior to the "removed" image */
2399 if (i > 0) {
2400 TRACE("Pre image copy: Copy %d images\n", i);
2401
2402 SelectObject (hdcBmp, hbmNewImage);
2403 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2404
2405 if (himl->hbmMask) {
2406 SelectObject (hdcBmp, hbmNewMask);
2407 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2408 }
2409 }
2410
2411 /* copy all images and masks behind the removed image */
2412 if (i < himl->cCurImage - 1) {
2413 TRACE("Post image copy!\n");
2414
2415 SelectObject (hdcBmp, hbmNewImage);
2416 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2417 (himl->cCurImage - i), i );
2418
2419 if (himl->hbmMask) {
2420 SelectObject (hdcBmp, hbmNewMask);
2421 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2422 (himl->cCurImage - i), i );
2423 }
2424 }
2425
2426 DeleteDC (hdcBmp);
2427
2428 /* delete old images and insert new ones */
2429 SelectObject (himl->hdcImage, hbmNewImage);
2430 DeleteObject (himl->hbmImage);
2431 himl->hbmImage = hbmNewImage;
2432 if (himl->hbmMask) {
2433 SelectObject (himl->hdcMask, hbmNewMask);
2434 DeleteObject (himl->hbmMask);
2435 himl->hbmMask = hbmNewMask;
2436 }
2437
2438 himl->cCurImage--;
2439 }
2440
2441 return TRUE;
2442 }
2443
2444
2445 /*************************************************************************
2446 * ImageList_Replace [COMCTL32.@]
2447 *
2448 * Replaces an image in an image list with a new image.
2449 *
2450 * PARAMS
2451 * himl [I] handle to image list
2452 * i [I] image index
2453 * hbmImage [I] handle to image bitmap
2454 * hbmMask [I] handle to mask bitmap. Can be NULL.
2455 *
2456 * RETURNS
2457 * Success: TRUE
2458 * Failure: FALSE
2459 */
2460
2461 BOOL WINAPI
2462 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2463 HBITMAP hbmMask)
2464 {
2465 HDC hdcImage;
2466 BITMAP bmp;
2467 POINT pt;
2468
2469 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2470
2471 if (!is_valid(himl)) {
2472 ERR("Invalid image list handle!\n");
2473 return FALSE;
2474 }
2475
2476 if ((i >= himl->cMaxImage) || (i < 0)) {
2477 ERR("Invalid image index!\n");
2478 return FALSE;
2479 }
2480
2481 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2482 return FALSE;
2483
2484 hdcImage = CreateCompatibleDC (0);
2485
2486 /* Replace Image */
2487 SelectObject (hdcImage, hbmImage);
2488
2489 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2490 goto done;
2491
2492 imagelist_point_from_index(himl, i, &pt);
2493 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2494 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2495
2496 if (himl->hbmMask)
2497 {
2498 HDC hdcTemp;
2499 HBITMAP hOldBitmapTemp;
2500
2501 hdcTemp = CreateCompatibleDC(0);
2502 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2503
2504 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2505 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2506 SelectObject(hdcTemp, hOldBitmapTemp);
2507 DeleteDC(hdcTemp);
2508
2509 /* Remove the background from the image
2510 */
2511 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2512 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2513 }
2514
2515 done:
2516 DeleteDC (hdcImage);
2517
2518 return TRUE;
2519 }
2520
2521
2522 /*************************************************************************
2523 * ImageList_ReplaceIcon [COMCTL32.@]
2524 *
2525 * Replaces an image in an image list using an icon.
2526 *
2527 * PARAMS
2528 * himl [I] handle to image list
2529 * i [I] image index
2530 * hIcon [I] handle to icon
2531 *
2532 * RETURNS
2533 * Success: index of the replaced image
2534 * Failure: -1
2535 */
2536
2537 INT WINAPI
2538 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2539 {
2540 HICON hBestFitIcon;
2541 ICONINFO ii;
2542 BITMAP bmp;
2543 BOOL ret;
2544 POINT pt;
2545
2546 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2547
2548 if (!is_valid(himl)) {
2549 ERR("invalid image list\n");
2550 return -1;
2551 }
2552 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2553 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2554 return -1;
2555 }
2556
2557 hBestFitIcon = CopyImage(
2558 hIcon, IMAGE_ICON,
2559 himl->cx, himl->cy,
2560 LR_COPYFROMRESOURCE);
2561 /* the above will fail if the icon wasn't loaded from a resource, so try
2562 * again without LR_COPYFROMRESOURCE flag */
2563 if (!hBestFitIcon)
2564 hBestFitIcon = CopyImage(
2565 hIcon, IMAGE_ICON,
2566 himl->cx, himl->cy,
2567 0);
2568 if (!hBestFitIcon)
2569 return -1;
2570
2571 if (nIndex == -1) {
2572 if (himl->cCurImage + 1 >= himl->cMaxImage)
2573 IMAGELIST_InternalExpandBitmaps(himl, 1);
2574
2575 nIndex = himl->cCurImage;
2576 himl->cCurImage++;
2577 }
2578
2579 if (himl->has_alpha && GetIconInfo (hBestFitIcon, &ii))
2580 {
2581 HDC hdcImage = CreateCompatibleDC( 0 );
2582 GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2583
2584 if (!ii.hbmColor)
2585 {
2586 UINT height = bmp.bmHeight / 2;
2587 HDC hdcMask = CreateCompatibleDC( 0 );
2588 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2589 SelectObject( hdcImage, color );
2590 SelectObject( hdcMask, ii.hbmMask );
2591 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2592 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2593 DeleteDC( hdcMask );
2594 DeleteObject( color );
2595 }
2596 else ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2597 ii.hbmColor, ii.hbmMask );
2598
2599 DeleteDC( hdcImage );
2600 DeleteObject (ii.hbmMask);
2601 if (ii.hbmColor) DeleteObject (ii.hbmColor);
2602 if (ret) goto done;
2603 }
2604
2605 imagelist_point_from_index(himl, nIndex, &pt);
2606
2607 if (himl->hbmMask)
2608 {
2609 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_IMAGE );
2610 PatBlt( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy, WHITENESS );
2611 DrawIconEx( himl->hdcMask, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_MASK );
2612 }
2613 else
2614 {
2615 COLORREF color = himl->clrBk != CLR_NONE ? himl->clrBk : comctl32_color.clrWindow;
2616 HBRUSH brush = CreateSolidBrush( GetNearestColor( himl->hdcImage, color ));
2617
2618 SelectObject( himl->hdcImage, brush );
2619 PatBlt( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy, PATCOPY );
2620 SelectObject( himl->hdcImage, GetStockObject(BLACK_BRUSH) );
2621 DeleteObject( brush );
2622 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_NORMAL );
2623 }
2624
2625 done:
2626 DestroyIcon(hBestFitIcon);
2627
2628 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2629 return nIndex;
2630 }
2631
2632
2633 /*************************************************************************
2634 * ImageList_SetBkColor [COMCTL32.@]
2635 *
2636 * Sets the background color of an image list.
2637 *
2638 * PARAMS
2639 * himl [I] handle to image list
2640 * clrBk [I] background color
2641 *
2642 * RETURNS
2643 * Success: previous background color
2644 * Failure: CLR_NONE
2645 */
2646
2647 COLORREF WINAPI
2648 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2649 {
2650 COLORREF clrOldBk;
2651
2652 if (!is_valid(himl))
2653 return CLR_NONE;
2654
2655 clrOldBk = himl->clrBk;
2656 himl->clrBk = clrBk;
2657 return clrOldBk;
2658 }
2659
2660
2661 /*************************************************************************
2662 * ImageList_SetDragCursorImage [COMCTL32.@]
2663 *
2664 * Combines the specified image with the current drag image
2665 *
2666 * PARAMS
2667 * himlDrag [I] handle to drag image list
2668 * iDrag [I] drag image index
2669 * dxHotspot [I] X position of the hot spot
2670 * dyHotspot [I] Y position of the hot spot
2671 *
2672 * RETURNS
2673 * Success: TRUE
2674 * Failure: FALSE
2675 *
2676 * NOTES
2677 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2678 * to do with a hotspot but are only the offset of the origin of the new
2679 * image relative to the origin of the old image.
2680 *
2681 * - When this function is called and the drag image is visible, a
2682 * short flickering occurs but this matches the Win9x behavior. It is
2683 * possible to fix the flickering using code like in ImageList_DragMove.
2684 */
2685
2686 BOOL WINAPI
2687 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2688 INT dxHotspot, INT dyHotspot)
2689 {
2690 HIMAGELIST himlTemp;
2691 BOOL visible;
2692
2693 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2694 return FALSE;
2695
2696 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2697 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2698
2699 visible = InternalDrag.bShow;
2700
2701 himlTemp = ImageList_Merge (InternalDrag.himlNoCursor, 0, himlDrag, iDrag,
2702 dxHotspot, dyHotspot);
2703
2704 if (visible) {
2705 /* hide the drag image */
2706 ImageList_DragShowNolock(FALSE);
2707 }
2708 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2709 (InternalDrag.himl->cy != himlTemp->cy)) {
2710 /* the size of the drag image changed, invalidate the buffer */
2711 DeleteObject(InternalDrag.hbmBg);
2712 InternalDrag.hbmBg = 0;
2713 }
2714
2715 if (InternalDrag.himl != InternalDrag.himlNoCursor)
2716 ImageList_Destroy (InternalDrag.himl);
2717 InternalDrag.himl = himlTemp;
2718
2719 if (visible) {
2720 /* show the drag image */
2721 ImageList_DragShowNolock(TRUE);
2722 }
2723
2724 return TRUE;
2725 }
2726
2727
2728 /*************************************************************************
2729 * ImageList_SetFilter [COMCTL32.@]
2730 *
2731 * Sets a filter (or does something completely different)!!???
2732 * It removes 12 Bytes from the stack (3 Parameters).
2733 *
2734 * PARAMS
2735 * himl [I] SHOULD be a handle to image list
2736 * i [I] COULD be an index?
2737 * dwFilter [I] ???
2738 *
2739 * RETURNS
2740 * Success: TRUE ???
2741 * Failure: FALSE ???
2742 *
2743 * BUGS
2744 * This is an UNDOCUMENTED function!!!!
2745 * empty stub.
2746 */
2747
2748 BOOL WINAPI
2749 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2750 {
2751 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2752
2753 return FALSE;
2754 }
2755
2756
2757 /*************************************************************************
2758 * ImageList_SetFlags [COMCTL32.@]
2759 *
2760 * Sets the image list flags.
2761 *
2762 * PARAMS
2763 * himl [I] Handle to image list
2764 * flags [I] Flags to set
2765 *
2766 * RETURNS
2767 * Old flags?
2768 *
2769 * BUGS
2770 * Stub.
2771 */
2772
2773 DWORD WINAPI
2774 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2775 {
2776 FIXME("(%p %08x):empty stub\n", himl, flags);
2777 return 0;
2778 }
2779
2780
2781 /*************************************************************************
2782 * ImageList_SetIconSize [COMCTL32.@]
2783 *
2784 * Sets the image size of the bitmap and deletes all images.
2785 *
2786 * PARAMS
2787 * himl [I] handle to image list
2788 * cx [I] image width
2789 * cy [I] image height
2790 *
2791 * RETURNS
2792 * Success: TRUE
2793 * Failure: FALSE
2794 */
2795
2796 BOOL WINAPI
2797 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2798 {
2799 INT nCount;
2800 HBITMAP hbmNew;
2801
2802 if (!is_valid(himl))
2803 return FALSE;
2804
2805 /* remove all images */
2806 himl->cMaxImage = himl->cInitial + 1;
2807 himl->cCurImage = 0;
2808 himl->cx = cx;
2809 himl->cy = cy;
2810
2811 /* initialize overlay mask indices */
2812 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2813 himl->nOvlIdx[nCount] = -1;
2814
2815 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2816 SelectObject (himl->hdcImage, hbmNew);
2817 DeleteObject (himl->hbmImage);
2818 himl->hbmImage = hbmNew;
2819
2820 if (himl->hbmMask) {
2821 SIZE sz;
2822 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2823 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2824 SelectObject (himl->hdcMask, hbmNew);
2825 DeleteObject (himl->hbmMask);
2826 himl->hbmMask = hbmNew;
2827 }
2828
2829 return TRUE;
2830 }
2831
2832
2833 /*************************************************************************
2834 * ImageList_SetImageCount [COMCTL32.@]
2835 *
2836 * Resizes an image list to the specified number of images.
2837 *
2838 * PARAMS
2839 * himl [I] handle to image list
2840 * iImageCount [I] number of images in the image list
2841 *
2842 * RETURNS
2843 * Success: TRUE
2844 * Failure: FALSE
2845 */
2846
2847 BOOL WINAPI
2848 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2849 {
2850 HDC hdcBitmap;
2851 HBITMAP hbmNewBitmap, hbmOld;
2852 INT nNewCount, nCopyCount;
2853
2854 TRACE("%p %d\n",himl,iImageCount);
2855
2856 if (!is_valid(himl))
2857 return FALSE;
2858
2859 nNewCount = iImageCount + 1;
2860 nCopyCount = min(himl->cCurImage, iImageCount);
2861
2862 hdcBitmap = CreateCompatibleDC (0);
2863
2864 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2865
2866 if (hbmNewBitmap != 0)
2867 {
2868 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2869 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2870 SelectObject (hdcBitmap, hbmOld);
2871
2872 /* FIXME: delete 'empty' image space? */
2873
2874 SelectObject (himl->hdcImage, hbmNewBitmap);
2875 DeleteObject (himl->hbmImage);
2876 himl->hbmImage = hbmNewBitmap;
2877 }
2878 else
2879 ERR("Could not create new image bitmap!\n");
2880
2881 if (himl->hbmMask)
2882 {
2883 SIZE sz;
2884 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2885 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2886 if (hbmNewBitmap != 0)
2887 {
2888 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2889 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2890 SelectObject (hdcBitmap, hbmOld);
2891
2892 /* FIXME: delete 'empty' image space? */
2893
2894 SelectObject (himl->hdcMask, hbmNewBitmap);
2895 DeleteObject (himl->hbmMask);
2896 himl->hbmMask = hbmNewBitmap;
2897 }
2898 else
2899 ERR("Could not create new mask bitmap!\n");
2900 }
2901
2902 DeleteDC (hdcBitmap);
2903
2904 if (himl->has_alpha)
2905 {
2906 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
2907 if (new_alpha) himl->has_alpha = new_alpha;
2908 else
2909 {
2910 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2911 himl->has_alpha = NULL;
2912 }
2913 }
2914
2915 /* Update max image count and current image count */
2916 himl->cMaxImage = nNewCount;
2917 himl->cCurImage = iImageCount;
2918
2919 return TRUE;
2920 }
2921
2922
2923 /*************************************************************************
2924 * ImageList_SetOverlayImage [COMCTL32.@]
2925 *
2926 * Assigns an overlay mask index to an existing image in an image list.
2927 *
2928 * PARAMS
2929 * himl [I] handle to image list
2930 * iImage [I] image index
2931 * iOverlay [I] overlay mask index
2932 *
2933 * RETURNS
2934 * Success: TRUE
2935 * Failure: FALSE
2936 */
2937
2938 BOOL WINAPI
2939 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2940 {
2941 if (!is_valid(himl))
2942 return FALSE;
2943 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2944 return FALSE;
2945 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2946 return FALSE;
2947 himl->nOvlIdx[iOverlay - 1] = iImage;
2948 return TRUE;
2949 }
2950
2951
2952
2953 /* helper for ImageList_Write - write bitmap to pstm
2954 * currently everything is written as 24 bit RGB, except masks
2955 */
2956 static BOOL
2957 _write_bitmap(HBITMAP hBitmap, LPSTREAM pstm)
2958 {
2959 LPBITMAPFILEHEADER bmfh;
2960 LPBITMAPINFOHEADER bmih;
2961 LPBYTE data = NULL, lpBits;
2962 BITMAP bm;
2963 INT bitCount, sizeImage, offBits, totalSize;
2964 HDC xdc;
2965 BOOL result = FALSE;
2966
2967 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2968 return FALSE;
2969
2970 bitCount = bm.bmBitsPixel;
2971 sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
2972
2973 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
2974 if(bitCount <= 8)
2975 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
2976 offBits = totalSize;
2977 totalSize += sizeImage;
2978
2979 data = Alloc(totalSize);
2980 bmfh = (LPBITMAPFILEHEADER)data;
2981 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
2982 lpBits = data + offBits;
2983
2984 /* setup BITMAPFILEHEADER */
2985 bmfh->bfType = (('M' << 8) | 'B');
2986 bmfh->bfSize = offBits;
2987 bmfh->bfReserved1 = 0;
2988 bmfh->bfReserved2 = 0;
2989 bmfh->bfOffBits = offBits;
2990
2991 /* setup BITMAPINFOHEADER */
2992 bmih->biSize = sizeof(BITMAPINFOHEADER);
2993 bmih->biWidth = bm.bmWidth;
2994 bmih->biHeight = bm.bmHeight;
2995 bmih->biPlanes = 1;
2996 bmih->biBitCount = bitCount;
2997 bmih->biCompression = BI_RGB;
2998 bmih->biSizeImage = sizeImage;
2999 bmih->biXPelsPerMeter = 0;
3000 bmih->biYPelsPerMeter = 0;
3001 bmih->biClrUsed = 0;
3002 bmih->biClrImportant = 0;
3003
3004 xdc = GetDC(0);
3005 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
3006 ReleaseDC(0, xdc);
3007 if (!result)
3008 goto failed;
3009
3010 TRACE("width %u, height %u, planes %u, bpp %u\n",
3011 bmih->biWidth, bmih->biHeight,
3012 bmih->biPlanes, bmih->biBitCount);
3013
3014 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
3015 goto failed;
3016
3017 result = TRUE;
3018
3019 failed:
3020 Free(data);
3021
3022 return result;
3023 }
3024
3025
3026 /*************************************************************************
3027 * ImageList_Write [COMCTL32.@]
3028 *
3029 * Writes an image list to a stream.
3030 *
3031 * PARAMS
3032 * himl [I] handle to image list
3033 * pstm [O] Pointer to a stream.
3034 *
3035 * RETURNS
3036 * Success: TRUE
3037 * Failure: FALSE
3038 *
3039 * BUGS
3040 * probably.
3041 */
3042
3043 BOOL WINAPI
3044 ImageList_Write (HIMAGELIST himl, LPSTREAM pstm)
3045 {
3046 ILHEAD ilHead;
3047 int i;
3048
3049 TRACE("%p %p\n", himl, pstm);
3050
3051 if (!is_valid(himl))
3052 return FALSE;
3053
3054 ilHead.usMagic = (('L' << 8) | 'I');
3055 ilHead.usVersion = 0x101;
3056 ilHead.cCurImage = himl->cCurImage;
3057 ilHead.cMaxImage = himl->cMaxImage;
3058 ilHead.cGrow = himl->cGrow;
3059 ilHead.cx = himl->cx;
3060 ilHead.cy = himl->cy;
3061 ilHead.bkcolor = himl->clrBk;
3062 ilHead.flags = himl->flags;
3063 for(i = 0; i < 4; i++) {
3064 ilHead.ovls[i] = himl->nOvlIdx[i];
3065 }
3066
3067 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3068 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3069
3070 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3071 return FALSE;
3072
3073 /* write the bitmap */
3074 if(!_write_bitmap(himl->hbmImage, pstm))
3075 return FALSE;
3076
3077 /* write the mask if we have one */
3078 if(himl->flags & ILC_MASK) {
3079 if(!_write_bitmap(himl->hbmMask, pstm))
3080 return FALSE;
3081 }
3082
3083 return TRUE;
3084 }
3085
3086
3087 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3088 {
3089 HBITMAP hbmNewBitmap;
3090 UINT ilc = (himl->flags & 0xFE);
3091 SIZE sz;
3092
3093 imagelist_get_bitmap_size( himl, count, &sz );
3094
3095 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3096 {
3097 char buffer[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
3098 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
3099
3100 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3101 sz.cx, sz.cy, himl->uBitsPixel);
3102
3103 memset( buffer, 0, sizeof(buffer) );
3104 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3105 bmi->bmiHeader.biWidth = sz.cx;
3106 bmi->bmiHeader.biHeight = sz.cy;
3107 bmi->bmiHeader.biPlanes = 1;
3108 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3109 bmi->bmiHeader.biCompression = BI_RGB;
3110
3111 if (himl->uBitsPixel <= ILC_COLOR8)
3112 {
3113 /* retrieve the default color map */
3114 HBITMAP tmp = CreateBitmap( 1, 1, 1, 1, NULL );
3115 GetDIBits( hdc, tmp, 0, 0, NULL, bmi, DIB_RGB_COLORS );
3116 DeleteObject( tmp );
3117 }
3118 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, 0, 0);
3119 }
3120 else /*if (ilc == ILC_COLORDDB)*/
3121 {
3122 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3123
3124 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3125 }
3126 TRACE("returning %p\n", hbmNewBitmap);
3127 return hbmNewBitmap;
3128 }
3129
3130 /*************************************************************************
3131 * ImageList_SetColorTable [COMCTL32.@]
3132 *
3133 * Sets the color table of an image list.
3134 *
3135 * PARAMS
3136 * himl [I] Handle to the image list.
3137 * uStartIndex [I] The first index to set.
3138 * cEntries [I] Number of entries to set.
3139 * prgb [I] New color information for color table for the image list.
3140 *
3141 * RETURNS
3142 * Success: Number of entries in the table that were set.
3143 * Failure: Zero.
3144 *
3145 * SEE
3146 * ImageList_Create(), SetDIBColorTable()
3147 */
3148
3149 UINT WINAPI
3150 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb)
3151 {
3152 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3153 }
3154
3155 /*************************************************************************
3156 * ImageList_CoCreateInstance [COMCTL32.@]
3157 *
3158 * Creates a new imagelist instance and returns an interface pointer to it.
3159 *
3160 * PARAMS
3161 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3162 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3163 * riid [I] Identifier of the requested interface.
3164 * ppv [O] Returns the address of the pointer requested, or NULL.
3165 *
3166 * RETURNS
3167 * Success: S_OK.
3168 * Failure: Error value.
3169 */
3170 HRESULT WINAPI
3171 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3172 {
3173 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3174
3175 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3176 return E_NOINTERFACE;
3177
3178 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3179 }
3180
3181
3182 /*************************************************************************
3183 * IImageList implementation
3184 */
3185
3186 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList *iface,
3187 REFIID iid, void **ppv)
3188 {
3189 HIMAGELIST This = (HIMAGELIST) iface;
3190 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3191
3192 if (!ppv) return E_INVALIDARG;
3193
3194 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IImageList, iid))
3195 *ppv = This;
3196 else
3197 {
3198 *ppv = NULL;
3199 return E_NOINTERFACE;
3200 }
3201
3202 IUnknown_AddRef((IUnknown*)*ppv);
3203 return S_OK;
3204 }
3205
3206 static ULONG WINAPI ImageListImpl_AddRef(IImageList *iface)
3207 {
3208 HIMAGELIST This = (HIMAGELIST) iface;
3209 ULONG ref = InterlockedIncrement(&This->ref);
3210
3211 TRACE("(%p) refcount=%u\n", iface, ref);
3212 return ref;
3213 }
3214
3215 static ULONG WINAPI ImageListImpl_Release(IImageList *iface)
3216 {
3217 HIMAGELIST This = (HIMAGELIST) iface;
3218 ULONG ref = InterlockedDecrement(&This->ref);
3219
3220 TRACE("(%p) refcount=%u\n", iface, ref);
3221
3222 if (ref == 0)
3223 {
3224 /* delete image bitmaps */
3225 if (This->hbmImage) DeleteObject (This->hbmImage);
3226 if (This->hbmMask) DeleteObject (This->hbmMask);
3227
3228 /* delete image & mask DCs */
3229 if (This->hdcImage) DeleteDC (This->hdcImage);
3230 if (This->hdcMask) DeleteDC (This->hdcMask);
3231
3232 /* delete blending brushes */
3233 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3234 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3235
3236 This->lpVtbl = NULL;
3237 HeapFree(GetProcessHeap(), 0, This->has_alpha);
3238 HeapFree(GetProcessHeap(), 0, This);
3239 }
3240
3241 return ref;
3242 }
3243
3244 static HRESULT WINAPI ImageListImpl_Add(IImageList *iface, HBITMAP hbmImage,
3245 HBITMAP hbmMask, int *pi)
3246 {
3247 HIMAGELIST This = (HIMAGELIST) iface;
3248 int ret;
3249
3250 if (!pi)
3251 return E_FAIL;
3252
3253 ret = ImageList_Add(This, hbmImage, hbmMask);
3254
3255 if (ret == -1)
3256 return E_FAIL;
3257
3258 *pi = ret;
3259 return S_OK;
3260 }
3261
3262 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList *iface, int i,
3263 HICON hicon, int *pi)
3264 {
3265 HIMAGELIST This = (HIMAGELIST) iface;
3266 int ret;
3267
3268 if (!pi)
3269 return E_FAIL;
3270
3271 ret = ImageList_ReplaceIcon(This, i, hicon);
3272
3273 if (ret == -1)
3274 return E_FAIL;
3275
3276 *pi = ret;
3277 return S_OK;
3278 }
3279
3280 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList *iface,
3281 int iImage, int iOverlay)
3282 {
3283 return ImageList_SetOverlayImage((HIMAGELIST) iface, iImage, iOverlay)
3284 ? S_OK : E_FAIL;
3285 }
3286
3287 static HRESULT WINAPI ImageListImpl_Replace(IImageList *iface, int i,
3288 HBITMAP hbmImage, HBITMAP hbmMask)
3289 {
3290 return ImageList_Replace((HIMAGELIST) iface, i, hbmImage, hbmMask) ? S_OK :
3291 E_FAIL;
3292 }
3293
3294 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList *iface, HBITMAP hbmImage,
3295 COLORREF crMask, int *pi)
3296 {
3297 HIMAGELIST This = (HIMAGELIST) iface;
3298 int ret;
3299
3300 if (!pi)
3301 return E_FAIL;
3302
3303 ret = ImageList_AddMasked(This, hbmImage, crMask);
3304
3305 if (ret == -1)
3306 return E_FAIL;
3307
3308 *pi = ret;
3309 return S_OK;
3310 }
3311
3312 static HRESULT WINAPI ImageListImpl_Draw(IImageList *iface,
3313 IMAGELISTDRAWPARAMS *pimldp)
3314 {
3315 HIMAGELIST This = (HIMAGELIST) iface;
3316 HIMAGELIST old_himl;
3317 int ret;
3318
3319 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3320 so we shall simulate the same */
3321 old_himl = pimldp->himl;
3322 pimldp->himl = This;
3323
3324 ret = ImageList_DrawIndirect(pimldp);
3325
3326 pimldp->himl = old_himl;
3327 return ret ? S_OK : E_INVALIDARG;
3328 }
3329
3330 static HRESULT WINAPI ImageListImpl_Remove(IImageList *iface, int i)
3331 {
3332 return (ImageList_Remove((HIMAGELIST) iface, i) == 0) ? E_INVALIDARG : S_OK;
3333 }
3334
3335 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList *iface, int i, UINT flags,
3336 HICON *picon)
3337 {
3338 HICON hIcon;
3339
3340 if (!picon)
3341 return E_FAIL;
3342
3343 hIcon = ImageList_GetIcon((HIMAGELIST) iface, i, flags);
3344
3345 if (hIcon == NULL)
3346 return E_FAIL;
3347
3348 *picon = hIcon;
3349 return S_OK;
3350 }
3351
3352 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList *iface, int i,
3353 IMAGEINFO *pImageInfo)
3354 {
3355 return ImageList_GetImageInfo((HIMAGELIST) iface, i, pImageInfo) ? S_OK : E_FAIL;
3356 }
3357
3358 static HRESULT WINAPI ImageListImpl_Copy(IImageList *iface, int iDst,
3359 IUnknown *punkSrc, int iSrc, UINT uFlags)
3360 {
3361 HIMAGELIST This = (HIMAGELIST) iface;
3362 IImageList *src = NULL;
3363 HRESULT ret;
3364
3365 if (!punkSrc)
3366 return E_FAIL;
3367
3368 /* TODO: Add test for IID_ImageList2 too */
3369 if (FAILED(IUnknown_QueryInterface(punkSrc, &IID_IImageList,
3370 (void **) &src)))
3371 return E_FAIL;
3372
3373 if (ImageList_Copy(This, iDst, (HIMAGELIST) src, iSrc, uFlags))
3374 ret = S_OK;
3375 else
3376 ret = E_FAIL;
3377
3378 IImageList_Release(src);
3379 return ret;
3380 }
3381
3382 static HRESULT WINAPI ImageListImpl_Merge(IImageList *iface, int i1,
3383 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3384 {
3385 HIMAGELIST This = (HIMAGELIST) iface;
3386 IImageList *iml2 = NULL;
3387 HIMAGELIST hNew;
3388 HRESULT ret = E_FAIL;
3389
3390 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3391
3392 /* TODO: Add test for IID_ImageList2 too */
3393 if (FAILED(IUnknown_QueryInterface(punk2, &IID_IImageList,
3394 (void **) &iml2)))
3395 return E_FAIL;
3396
3397 hNew = ImageList_Merge(This, i1, (HIMAGELIST) iml2, i2, dx, dy);
3398
3399 /* Get the interface for the new image list */
3400 if (hNew)
3401 {
3402 IImageList *imerge = (IImageList*)hNew;
3403
3404 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3405 IImageList_Release(imerge);
3406 }
3407
3408 IImageList_Release(iml2);
3409 return ret;
3410 }
3411
3412 static HRESULT WINAPI ImageListImpl_Clone(IImageList *iface, REFIID riid, void **ppv)
3413 {
3414 HIMAGELIST This = (HIMAGELIST) iface;
3415 HIMAGELIST clone;
3416 HRESULT ret = E_FAIL;
3417
3418 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3419
3420 clone = ImageList_Duplicate(This);
3421
3422 /* Get the interface for the new image list */
3423 if (clone)
3424 {
3425 IImageList *iclone = (IImageList*)clone;
3426
3427 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3428 IImageList_Release(iclone);
3429 }
3430
3431 return ret;
3432 }
3433
3434 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList *iface, int i,
3435 RECT *prc)
3436 {
3437 HIMAGELIST This = (HIMAGELIST) iface;
3438 IMAGEINFO info;
3439
3440 if (!prc)
3441 return E_FAIL;
3442
3443 if (!ImageList_GetImageInfo(This, i, &info))
3444 return E_FAIL;
3445
3446 return CopyRect(prc, &info.rcImage) ? S_OK : E_FAIL;
3447 }
3448
3449 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList *iface, int *cx,
3450 int *cy)
3451 {
3452 HIMAGELIST This = (HIMAGELIST) iface;
3453
3454 return ImageList_GetIconSize(This, cx, cy) ? S_OK : E_INVALIDARG;
3455 }
3456
3457 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList *iface, int cx,
3458 int cy)
3459 {
3460 return ImageList_SetIconSize((HIMAGELIST) iface, cx, cy) ? S_OK : E_FAIL;
3461 }
3462
3463 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList *iface, int *pi)
3464 {
3465 *pi = ImageList_GetImageCount((HIMAGELIST) iface);
3466 return S_OK;
3467 }
3468
3469 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList *iface,
3470 UINT uNewCount)
3471 {
3472 return ImageList_SetImageCount((HIMAGELIST) iface, uNewCount) ? S_OK : E_FAIL;
3473 }
3474
3475 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList *iface, COLORREF clrBk,
3476 COLORREF *pclr)
3477 {
3478 *pclr = ImageList_SetBkColor((HIMAGELIST) iface, clrBk);
3479 return S_OK;
3480 }
3481
3482 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList *iface, COLORREF *pclr)
3483 {
3484 *pclr = ImageList_GetBkColor((HIMAGELIST) iface);
3485 return S_OK;
3486 }
3487
3488 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList *iface, int iTrack,
3489 int dxHotspot, int dyHotspot)
3490 {
3491 return ImageList_BeginDrag((HIMAGELIST) iface, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3492 }
3493
3494 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList *iface)
3495 {
3496 ImageList_EndDrag();
3497 return S_OK;
3498 }
3499
3500 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList *iface, HWND hwndLock,
3501 int x, int y)
3502 {
3503 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3504 }
3505
3506 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList *iface, HWND hwndLock)
3507 {
3508 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3509 }
3510
3511 static HRESULT WINAPI ImageListImpl_DragMove(IImageList *iface, int x, int y)
3512 {
3513 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3514 }
3515
3516 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList *iface,
3517 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3518 {
3519 IImageList *iml2 = NULL;
3520 HRESULT ret;
3521
3522 if (!punk)
3523 return E_FAIL;
3524
3525 /* TODO: Add test for IID_ImageList2 too */
3526 if (FAILED(IUnknown_QueryInterface(punk, &IID_IImageList,
3527 (void **) &iml2)))
3528 return E_FAIL;
3529
3530 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3531 dyHotspot);
3532
3533 IImageList_Release(iml2);
3534
3535 return ret ? S_OK : E_FAIL;
3536 }
3537
3538 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList *iface, BOOL fShow)
3539 {
3540 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3541 }
3542
3543 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList *iface, POINT *ppt,
3544 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3545 {
3546 HRESULT ret = E_FAIL;
3547 HIMAGELIST hNew;
3548
3549 if (!ppv)
3550 return E_FAIL;
3551
3552 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3553
3554 /* Get the interface for the new image list */
3555 if (hNew)
3556 {
3557 IImageList *idrag = (IImageList*)hNew;
3558
3559 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3560 IImageList_Release(idrag);
3561 }
3562
3563 return ret;
3564 }
3565
3566 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList *iface, int i,
3567 DWORD *dwFlags)
3568 {
3569 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3570 return E_NOTIMPL;
3571 }
3572
3573 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList *iface, int iOverlay,
3574 int *piIndex)
3575 {
3576 HIMAGELIST This = (HIMAGELIST) iface;
3577 int i;
3578
3579 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3580 return E_FAIL;
3581
3582 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3583 {
3584 if (This->nOvlIdx[i] == iOverlay)
3585 {
3586 *piIndex = i + 1;
3587 return S_OK;
3588 }
3589 }
3590
3591 return E_FAIL;
3592 }
3593
3594
3595 static const IImageListVtbl ImageListImpl_Vtbl = {
3596 ImageListImpl_QueryInterface,
3597 ImageListImpl_AddRef,
3598 ImageListImpl_Release,
3599 ImageListImpl_Add,
3600 ImageListImpl_ReplaceIcon,
3601 ImageListImpl_SetOverlayImage,
3602 ImageListImpl_Replace,
3603 ImageListImpl_AddMasked,
3604 ImageListImpl_Draw,
3605 ImageListImpl_Remove,
3606 ImageListImpl_GetIcon,
3607 ImageListImpl_GetImageInfo,
3608 ImageListImpl_Copy,
3609 ImageListImpl_Merge,
3610 ImageListImpl_Clone,
3611 ImageListImpl_GetImageRect,
3612 ImageListImpl_GetIconSize,
3613 ImageListImpl_SetIconSize,
3614 ImageListImpl_GetImageCount,
3615 ImageListImpl_SetImageCount,
3616 ImageListImpl_SetBkColor,
3617 ImageListImpl_GetBkColor,
3618 ImageListImpl_BeginDrag,
3619 ImageListImpl_EndDrag,
3620 ImageListImpl_DragEnter,
3621 ImageListImpl_DragLeave,
3622 ImageListImpl_DragMove,
3623 ImageListImpl_SetDragCursorImage,
3624 ImageListImpl_DragShowNolock,
3625 ImageListImpl_GetDragImage,
3626 ImageListImpl_GetItemFlags,
3627 ImageListImpl_GetOverlayImage
3628 };
3629
3630 static BOOL is_valid(HIMAGELIST himl)
3631 {
3632 BOOL valid;
3633 __TRY
3634 {
3635 valid = himl && himl->lpVtbl == &ImageListImpl_Vtbl;
3636 }
3637 __EXCEPT_PAGE_FAULT
3638 {
3639 valid = FALSE;
3640 }
3641 __ENDTRY
3642 return valid;
3643 }
3644
3645 /*************************************************************************
3646 * HIMAGELIST_QueryInterface [COMCTL32.@]
3647 *
3648 * Returns a pointer to an IImageList or IImageList2 object for the given
3649 * HIMAGELIST.
3650 *
3651 * PARAMS
3652 * himl [I] Image list handle.
3653 * riid [I] Identifier of the requested interface.
3654 * ppv [O] Returns the address of the pointer requested, or NULL.
3655 *
3656 * RETURNS
3657 * Success: S_OK.
3658 * Failure: Error value.
3659 */
3660 HRESULT WINAPI
3661 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3662 {
3663 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3664 return IImageList_QueryInterface((IImageList *) himl, riid, ppv);
3665 }
3666
3667 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3668 {
3669 HIMAGELIST This;
3670 HRESULT ret;
3671
3672 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3673
3674 *ppv = NULL;
3675
3676 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3677
3678 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct _IMAGELIST));
3679 if (!This) return E_OUTOFMEMORY;
3680
3681 This->lpVtbl = &ImageListImpl_Vtbl;
3682 This->ref = 1;
3683
3684 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
3685 IUnknown_Release((IUnknown*)This);
3686
3687 return ret;
3688 }