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