kernel32/profile: Move GetPrivateProfileSectionA to its own test.
[wine] / dlls / 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  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  *
24  * NOTE
25  *
26  * This code was audited for completeness against the documented features
27  * of Comctl32.dll version 6.0 on Sep. 12, 2002, by Dimitrie O. Paun.
28  *
29  * Unless otherwise noted, we believe this code to be complete, as per
30  * the specification mentioned above.
31  * If you discover missing features, or bugs, please note them below.
32  *
33  *  TODO:
34  *    - Add support for ILD_PRESERVEALPHA, ILD_SCALE, ILD_DPISCALE
35  *    - Add support for ILS_GLOW, ILS_SHADOW, ILS_SATURATE, ILS_ALPHA
36  *    - Thread-safe locking
37  */
38
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #define COBJMACROS
44
45 #include "winerror.h"
46 #include "windef.h"
47 #include "winbase.h"
48 #include "objbase.h"
49 #include "wingdi.h"
50 #include "winuser.h"
51 #include "commctrl.h"
52 #include "comctl32.h"
53 #include "imagelist.h"
54 #include "wine/debug.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL(imagelist);
57
58
59 #define MAX_OVERLAYIMAGE 15
60
61 /* internal image list data used for Drag & Drop operations */
62 typedef struct
63 {
64     HWND        hwnd;
65     HIMAGELIST  himl;
66     /* position of the drag image relative to the window */
67     INT         x;
68     INT         y;
69     /* offset of the hotspot relative to the origin of the image */
70     INT         dxHotspot;
71     INT         dyHotspot;
72     /* is the drag image visible */
73     BOOL        bShow;
74     /* saved background */
75     HBITMAP     hbmBg;
76 } INTERNALDRAG;
77
78 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, FALSE, 0 };
79
80 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count, UINT width);
81
82 static inline BOOL is_valid(HIMAGELIST himl)
83 {
84     return himl && himl->magic == IMAGELIST_MAGIC;
85 }
86
87 /*
88  * An imagelist with N images is tiled like this:
89  *
90  *   N/4 ->
91  *
92  * 4 048C..
93  *   159D..
94  * | 26AE.N
95  * V 37BF.
96  */
97
98 #define TILE_COUNT 4
99
100 static inline UINT imagelist_height( UINT count )
101 {
102     return ((count + TILE_COUNT - 1)/TILE_COUNT);
103 }
104
105 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
106 {
107     pt->x = (index%TILE_COUNT) * himl->cx;
108     pt->y = (index/TILE_COUNT) * himl->cy;
109 }
110
111 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, UINT cx, SIZE *sz )
112 {
113     sz->cx = cx * TILE_COUNT;
114     sz->cy = imagelist_height( count ) * himl->cy;
115 }
116
117 /*
118  * imagelist_copy_images()
119  *
120  * Copies a block of count images from offset src in the list to offset dest.
121  * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
122  */
123 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
124                                           UINT src, UINT count, UINT dest )
125 {
126     POINT ptSrc, ptDest;
127     SIZE sz;
128     UINT i;
129
130     for ( i=0; i<TILE_COUNT; i++ )
131     {
132         imagelist_point_from_index( himl, src+i, &ptSrc );
133         imagelist_point_from_index( himl, dest+i, &ptDest );
134         sz.cx = himl->cx;
135         sz.cy = himl->cy * imagelist_height( count - i );
136
137         BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
138                 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
139     }
140 }
141
142 /*************************************************************************
143  * IMAGELIST_InternalExpandBitmaps [Internal]
144  *
145  * Expands the bitmaps of an image list by the given number of images.
146  *
147  * PARAMS
148  *     himl        [I] handle to image list
149  *     nImageCount [I] number of images to add
150  *
151  * RETURNS
152  *     nothing
153  *
154  * NOTES
155  *     This function CANNOT be used to reduce the number of images.
156  */
157 static void
158 IMAGELIST_InternalExpandBitmaps (HIMAGELIST himl, INT nImageCount, INT cx, INT cy)
159 {
160     HDC     hdcBitmap;
161     HBITMAP hbmNewBitmap, hbmNull;
162     INT     nNewCount;
163     SIZE    sz;
164
165     if ((himl->cCurImage + nImageCount <= himl->cMaxImage)
166         && (himl->cy >= cy))
167         return;
168
169     if (cx == 0) cx = himl->cx;
170     nNewCount = himl->cCurImage + nImageCount + himl->cGrow;
171
172     imagelist_get_bitmap_size(himl, nNewCount, cx, &sz);
173
174     TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, cy, nNewCount);
175     hdcBitmap = CreateCompatibleDC (0);
176
177     hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount, cx);
178
179     if (hbmNewBitmap == 0)
180         ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, cy);
181
182     if (himl->cCurImage)
183     {
184         hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
185         BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
186                 himl->hdcImage, 0, 0, SRCCOPY);
187         SelectObject (hdcBitmap, hbmNull);
188     }
189     SelectObject (himl->hdcImage, hbmNewBitmap);
190     DeleteObject (himl->hbmImage);
191     himl->hbmImage = hbmNewBitmap;
192
193     if (himl->flags & ILC_MASK)
194     {
195         hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
196
197         if (hbmNewBitmap == 0)
198             ERR("creating new mask bitmap!\n");
199
200         if(himl->cCurImage)
201         {
202             hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
203             BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
204                     himl->hdcMask, 0, 0, SRCCOPY);
205             SelectObject (hdcBitmap, hbmNull);
206         }
207         SelectObject (himl->hdcMask, hbmNewBitmap);
208         DeleteObject (himl->hbmMask);
209         himl->hbmMask = hbmNewBitmap;
210     }
211
212     himl->cMaxImage = nNewCount;
213
214     DeleteDC (hdcBitmap);
215 }
216
217
218 /*************************************************************************
219  * ImageList_Add [COMCTL32.@]
220  *
221  * Add an image or images to an image list.
222  *
223  * PARAMS
224  *     himl     [I] handle to image list
225  *     hbmImage [I] handle to image bitmap
226  *     hbmMask  [I] handle to mask bitmap
227  *
228  * RETURNS
229  *     Success: Index of the first new image.
230  *     Failure: -1
231  */
232
233 INT WINAPI
234 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
235 {
236     HDC     hdcBitmap, hdcTemp;
237     INT     nFirstIndex, nImageCount, i;
238     BITMAP  bmp;
239     HBITMAP hOldBitmap, hOldBitmapTemp;
240     POINT   pt;
241
242     TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
243     if (!is_valid(himl))
244         return -1;
245
246     if (!GetObjectW(hbmImage, sizeof(BITMAP), (LPVOID)&bmp))
247         return -1;
248
249     nImageCount = bmp.bmWidth / himl->cx;
250
251     IMAGELIST_InternalExpandBitmaps (himl, nImageCount, bmp.bmWidth, bmp.bmHeight);
252
253     hdcBitmap = CreateCompatibleDC(0);
254
255     hOldBitmap = SelectObject(hdcBitmap, hbmImage);
256
257     for (i=0; i<nImageCount; i++)
258     {
259         imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
260
261         /* Copy result to the imagelist
262         */
263         BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
264                 hdcBitmap, i*himl->cx, 0, SRCCOPY );
265
266         if (!himl->hbmMask)
267              continue;
268
269         hdcTemp   = CreateCompatibleDC(0);
270         hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
271
272         BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
273                 hdcTemp, i*himl->cx, 0, SRCCOPY );
274
275         SelectObject(hdcTemp, hOldBitmapTemp);
276         DeleteDC(hdcTemp);
277
278         /* Remove the background from the image
279         */
280         BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
281                 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
282     }
283
284     SelectObject(hdcBitmap, hOldBitmap);
285     DeleteDC(hdcBitmap);
286
287     nFirstIndex = himl->cCurImage;
288     himl->cCurImage += nImageCount;
289
290     return nFirstIndex;
291 }
292
293
294 /*************************************************************************
295  * ImageList_AddIcon [COMCTL32.@]
296  *
297  * Adds an icon to an image list.
298  *
299  * PARAMS
300  *     himl  [I] handle to image list
301  *     hIcon [I] handle to icon
302  *
303  * RETURNS
304  *     Success: index of the new image
305  *     Failure: -1
306  */
307 #undef ImageList_AddIcon
308 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
309 {
310     return ImageList_ReplaceIcon (himl, -1, hIcon);
311 }
312
313
314 /*************************************************************************
315  * ImageList_AddMasked [COMCTL32.@]
316  *
317  * Adds an image or images to an image list and creates a mask from the
318  * specified bitmap using the mask color.
319  *
320  * PARAMS
321  *     himl    [I] handle to image list.
322  *     hBitmap [I] handle to bitmap
323  *     clrMask [I] mask color.
324  *
325  * RETURNS
326  *     Success: Index of the first new image.
327  *     Failure: -1
328  */
329
330 INT WINAPI
331 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
332 {
333     HDC    hdcMask, hdcBitmap;
334     INT    i, nIndex, nImageCount;
335     BITMAP bmp;
336     HBITMAP hOldBitmap;
337     HBITMAP hMaskBitmap=0;
338     COLORREF bkColor;
339     POINT  pt;
340
341     TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
342     if (!is_valid(himl))
343         return -1;
344
345     if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
346         return -1;
347
348     if (himl->cx > 0)
349         nImageCount = bmp.bmWidth / himl->cx;
350     else
351         nImageCount = 0;
352
353     IMAGELIST_InternalExpandBitmaps (himl, nImageCount, bmp.bmWidth, bmp.bmHeight);
354
355     nIndex = himl->cCurImage;
356     himl->cCurImage += nImageCount;
357
358     hdcBitmap = CreateCompatibleDC(0);
359     hOldBitmap = SelectObject(hdcBitmap, hBitmap);
360
361     /* Create a temp Mask so we can remove the background of the Image */
362     hdcMask = CreateCompatibleDC(0);
363     hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
364     SelectObject(hdcMask, hMaskBitmap);
365
366     /* create monochrome image to the mask bitmap */
367     bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
368     SetBkColor (hdcBitmap, bkColor);
369     BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
370
371     SetBkColor(hdcBitmap, RGB(255,255,255));
372
373     /*
374      * Remove the background from the image
375      *
376      * WINDOWS BUG ALERT!!!!!!
377      *  The statement below should not be done in common practice
378      *  but this is how ImageList_AddMasked works in Windows.
379      *  It overwrites the original bitmap passed, this was discovered
380      *  by using the same bitmap to iterate the different styles
381      *  on windows where it failed (BUT ImageList_Add is OK)
382      *  This is here in case some apps rely on this bug
383      *
384      *  Blt mode 0x220326 is NOTSRCAND
385      */
386     BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
387
388     /* Copy result to the imagelist */
389     for (i=0; i<nImageCount; i++)
390     {
391         imagelist_point_from_index( himl, nIndex + i, &pt );
392         BitBlt(himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
393                 hdcBitmap, i*himl->cx, 0, SRCCOPY);
394         BitBlt(himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
395                 hdcMask, i*himl->cx, 0, SRCCOPY);
396     }
397
398     /* Clean up */
399     SelectObject(hdcBitmap, hOldBitmap);
400     DeleteDC(hdcBitmap);
401     DeleteObject(hMaskBitmap);
402     DeleteDC(hdcMask);
403
404     return nIndex;
405 }
406
407
408 /*************************************************************************
409  * ImageList_BeginDrag [COMCTL32.@]
410  *
411  * Creates a temporary image list that contains one image. It will be used
412  * as a drag image.
413  *
414  * PARAMS
415  *     himlTrack [I] handle to the source image list
416  *     iTrack    [I] index of the drag image in the source image list
417  *     dxHotspot [I] X position of the hot spot of the drag image
418  *     dyHotspot [I] Y position of the hot spot of the drag image
419  *
420  * RETURNS
421  *     Success: TRUE
422  *     Failure: FALSE
423  */
424
425 BOOL WINAPI
426 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
427                      INT dxHotspot, INT dyHotspot)
428 {
429     INT cx, cy;
430
431     TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
432           dxHotspot, dyHotspot);
433
434     if (!is_valid(himlTrack))
435         return FALSE;
436
437     if (InternalDrag.himl)
438         ImageList_EndDrag ();
439
440     cx = himlTrack->cx;
441     cy = himlTrack->cy;
442
443     InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
444     if (InternalDrag.himl == NULL) {
445         WARN("Error creating drag image list!\n");
446         return FALSE;
447     }
448
449     InternalDrag.dxHotspot = dxHotspot;
450     InternalDrag.dyHotspot = dyHotspot;
451
452     /* copy image */
453     BitBlt (InternalDrag.himl->hdcImage, 0, 0, cx, cy, himlTrack->hdcImage, iTrack * cx, 0, SRCCOPY);
454
455     /* copy mask */
456     BitBlt (InternalDrag.himl->hdcMask, 0, 0, cx, cy, himlTrack->hdcMask, iTrack * cx, 0, SRCCOPY);
457
458     InternalDrag.himl->cCurImage = 1;
459
460     return TRUE;
461 }
462
463
464 /*************************************************************************
465  * ImageList_Copy [COMCTL32.@]
466  *
467  *  Copies an image of the source image list to an image of the
468  *  destination image list. Images can be copied or swapped.
469  *
470  * PARAMS
471  *     himlDst [I] handle to the destination image list
472  *     iDst    [I] destination image index.
473  *     himlSrc [I] handle to the source image list
474  *     iSrc    [I] source image index
475  *     uFlags  [I] flags for the copy operation
476  *
477  * RETURNS
478  *     Success: TRUE
479  *     Failure: FALSE
480  *
481  * NOTES
482  *     Copying from one image list to another is possible. The original
483  *     implementation just copies or swaps within one image list.
484  *     Could this feature become a bug??? ;-)
485  */
486
487 BOOL WINAPI
488 ImageList_Copy (HIMAGELIST himlDst, INT iDst,   HIMAGELIST himlSrc,
489                 INT iSrc, UINT uFlags)
490 {
491     POINT ptSrc, ptDst;
492
493     TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
494
495     if (!is_valid(himlSrc) || !is_valid(himlDst))
496         return FALSE;
497     if ((iDst < 0) || (iDst >= himlDst->cCurImage))
498         return FALSE;
499     if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
500         return FALSE;
501
502     imagelist_point_from_index( himlDst, iDst, &ptDst );
503     imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
504
505     if (uFlags & ILCF_SWAP) {
506         /* swap */
507         HDC     hdcBmp;
508         HBITMAP hbmTempImage, hbmTempMask;
509
510         hdcBmp = CreateCompatibleDC (0);
511
512         /* create temporary bitmaps */
513         hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
514                                        himlSrc->uBitsPixel, NULL);
515         hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
516                                       1, NULL);
517
518         /* copy (and stretch) destination to temporary bitmaps.(save) */
519         /* image */
520         SelectObject (hdcBmp, hbmTempImage);
521         StretchBlt   (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
522                       himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
523                       SRCCOPY);
524         /* mask */
525         SelectObject (hdcBmp, hbmTempMask);
526         StretchBlt   (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
527                       himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
528                       SRCCOPY);
529
530         /* copy (and stretch) source to destination */
531         /* image */
532         StretchBlt   (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
533                       himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
534                       SRCCOPY);
535         /* mask */
536         StretchBlt   (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
537                       himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
538                       SRCCOPY);
539
540         /* copy (without stretching) temporary bitmaps to source (restore) */
541         /* mask */
542         BitBlt       (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
543                       hdcBmp, 0, 0, SRCCOPY);
544
545         /* image */
546         BitBlt       (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
547                       hdcBmp, 0, 0, SRCCOPY);
548         /* delete temporary bitmaps */
549         DeleteObject (hbmTempMask);
550         DeleteObject (hbmTempImage);
551         DeleteDC(hdcBmp);
552     }
553     else {
554         /* copy image */
555         StretchBlt   (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
556                       himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
557                       SRCCOPY);
558
559         /* copy mask */
560         StretchBlt   (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
561                       himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
562                       SRCCOPY);
563     }
564
565     return TRUE;
566 }
567
568
569 /*************************************************************************
570  * ImageList_Create [COMCTL32.@]
571  *
572  * Creates a new image list.
573  *
574  * PARAMS
575  *     cx       [I] image height
576  *     cy       [I] image width
577  *     flags    [I] creation flags
578  *     cInitial [I] initial number of images in the image list
579  *     cGrow    [I] number of images by which image list grows
580  *
581  * RETURNS
582  *     Success: Handle to the created image list
583  *     Failure: NULL
584  */
585 HIMAGELIST WINAPI
586 ImageList_Create (INT cx, INT cy, UINT flags,
587                   INT cInitial, INT cGrow)
588 {
589     HIMAGELIST himl;
590     INT      nCount;
591     HBITMAP  hbmTemp;
592     UINT     ilc = (flags & 0xFE);
593     static const WORD aBitBlend25[] =
594         {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
595
596     static const WORD aBitBlend50[] =
597         {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
598
599     TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
600
601     himl = (HIMAGELIST)Alloc (sizeof(struct _IMAGELIST));
602     if (!himl)
603         return NULL;
604
605     cGrow = (cGrow < 4) ? 4 : (cGrow + 3) & ~3;
606
607     himl->magic     = IMAGELIST_MAGIC;
608     himl->cx        = cx;
609     himl->cy        = cy;
610     himl->flags     = flags;
611     himl->cMaxImage = cInitial + cGrow;
612     himl->cInitial  = cInitial;
613     himl->cGrow     = cGrow;
614     himl->clrFg     = CLR_DEFAULT;
615     himl->clrBk     = CLR_NONE;
616
617     /* initialize overlay mask indices */
618     for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
619         himl->nOvlIdx[nCount] = -1;
620
621     /* Create Image & Mask DCs */
622     himl->hdcImage = CreateCompatibleDC (0);
623     if (!himl->hdcImage)
624         goto cleanup;
625     if (himl->flags & ILC_MASK){
626         himl->hdcMask = CreateCompatibleDC(0);
627         if (!himl->hdcMask)
628             goto cleanup;
629     }
630
631     /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
632     if (ilc == ILC_COLOR)
633         ilc = ILC_COLOR4;
634
635     if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
636         himl->uBitsPixel = ilc;
637     else
638         himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
639
640     if (himl->cMaxImage > 0) {
641         himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage, cx);
642         SelectObject(himl->hdcImage, himl->hbmImage);
643     } else
644         himl->hbmImage = 0;
645
646     if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
647         SIZE sz;
648
649         imagelist_get_bitmap_size(himl, himl->cMaxImage, himl->cx, &sz);
650         himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
651         if (himl->hbmMask == 0) {
652             ERR("Error creating mask bitmap!\n");
653             goto cleanup;
654         }
655         SelectObject(himl->hdcMask, himl->hbmMask);
656     }
657     else
658         himl->hbmMask = 0;
659
660     /* create blending brushes */
661     hbmTemp = CreateBitmap (8, 8, 1, 1, &aBitBlend25);
662     himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
663     DeleteObject (hbmTemp);
664
665     hbmTemp = CreateBitmap (8, 8, 1, 1, &aBitBlend50);
666     himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
667     DeleteObject (hbmTemp);
668
669     TRACE("created imagelist %p\n", himl);
670     return himl;
671
672 cleanup:
673     if (himl) ImageList_Destroy(himl);
674     return NULL;
675 }
676
677
678 /*************************************************************************
679  * ImageList_Destroy [COMCTL32.@]
680  *
681  * Destroys an image list.
682  *
683  * PARAMS
684  *     himl [I] handle to image list
685  *
686  * RETURNS
687  *     Success: TRUE
688  *     Failure: FALSE
689  */
690
691 BOOL WINAPI
692 ImageList_Destroy (HIMAGELIST himl)
693 {
694     if (!is_valid(himl))
695         return FALSE;
696
697     /* delete image bitmaps */
698     if (himl->hbmImage)
699         DeleteObject (himl->hbmImage);
700     if (himl->hbmMask)
701         DeleteObject (himl->hbmMask);
702
703     /* delete image & mask DCs */
704     if (himl->hdcImage)
705         DeleteDC(himl->hdcImage);
706     if (himl->hdcMask)
707         DeleteDC(himl->hdcMask);
708
709     /* delete blending brushes */
710     if (himl->hbrBlend25)
711         DeleteObject (himl->hbrBlend25);
712     if (himl->hbrBlend50)
713         DeleteObject (himl->hbrBlend50);
714
715     ZeroMemory(himl, sizeof(*himl));
716     Free (himl);
717
718     return TRUE;
719 }
720
721
722 /*************************************************************************
723  * ImageList_DragEnter [COMCTL32.@]
724  *
725  * Locks window update and displays the drag image at the given position.
726  *
727  * PARAMS
728  *     hwndLock [I] handle of the window that owns the drag image.
729  *     x        [I] X position of the drag image.
730  *     y        [I] Y position of the drag image.
731  *
732  * RETURNS
733  *     Success: TRUE
734  *     Failure: FALSE
735  *
736  * NOTES
737  *     The position of the drag image is relative to the window, not
738  *     the client area.
739  */
740
741 BOOL WINAPI
742 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
743 {
744     TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
745
746     if (!is_valid(InternalDrag.himl))
747         return FALSE;
748
749     if (hwndLock)
750         InternalDrag.hwnd = hwndLock;
751     else
752         InternalDrag.hwnd = GetDesktopWindow ();
753
754     InternalDrag.x = x;
755     InternalDrag.y = y;
756
757     /* draw the drag image and save the background */
758     if (!ImageList_DragShowNolock(TRUE)) {
759         return FALSE;
760     }
761
762     return TRUE;
763 }
764
765
766 /*************************************************************************
767  * ImageList_DragLeave [COMCTL32.@]
768  *
769  * Unlocks window update and hides the drag image.
770  *
771  * PARAMS
772  *     hwndLock [I] handle of the window that owns the drag image.
773  *
774  * RETURNS
775  *     Success: TRUE
776  *     Failure: FALSE
777  */
778
779 BOOL WINAPI
780 ImageList_DragLeave (HWND hwndLock)
781 {
782     /* As we don't save drag info in the window this can lead to problems if
783        an app does not supply the same window as DragEnter */
784     /* if (hwndLock)
785         InternalDrag.hwnd = hwndLock;
786     else
787         InternalDrag.hwnd = GetDesktopWindow (); */
788     if(!hwndLock)
789         hwndLock = GetDesktopWindow();
790     if(InternalDrag.hwnd != hwndLock)
791         FIXME("DragLeave hWnd != DragEnter hWnd\n");
792
793     ImageList_DragShowNolock (FALSE);
794
795     return TRUE;
796 }
797
798
799 /*************************************************************************
800  * ImageList_InternalDragDraw [Internal]
801  *
802  * Draws the drag image.
803  *
804  * PARAMS
805  *     hdc [I] device context to draw into.
806  *     x   [I] X position of the drag image.
807  *     y   [I] Y position of the drag image.
808  *
809  * RETURNS
810  *     Success: TRUE
811  *     Failure: FALSE
812  *
813  * NOTES
814  *     The position of the drag image is relative to the window, not
815  *     the client area.
816  *
817  */
818
819 static inline void
820 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
821 {
822     IMAGELISTDRAWPARAMS imldp;
823
824     ZeroMemory (&imldp, sizeof(imldp));
825     imldp.cbSize  = sizeof(imldp);
826     imldp.himl    = InternalDrag.himl;
827     imldp.i       = 0;
828     imldp.hdcDst  = hdc,
829     imldp.x       = x;
830     imldp.y       = y;
831     imldp.rgbBk   = CLR_DEFAULT;
832     imldp.rgbFg   = CLR_DEFAULT;
833     imldp.fStyle  = ILD_NORMAL;
834     imldp.fState  = ILS_ALPHA;
835     imldp.Frame   = 128;
836
837     /* FIXME: instead of using the alpha blending, we should
838      * create a 50% mask, and draw it semitransparantly that way */
839     ImageList_DrawIndirect (&imldp);
840 }
841
842 /*************************************************************************
843  * ImageList_DragMove [COMCTL32.@]
844  *
845  * Moves the drag image.
846  *
847  * PARAMS
848  *     x [I] X position of the drag image.
849  *     y [I] Y position of the drag image.
850  *
851  * RETURNS
852  *     Success: TRUE
853  *     Failure: FALSE
854  *
855  * NOTES
856  *     The position of the drag image is relative to the window, not
857  *     the client area.
858  *
859  * BUGS
860  *     The drag image should be drawn semitransparent.
861  */
862
863 BOOL WINAPI
864 ImageList_DragMove (INT x, INT y)
865 {
866     TRACE("(x=%d y=%d)\n", x, y);
867
868     if (!is_valid(InternalDrag.himl))
869         return FALSE;
870
871     /* draw/update the drag image */
872     if (InternalDrag.bShow) {
873         HDC hdcDrag;
874         HDC hdcOffScreen;
875         HDC hdcBg;
876         HBITMAP hbmOffScreen;
877         INT origNewX, origNewY;
878         INT origOldX, origOldY;
879         INT origRegX, origRegY;
880         INT sizeRegX, sizeRegY;
881
882
883         /* calculate the update region */
884         origNewX = x - InternalDrag.dxHotspot;
885         origNewY = y - InternalDrag.dyHotspot;
886         origOldX = InternalDrag.x - InternalDrag.dxHotspot;
887         origOldY = InternalDrag.y - InternalDrag.dyHotspot;
888         origRegX = min(origNewX, origOldX);
889         origRegY = min(origNewY, origOldY);
890         sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
891         sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
892
893         hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
894                           DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
895         hdcOffScreen = CreateCompatibleDC(hdcDrag);
896         hdcBg = CreateCompatibleDC(hdcDrag);
897
898         hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
899         SelectObject(hdcOffScreen, hbmOffScreen);
900         SelectObject(hdcBg, InternalDrag.hbmBg);
901
902         /* get the actual background of the update region */
903         BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
904                origRegX, origRegY, SRCCOPY);
905         /* erase the old image */
906         BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
907                InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
908                SRCCOPY);
909         /* save the background */
910         BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
911                hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
912         /* draw the image */
913         ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX, 
914                                    origNewY - origRegY);
915         /* draw the update region to the screen */
916         BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
917                hdcOffScreen, 0, 0, SRCCOPY);
918
919         DeleteDC(hdcBg);
920         DeleteDC(hdcOffScreen);
921         DeleteObject(hbmOffScreen);
922         ReleaseDC(InternalDrag.hwnd, hdcDrag);
923     }
924
925     /* update the image position */
926     InternalDrag.x = x;
927     InternalDrag.y = y;
928
929     return TRUE;
930 }
931
932
933 /*************************************************************************
934  * ImageList_DragShowNolock [COMCTL32.@]
935  *
936  * Shows or hides the drag image.
937  *
938  * PARAMS
939  *     bShow [I] TRUE shows the drag image, FALSE hides it.
940  *
941  * RETURNS
942  *     Success: TRUE
943  *     Failure: FALSE
944  *
945  * BUGS
946  *     The drag image should be drawn semitransparent.
947  */
948
949 BOOL WINAPI
950 ImageList_DragShowNolock (BOOL bShow)
951 {
952     HDC hdcDrag;
953     HDC hdcBg;
954     INT x, y;
955
956     if (!is_valid(InternalDrag.himl))
957         return FALSE;
958     
959     TRACE("bShow=0x%X!\n", bShow);
960
961     /* DragImage is already visible/hidden */
962     if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
963         return FALSE;
964     }
965
966     /* position of the origin of the DragImage */
967     x = InternalDrag.x - InternalDrag.dxHotspot;
968     y = InternalDrag.y - InternalDrag.dyHotspot;
969
970     hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
971                          DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
972     if (!hdcDrag) {
973         return FALSE;
974     }
975
976     hdcBg = CreateCompatibleDC(hdcDrag);
977     if (!InternalDrag.hbmBg) {
978         InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
979                     InternalDrag.himl->cx, InternalDrag.himl->cy);
980     }
981     SelectObject(hdcBg, InternalDrag.hbmBg);
982
983     if (bShow) {
984         /* save the background */
985         BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
986                hdcDrag, x, y, SRCCOPY);
987         /* show the image */
988         ImageList_InternalDragDraw(hdcDrag, x, y);
989     } else {
990         /* hide the image */
991         BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
992                hdcBg, 0, 0, SRCCOPY);
993     }
994
995     InternalDrag.bShow = !InternalDrag.bShow;
996
997     DeleteDC(hdcBg);
998     ReleaseDC (InternalDrag.hwnd, hdcDrag);
999     return TRUE;
1000 }
1001
1002
1003 /*************************************************************************
1004  * ImageList_Draw [COMCTL32.@]
1005  *
1006  * Draws an image.
1007  *
1008  * PARAMS
1009  *     himl   [I] handle to image list
1010  *     i      [I] image index
1011  *     hdc    [I] handle to device context
1012  *     x      [I] x position
1013  *     y      [I] y position
1014  *     fStyle [I] drawing flags
1015  *
1016  * RETURNS
1017  *     Success: TRUE
1018  *     Failure: FALSE
1019  *
1020  * SEE
1021  *     ImageList_DrawEx.
1022  */
1023
1024 BOOL WINAPI
1025 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1026 {
1027     return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0, 
1028                              CLR_DEFAULT, CLR_DEFAULT, fStyle);
1029 }
1030
1031
1032 /*************************************************************************
1033  * ImageList_DrawEx [COMCTL32.@]
1034  *
1035  * Draws an image and allows to use extended drawing features.
1036  *
1037  * PARAMS
1038  *     himl   [I] handle to image list
1039  *     i      [I] image index
1040  *     hdc    [I] handle to device context
1041  *     x      [I] X position
1042  *     y      [I] Y position
1043  *     dx     [I] X offset
1044  *     dy     [I] Y offset
1045  *     rgbBk  [I] background color
1046  *     rgbFg  [I] foreground color
1047  *     fStyle [I] drawing flags
1048  *
1049  * RETURNS
1050  *     Success: TRUE
1051  *     Failure: FALSE
1052  *
1053  * NOTES
1054  *     Calls ImageList_DrawIndirect.
1055  *
1056  * SEE
1057  *     ImageList_DrawIndirect.
1058  */
1059
1060 BOOL WINAPI
1061 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1062                   INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1063                   UINT fStyle)
1064 {
1065     IMAGELISTDRAWPARAMS imldp;
1066
1067     ZeroMemory (&imldp, sizeof(imldp));
1068     imldp.cbSize  = sizeof(imldp);
1069     imldp.himl    = himl;
1070     imldp.i       = i;
1071     imldp.hdcDst  = hdc,
1072     imldp.x       = x;
1073     imldp.y       = y;
1074     imldp.cx      = dx;
1075     imldp.cy      = dy;
1076     imldp.rgbBk   = rgbBk;
1077     imldp.rgbFg   = rgbFg;
1078     imldp.fStyle  = fStyle;
1079
1080     return ImageList_DrawIndirect (&imldp);
1081 }
1082
1083
1084 /*************************************************************************
1085  * ImageList_DrawIndirect [COMCTL32.@]
1086  *
1087  * Draws an image using various parameters specified in pimldp.
1088  *
1089  * PARAMS
1090  *     pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1091  *
1092  * RETURNS
1093  *     Success: TRUE
1094  *     Failure: FALSE
1095  */
1096
1097 BOOL WINAPI
1098 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1099 {
1100     INT cx, cy, nOvlIdx;
1101     DWORD fState, dwRop;
1102     UINT fStyle;
1103     COLORREF oldImageBk, oldImageFg;
1104     HDC hImageDC, hImageListDC, hMaskListDC;
1105     HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1106     BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1107     HIMAGELIST himl;
1108     POINT pt;
1109
1110     if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1111     if (!is_valid(himl)) return FALSE;
1112     if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1113
1114     imagelist_point_from_index( himl, pimldp->i, &pt );
1115     pt.x += pimldp->xBitmap;
1116     pt.y += pimldp->yBitmap;
1117
1118     fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1119     fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1120     cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1121     cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1122
1123     bIsTransparent = (fStyle & ILD_TRANSPARENT);
1124     if( pimldp->rgbBk == CLR_NONE )
1125         bIsTransparent = TRUE;
1126     if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1127         bIsTransparent = TRUE;
1128     bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1129     bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1130
1131     TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1132           himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1133
1134     /* we will use these DCs to access the images and masks in the ImageList */
1135     hImageListDC = himl->hdcImage;
1136     hMaskListDC  = himl->hdcMask;
1137
1138     /* these will accumulate the image and mask for the image we're drawing */
1139     hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1140     hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1141     hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1142
1143     /* Create a compatible DC. */
1144     if (!hImageListDC || !hImageDC || !hImageBmp ||
1145         (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1146         goto cleanup;
1147     
1148     hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1149   
1150     /*
1151      * To obtain a transparent look, background color should be set
1152      * to white and foreground color to black when blting the
1153      * monochrome mask.
1154      */
1155     oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1156     oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1157
1158     /*
1159      * Draw the initial image
1160      */
1161     if( bMask ) {
1162         if (himl->hbmMask) {
1163             HBRUSH hOldBrush;
1164             hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1165             PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1166             BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1167             DeleteObject (SelectObject (hImageDC, hOldBrush));
1168             if( bIsTransparent )
1169             {
1170                 BitBlt ( pimldp->hdcDst, pimldp->x,  pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1171                 bResult = TRUE;
1172                 goto end;
1173             }
1174         } else {
1175             HBRUSH hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1176             PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1177             SelectObject(hImageDC, hOldBrush);
1178         }
1179     } else {
1180         /* blend the image with the needed solid background */
1181         COLORREF colour = RGB(0,0,0);
1182         HBRUSH hOldBrush;
1183
1184         if( !bIsTransparent )
1185         {
1186             colour = pimldp->rgbBk;
1187             if( colour == CLR_DEFAULT )
1188                 colour = himl->clrBk;
1189             if( colour == CLR_NONE )
1190                 colour = GetBkColor(pimldp->hdcDst);
1191         }
1192
1193         hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1194         PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1195         BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1196         BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1197         DeleteObject (SelectObject (hImageDC, hOldBrush));
1198     }
1199
1200     /* Time for blending, if required */
1201     if (bBlend) {
1202         HBRUSH hBlendBrush, hOldBrush;
1203         COLORREF clrBlend = pimldp->rgbFg;
1204         HDC hBlendMaskDC = hImageListDC;
1205         HBITMAP hOldBitmap;
1206
1207         /* Create the blend Mask */
1208         hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1209         hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1210         hOldBrush = (HBRUSH) SelectObject(hBlendMaskDC, hBlendBrush);
1211         PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1212         SelectObject(hBlendMaskDC, hOldBrush);
1213
1214         /* Modify the blend mask if an Image Mask exist */
1215         if(himl->hbmMask) {
1216             BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1217             BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1218         }
1219
1220         /* now apply blend to the current image given the BlendMask */
1221         if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1222         else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1223         hOldBrush = (HBRUSH) SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1224         BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1225         DeleteObject(SelectObject(hImageDC, hOldBrush));
1226         SelectObject(hBlendMaskDC, hOldBitmap);
1227     }
1228
1229     /* Now do the overlay image, if any */
1230     nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1231     if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1232         nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1233         if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1234             POINT ptOvl;
1235             imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1236             ptOvl.x += pimldp->xBitmap;
1237             if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1238                 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1239             BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1240         }
1241     }
1242
1243     if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1244     if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1245     if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1246     if (fState & ILS_ALPHA) FIXME("ILS_ALPHA: unimplemented!\n");
1247
1248     if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1249     if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1250     if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1251
1252     /* now copy the image to the screen */
1253     dwRop = SRCCOPY;
1254     if (himl->hbmMask && bIsTransparent ) {
1255         COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1256         COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1257         BitBlt (pimldp->hdcDst, pimldp->x,  pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1258         SetBkColor(pimldp->hdcDst, oldDstBk);
1259         SetTextColor(pimldp->hdcDst, oldDstFg);
1260         dwRop = SRCPAINT;
1261     }
1262     if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1263     BitBlt (pimldp->hdcDst, pimldp->x,  pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1264
1265     bResult = TRUE;
1266 end:
1267     /* cleanup the mess */
1268     SetBkColor(hImageDC, oldImageBk);
1269     SetTextColor(hImageDC, oldImageFg);
1270     SelectObject(hImageDC, hOldImageBmp);
1271 cleanup:
1272     DeleteObject(hBlendMaskBmp);
1273     DeleteObject(hImageBmp);
1274     DeleteDC(hImageDC);
1275
1276     return bResult;
1277 }
1278
1279
1280 /*************************************************************************
1281  * ImageList_Duplicate [COMCTL32.@]
1282  *
1283  * Duplicates an image list.
1284  *
1285  * PARAMS
1286  *     himlSrc [I] source image list handle
1287  *
1288  * RETURNS
1289  *     Success: Handle of duplicated image list.
1290  *     Failure: NULL
1291  */
1292
1293 HIMAGELIST WINAPI
1294 ImageList_Duplicate (HIMAGELIST himlSrc)
1295 {
1296     HIMAGELIST himlDst;
1297
1298     if (!is_valid(himlSrc)) {
1299         ERR("Invalid image list handle!\n");
1300         return NULL;
1301     }
1302
1303     himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1304                                 himlSrc->cInitial, himlSrc->cGrow);
1305
1306     if (himlDst)
1307     {
1308         SIZE sz;
1309
1310         imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, himlSrc->cx, &sz);
1311         BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1312                 himlSrc->hdcImage, 0, 0, SRCCOPY);
1313
1314         if (himlDst->hbmMask)
1315             BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1316                     himlSrc->hdcMask, 0, 0, SRCCOPY);
1317
1318         himlDst->cCurImage = himlSrc->cCurImage;
1319         himlDst->cMaxImage = himlSrc->cMaxImage;
1320     }
1321     return himlDst;
1322 }
1323
1324
1325 /*************************************************************************
1326  * ImageList_EndDrag [COMCTL32.@]
1327  *
1328  * Finishes a drag operation.
1329  *
1330  * PARAMS
1331  *     no Parameters
1332  *
1333  * RETURNS
1334  *     Success: TRUE
1335  *     Failure: FALSE
1336  */
1337
1338 VOID WINAPI
1339 ImageList_EndDrag (void)
1340 {
1341     /* cleanup the InternalDrag struct */
1342     InternalDrag.hwnd = 0;
1343     ImageList_Destroy (InternalDrag.himl);
1344     InternalDrag.himl = 0;
1345     InternalDrag.x= 0;
1346     InternalDrag.y= 0;
1347     InternalDrag.dxHotspot = 0;
1348     InternalDrag.dyHotspot = 0;
1349     InternalDrag.bShow = FALSE;
1350     DeleteObject(InternalDrag.hbmBg);
1351     InternalDrag.hbmBg = 0;
1352 }
1353
1354
1355 /*************************************************************************
1356  * ImageList_GetBkColor [COMCTL32.@]
1357  *
1358  * Returns the background color of an image list.
1359  *
1360  * PARAMS
1361  *     himl [I] Image list handle.
1362  *
1363  * RETURNS
1364  *     Success: background color
1365  *     Failure: CLR_NONE
1366  */
1367
1368 COLORREF WINAPI
1369 ImageList_GetBkColor (HIMAGELIST himl)
1370 {
1371     return himl ? himl->clrBk : CLR_NONE;
1372 }
1373
1374
1375 /*************************************************************************
1376  * ImageList_GetDragImage [COMCTL32.@]
1377  *
1378  * Returns the handle to the internal drag image list.
1379  *
1380  * PARAMS
1381  *     ppt        [O] Pointer to the drag position. Can be NULL.
1382  *     pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1383  *
1384  * RETURNS
1385  *     Success: Handle of the drag image list.
1386  *     Failure: NULL.
1387  */
1388
1389 HIMAGELIST WINAPI
1390 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1391 {
1392     if (is_valid(InternalDrag.himl)) {
1393         if (ppt) {
1394             ppt->x = InternalDrag.x;
1395             ppt->y = InternalDrag.y;
1396         }
1397         if (pptHotspot) {
1398             pptHotspot->x = InternalDrag.dxHotspot;
1399             pptHotspot->y = InternalDrag.dyHotspot;
1400         }
1401         return (InternalDrag.himl);
1402     }
1403
1404     return NULL;
1405 }
1406
1407
1408 /*************************************************************************
1409  * ImageList_GetFlags [COMCTL32.@]
1410  *
1411  * Gets the flags of the specified image list.
1412  *
1413  * PARAMS
1414  *     himl [I] Handle to image list
1415  *
1416  * RETURNS
1417  *     Image list flags.
1418  *
1419  * BUGS
1420  *    Stub.
1421  */
1422
1423 DWORD WINAPI
1424 ImageList_GetFlags(HIMAGELIST himl)
1425 {
1426     TRACE("%p\n", himl);
1427
1428     return is_valid(himl) ? himl->flags : 0;
1429 }
1430
1431
1432 /*************************************************************************
1433  * ImageList_GetIcon [COMCTL32.@]
1434  *
1435  * Creates an icon from a masked image of an image list.
1436  *
1437  * PARAMS
1438  *     himl  [I] handle to image list
1439  *     i     [I] image index
1440  *     flags [I] drawing style flags
1441  *
1442  * RETURNS
1443  *     Success: icon handle
1444  *     Failure: NULL
1445  */
1446
1447 HICON WINAPI
1448 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1449 {
1450     ICONINFO ii;
1451     HICON hIcon;
1452     HBITMAP hOldDstBitmap;
1453     HDC hdcDst;
1454     POINT pt;
1455
1456     TRACE("%p %d %d\n", himl, i, fStyle);
1457     if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1458
1459     ii.fIcon = TRUE;
1460     ii.xHotspot = 0;
1461     ii.yHotspot = 0;
1462
1463     /* create colour bitmap */
1464     hdcDst = GetDC(0);
1465     ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1466     ReleaseDC(0, hdcDst);
1467
1468     hdcDst = CreateCompatibleDC(0);
1469
1470     imagelist_point_from_index( himl, i, &pt );
1471
1472     /* draw mask*/
1473     ii.hbmMask  = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1474     hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1475     if (himl->hbmMask) {
1476         BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1477                 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1478     }
1479     else
1480         PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1481
1482     /* draw image*/
1483     SelectObject (hdcDst, ii.hbmColor);
1484     BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1485             himl->hdcImage, pt.x, pt.y, SRCCOPY);
1486
1487     /*
1488      * CreateIconIndirect requires us to deselect the bitmaps from
1489      * the DCs before calling
1490      */
1491     SelectObject(hdcDst, hOldDstBitmap);
1492
1493     hIcon = CreateIconIndirect (&ii);
1494
1495     DeleteObject (ii.hbmMask);
1496     DeleteObject (ii.hbmColor);
1497     DeleteDC (hdcDst);
1498
1499     return hIcon;
1500 }
1501
1502
1503 /*************************************************************************
1504  * ImageList_GetIconSize [COMCTL32.@]
1505  *
1506  * Retrieves the size of an image in an image list.
1507  *
1508  * PARAMS
1509  *     himl [I] handle to image list
1510  *     cx   [O] pointer to the image width.
1511  *     cy   [O] pointer to the image height.
1512  *
1513  * RETURNS
1514  *     Success: TRUE
1515  *     Failure: FALSE
1516  *
1517  * NOTES
1518  *     All images in an image list have the same size.
1519  */
1520
1521 BOOL WINAPI
1522 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1523 {
1524     if (!is_valid(himl))
1525         return FALSE;
1526     if ((himl->cx <= 0) || (himl->cy <= 0))
1527         return FALSE;
1528
1529     if (cx)
1530         *cx = himl->cx;
1531     if (cy)
1532         *cy = himl->cy;
1533
1534     return TRUE;
1535 }
1536
1537
1538 /*************************************************************************
1539  * ImageList_GetImageCount [COMCTL32.@]
1540  *
1541  * Returns the number of images in an image list.
1542  *
1543  * PARAMS
1544  *     himl [I] handle to image list
1545  *
1546  * RETURNS
1547  *     Success: Number of images.
1548  *     Failure: 0
1549  */
1550
1551 INT WINAPI
1552 ImageList_GetImageCount (HIMAGELIST himl)
1553 {
1554     if (!is_valid(himl))
1555         return 0;
1556
1557     return himl->cCurImage;
1558 }
1559
1560
1561 /*************************************************************************
1562  * ImageList_GetImageInfo [COMCTL32.@]
1563  *
1564  * Returns information about an image in an image list.
1565  *
1566  * PARAMS
1567  *     himl       [I] handle to image list
1568  *     i          [I] image index
1569  *     pImageInfo [O] pointer to the image information
1570  *
1571  * RETURNS
1572  *     Success: TRUE
1573  *     Failure: FALSE
1574  */
1575
1576 BOOL WINAPI
1577 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1578 {
1579     POINT pt;
1580
1581     if (!is_valid(himl) || (pImageInfo == NULL))
1582         return FALSE;
1583     if ((i < 0) || (i >= himl->cCurImage))
1584         return FALSE;
1585
1586     pImageInfo->hbmImage = himl->hbmImage;
1587     pImageInfo->hbmMask  = himl->hbmMask;
1588
1589     imagelist_point_from_index( himl, i, &pt );
1590     pImageInfo->rcImage.top    = pt.y;
1591     pImageInfo->rcImage.bottom = pt.y + himl->cy;
1592     pImageInfo->rcImage.left   = pt.x;
1593     pImageInfo->rcImage.right  = pt.x + himl->cx;
1594
1595     return TRUE;
1596 }
1597
1598
1599 /*************************************************************************
1600  * ImageList_GetImageRect [COMCTL32.@]
1601  *
1602  * Retrieves the rectangle of the specified image in an image list.
1603  *
1604  * PARAMS
1605  *     himl   [I] handle to image list
1606  *     i      [I] image index
1607  *     lpRect [O] pointer to the image rectangle
1608  *
1609  * RETURNS
1610  *    Success: TRUE
1611  *    Failure: FALSE
1612  *
1613  * NOTES
1614  *    This is an UNDOCUMENTED function!!!
1615  */
1616
1617 BOOL WINAPI
1618 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1619 {
1620     POINT pt;
1621
1622     if (!is_valid(himl) || (lpRect == NULL))
1623         return FALSE;
1624     if ((i < 0) || (i >= himl->cCurImage))
1625         return FALSE;
1626
1627     imagelist_point_from_index( himl, i, &pt );
1628     lpRect->left   = pt.x;
1629     lpRect->top    = pt.y;
1630     lpRect->right  = pt.x + himl->cx;
1631     lpRect->bottom = pt.y + himl->cy;
1632
1633     return TRUE;
1634 }
1635
1636
1637 /*************************************************************************
1638  * ImageList_LoadImage  [COMCTL32.@]
1639  * ImageList_LoadImageA [COMCTL32.@]
1640  *
1641  * Creates an image list from a bitmap, icon or cursor.
1642  *
1643  * See ImageList_LoadImageW.
1644  */
1645
1646 HIMAGELIST WINAPI
1647 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1648                         COLORREF clrMask, UINT uType, UINT uFlags)
1649 {
1650     HIMAGELIST himl;
1651     LPWSTR lpbmpW;
1652     DWORD len;
1653
1654     if (!HIWORD(lpbmp))
1655         return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1656                                     uType, uFlags);
1657
1658     len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1659     lpbmpW = Alloc(len * sizeof(WCHAR));
1660     MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1661
1662     himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1663     Free (lpbmpW);
1664     return himl;
1665 }
1666
1667
1668 /*************************************************************************
1669  * ImageList_LoadImageW [COMCTL32.@]
1670  *
1671  * Creates an image list from a bitmap, icon or cursor.
1672  *
1673  * PARAMS
1674  *     hi      [I] instance handle
1675  *     lpbmp   [I] name or id of the image
1676  *     cx      [I] width of each image
1677  *     cGrow   [I] number of images to expand
1678  *     clrMask [I] mask color
1679  *     uType   [I] type of image to load
1680  *     uFlags  [I] loading flags
1681  *
1682  * RETURNS
1683  *     Success: handle to the loaded image list
1684  *     Failure: NULL
1685  *
1686  * SEE
1687  *     LoadImage ()
1688  */
1689
1690 HIMAGELIST WINAPI
1691 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1692                       COLORREF clrMask, UINT uType, UINT uFlags)
1693 {
1694     HIMAGELIST himl = NULL;
1695     HANDLE   handle;
1696     INT      nImageCount;
1697
1698     handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
1699     if (!handle) {
1700         WARN("Couldn't load image\n");
1701         return NULL;
1702     }
1703
1704     if (uType == IMAGE_BITMAP) {
1705         BITMAP bmp;
1706         GetObjectW (handle, sizeof(BITMAP), &bmp);
1707
1708         /* To match windows behavior, if cx is set to zero and
1709          the flag DI_DEFAULTSIZE is specified, cx becomes the
1710          system metric value for icons. If the flag is not specified
1711          the function sets the size to the height of the bitmap */
1712         if (cx == 0)
1713         {
1714             if (uFlags & DI_DEFAULTSIZE)
1715                 cx = GetSystemMetrics (SM_CXICON);
1716             else
1717                 cx = bmp.bmHeight;
1718         }
1719
1720         nImageCount = bmp.bmWidth / cx;
1721
1722         himl = ImageList_Create (cx, bmp.bmHeight, ILC_MASK | ILC_COLOR,
1723                                  nImageCount, cGrow);
1724         if (!himl) {
1725             DeleteObject (handle);
1726             return NULL;
1727         }
1728         ImageList_AddMasked (himl, (HBITMAP)handle, clrMask);
1729     }
1730     else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
1731         ICONINFO ii;
1732         BITMAP bmp;
1733
1734         GetIconInfo (handle, &ii);
1735         GetObjectW (ii.hbmColor, sizeof(BITMAP), (LPVOID)&bmp);
1736         himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
1737                                  ILC_MASK | ILC_COLOR, 1, cGrow);
1738         if (!himl) {
1739             DeleteObject (ii.hbmColor);
1740             DeleteObject (ii.hbmMask);
1741             DeleteObject (handle);
1742             return NULL;
1743         }
1744         ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
1745         DeleteObject (ii.hbmColor);
1746         DeleteObject (ii.hbmMask);
1747     }
1748
1749     DeleteObject (handle);
1750
1751     return himl;
1752 }
1753
1754
1755 /*************************************************************************
1756  * ImageList_Merge [COMCTL32.@]
1757  *
1758  * Create an image list containing a merged image from two image lists.
1759  *
1760  * PARAMS
1761  *     himl1 [I] handle to first image list
1762  *     i1    [I] first image index
1763  *     himl2 [I] handle to second image list
1764  *     i2    [I] second image index
1765  *     dx    [I] X offset of the second image relative to the first.
1766  *     dy    [I] Y offset of the second image relative to the first.
1767  *
1768  * RETURNS
1769  *     Success: The newly created image list. It contains a single image
1770  *              consisting of the second image merged with the first.
1771  *     Failure: NULL, if either himl1 or himl2 are invalid.
1772  *
1773  * NOTES
1774  *   - The returned image list should be deleted by the caller using
1775  *     ImageList_Destroy() when it is no longer required.
1776  *   - If either i1 or i2 are not valid image indices they will be treated
1777  *     as a blank image.
1778  */
1779 HIMAGELIST WINAPI
1780 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
1781                  INT dx, INT dy)
1782 {
1783     HIMAGELIST himlDst = NULL;
1784     INT      cxDst, cyDst;
1785     INT      xOff1, yOff1, xOff2, yOff2;
1786     POINT    pt1, pt2;
1787
1788     TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
1789            i2, dx, dy);
1790
1791     if (!is_valid(himl1) || !is_valid(himl2))
1792         return NULL;
1793
1794     if (dx > 0) {
1795         cxDst = max (himl1->cx, dx + himl2->cx);
1796         xOff1 = 0;
1797         xOff2 = dx;
1798     }
1799     else if (dx < 0) {
1800         cxDst = max (himl2->cx, himl1->cx - dx);
1801         xOff1 = -dx;
1802         xOff2 = 0;
1803     }
1804     else {
1805         cxDst = max (himl1->cx, himl2->cx);
1806         xOff1 = 0;
1807         xOff2 = 0;
1808     }
1809
1810     if (dy > 0) {
1811         cyDst = max (himl1->cy, dy + himl2->cy);
1812         yOff1 = 0;
1813         yOff2 = dy;
1814     }
1815     else if (dy < 0) {
1816         cyDst = max (himl2->cy, himl1->cy - dy);
1817         yOff1 = -dy;
1818         yOff2 = 0;
1819     }
1820     else {
1821         cyDst = max (himl1->cy, himl2->cy);
1822         yOff1 = 0;
1823         yOff2 = 0;
1824     }
1825
1826     himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | ILC_COLOR, 1, 1);
1827
1828     if (himlDst)
1829     {
1830         imagelist_point_from_index( himl1, i1, &pt1 );
1831         imagelist_point_from_index( himl1, i2, &pt2 );
1832
1833         /* copy image */
1834         BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
1835         if (i1 >= 0 && i1 < himl1->cCurImage)
1836             BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
1837         if (i2 >= 0 && i2 < himl2->cCurImage)
1838         {
1839             BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
1840             BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
1841         }
1842
1843         /* copy mask */
1844         BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
1845         if (i1 >= 0 && i1 < himl1->cCurImage)
1846             BitBlt (himlDst->hdcMask,  xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask,  pt1.x, pt1.y, SRCCOPY);
1847         if (i2 >= 0 && i2 < himl2->cCurImage)
1848             BitBlt (himlDst->hdcMask,  xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask,  pt2.x, pt2.y, SRCAND);
1849
1850         himlDst->cCurImage = 1;
1851     }
1852
1853     return himlDst;
1854 }
1855
1856
1857 /***********************************************************************
1858  *           DIB_GetDIBWidthBytes
1859  *
1860  * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
1861  */
1862 static int DIB_GetDIBWidthBytes( int width, int depth )
1863 {
1864     int words;
1865
1866     switch(depth)
1867     {
1868     case 1:  words = (width + 31) / 32; break;
1869     case 4:  words = (width + 7) / 8; break;
1870     case 8:  words = (width + 3) / 4; break;
1871     case 15:
1872     case 16: words = (width + 1) / 2; break;
1873     case 24: words = (width * 3 + 3)/4; break;
1874
1875     default:
1876         WARN("(%d): Unsupported depth\n", depth );
1877         /* fall through */
1878     case 32:
1879         words = width;
1880         break;
1881     }
1882     return 4 * words;
1883 }
1884
1885 /***********************************************************************
1886  *           DIB_GetDIBImageBytes
1887  *
1888  * Return the number of bytes used to hold the image in a DIB bitmap.
1889  */
1890 static int DIB_GetDIBImageBytes( int width, int height, int depth )
1891 {
1892     return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
1893 }
1894
1895
1896 /* helper for ImageList_Read, see comments below */
1897 static BOOL _read_bitmap(HIMAGELIST himl, HDC hdcIml, LPSTREAM pstm)
1898 {
1899     BITMAPFILEHEADER    bmfh;
1900     int bitsperpixel, palspace;
1901     char bmi_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
1902     LPBITMAPINFO bmi = (LPBITMAPINFO)bmi_buf;
1903     int                result = FALSE;
1904     LPBYTE             bits = NULL;
1905
1906     if (!SUCCEEDED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
1907         return FALSE;
1908
1909     if (bmfh.bfType != (('M'<<8)|'B'))
1910         return FALSE;
1911
1912     if (!SUCCEEDED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
1913         return FALSE;
1914
1915     if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
1916         return FALSE;
1917
1918     TRACE("width %u, height %u, planes %u, bpp %u\n",
1919           bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
1920           bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
1921
1922     bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
1923     if (bitsperpixel<=8)
1924         palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
1925     else
1926         palspace = 0;
1927
1928     bmi->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bitsperpixel);
1929
1930     /* read the palette right after the end of the bitmapinfoheader */
1931     if (palspace && !SUCCEEDED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
1932         goto error;
1933
1934     bits = Alloc(bmi->bmiHeader.biSizeImage);
1935     if (!bits)
1936         goto error;
1937     if (!SUCCEEDED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
1938         goto error;
1939
1940     if (!StretchDIBits(hdcIml, 0, 0, bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
1941                   0, 0, bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
1942                   bits, bmi, DIB_RGB_COLORS, SRCCOPY))
1943         goto error;
1944     result = TRUE;
1945
1946 error:
1947     Free(bits);
1948     return result;
1949 }
1950
1951 /*************************************************************************
1952  * ImageList_Read [COMCTL32.@]
1953  *
1954  * Reads an image list from a stream.
1955  *
1956  * PARAMS
1957  *     pstm [I] pointer to a stream
1958  *
1959  * RETURNS
1960  *     Success: handle to image list
1961  *     Failure: NULL
1962  *
1963  * The format is like this:
1964  *      ILHEAD                  ilheadstruct;
1965  *
1966  * for the color image part:
1967  *      BITMAPFILEHEADER        bmfh;
1968  *      BITMAPINFOHEADER        bmih;
1969  * only if it has a palette:
1970  *      RGBQUAD         rgbs[nr_of_paletted_colors];
1971  *
1972  *      BYTE                    colorbits[imagesize];
1973  *
1974  * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
1975  *      BITMAPFILEHEADER        bmfh_mask;
1976  *      BITMAPINFOHEADER        bmih_mask;
1977  * only if it has a palette (it usually does not):
1978  *      RGBQUAD         rgbs[nr_of_paletted_colors];
1979  *
1980  *      BYTE                    maskbits[imagesize];
1981  */
1982 HIMAGELIST WINAPI ImageList_Read (LPSTREAM pstm)
1983 {
1984     ILHEAD      ilHead;
1985     HIMAGELIST  himl;
1986     int         i;
1987
1988     TRACE("%p\n", pstm);
1989
1990     if (!SUCCEEDED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
1991         return NULL;
1992     if (ilHead.usMagic != (('L' << 8) | 'I'))
1993         return NULL;
1994     if (ilHead.usVersion != 0x101) /* probably version? */
1995         return NULL;
1996
1997     TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
1998           ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
1999
2000     himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2001     if (!himl)
2002         return NULL;
2003
2004     if (!_read_bitmap(himl, himl->hdcImage, pstm))
2005     {
2006         WARN("failed to read bitmap from stream\n");
2007         return NULL;
2008     }
2009     if (ilHead.flags & ILC_MASK)
2010     {
2011         if (!_read_bitmap(himl, himl->hdcMask, pstm))
2012         {
2013             WARN("failed to read mask bitmap from stream\n");
2014             return NULL;
2015         }
2016     }
2017
2018     himl->cCurImage = ilHead.cCurImage;
2019     himl->cMaxImage = ilHead.cMaxImage;
2020
2021     ImageList_SetBkColor(himl,ilHead.bkcolor);
2022     for (i=0;i<4;i++)
2023         ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2024     return himl;
2025 }
2026
2027
2028 /*************************************************************************
2029  * ImageList_Remove [COMCTL32.@]
2030  *
2031  * Removes an image from an image list
2032  *
2033  * PARAMS
2034  *     himl [I] image list handle
2035  *     i    [I] image index
2036  *
2037  * RETURNS
2038  *     Success: TRUE
2039  *     Failure: FALSE
2040  */
2041
2042 BOOL WINAPI
2043 ImageList_Remove (HIMAGELIST himl, INT i)
2044 {
2045     HBITMAP hbmNewImage, hbmNewMask;
2046     HDC     hdcBmp;
2047     INT     nCount;
2048     SIZE    sz;
2049
2050     TRACE("(himl=%p i=%d)\n", himl, i);
2051
2052     if (!is_valid(himl)) {
2053         ERR("Invalid image list handle!\n");
2054         return FALSE;
2055     }
2056
2057     if ((i < -1) || (i >= himl->cCurImage)) {
2058         TRACE("index out of range! %d\n", i);
2059         return FALSE;
2060     }
2061
2062     if (i == -1) {
2063         /* remove all */
2064         if (himl->cCurImage == 0) {
2065             /* remove all on empty ImageList is allowed */
2066             TRACE("remove all on empty ImageList!\n");
2067             return TRUE;
2068         }
2069
2070         himl->cMaxImage = himl->cInitial + himl->cGrow;
2071         himl->cCurImage = 0;
2072         for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2073              himl->nOvlIdx[nCount] = -1;
2074
2075         hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage, himl->cx);
2076         SelectObject (himl->hdcImage, hbmNewImage);
2077         DeleteObject (himl->hbmImage);
2078         himl->hbmImage = hbmNewImage;
2079
2080         if (himl->hbmMask) {
2081
2082             imagelist_get_bitmap_size(himl, himl->cMaxImage, himl->cx, &sz);
2083             hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2084             SelectObject (himl->hdcMask, hbmNewMask);
2085             DeleteObject (himl->hbmMask);
2086             himl->hbmMask = hbmNewMask;
2087         }
2088     }
2089     else {
2090         /* delete one image */
2091         TRACE("Remove single image! %d\n", i);
2092
2093         /* create new bitmap(s) */
2094         nCount = (himl->cCurImage + himl->cGrow - 1);
2095
2096         TRACE(" - Number of images: %d / %d (Old/New)\n",
2097                  himl->cCurImage, himl->cCurImage - 1);
2098         TRACE(" - Max. number of images: %d / %d (Old/New)\n",
2099                  himl->cMaxImage, himl->cCurImage + himl->cGrow - 1);
2100
2101         hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, nCount, himl->cx);
2102
2103         imagelist_get_bitmap_size(himl, nCount, himl->cx, &sz );
2104         if (himl->hbmMask)
2105             hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2106         else
2107             hbmNewMask = 0;  /* Just to keep compiler happy! */
2108
2109         hdcBmp = CreateCompatibleDC (0);
2110
2111         /* copy all images and masks prior to the "removed" image */
2112         if (i > 0) {
2113             TRACE("Pre image copy: Copy %d images\n", i);
2114
2115             SelectObject (hdcBmp, hbmNewImage);
2116             imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2117
2118             if (himl->hbmMask) {
2119                 SelectObject (hdcBmp, hbmNewMask);
2120                 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2121             }
2122         }
2123
2124         /* copy all images and masks behind the removed image */
2125         if (i < himl->cCurImage - 1) {
2126             TRACE("Post image copy!\n");
2127
2128             SelectObject (hdcBmp, hbmNewImage);
2129             imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i,
2130                                    (himl->cCurImage - i - 1), i - 1 );
2131
2132             if (himl->hbmMask) {
2133                 SelectObject (hdcBmp, hbmNewMask);
2134                 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i,
2135                                        (himl->cCurImage - i - 1), i - 1 );
2136             }
2137         }
2138
2139         DeleteDC (hdcBmp);
2140
2141         /* delete old images and insert new ones */
2142         SelectObject (himl->hdcImage, hbmNewImage);
2143         DeleteObject (himl->hbmImage);
2144         himl->hbmImage = hbmNewImage;
2145         if (himl->hbmMask) {
2146             SelectObject (himl->hdcMask, hbmNewMask);
2147             DeleteObject (himl->hbmMask);
2148             himl->hbmMask = hbmNewMask;
2149         }
2150
2151         himl->cCurImage--;
2152         himl->cMaxImage = himl->cCurImage + himl->cGrow;
2153     }
2154
2155     return TRUE;
2156 }
2157
2158
2159 /*************************************************************************
2160  * ImageList_Replace [COMCTL32.@]
2161  *
2162  * Replaces an image in an image list with a new image.
2163  *
2164  * PARAMS
2165  *     himl     [I] handle to image list
2166  *     i        [I] image index
2167  *     hbmImage [I] handle to image bitmap
2168  *     hbmMask  [I] handle to mask bitmap. Can be NULL.
2169  *
2170  * RETURNS
2171  *     Success: TRUE
2172  *     Failure: FALSE
2173  */
2174
2175 BOOL WINAPI
2176 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2177                    HBITMAP hbmMask)
2178 {
2179     HDC hdcImage;
2180     BITMAP bmp;
2181     HBITMAP hOldBitmap;
2182     POINT pt;
2183
2184     TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2185
2186     if (!is_valid(himl)) {
2187         ERR("Invalid image list handle!\n");
2188         return FALSE;
2189     }
2190
2191     if ((i >= himl->cMaxImage) || (i < 0)) {
2192         ERR("Invalid image index!\n");
2193         return FALSE;
2194     }
2195
2196     if (!GetObjectW(hbmImage, sizeof(BITMAP), (LPVOID)&bmp))
2197         return FALSE;
2198
2199     hdcImage = CreateCompatibleDC (0);
2200
2201     /* Replace Image */
2202     hOldBitmap = SelectObject (hdcImage, hbmImage);
2203
2204     imagelist_point_from_index(himl, i, &pt);
2205     StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2206                   hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2207
2208     if (himl->hbmMask)
2209     {
2210         HDC hdcTemp;
2211         HBITMAP hOldBitmapTemp;
2212
2213         hdcTemp   = CreateCompatibleDC(0);
2214         hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2215
2216         StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2217                       hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2218         SelectObject(hdcTemp, hOldBitmapTemp);
2219         DeleteDC(hdcTemp);
2220
2221         /* Remove the background from the image
2222         */
2223         BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2224                 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2225     }
2226
2227     SelectObject (hdcImage, hOldBitmap);
2228     DeleteDC (hdcImage);
2229
2230     return TRUE;
2231 }
2232
2233
2234 /*************************************************************************
2235  * ImageList_ReplaceIcon [COMCTL32.@]
2236  *
2237  * Replaces an image in an image list using an icon.
2238  *
2239  * PARAMS
2240  *     himl  [I] handle to image list
2241  *     i     [I] image index
2242  *     hIcon [I] handle to icon
2243  *
2244  * RETURNS
2245  *     Success: index of the replaced image
2246  *     Failure: -1
2247  */
2248
2249 INT WINAPI
2250 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2251 {
2252     HDC     hdcImage;
2253     HICON   hBestFitIcon;
2254     HBITMAP hbmOldSrc;
2255     ICONINFO  ii;
2256     BITMAP  bmp;
2257     BOOL    ret;
2258     POINT   pt;
2259
2260     TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2261
2262     if (!is_valid(himl)) {
2263         ERR("invalid image list\n");
2264         return -1;
2265     }
2266     if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2267         ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2268         return -1;
2269     }
2270
2271     hBestFitIcon = CopyImage(
2272         hIcon, IMAGE_ICON,
2273         himl->cx, himl->cy,
2274         LR_COPYFROMRESOURCE);
2275     /* the above will fail if the icon wasn't loaded from a resource, so try
2276      * again without LR_COPYFROMRESOURCE flag */
2277     if (!hBestFitIcon)
2278         hBestFitIcon = CopyImage(
2279             hIcon, IMAGE_ICON,
2280             himl->cx, himl->cy,
2281             0);
2282     if (!hBestFitIcon)
2283         return -1;
2284
2285     ret = GetIconInfo (hBestFitIcon, &ii);
2286     if (!ret) {
2287         DestroyIcon(hBestFitIcon);
2288         return -1;
2289     }
2290
2291     if (ii.hbmColor == 0)
2292         ERR("no color!\n");
2293     ret = GetObjectW (ii.hbmMask, sizeof(BITMAP), (LPVOID)&bmp);
2294     if (!ret) {
2295         ERR("couldn't get mask bitmap info\n");
2296         if (ii.hbmColor)
2297             DeleteObject (ii.hbmColor);
2298         if (ii.hbmMask)
2299             DeleteObject (ii.hbmMask);
2300         DestroyIcon(hBestFitIcon);
2301         return -1;
2302     }
2303
2304     if (nIndex == -1) {
2305         if (himl->cCurImage + 1 > himl->cMaxImage)
2306             IMAGELIST_InternalExpandBitmaps (himl, 1, 0, 0);
2307
2308         nIndex = himl->cCurImage;
2309         himl->cCurImage++;
2310     }
2311
2312     hdcImage = CreateCompatibleDC (0);
2313     TRACE("hdcImage=%p\n", hdcImage);
2314     if (hdcImage == 0)
2315         ERR("invalid hdcImage!\n");
2316
2317     SetTextColor(himl->hdcImage, RGB(0,0,0));
2318     SetBkColor  (himl->hdcImage, RGB(255,255,255));
2319     hbmOldSrc = SelectObject (hdcImage, ii.hbmColor);
2320
2321     imagelist_point_from_index(himl, nIndex, &pt);
2322     StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2323                   hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2324
2325     if (himl->hbmMask) {
2326         SelectObject (hdcImage, ii.hbmMask);
2327         StretchBlt   (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2328                       hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2329     }
2330
2331     SelectObject (hdcImage, hbmOldSrc);
2332
2333     DestroyIcon(hBestFitIcon);
2334     if (hdcImage)
2335         DeleteDC (hdcImage);
2336     if (ii.hbmColor)
2337         DeleteObject (ii.hbmColor);
2338     if (ii.hbmMask)
2339         DeleteObject (ii.hbmMask);
2340
2341     TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2342     return nIndex;
2343 }
2344
2345
2346 /*************************************************************************
2347  * ImageList_SetBkColor [COMCTL32.@]
2348  *
2349  * Sets the background color of an image list.
2350  *
2351  * PARAMS
2352  *     himl  [I] handle to image list
2353  *     clrBk [I] background color
2354  *
2355  * RETURNS
2356  *     Success: previous background color
2357  *     Failure: CLR_NONE
2358  */
2359
2360 COLORREF WINAPI
2361 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2362 {
2363     COLORREF clrOldBk;
2364
2365     if (!is_valid(himl))
2366         return CLR_NONE;
2367
2368     clrOldBk = himl->clrBk;
2369     himl->clrBk = clrBk;
2370     return clrOldBk;
2371 }
2372
2373
2374 /*************************************************************************
2375  * ImageList_SetDragCursorImage [COMCTL32.@]
2376  *
2377  * Combines the specified image with the current drag image
2378  *
2379  * PARAMS
2380  *     himlDrag  [I] handle to drag image list
2381  *     iDrag     [I] drag image index
2382  *     dxHotspot [I] X position of the hot spot
2383  *     dyHotspot [I] Y position of the hot spot
2384  *
2385  * RETURNS
2386  *     Success: TRUE
2387  *     Failure: FALSE
2388  *
2389  * NOTES
2390  *   - The names dxHotspot, dyHotspot are misleading because they have nothing
2391  *     to do with a hotspot but are only the offset of the origin of the new
2392  *     image relative to the origin of the old image.
2393  *
2394  *   - When this function is called and the drag image is visible, a
2395  *     short flickering occurs but this matches the Win9x behavior. It is
2396  *     possible to fix the flickering using code like in ImageList_DragMove.
2397  */
2398
2399 BOOL WINAPI
2400 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2401                               INT dxHotspot, INT dyHotspot)
2402 {
2403     HIMAGELIST himlTemp;
2404     BOOL visible;
2405
2406     if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2407         return FALSE;
2408
2409     TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2410            dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2411
2412     visible = InternalDrag.bShow;
2413
2414     himlTemp = ImageList_Merge (InternalDrag.himl, 0, himlDrag, iDrag,
2415                                 dxHotspot, dyHotspot);
2416
2417     if (visible) {
2418         /* hide the drag image */
2419         ImageList_DragShowNolock(FALSE);
2420     }
2421     if ((InternalDrag.himl->cx != himlTemp->cx) ||
2422            (InternalDrag.himl->cy != himlTemp->cy)) {
2423         /* the size of the drag image changed, invalidate the buffer */
2424         DeleteObject(InternalDrag.hbmBg);
2425         InternalDrag.hbmBg = 0;
2426     }
2427
2428     ImageList_Destroy (InternalDrag.himl);
2429     InternalDrag.himl = himlTemp;
2430
2431     if (visible) {
2432         /* show the drag image */
2433         ImageList_DragShowNolock(TRUE);
2434     }
2435
2436     return TRUE;
2437 }
2438
2439
2440 /*************************************************************************
2441  * ImageList_SetFilter [COMCTL32.@]
2442  *
2443  * Sets a filter (or does something completely different)!!???
2444  * It removes 12 Bytes from the stack (3 Parameters).
2445  *
2446  * PARAMS
2447  *     himl     [I] SHOULD be a handle to image list
2448  *     i        [I] COULD be an index?
2449  *     dwFilter [I] ???
2450  *
2451  * RETURNS
2452  *     Success: TRUE ???
2453  *     Failure: FALSE ???
2454  *
2455  * BUGS
2456  *     This is an UNDOCUMENTED function!!!!
2457  *     empty stub.
2458  */
2459
2460 BOOL WINAPI
2461 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2462 {
2463     FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2464
2465     return FALSE;
2466 }
2467
2468
2469 /*************************************************************************
2470  * ImageList_SetFlags [COMCTL32.@]
2471  *
2472  * Sets the image list flags.
2473  *
2474  * PARAMS
2475  *     himl  [I] Handle to image list
2476  *     flags [I] Flags to set
2477  *
2478  * RETURNS
2479  *     Old flags?
2480  *
2481  * BUGS
2482  *    Stub.
2483  */
2484
2485 DWORD WINAPI
2486 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2487 {
2488     FIXME("(%p %08x):empty stub\n", himl, flags);
2489     return 0;
2490 }
2491
2492
2493 /*************************************************************************
2494  * ImageList_SetIconSize [COMCTL32.@]
2495  *
2496  * Sets the image size of the bitmap and deletes all images.
2497  *
2498  * PARAMS
2499  *     himl [I] handle to image list
2500  *     cx   [I] image width
2501  *     cy   [I] image height
2502  *
2503  * RETURNS
2504  *     Success: TRUE
2505  *     Failure: FALSE
2506  */
2507
2508 BOOL WINAPI
2509 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2510 {
2511     INT nCount;
2512     HBITMAP hbmNew;
2513
2514     if (!is_valid(himl))
2515         return FALSE;
2516
2517     /* remove all images */
2518     himl->cMaxImage = himl->cInitial + himl->cGrow;
2519     himl->cCurImage = 0;
2520     himl->cx        = cx;
2521     himl->cy        = cy;
2522
2523     /* initialize overlay mask indices */
2524     for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2525         himl->nOvlIdx[nCount] = -1;
2526
2527     hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage, himl->cx);
2528     SelectObject (himl->hdcImage, hbmNew);
2529     DeleteObject (himl->hbmImage);
2530     himl->hbmImage = hbmNew;
2531
2532     if (himl->hbmMask) {
2533         SIZE sz;
2534         imagelist_get_bitmap_size(himl, himl->cMaxImage, himl->cx, &sz);
2535         hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2536         SelectObject (himl->hdcMask, hbmNew);
2537         DeleteObject (himl->hbmMask);
2538         himl->hbmMask = hbmNew;
2539     }
2540
2541     return TRUE;
2542 }
2543
2544
2545 /*************************************************************************
2546  * ImageList_SetImageCount [COMCTL32.@]
2547  *
2548  * Resizes an image list to the specified number of images.
2549  *
2550  * PARAMS
2551  *     himl        [I] handle to image list
2552  *     iImageCount [I] number of images in the image list
2553  *
2554  * RETURNS
2555  *     Success: TRUE
2556  *     Failure: FALSE
2557  */
2558
2559 BOOL WINAPI
2560 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2561 {
2562     HDC     hdcBitmap;
2563     HBITMAP hbmNewBitmap;
2564     INT     nNewCount, nCopyCount;
2565
2566     TRACE("%p %d\n",himl,iImageCount);
2567
2568     if (!is_valid(himl))
2569         return FALSE;
2570     if (iImageCount < 0)
2571         return FALSE;
2572     if (himl->cMaxImage > iImageCount)
2573     {
2574         himl->cCurImage = iImageCount;
2575         /* TODO: shrink the bitmap when cMaxImage-cCurImage>cGrow ? */
2576         return TRUE;
2577     }
2578
2579     nNewCount = iImageCount + himl->cGrow;
2580     nCopyCount = min(himl->cCurImage, iImageCount);
2581
2582     hdcBitmap = CreateCompatibleDC (0);
2583
2584     hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount, himl->cx);
2585
2586     if (hbmNewBitmap != 0)
2587     {
2588         SelectObject (hdcBitmap, hbmNewBitmap);
2589         imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2590
2591         /* FIXME: delete 'empty' image space? */
2592
2593         SelectObject (himl->hdcImage, hbmNewBitmap);
2594         DeleteObject (himl->hbmImage);
2595         himl->hbmImage = hbmNewBitmap;
2596     }
2597     else
2598         ERR("Could not create new image bitmap !\n");
2599
2600     if (himl->hbmMask)
2601     {
2602         SIZE sz;
2603         imagelist_get_bitmap_size( himl, nNewCount, himl->cx, &sz );
2604         hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2605         if (hbmNewBitmap != 0)
2606         {
2607             SelectObject (hdcBitmap, hbmNewBitmap);
2608             imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2609
2610             /* FIXME: delete 'empty' image space? */
2611
2612             SelectObject (himl->hdcMask, hbmNewBitmap);
2613             DeleteObject (himl->hbmMask);
2614             himl->hbmMask = hbmNewBitmap;
2615         }
2616         else
2617             ERR("Could not create new mask bitmap!\n");
2618     }
2619
2620     DeleteDC (hdcBitmap);
2621
2622     /* Update max image count and current image count */
2623     himl->cMaxImage = nNewCount;
2624     himl->cCurImage = iImageCount;
2625
2626     return TRUE;
2627 }
2628
2629
2630 /*************************************************************************
2631  * ImageList_SetOverlayImage [COMCTL32.@]
2632  *
2633  * Assigns an overlay mask index to an existing image in an image list.
2634  *
2635  * PARAMS
2636  *     himl     [I] handle to image list
2637  *     iImage   [I] image index
2638  *     iOverlay [I] overlay mask index
2639  *
2640  * RETURNS
2641  *     Success: TRUE
2642  *     Failure: FALSE
2643  */
2644
2645 BOOL WINAPI
2646 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2647 {
2648     if (!is_valid(himl))
2649         return FALSE;
2650     if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2651         return FALSE;
2652     if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2653         return FALSE;
2654     himl->nOvlIdx[iOverlay - 1] = iImage;
2655     return TRUE;
2656 }
2657
2658
2659
2660 /* helper for ImageList_Write - write bitmap to pstm
2661  * currently everything is written as 24 bit RGB, except masks
2662  */
2663 static BOOL
2664 _write_bitmap(HBITMAP hBitmap, LPSTREAM pstm)
2665 {
2666     LPBITMAPFILEHEADER bmfh;
2667     LPBITMAPINFOHEADER bmih;
2668     LPBYTE data = NULL, lpBits;
2669     BITMAP bm;
2670     INT bitCount, sizeImage, offBits, totalSize;
2671     HDC xdc;
2672     BOOL result = FALSE;
2673
2674     if (!GetObjectW(hBitmap, sizeof(BITMAP), (LPVOID)&bm))
2675         return FALSE;
2676
2677     bitCount = bm.bmBitsPixel == 1 ? 1 : 24;
2678     sizeImage = DIB_GetDIBImageBytes(bm.bmWidth, bm.bmHeight, bitCount);
2679
2680     totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
2681     if(bitCount != 24)
2682         totalSize += (1 << bitCount) * sizeof(RGBQUAD);
2683     offBits = totalSize;
2684     totalSize += sizeImage;
2685
2686     data = Alloc(totalSize);
2687     bmfh = (LPBITMAPFILEHEADER)data;
2688     bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
2689     lpBits = data + offBits;
2690
2691     /* setup BITMAPFILEHEADER */
2692     bmfh->bfType      = (('M' << 8) | 'B');
2693     bmfh->bfSize      = 0;
2694     bmfh->bfReserved1 = 0;
2695     bmfh->bfReserved2 = 0;
2696     bmfh->bfOffBits   = offBits;
2697
2698     /* setup BITMAPINFOHEADER */
2699     bmih->biSize          = sizeof(BITMAPINFOHEADER);
2700     bmih->biWidth         = bm.bmWidth;
2701     bmih->biHeight        = bm.bmHeight;
2702     bmih->biPlanes        = 1;
2703     bmih->biBitCount      = bitCount;
2704     bmih->biCompression   = BI_RGB;
2705     bmih->biSizeImage     = sizeImage;
2706     bmih->biXPelsPerMeter = 0;
2707     bmih->biYPelsPerMeter = 0;
2708     bmih->biClrUsed       = 0;
2709     bmih->biClrImportant  = 0;
2710
2711     xdc = GetDC(0);
2712     result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
2713     ReleaseDC(0, xdc);
2714     if (!result)
2715         goto failed;
2716
2717     TRACE("width %u, height %u, planes %u, bpp %u\n",
2718           bmih->biWidth, bmih->biHeight,
2719           bmih->biPlanes, bmih->biBitCount);
2720
2721     if(bitCount == 1) {
2722         /* Hack. */
2723         LPBITMAPINFO inf = (LPBITMAPINFO)bmih;
2724         inf->bmiColors[0].rgbRed = inf->bmiColors[0].rgbGreen = inf->bmiColors[0].rgbBlue = 0;
2725         inf->bmiColors[1].rgbRed = inf->bmiColors[1].rgbGreen = inf->bmiColors[1].rgbBlue = 0xff;
2726     }
2727
2728     if(!SUCCEEDED(IStream_Write(pstm, data, totalSize, NULL)))
2729         goto failed;
2730
2731     result = TRUE;
2732
2733 failed:
2734     Free(data);
2735
2736     return result;
2737 }
2738
2739
2740 /*************************************************************************
2741  * ImageList_Write [COMCTL32.@]
2742  *
2743  * Writes an image list to a stream.
2744  *
2745  * PARAMS
2746  *     himl [I] handle to image list
2747  *     pstm [O] Pointer to a stream.
2748  *
2749  * RETURNS
2750  *     Success: TRUE
2751  *     Failure: FALSE
2752  *
2753  * BUGS
2754  *     probably.
2755  */
2756
2757 BOOL WINAPI
2758 ImageList_Write (HIMAGELIST himl, LPSTREAM pstm)
2759 {
2760     ILHEAD ilHead;
2761     int i;
2762
2763     TRACE("%p %p\n", himl, pstm);
2764
2765     if (!is_valid(himl))
2766         return FALSE;
2767
2768     ilHead.usMagic   = (('L' << 8) | 'I');
2769     ilHead.usVersion = 0x101;
2770     ilHead.cCurImage = himl->cCurImage;
2771     ilHead.cMaxImage = himl->cMaxImage;
2772     ilHead.cGrow     = himl->cGrow;
2773     ilHead.cx        = himl->cx;
2774     ilHead.cy        = himl->cy;
2775     ilHead.bkcolor   = himl->clrBk;
2776     ilHead.flags     = himl->flags;
2777     for(i = 0; i < 4; i++) {
2778         ilHead.ovls[i] = himl->nOvlIdx[i];
2779     }
2780
2781     TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
2782           ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2783
2784     if(!SUCCEEDED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
2785         return FALSE;
2786
2787     /* write the bitmap */
2788     if(!_write_bitmap(himl->hbmImage, pstm))
2789         return FALSE;
2790
2791     /* write the mask if we have one */
2792     if(himl->flags & ILC_MASK) {
2793         if(!_write_bitmap(himl->hbmMask, pstm))
2794             return FALSE;
2795     }
2796
2797     return TRUE;
2798 }
2799
2800
2801 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count, UINT width)
2802 {
2803     HBITMAP hbmNewBitmap;
2804     UINT ilc = (himl->flags & 0xFE);
2805     SIZE sz;
2806
2807     imagelist_get_bitmap_size( himl, count, width, &sz );
2808
2809     if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
2810     {
2811         VOID* bits;
2812         BITMAPINFO *bmi;
2813
2814         TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
2815               sz.cx, sz.cy, himl->uBitsPixel);
2816
2817         if (himl->uBitsPixel <= ILC_COLOR8)
2818         {
2819             LPPALETTEENTRY pal;
2820             ULONG i, colors;
2821             BYTE temp;
2822
2823             colors = 1 << himl->uBitsPixel;
2824             bmi = Alloc(sizeof(BITMAPINFOHEADER) +
2825                             sizeof(PALETTEENTRY) * colors);
2826
2827             pal = (LPPALETTEENTRY)bmi->bmiColors;
2828             GetPaletteEntries(GetStockObject(DEFAULT_PALETTE), 0, colors, pal);
2829
2830             /* Swap colors returned by GetPaletteEntries so we can use them for
2831              * CreateDIBSection call. */
2832             for (i = 0; i < colors; i++)
2833             {
2834                 temp = pal[i].peBlue;
2835                 bmi->bmiColors[i].rgbRed = pal[i].peRed;
2836                 bmi->bmiColors[i].rgbBlue = temp;
2837             }
2838         }
2839         else
2840         {
2841             bmi = Alloc(sizeof(BITMAPINFOHEADER));
2842         }
2843
2844         bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2845         bmi->bmiHeader.biWidth = sz.cx;
2846         bmi->bmiHeader.biHeight = sz.cy;
2847         bmi->bmiHeader.biPlanes = 1;
2848         bmi->bmiHeader.biBitCount = himl->uBitsPixel;
2849         bmi->bmiHeader.biCompression = BI_RGB;
2850         bmi->bmiHeader.biSizeImage = 0;
2851         bmi->bmiHeader.biXPelsPerMeter = 0;
2852         bmi->bmiHeader.biYPelsPerMeter = 0;
2853         bmi->bmiHeader.biClrUsed = 0;
2854         bmi->bmiHeader.biClrImportant = 0;
2855
2856         hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, &bits, 0, 0);
2857
2858         Free (bmi);
2859     }
2860     else /*if (ilc == ILC_COLORDDB)*/
2861     {
2862         TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
2863
2864         hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
2865     }
2866     TRACE("returning %p\n", hbmNewBitmap);
2867     return hbmNewBitmap;
2868 }
2869
2870 /*************************************************************************
2871  * ImageList_SetColorTable [COMCTL32.@]
2872  *
2873  * Sets the color table of an image list.
2874  *
2875  * PARAMS
2876  *     himl        [I] Handle to the image list.
2877  *     uStartIndex [I] The first index to set.
2878  *     cEntries    [I] Number of entries to set.
2879  *     prgb        [I] New color information for color table for the image list.
2880  *
2881  * RETURNS
2882  *     Success: Number of entries in the table that were set.
2883  *     Failure: Zero.
2884  *
2885  * SEE
2886  *     ImageList_Create(), SetDIBColorTable()
2887  */
2888
2889 UINT WINAPI
2890 ImageList_SetColorTable (HIMAGELIST himl, UINT uStartIndex, UINT cEntries, CONST RGBQUAD * prgb)
2891 {
2892     return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
2893 }