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