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