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