Added pointer validation and additional sanity check to IsValidSid().
[wine] / dlls / comctl32 / imagelist.c
1 /*
2  *  ImageList implementation
3  *
4  *  Copyright 1998 Eric Kohl
5  *
6  *  TODO:
7  *    - Fix ImageList_DrawIndirect (xBitmap, yBitmap, rgbFg, rgbBk, dwRop).
8  *    - Fix ImageList_GetIcon.
9  *    - Fix drag functions.
10  *    - Fix ImageList_Read and ImageList_Write.
11  *    - Fix ImageList_SetFilter (undocumented).
12  *      BTW does anybody know anything about this function???
13  *        - It removes 12 Bytes from the stack (3 Parameters).
14  *        - First parameter SHOULD be a HIMAGELIST.
15  *        - Second parameter COULD be an index?????
16  *        - Third parameter.... ?????????????????????
17  *
18  *  Comments:
19  *    - ImageList_Draw, ImageList_DrawEx and ImageList_GetIcon use
20  *      ImageList_DrawIndirect. Since ImageList_DrawIndirect is still
21  *      partially imlemented, the functions mentioned above will be 
22  *      limited in functionality too.
23  */
24
25 /* This must be defined because the HIMAGELIST type is just a pointer
26  * to the _IMAGELIST data structure. But M$ does not want us to know
27  * anything about its contents. Applications just see a pointer to
28  * an empty structure. It's just to keep compatibility.
29  */
30 #define __WINE_IMAGELIST_C
31
32  
33 #include "wine/obj_base.h"
34 #include "wine/obj_storage.h"
35 #include "imagelist.h"
36 #include "commctrl.h"
37 #include "debugtools.h"
38
39 DEFAULT_DEBUG_CHANNEL(imagelist)
40
41
42 #define _MAX(a,b) (((a)>(b))?(a):(b))
43 #define _MIN(a,b) (((a)>(b))?(b):(a))
44
45 #define MAX_OVERLAYIMAGE 15
46
47
48 /* internal image list data used for Drag & Drop operations */
49
50 static HIMAGELIST himlInternalDrag = NULL;
51 static INT      nInternalDragHotspotX = 0;
52 static INT      nInternalDragHotspotY = 0;
53
54 static HWND     hwndInternalDrag = 0;
55 static INT      xInternalPos = 0;
56 static INT      yInternalPos = 0;
57
58 static HDC      hdcBackBuffer = 0;
59 static HBITMAP  hbmBackBuffer = 0;
60
61
62 /*************************************************************************
63  * IMAGELIST_InternalExpandBitmaps [Internal] 
64  *
65  * Expands the bitmaps of an image list by the given number of images.
66  *
67  * PARAMS
68  *     himl        [I] handle to image list
69  *     nImageCount [I] number of images to add
70  *
71  * RETURNS
72  *     nothing
73  *
74  * NOTES
75  *     This function can NOT be used to reduce the number of images.
76  */
77
78 static VOID
79 IMAGELIST_InternalExpandBitmaps (HIMAGELIST himl, INT nImageCount)
80 {
81     HDC     hdcImageList, hdcBitmap;
82     HBITMAP hbmNewBitmap;
83     INT     nNewWidth, nNewCount;
84
85     TRACE("Create expanded bitmaps!\n");
86
87     nNewCount = himl->cCurImage + nImageCount + himl->cGrow;
88     nNewWidth = nNewCount * himl->cx;
89
90     hdcImageList = CreateCompatibleDC (0);
91     hdcBitmap = CreateCompatibleDC (0);
92
93     hbmNewBitmap =
94         CreateBitmap (nNewWidth, himl->cy, 1, himl->uBitsPixel, NULL);
95     if (hbmNewBitmap == 0)
96         ERR("creating new image bitmap!\n");
97
98     SelectObject (hdcImageList, himl->hbmImage);
99     SelectObject (hdcBitmap, hbmNewBitmap);
100     BitBlt (hdcBitmap, 0, 0, himl->cCurImage * himl->cx, himl->cy,
101               hdcImageList, 0, 0, SRCCOPY);
102
103     DeleteObject (himl->hbmImage);
104     himl->hbmImage = hbmNewBitmap;
105
106     if (himl->hbmMask) {
107         hbmNewBitmap = 
108             CreateBitmap (nNewWidth, himl->cy, 1, 1, NULL);
109
110         if (hbmNewBitmap == 0)
111             ERR("creating new mask bitmap!");
112
113         SelectObject (hdcImageList, himl->hbmMask);
114         SelectObject (hdcBitmap, hbmNewBitmap);
115         BitBlt (hdcBitmap, 0, 0, himl->cCurImage * himl->cx, himl->cy,
116                   hdcImageList, 0, 0, SRCCOPY);
117         DeleteObject (himl->hbmMask);
118         himl->hbmMask = hbmNewBitmap;
119     }
120
121     himl->cMaxImage = nNewCount;
122
123     DeleteDC (hdcImageList);
124     DeleteDC (hdcBitmap);
125 }
126
127
128 /*************************************************************************
129  * ImageList_Add [COMCTL32.39]
130  *
131  * Add an image or images to an image list.
132  *
133  * PARAMS
134  *     himl     [I] handle to image list
135  *     hbmImage [I] handle to image bitmap
136  *     hbmMask  [I] handle to mask bitmap
137  *
138  * RETURNS
139  *     Success: Index of the first new image.
140  *     Failure: -1
141  */
142
143 INT WINAPI
144 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
145 {
146     HDC    hdcSrc, hdcDst;
147     INT    nFirstIndex, nImageCount;
148     INT    nStartX;
149     BITMAP bmp;
150
151     if (!himl || !hbmImage)
152         return -1;
153
154     GetObjectA (hbmImage, sizeof(BITMAP), (LPVOID)&bmp);
155     nImageCount = bmp.bmWidth / himl->cx;
156
157     if (himl->cCurImage + nImageCount >= himl->cMaxImage)
158         IMAGELIST_InternalExpandBitmaps (himl, nImageCount);
159
160     nStartX = himl->cCurImage * himl->cx;
161
162     hdcSrc = CreateCompatibleDC (0);
163     hdcDst = CreateCompatibleDC (0);
164
165     /* copy image bitmap */
166     SelectObject (hdcDst, himl->hbmImage);
167     SelectObject (hdcSrc, hbmImage);
168     BitBlt (hdcDst, himl->cCurImage * himl->cx, 0,
169               bmp.bmWidth, himl->cy, hdcSrc, 0, 0, SRCCOPY);
170           
171     if (himl->hbmMask) {
172         if (hbmMask) {
173             /* copy mask bitmap */
174             SelectObject (hdcDst, himl->hbmMask);
175             SelectObject (hdcSrc, hbmMask);
176             BitBlt (hdcDst, nStartX, 0, bmp.bmWidth, himl->cy,
177                       hdcSrc, 0, 0, SRCCOPY);
178         }
179         else {
180             /* copy monochrome image to the mask bitmap */
181             SelectObject (hdcDst, himl->hbmMask);
182             SelectObject (hdcSrc, hbmImage);
183             SetBkColor (hdcSrc, GetNearestColor (hdcSrc,
184                           GetPixel (hdcSrc, 0, 0)));
185             BitBlt (hdcDst, nStartX, 0, bmp.bmWidth, himl->cy,
186                       hdcSrc, nStartX, 0, SRCCOPY);
187         }
188     }
189
190     DeleteDC (hdcSrc);
191     DeleteDC (hdcDst);
192
193     nFirstIndex = himl->cCurImage;
194     himl->cCurImage += nImageCount;
195
196     return nFirstIndex;
197 }
198
199
200 /*************************************************************************
201  * ImageList_AddIcon [COMCTL32.40]
202  *
203  * Adds an icon to an image list.
204  *
205  * PARAMS
206  *     himl  [I] handle to image list
207  *     hIcon [I] handle to icon
208  *
209  * RETURNS
210  *     Success: index of the new image
211  *     Failure: -1
212  */
213
214 INT WINAPI
215 ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
216 {
217     return ImageList_ReplaceIcon (himl, -1, hIcon);
218 }
219
220
221 /*************************************************************************
222  * ImageList_AddMasked [COMCTL32.41] 
223  *
224  * Adds an image or images to an image list and creates a mask from the
225  * specified bitmap using the mask color.
226  *
227  * PARAMS
228  *     himl    [I] handle to image list.
229  *     hBitmap [I] handle to bitmap
230  *     clrMask [I] mask color.
231  *
232  * RETURNS
233  *     Success: Index of the first new image.
234  *     Failure: -1
235  */
236
237 INT WINAPI
238 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
239 {
240     HDC    hdcImage, hdcMask, hdcBitmap;
241     INT    nIndex, nImageCount;
242     BITMAP bmp;
243
244     if (himl == NULL)
245         return -1;
246
247     if (!GetObjectA (hBitmap, sizeof(BITMAP), &bmp))
248         return -1;
249
250     nImageCount = bmp.bmWidth / himl->cx;
251
252     if (himl->cCurImage + nImageCount >= himl->cMaxImage)
253         IMAGELIST_InternalExpandBitmaps (himl, nImageCount);
254
255     nIndex = himl->cCurImage;
256     himl->cCurImage += nImageCount;
257
258     hdcImage  = CreateCompatibleDC (0);
259     hdcBitmap = CreateCompatibleDC (0);
260
261     SelectObject (hdcBitmap, hBitmap);
262     SelectObject (hdcImage, himl->hbmImage);
263     BitBlt (hdcImage, nIndex * himl->cx, 0, bmp.bmWidth, himl->cy,
264               hdcBitmap, 0, 0, SRCCOPY);
265
266     if (himl->hbmMask) {
267         COLORREF bkColor = (clrMask != CLR_DEFAULT) ? clrMask :
268             GetNearestColor (hdcBitmap, GetPixel (hdcBitmap, 0, 0));
269
270         /* create mask from image */
271         hdcMask = CreateCompatibleDC (0);
272         SelectObject (hdcMask, himl->hbmMask);
273
274         /* create monochrome image to the mask bitmap */
275         SetBkColor (hdcBitmap, bkColor);
276         BitBlt (hdcMask, nIndex * himl->cx, 0, bmp.bmWidth, himl->cy,
277                   hdcBitmap, 0, 0, SRCCOPY);
278
279         DeleteDC (hdcMask);
280     }
281
282     DeleteDC (hdcImage);
283     DeleteDC (hdcBitmap);
284   
285     return nIndex;
286 }
287
288
289 /*************************************************************************
290  * ImageList_BeginDrag [COMCTL32.42] 
291  *
292  * Creates a temporary image list that contains one image. It will be used
293  * as a drag image.
294  *
295  * PARAMS
296  *     himlTrack [I] handle to the source image list
297  *     iTrack    [I] index of the drag image in the source image list
298  *     dxHotspot [I] X position of the hot spot of the drag image
299  *     dyHotspot [I] Y position of the hot spot of the drag image
300  *
301  * RETURNS
302  *     Success: TRUE
303  *     Failure: FALSE
304  */
305
306 BOOL WINAPI
307 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
308                      INT dxHotspot, INT dyHotspot)
309 {
310     HDC hdcSrc, hdcDst;
311
312     FIXME("partially implemented!\n");
313
314     if (himlTrack == NULL)
315         return FALSE;
316
317     if (himlInternalDrag)
318         ImageList_EndDrag ();
319
320     himlInternalDrag =
321         ImageList_Create (himlTrack->cx, himlTrack->cy,
322                           himlTrack->flags, 1, 1);
323     if (himlInternalDrag == NULL) {
324         ERR("Error creating drag image list!\n");
325         return FALSE;
326     }
327
328     nInternalDragHotspotX = dxHotspot;
329     nInternalDragHotspotY = dyHotspot;
330
331     hdcSrc = CreateCompatibleDC (0);
332     hdcDst = CreateCompatibleDC (0);
333
334     /* copy image */
335     SelectObject (hdcSrc, himlTrack->hbmImage);
336     SelectObject (hdcDst, himlInternalDrag->hbmImage);
337     StretchBlt (hdcDst, 0, 0, himlInternalDrag->cx, himlInternalDrag->cy, hdcSrc,
338                   iTrack * himlTrack->cx, 0, himlTrack->cx, himlTrack->cy, SRCCOPY);
339
340     /* copy mask */
341     SelectObject (hdcSrc, himlTrack->hbmMask);
342     SelectObject (hdcDst, himlInternalDrag->hbmMask);
343     StretchBlt (hdcDst, 0, 0, himlInternalDrag->cx, himlInternalDrag->cy, hdcSrc,
344                   iTrack * himlTrack->cx, 0, himlTrack->cx, himlTrack->cy, SRCCOPY);
345
346     DeleteDC (hdcSrc);
347     DeleteDC (hdcDst);
348
349     himlInternalDrag->cCurImage = 1;
350
351     return TRUE;
352 }
353
354
355 /*************************************************************************
356  * ImageList_Copy [COMCTL32.43] 
357  *
358  *  Copies an image of the source image list to an image of the 
359  *  destination image list. Images can be copied or swapped.
360  *
361  * PARAMS
362  *     himlDst [I] handle to the destination image list
363  *     iDst    [I] destination image index.
364  *     himlSrc [I] handle to the source image list
365  *     iSrc    [I] source image index
366  *     uFlags  [I] flags for the copy operation
367  *
368  * RETURNS
369  *     Success: TRUE
370  *     Failure: FALSE
371  *
372  * NOTES
373  *     Copying from one image list to another is possible. The original
374  *     implementation just copies or swapps within one image list.
375  *     Could this feature become a bug??? ;-)
376  */
377
378 BOOL WINAPI
379 ImageList_Copy (HIMAGELIST himlDst, INT iDst,   HIMAGELIST himlSrc,
380                 INT iSrc, INT uFlags)
381 {
382     HDC hdcSrc, hdcDst;    
383
384     TRACE("iDst=%d  iSrc=%d\n", iDst, iSrc);
385
386     if ((himlSrc == NULL) || (himlDst == NULL))
387         return FALSE;
388     if ((iDst < 0) || (iDst >= himlDst->cCurImage))
389         return FALSE;
390     if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
391         return FALSE;
392
393     hdcSrc = CreateCompatibleDC (0);
394     if (himlDst == himlSrc)
395         hdcDst = hdcSrc;
396     else
397         hdcDst = CreateCompatibleDC (0);
398
399     if (uFlags & ILCF_SWAP) {
400         /* swap */
401         HBITMAP hbmTempImage, hbmTempMask;
402
403         /* create temporary bitmaps */
404         hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
405                                        himlSrc->uBitsPixel, NULL);
406         hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
407                                       1, NULL);
408
409         /* copy (and stretch) destination to temporary bitmaps.(save) */
410         /* image */
411         SelectObject (hdcSrc, himlDst->hbmImage);
412         SelectObject (hdcDst, hbmTempImage);
413         StretchBlt (hdcDst, 0, 0, himlSrc->cx, himlSrc->cy,
414                       hdcSrc, iDst * himlDst->cx, 0, himlDst->cx, himlDst->cy,
415                       SRCCOPY);
416         /* mask */
417         SelectObject (hdcSrc, himlDst->hbmMask);
418         SelectObject (hdcDst, hbmTempMask);
419         StretchBlt (hdcDst, 0, 0, himlSrc->cx, himlSrc->cy,
420                       hdcSrc, iDst * himlDst->cx, 0, himlDst->cx, himlDst->cy,
421                       SRCCOPY);
422
423         /* copy (and stretch) source to destination */
424         /* image */
425         SelectObject (hdcSrc, himlSrc->hbmImage);
426         SelectObject (hdcDst, himlDst->hbmImage);
427         StretchBlt (hdcDst, iDst * himlDst->cx, 0, himlDst->cx, himlDst->cy,
428                       hdcSrc, iSrc * himlSrc->cx, 0, himlSrc->cx, himlSrc->cy,
429                       SRCCOPY);
430         /* mask */
431         SelectObject (hdcSrc, himlSrc->hbmMask);
432         SelectObject (hdcDst, himlDst->hbmMask);
433         StretchBlt (hdcDst, iDst * himlDst->cx, 0, himlDst->cx, himlDst->cy,
434                       hdcSrc, iSrc * himlSrc->cx, 0, himlSrc->cx, himlSrc->cy,
435                       SRCCOPY);
436
437         /* copy (without stretching) temporary bitmaps to source (restore) */
438         /* image */
439         SelectObject (hdcSrc, hbmTempImage);
440         SelectObject (hdcDst, himlSrc->hbmImage);
441         BitBlt (hdcDst, iSrc * himlSrc->cx, 0, himlSrc->cx, himlSrc->cy,
442                   hdcSrc, 0, 0, SRCCOPY);
443         /* mask */
444         SelectObject (hdcSrc, hbmTempMask);
445         SelectObject (hdcDst, himlSrc->hbmMask);
446         BitBlt (hdcDst, iSrc * himlSrc->cx, 0, himlSrc->cx, himlSrc->cy,
447                   hdcSrc, 0, 0, SRCCOPY);
448
449         /* delete temporary bitmaps */
450         DeleteObject (hbmTempMask);
451         DeleteObject (hbmTempImage);
452     }
453     else {
454         /* copy image */
455         SelectObject (hdcSrc, himlSrc->hbmImage);
456         if (himlSrc == himlDst)
457             hdcDst = hdcSrc;
458         else
459             SelectObject (hdcDst, himlDst->hbmImage);
460         StretchBlt (hdcDst, iDst * himlDst->cx, 0, himlDst->cx, himlDst->cy,
461                       hdcSrc, iSrc * himlSrc->cx, 0, himlSrc->cx, himlSrc->cy,
462                       SRCCOPY);
463
464         /* copy mask */
465         SelectObject (hdcSrc, himlSrc->hbmMask);
466         if (himlSrc == himlDst)
467             hdcDst = hdcSrc;
468         else
469             SelectObject (hdcDst, himlDst->hbmMask);
470         StretchBlt (hdcDst, iDst * himlDst->cx, 0, himlDst->cx, himlDst->cy,
471                       hdcSrc, iSrc * himlSrc->cx, 0, himlSrc->cx, himlSrc->cy,
472                       SRCCOPY);
473     }
474
475     DeleteDC (hdcSrc);
476     if (himlSrc != himlDst)
477         DeleteDC (hdcDst);
478
479     return TRUE;
480 }
481
482
483 /*************************************************************************
484  * ImageList_Create [COMCTL32.44]  Creates a new image list.
485  *
486  * PARAMS
487  *     cx       [I] image height
488  *     cy       [I] image width
489  *     flags    [I] creation flags
490  *     cInitial [I] initial number of images in the image list
491  *     cGrow    [I] number of images by which image list grows
492  *
493  * RETURNS
494  *     Success: Handle to the created image list
495  *     Failure: NULL
496  */
497
498 HIMAGELIST WINAPI
499 ImageList_Create (INT cx, INT cy, UINT flags,
500                   INT cInitial, INT cGrow)
501 {
502     HIMAGELIST himl;
503     HDC      hdc;
504     INT      nCount;
505     HBITMAP  hbmTemp;
506     static WORD aBitBlend25[] = 
507         {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
508
509     static WORD aBitBlend50[] =
510         {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
511
512     TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
513
514     himl = (HIMAGELIST)COMCTL32_Alloc (sizeof(struct _IMAGELIST));
515     if (!himl)
516         return NULL;
517
518     himl->cx        = cx;
519     himl->cy        = cy;
520     himl->flags     = flags;
521     himl->cMaxImage = cInitial + cGrow;
522     himl->cInitial  = cInitial;
523     himl->cGrow     = cGrow;
524     himl->cCurImage = 0;
525     himl->clrFg     = CLR_DEFAULT;
526     himl->clrBk     = CLR_NONE;
527
528     /* initialize overlay mask indices */
529     for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
530         himl->nOvlIdx[nCount] = -1;
531
532     hdc = CreateCompatibleDC (0);
533     himl->uBitsPixel = (UINT)GetDeviceCaps (hdc, BITSPIXEL);
534     DeleteDC (hdc);
535
536     TRACE("Image: %d Bits per Pixel\n", himl->uBitsPixel);
537
538     himl->hbmImage =
539         CreateBitmap (himl->cx * himl->cMaxImage, himl->cy,
540                         1, himl->uBitsPixel, NULL);
541     if (himl->hbmImage == 0) {
542         ERR("Error creating image bitmap!\n");
543         return NULL;
544     }
545
546     if (himl->flags & ILC_MASK) {
547         himl->hbmMask = CreateBitmap (himl->cx * himl->cMaxImage, himl->cy,
548                                         1, 1, NULL);
549         if (himl->hbmMask == 0) {
550             ERR("Error creating mask bitmap!\n");
551             if (himl->hbmImage)
552                 DeleteObject (himl->hbmImage);
553             return NULL;
554         }
555     }
556     else
557         himl->hbmMask = 0;
558
559     /* create blending brushes */
560     hbmTemp = CreateBitmap (8, 8, 1, 1, &aBitBlend25);
561     himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
562     DeleteObject (hbmTemp);
563
564     hbmTemp = CreateBitmap (8, 8, 1, 1, &aBitBlend50);
565     himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
566     DeleteObject (hbmTemp);
567
568     return himl;
569 }
570
571
572 /*************************************************************************
573  * ImageList_Destroy [COMCTL32.45] 
574  *
575  * Destroys an image list.
576  *
577  * PARAMS
578  *     himl [I] handle to image list
579  *
580  * RETURNS
581  *     Success: TRUE
582  *     Failure: FALSE
583  */
584
585 BOOL WINAPI
586 ImageList_Destroy (HIMAGELIST himl)
587
588     if (!himl)
589         return FALSE;
590
591     /* delete image bitmaps */
592     if (himl->hbmImage)
593         DeleteObject (himl->hbmImage);
594     if (himl->hbmMask)
595         DeleteObject (himl->hbmMask);
596
597     /* delete blending brushes */
598     if (himl->hbrBlend25)
599         DeleteObject (himl->hbrBlend25);
600     if (himl->hbrBlend50)
601         DeleteObject (himl->hbrBlend50);
602         
603     COMCTL32_Free (himl);
604
605     return TRUE;
606 }
607
608
609 /*************************************************************************
610  * ImageList_DragEnter [COMCTL32.46] 
611  *
612  * Locks window update and displays the drag image at the given position.
613  *
614  * PARAMS
615  *     hwndLock [I] handle of the window that owns the drag image.
616  *     x        [I] X position of the drag image.
617  *     y        [I] Y position of the drag image.
618  *
619  * RETURNS
620  *     Success: TRUE
621  *     Failure: FALSE
622  *
623  * NOTES
624  *     The position of the drag image is relative to the window, not
625  *     the client area.
626  */
627
628 BOOL WINAPI
629 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
630 {
631     if (himlInternalDrag == NULL)
632         return FALSE;
633
634     if (hwndLock)
635         hwndInternalDrag = hwndLock;
636     else
637         hwndInternalDrag = GetDesktopWindow ();
638
639     xInternalPos = x;
640     yInternalPos = y;
641
642     hdcBackBuffer = CreateCompatibleDC (0);
643     hbmBackBuffer = CreateCompatibleBitmap (hdcBackBuffer,
644                 himlInternalDrag->cx, himlInternalDrag->cy);
645
646     ImageList_DragShowNolock (TRUE);
647
648     return FALSE;
649 }
650
651
652 /*************************************************************************
653  * ImageList_DragLeave [COMCTL32.47] 
654  *
655  * Unlocks window update and hides the drag image.
656  *
657  * PARAMS
658  *     hwndLock [I] handle of the window that owns the drag image.
659  *
660  * RETURNS
661  *     Success: TRUE
662  *     Failure: FALSE
663  */
664
665 BOOL WINAPI
666 ImageList_DragLeave (HWND hwndLock)
667 {
668     if (hwndLock)
669         hwndInternalDrag = hwndLock;
670     else
671         hwndInternalDrag = GetDesktopWindow ();
672
673     ImageList_DragShowNolock (FALSE);
674
675     DeleteDC (hdcBackBuffer);
676     DeleteObject (hbmBackBuffer);
677
678     return TRUE;
679 }
680
681
682 /*************************************************************************
683  * ImageList_DragMove [COMCTL32.48] 
684  *
685  * Moves the drag image.
686  *
687  * PARAMS
688  *     x [I] X position of the drag image.
689  *     y [I] Y position of the drag image.
690  *
691  * RETURNS
692  *     Success: TRUE
693  *     Failure: FALSE
694  *
695  * NOTES
696  *     The position of the drag image is relative to the window, not
697  *     the client area.
698  */
699
700 BOOL WINAPI
701 ImageList_DragMove (INT x, INT y)
702 {
703     ImageList_DragShowNolock (FALSE);
704
705     xInternalPos = x;
706     yInternalPos = y;
707
708     ImageList_DragShowNolock (TRUE);
709
710     return FALSE;
711 }
712
713
714 /*************************************************************************
715  * ImageList_DragShowNolock [COMCTL32.49] 
716  *
717  * Shows or hides the drag image.
718  *
719  * PARAMS
720  *     bShow [I] TRUE shows the drag image, FALSE hides it.
721  *
722  * RETURNS
723  *     Success: TRUE
724  *     Failure: FALSE
725  *
726  * FIXME
727  *     semi-stub.
728  */
729
730 BOOL WINAPI
731 ImageList_DragShowNolock (BOOL bShow)
732 {
733     HDC hdcDrag;
734
735     FIXME("semi-stub!\n");
736     TRACE("bShow=0x%X!\n", bShow);
737
738     hdcDrag = GetDCEx (hwndInternalDrag, 0,
739                          DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
740
741     if (bShow) {
742         /* show drag image */
743
744         /* save background */
745
746         /* draw drag image */
747
748     }
749     else {
750         /* hide drag image */
751
752         /* restore background */
753
754     }
755
756     ReleaseDC (hwndInternalDrag, hdcDrag);
757
758     return FALSE;
759 }
760
761
762 /*************************************************************************
763  * ImageList_Draw [COMCTL32.50] Draws an image.
764  *
765  * PARAMS
766  *     himl   [I] handle to image list
767  *     i      [I] image index
768  *     hdc    [I] handle to device context
769  *     x      [I] x position
770  *     y      [I] y position
771  *     fStyle [I] drawing flags
772  *
773  * RETURNS
774  *     Success: TRUE
775  *     Failure: FALSE
776  *
777  * NOTES
778  *     Calls ImageList_DrawIndirect.
779  *
780  * SEE
781  *     ImageList_DrawIndirect.
782  */
783
784 BOOL WINAPI
785 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc,
786                 INT x, INT y, UINT fStyle)
787 {
788     IMAGELISTDRAWPARAMS imldp;
789
790     imldp.cbSize  = sizeof(IMAGELISTDRAWPARAMS);
791     imldp.himl    = himl;
792     imldp.i       = i;
793     imldp.hdcDst  = hdc,
794     imldp.x       = x;
795     imldp.y       = y;
796     imldp.cx      = 0;
797     imldp.cy      = 0;
798     imldp.xBitmap = 0;
799     imldp.yBitmap = 0;
800     imldp.rgbBk   = CLR_DEFAULT;
801     imldp.rgbFg   = CLR_DEFAULT;
802     imldp.fStyle  = fStyle;
803     imldp.dwRop   = 0;
804
805     return ImageList_DrawIndirect (&imldp);
806 }
807
808
809 /*************************************************************************
810  * ImageList_DrawEx [COMCTL32.51]
811  *
812  * Draws an image and allows to use extended drawing features.
813  *
814  * PARAMS
815  *     himl   [I] handle to image list
816  *     i      [I] image index
817  *     hdc    [I] handle to device context
818  *     x      [I] X position
819  *     y      [I] Y position
820  *     xOffs  [I] X offset
821  *     yOffs  [I] Y offset
822  *     rgbBk  [I] background color
823  *     rgbFg  [I] foreground color
824  *     fStyle [I] drawing flags
825  *
826  * RETURNS
827  *     Success: TRUE
828  *     Failure: FALSE
829  *
830  * NOTES
831  *     Calls ImageList_DrawIndirect.
832  *
833  * SEE
834  *     ImageList_DrawIndirect.
835  */
836
837 BOOL WINAPI
838 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
839                   INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
840                   UINT fStyle)
841 {
842     IMAGELISTDRAWPARAMS imldp;
843
844     imldp.cbSize  = sizeof(IMAGELISTDRAWPARAMS);
845     imldp.himl    = himl;
846     imldp.i       = i;
847     imldp.hdcDst  = hdc,
848     imldp.x       = x;
849     imldp.y       = y;
850     imldp.cx      = dx;
851     imldp.cy      = dy;
852     imldp.xBitmap = 0;
853     imldp.yBitmap = 0;
854     imldp.rgbBk   = rgbBk;
855     imldp.rgbFg   = rgbFg;
856     imldp.fStyle  = fStyle;
857     imldp.dwRop   = 0;
858
859     return ImageList_DrawIndirect (&imldp);
860 }
861
862
863 /*************************************************************************
864  * ImageList_DrawIndirect [COMCTL32.52] 
865  *
866  * Draws an image using ...
867  *
868  * PARAMS
869  *     pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
870  *
871  * RETURNS
872  *     Success: TRUE
873  *     Failure: FALSE
874  */
875
876 BOOL WINAPI
877 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
878 {
879     HIMAGELIST himlLocal;
880     HDC      hdcImageList, hdcTempImage;
881     HBITMAP  hbmTempImage;
882     HBRUSH   hBrush, hOldBrush;
883     INT      cx, cy;
884     INT      nOvlIdx;
885     COLORREF   clrBlend;
886     BOOL     bImage;       /* draw image ? */
887     BOOL     bImageTrans;  /* draw image transparent ? */
888     BOOL     bMask;        /* draw mask ? */
889     BOOL     bMaskTrans;   /* draw mask transparent ? */
890     BOOL     bBlend25;
891     BOOL     bBlend50;
892
893     if (pimldp == NULL)
894         return FALSE;
895     if (pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS))
896         return FALSE;
897     if (pimldp->himl == NULL)
898         return FALSE;
899     if ((pimldp->i < 0) || (pimldp->i > pimldp->himl->cCurImage))
900         return FALSE;
901
902     himlLocal = pimldp->himl;
903
904     cx = (pimldp->cx == 0) ? himlLocal->cx : pimldp->cx;
905     cy = (pimldp->cy == 0) ? himlLocal->cy : pimldp->cy;
906
907     /* ILD_NORMAL state */
908     bImage      = TRUE;
909     bImageTrans = FALSE;
910     bMask       = FALSE;
911     bMaskTrans  = FALSE;
912     bBlend25    = FALSE;
913     bBlend50    = FALSE;
914     if ((himlLocal->clrBk == CLR_NONE) && (himlLocal->hbmMask))
915     {
916         bImageTrans = TRUE;
917         bMask = TRUE;
918         bMaskTrans = TRUE;
919     }
920     
921     /* ILD_IMAGE state (changes) */
922     if (pimldp->fStyle & ILD_IMAGE)
923     {
924         bMask = FALSE;
925         bImage = TRUE;
926         bImageTrans = FALSE;
927     }
928     
929     /* ILD_MASK state (changes) */
930     if ((pimldp->fStyle & ILD_MASK) && (himlLocal->hbmMask))
931     {
932         bMask  = TRUE;
933         bMaskTrans = FALSE;
934         bImage = FALSE;
935     }
936     if ((pimldp->fStyle & ILD_TRANSPARENT) && (himlLocal->hbmMask))
937     {
938         bMaskTrans = TRUE;
939         bImageTrans = TRUE;
940     }
941     if ((himlLocal->clrBk == CLR_NONE) && (himlLocal->hbmMask))
942         bMaskTrans = TRUE;
943
944     if (pimldp->fStyle & ILD_BLEND50)
945         bBlend50 = TRUE;
946     else if (pimldp->fStyle & ILD_BLEND25)
947         bBlend25 = TRUE;
948
949     hdcImageList = CreateCompatibleDC (0);
950
951     if (bMask)
952     {
953         /* draw the mask */
954         SelectObject (hdcImageList, himlLocal->hbmMask);
955         SetBkColor (hdcImageList, RGB(255, 255, 255));
956         SetTextColor (hdcImageList, RGB(0, 0, 0));
957         BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy,
958                   hdcImageList, himlLocal->cx * pimldp->i, 0,
959                   bMaskTrans ? SRCAND : SRCCOPY);
960     }
961
962     if (bImage)
963     {
964         /* draw the image */
965         SelectObject (hdcImageList, himlLocal->hbmImage);
966
967         if (!bImageTrans)
968         {
969             hBrush = CreateSolidBrush (himlLocal->clrBk);
970             hOldBrush = SelectObject (pimldp->hdcDst, hBrush);
971             PatBlt (pimldp->hdcDst, pimldp->x, pimldp->y,
972                       cx, cy, PATCOPY);
973             DeleteObject (SelectObject (pimldp->hdcDst, hOldBrush));
974         }
975
976         BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy,
977                   hdcImageList, himlLocal->cx * pimldp->i, 0, SRCPAINT);
978
979         if (bBlend25 || bBlend50)
980         {
981             if (pimldp->rgbFg == CLR_DEFAULT)
982                 clrBlend = GetSysColor (COLOR_HIGHLIGHT);
983             else
984                 clrBlend = pimldp->rgbFg;
985
986             hdcTempImage = CreateCompatibleDC (0);
987             hbmTempImage = CreateBitmap (himlLocal->cx, himlLocal->cy,
988                                            1, himlLocal->uBitsPixel, NULL);
989             SelectObject (hdcTempImage, hbmTempImage);
990
991
992             /* mask */
993             SelectObject (hdcTempImage,
994                             bBlend50 ? himlLocal->hbrBlend50 : himlLocal->hbrBlend25);
995             PatBlt (hdcTempImage, 0, 0, himlLocal->cx, himlLocal->cy, PATCOPY);
996
997             SelectObject (hdcImageList, himlLocal->hbmMask);
998             BitBlt (hdcTempImage, 0, 0, himlLocal->cx,
999                       himlLocal->cy, hdcImageList, 
1000                       pimldp->i * himlLocal->cx, 0, SRCPAINT);
1001
1002             BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy,
1003                       hdcTempImage, 0, 0, SRCAND);
1004
1005             /* fill */
1006             hBrush = CreateSolidBrush (clrBlend);
1007             SelectObject (hdcTempImage, hBrush);
1008             PatBlt (hdcTempImage, 0, 0, himlLocal->cx, himlLocal->cy, PATCOPY);
1009             DeleteObject (hBrush);
1010
1011             SelectObject (hdcTempImage,
1012                             bBlend50 ? himlLocal->hbrBlend50 : himlLocal->hbrBlend25);
1013             PatBlt (hdcTempImage, 0, 0, himlLocal->cx, himlLocal->cy, 0x0A0329);
1014
1015             SelectObject (hdcImageList, himlLocal->hbmMask);
1016             BitBlt (hdcTempImage, 0, 0, himlLocal->cx,
1017                       himlLocal->cy, hdcImageList, 
1018                       pimldp->i * himlLocal->cx, 0, SRCPAINT);
1019
1020             BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy,
1021                       hdcTempImage, 0, 0, SRCPAINT);
1022
1023             DeleteObject (hbmTempImage);
1024             DeleteDC (hdcTempImage);
1025         }
1026     }   
1027
1028     /* Draw overlay image */
1029     if (pimldp->fStyle & 0x0700) {
1030         nOvlIdx = (pimldp->fStyle & 0x0700) >> 8;
1031         if ((nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1032             nOvlIdx = pimldp->himl->nOvlIdx[nOvlIdx - 1];
1033             if ((nOvlIdx >= 0) && (nOvlIdx <= pimldp->himl->cCurImage)) {
1034                 if (pimldp->himl->hbmMask) {  
1035                     SelectObject (hdcImageList, pimldp->himl->hbmMask);
1036                     BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy,
1037                               hdcImageList, pimldp->himl->cx * nOvlIdx, 0,
1038                               SRCAND);
1039                 }  
1040                 SelectObject (hdcImageList, pimldp->himl->hbmImage);
1041                 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y,
1042                           cx, cy, hdcImageList,
1043                           pimldp->himl->cx * nOvlIdx, 0, SRCPAINT);
1044             }
1045         }
1046     }
1047
1048     DeleteDC (hdcImageList);
1049   
1050     return TRUE;
1051 }
1052
1053
1054 /*************************************************************************
1055  * ImageList_Duplicate [COMCTL32.53] Duplicates an image list.
1056  *
1057  * PARAMS
1058  *     himlSrc [I] source image list handle
1059  *
1060  * RETURNS
1061  *     Success: Handle of duplicated image list.
1062  *     Failure: NULL
1063  */
1064
1065 HIMAGELIST WINAPI
1066 ImageList_Duplicate (HIMAGELIST himlSrc)
1067 {
1068     HIMAGELIST himlDst;
1069     HDC hdcSrc, hdcDst;
1070
1071     if (himlSrc == NULL) {
1072         ERR("Invalid image list handle!\n");
1073         return NULL;
1074     }
1075
1076     himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1077                                 himlSrc->cInitial, himlSrc->cGrow);
1078
1079     if (himlDst)
1080     {
1081         hdcSrc = CreateCompatibleDC (0);
1082         hdcDst = CreateCompatibleDC (0);
1083         SelectObject (hdcSrc, himlSrc->hbmImage);
1084         SelectObject (hdcDst, himlDst->hbmImage);
1085         BitBlt (hdcDst, 0, 0, himlSrc->cCurImage * himlSrc->cx, himlSrc->cy,
1086                   hdcSrc, 0, 0, SRCCOPY);
1087
1088         if (himlDst->hbmMask)
1089         {
1090             SelectObject (hdcSrc, himlSrc->hbmMask);
1091             SelectObject (hdcDst, himlDst->hbmMask);
1092             BitBlt (hdcDst, 0, 0, himlSrc->cCurImage * himlSrc->cx,
1093                       himlSrc->cy, hdcSrc, 0, 0, SRCCOPY);
1094         }
1095
1096         DeleteDC (hdcDst);
1097         DeleteDC (hdcSrc);
1098     }
1099
1100     return himlDst;
1101 }
1102
1103
1104 /*************************************************************************
1105  * ImageList_EndDrag [COMCTL32.54] Finishes a drag operation.
1106  *
1107  * Finishes a drag operation.
1108  *
1109  * PARAMS
1110  *     no Parameters
1111  *
1112  * RETURNS
1113  *     Success: TRUE
1114  *     Failure: FALSE
1115  *
1116  * BUGS
1117  *     semi-stub.
1118  */
1119
1120 BOOL WINAPI
1121 ImageList_EndDrag (void)
1122 {
1123     FIXME("semi-stub!\n");
1124
1125     if (himlInternalDrag)
1126     {
1127
1128         ImageList_Destroy (himlInternalDrag);
1129         himlInternalDrag = NULL;
1130
1131         nInternalDragHotspotX = 0;
1132         nInternalDragHotspotY = 0;
1133
1134     }
1135
1136     return TRUE;
1137 }
1138
1139
1140 /*************************************************************************
1141  * ImageList_GetBkColor [COMCTL32.55]
1142  *
1143  * Returns the background color of an image list.
1144  *
1145  * PARAMS
1146  *     himl [I] Image list handle.
1147  *
1148  * RETURNS
1149  *     Success: background color
1150  *     Failure: CLR_NONE
1151  */
1152
1153 COLORREF WINAPI
1154 ImageList_GetBkColor (HIMAGELIST himl)
1155 {
1156     if (himl == NULL)
1157         return CLR_NONE;
1158
1159     return himl->clrBk;
1160 }
1161
1162
1163 /*************************************************************************
1164  * ImageList_GetDragImage [COMCTL32.56]
1165  *
1166  * Returns the handle to the internal drag image list.
1167  *
1168  * PARAMS
1169  *     ppt        [O] Pointer to the drag position. Can be NULL.
1170  *     pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1171  *
1172  * RETURNS
1173  *     Success: Handle of the drag image list.
1174  *     Failure: NULL.
1175  *
1176  * BUGS
1177  *     semi-stub.
1178  */
1179
1180 HIMAGELIST WINAPI
1181 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1182 {
1183     FIXME("semi-stub!\n");
1184
1185     if (himlInternalDrag)
1186         return (himlInternalDrag);
1187
1188     return NULL;
1189 }
1190
1191
1192 /*************************************************************************
1193  * ImageList_GetIcon [COMCTL32.57] 
1194  *
1195  * Creates an icon from a masked image of an image list.
1196  *
1197  * PARAMS
1198  *     himl  [I] handle to image list
1199  *     i     [I] image index
1200  *     flags [I] drawing style flags
1201  *
1202  * RETURNS
1203  *     Success: icon handle
1204  *     Failure: NULL
1205  */
1206
1207 HICON WINAPI
1208 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1209 {
1210     ICONINFO ii;
1211     HICON  hIcon;
1212     HDC    hdcSrc, hdcDst;
1213
1214     if ((himl == NULL) || (i < 0) || (i >= himl->cCurImage))
1215         return 0;
1216
1217     hdcSrc = CreateCompatibleDC(0);
1218     hdcDst = CreateCompatibleDC(0);
1219
1220     ii.fIcon = TRUE;
1221     ii.hbmMask  = CreateCompatibleBitmap (hdcDst, himl->cx, himl->cy);
1222
1223     /* draw mask*/
1224     SelectObject (hdcDst, ii.hbmMask);
1225     if (himl->hbmMask) {
1226         SelectObject (hdcSrc, himl->hbmMask);
1227         BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1228                   hdcSrc, i * himl->cx, 0, SRCCOPY);
1229     }
1230     else
1231         PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1232
1233     /* draw image*/
1234     SelectObject (hdcSrc, himl->hbmImage);
1235     ii.hbmColor = CreateCompatibleBitmap (hdcSrc, himl->cx, himl->cy);
1236     SelectObject (hdcDst, ii.hbmColor);
1237     BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1238               hdcSrc, i * himl->cx, 0, SRCCOPY);
1239
1240     hIcon = CreateIconIndirect (&ii);    
1241
1242     DeleteDC (hdcSrc);
1243     DeleteDC (hdcDst);
1244     DeleteObject (ii.hbmMask);
1245     DeleteObject (ii.hbmColor);
1246
1247     return hIcon;
1248 }
1249
1250
1251 /*************************************************************************
1252  * ImageList_GetIconSize [COMCTL32.58]
1253  *
1254  * Retrieves the size of an image in an image list.
1255  *
1256  * PARAMS
1257  *     himl [I] handle to image list
1258  *     cx   [O] pointer to the image width.
1259  *     cy   [O] pointer to the image height.
1260  *
1261  * RETURNS
1262  *     Success: TRUE
1263  *     Failure: FALSE
1264  *
1265  * NOTES
1266  *     All images in an image list have the same size.
1267  */
1268
1269 BOOL WINAPI
1270 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1271 {
1272     if (himl == NULL)
1273         return FALSE;
1274     if ((himl->cx <= 0) || (himl->cy <= 0))
1275         return FALSE;
1276
1277     if (cx)
1278         *cx = himl->cx;
1279     if (cy)
1280         *cy = himl->cy;
1281
1282     return TRUE;
1283 }
1284
1285
1286 /*************************************************************************
1287  * ImageList_GetImageCount [COMCTL32.59]
1288  *
1289  * Returns the number of images in an image list.
1290  *
1291  * PARAMS
1292  *     himl [I] handle to image list
1293  *
1294  * RETURNS
1295  *     Success: Number of images.
1296  *     Failure: 0
1297  */
1298
1299 INT WINAPI
1300 ImageList_GetImageCount (HIMAGELIST himl)
1301 {
1302     if (himl == NULL)
1303         return 0;
1304
1305     return himl->cCurImage;
1306 }
1307
1308
1309 /*************************************************************************
1310  * ImageList_GetImageInfo [COMCTL32.60]
1311  *
1312  * Returns information about an image in an image list.
1313  *
1314  * PARAMS
1315  *     himl       [I] handle to image list
1316  *     i          [I] image index
1317  *     pImageInfo [O] pointer to the image information
1318  *
1319  * RETURNS
1320  *     Success: TRUE
1321  *     Failure: FALSE
1322  */
1323
1324 BOOL WINAPI
1325 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1326 {
1327     if ((himl == NULL) || (pImageInfo == NULL))
1328         return FALSE;
1329     if ((i < 0) || (i >= himl->cCurImage))
1330         return FALSE;
1331
1332     pImageInfo->hbmImage = himl->hbmImage;
1333     pImageInfo->hbmMask  = himl->hbmMask;
1334     
1335     pImageInfo->rcImage.top    = 0;
1336     pImageInfo->rcImage.bottom = himl->cy;
1337     pImageInfo->rcImage.left   = i * himl->cx;
1338     pImageInfo->rcImage.right  = (i+1) * himl->cx;
1339     
1340     return TRUE;
1341 }
1342
1343
1344 /*************************************************************************
1345  * ImageList_GetImageRect [COMCTL32.61] 
1346  *
1347  * Retrieves the rectangle of the specified image in an image list.
1348  *
1349  * PARAMS
1350  *     himl   [I] handle to image list
1351  *     i      [I] image index
1352  *     lpRect [O] pointer to the image rectangle
1353  *
1354  * RETURNS
1355  *    Success: TRUE
1356  *    Failure: FALSE
1357  *
1358  * NOTES
1359  *    This is an UNDOCUMENTED function!!!
1360  */
1361
1362 BOOL WINAPI
1363 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1364 {
1365     if ((himl == NULL) || (lpRect == NULL))
1366         return FALSE;
1367     if ((i < 0) || (i >= himl->cCurImage))
1368         return FALSE;
1369
1370     lpRect->left   = i * himl->cx;
1371     lpRect->top    = 0;
1372     lpRect->right  = lpRect->left + himl->cx;
1373     lpRect->bottom = himl->cy;
1374
1375     return TRUE;
1376 }
1377
1378
1379 /*************************************************************************
1380  * ImageList_LoadImage32A [COMCTL32.63][COMCTL32.62]
1381  *
1382  * Creates an image list from a bitmap, icon or cursor.
1383  *
1384  * PARAMS
1385  *     hi      [I] instance handle
1386  *     lpbmp   [I] name or id of the image
1387  *     cx      [I] width of each image
1388  *     cGrow   [I] number of images to expand
1389  *     clrMask [I] mask color
1390  *     uType   [I] type of image to load
1391  *     uFlags  [I] loading flags
1392  *
1393  * RETURNS
1394  *     Success: handle to the loaded image list
1395  *     Failure: NULL
1396  *
1397  * SEE
1398  *     LoadImage ()
1399  */
1400
1401 HIMAGELIST WINAPI
1402 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx,       INT cGrow,
1403                         COLORREF clrMask, UINT uType,   UINT uFlags)
1404 {
1405     HIMAGELIST himl = NULL;
1406     HANDLE   handle;
1407     INT      nImageCount;
1408
1409     handle = LoadImageA (hi, lpbmp, uType, 0, 0, uFlags);
1410     if (!handle) {
1411         ERR("Error loading image!\n");
1412         return NULL;
1413     }
1414
1415     if (uType == IMAGE_BITMAP) {
1416         BITMAP bmp;
1417         GetObjectA (handle, sizeof(BITMAP), &bmp);
1418
1419         /* To match windows behavior, if cx is set to zero and
1420          the flag DI_DEFAULTSIZE is specified, cx becomes the
1421          system metric value for icons. If the flag is not specified
1422          the function sets the size to the height of the bitmap */
1423         if (cx == 0)
1424         {
1425             if (uFlags & DI_DEFAULTSIZE)
1426                 cx = GetSystemMetrics (SM_CXICON);
1427             else
1428                 cx = bmp.bmHeight;
1429         }
1430
1431         nImageCount = bmp.bmWidth / cx;
1432
1433         himl = ImageList_Create (cx, bmp.bmHeight, ILC_MASK | ILC_COLOR,
1434                                  nImageCount, cGrow);
1435         ImageList_AddMasked (himl, (HBITMAP)handle, clrMask);
1436     }
1437     else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
1438         ICONINFO ii;
1439         BITMAP bmp;
1440
1441         GetIconInfo (handle, &ii);
1442         GetObjectA (ii.hbmColor, sizeof(BITMAP), (LPVOID)&bmp);
1443         himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight, 
1444                                  ILC_MASK | ILC_COLOR, 1, cGrow);
1445         ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
1446         DeleteObject (ii.hbmColor);
1447         DeleteObject (ii.hbmMask);
1448     }
1449
1450     DeleteObject (handle);
1451     
1452     return himl;
1453 }
1454
1455
1456 /*************************************************************************
1457  * ImageList_LoadImage32W [COMCTL32.64]
1458  *
1459  * Creates an image list from a bitmap, icon or cursor.
1460  *
1461  * PARAMS
1462  *     hi      [I] instance handle
1463  *     lpbmp   [I] name or id of the image
1464  *     cx      [I] width of each image
1465  *     cGrow   [I] number of images to expand
1466  *     clrMask [I] mask color
1467  *     uType   [I] type of image to load
1468  *     uFlags  [I] loading flags
1469  *
1470  * RETURNS
1471  *     Success: handle to the loaded image list
1472  *     Failure: NULL
1473  *
1474  * SEE
1475  *     LoadImage ()
1476  */
1477
1478 HIMAGELIST WINAPI
1479 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1480                         COLORREF clrMask, UINT uType,   UINT uFlags)
1481 {
1482     HIMAGELIST himl = NULL;
1483     HANDLE   handle;
1484     INT      nImageCount;
1485
1486     handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
1487     if (!handle) {
1488         ERR("Error loading image!\n");
1489         return NULL;
1490     }
1491
1492     if (uType == IMAGE_BITMAP) {
1493         BITMAP bmp;
1494         GetObjectA (handle, sizeof(BITMAP), &bmp);
1495         nImageCount = bmp.bmWidth / cx;
1496
1497         himl = ImageList_Create (cx, bmp.bmHeight, ILC_MASK | ILC_COLOR,
1498                                  nImageCount, cGrow);
1499         ImageList_AddMasked (himl, (HBITMAP)handle, clrMask);
1500     }
1501     else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
1502         ICONINFO ii;
1503         BITMAP bmp;
1504
1505         GetIconInfo (handle, &ii);
1506         GetObjectA (ii.hbmMask, sizeof(BITMAP), (LPVOID)&bmp);
1507         himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight, 
1508                                  ILC_MASK | ILC_COLOR, 1, cGrow);
1509         ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
1510         DeleteObject (ii.hbmColor);
1511         DeleteObject (ii.hbmMask);
1512     }
1513
1514     DeleteObject (handle);
1515     
1516     return himl;
1517 }
1518
1519
1520 /*************************************************************************
1521  * ImageList_Merge [COMCTL32.65] 
1522  *
1523  * Creates a new image list that contains a merged image from the specified
1524  * images of both source image lists.
1525  *
1526  * PARAMS
1527  *     himl1 [I] handle to first image list
1528  *     i1    [I] first image index
1529  *     himl2 [I] handle to second image list
1530  *     i2    [I] second image index
1531  *     dx    [I] X offset of the second image relative to the first.
1532  *     dy    [I] Y offset of the second image relative to the first.
1533  *
1534  * RETURNS
1535  *     Success: handle of the merged image list.
1536  *     Failure: NULL
1537  */
1538
1539 HIMAGELIST WINAPI
1540 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
1541                  INT dx, INT dy)
1542 {
1543     HIMAGELIST himlDst = NULL;
1544     HDC      hdcSrcImage, hdcDstImage;
1545     INT      cxDst, cyDst;
1546     INT      xOff1, yOff1, xOff2, yOff2;
1547     INT      nX1, nX2;
1548
1549     if ((himl1 == NULL) || (himl2 == NULL))
1550         return NULL;
1551
1552     /* check indices */
1553     if ((i1 < 0) || (i1 >= himl1->cCurImage)) {
1554         ERR("Index 1 out of range! %d\n", i1);
1555         return NULL;
1556     }
1557
1558     if ((i2 < 0) || (i2 >= himl2->cCurImage)) {
1559         ERR("Index 2 out of range! %d\n", i2);
1560         return NULL;
1561     }
1562
1563     if (dx > 0) {
1564         cxDst = _MAX (himl1->cx, dx + himl2->cx);
1565         xOff1 = 0;
1566         xOff2 = dx;
1567     }
1568     else if (dx < 0) {
1569         cxDst = _MAX (himl2->cx, himl1->cx - dx);
1570         xOff1 = -dx;
1571         xOff2 = 0;
1572     }
1573     else {
1574         cxDst = _MAX (himl1->cx, himl2->cx);
1575         xOff1 = 0;
1576         xOff2 = 0;
1577     }
1578
1579     if (dy > 0) {
1580         cyDst = _MAX (himl1->cy, dy + himl2->cy);
1581         yOff1 = 0;
1582         yOff2 = dy;
1583     }
1584     else if (dy < 0) {
1585         cyDst = _MAX (himl2->cy, himl1->cy - dy);
1586         yOff1 = -dy;
1587         yOff2 = 0;
1588     }
1589     else {
1590         cyDst = _MAX (himl1->cy, himl2->cy);
1591         yOff1 = 0;
1592         yOff2 = 0;
1593     }
1594
1595     himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | ILC_COLOR, 1, 1);
1596
1597     if (himlDst) {
1598         hdcSrcImage = CreateCompatibleDC (0);
1599         hdcDstImage = CreateCompatibleDC (0);
1600         nX1 = i1 * himl1->cx;
1601         nX2 = i2 * himl2->cx;
1602         
1603         /* copy image */
1604         SelectObject (hdcSrcImage, himl1->hbmImage);
1605         SelectObject (hdcDstImage, himlDst->hbmImage);
1606         BitBlt (hdcDstImage, 0, 0, cxDst, cyDst, 
1607                   hdcSrcImage, 0, 0, BLACKNESS);
1608         BitBlt (hdcDstImage, xOff1, yOff1, himl1->cx, himl1->cy, 
1609                   hdcSrcImage, nX1, 0, SRCCOPY);
1610
1611         SelectObject (hdcSrcImage, himl2->hbmMask);
1612         BitBlt (hdcDstImage, xOff2, yOff2, himl2->cx, himl2->cy, 
1613                   hdcSrcImage, nX2, 0, SRCAND);
1614
1615         SelectObject (hdcSrcImage, himl2->hbmImage);
1616         BitBlt (hdcDstImage, xOff2, yOff2, himl2->cx, himl2->cy, 
1617                   hdcSrcImage, nX2, 0, SRCPAINT);
1618
1619         /* copy mask */
1620         SelectObject (hdcSrcImage, himl1->hbmMask);
1621         SelectObject (hdcDstImage, himlDst->hbmMask);
1622         BitBlt (hdcDstImage, 0, 0, cxDst, cyDst, 
1623                   hdcSrcImage, 0, 0, WHITENESS);
1624         BitBlt (hdcDstImage, xOff1, yOff1, himl1->cx, himl1->cy, 
1625                   hdcSrcImage, nX1, 0, SRCCOPY);
1626
1627         SelectObject (hdcSrcImage, himl2->hbmMask);
1628         BitBlt (hdcDstImage, xOff2, yOff2, himl2->cx, himl2->cy, 
1629                   hdcSrcImage, nX2, 0, SRCAND);
1630
1631         DeleteDC (hdcSrcImage);
1632         DeleteDC (hdcDstImage);
1633     }
1634    
1635     return himlDst;
1636 }
1637
1638
1639 /*************************************************************************
1640  * ImageList_Read [COMCTL32.66]
1641  *
1642  * Reads an image list from a stream.
1643  *
1644  * PARAMS
1645  *     pstm [I] pointer to a stream
1646  *
1647  * RETURNS
1648  *     Success: handle to image list
1649  *     Failure: NULL
1650  *
1651  * NOTES
1652  *     This function can not be implemented yet, because
1653  *     IStream32::Read is not implemented yet.
1654  *
1655  * BUGS
1656  *     empty stub.
1657  */
1658
1659 HIMAGELIST WINAPI ImageList_Read (LPSTREAM pstm)
1660 {
1661     FIXME("empty stub!\n");
1662
1663
1664     return NULL;
1665 }
1666
1667
1668 /*************************************************************************
1669  * ImageList_Remove [COMCTL32.67] Removes an image from an image list
1670  *
1671  * PARAMS
1672  *     himl [I] image list handle
1673  *     i    [I] image index
1674  *
1675  * RETURNS
1676  *     Success: TRUE
1677  *     Failure: FALSE
1678  */
1679
1680 BOOL WINAPI
1681 ImageList_Remove (HIMAGELIST himl, INT i)
1682 {
1683     HBITMAP hbmNewImage, hbmNewMask;
1684     HDC     hdcSrc, hdcDst;
1685     INT     cxNew, nCount;
1686
1687     if ((i < -1) || (i >= himl->cCurImage)) {
1688         ERR("index out of range! %d\n", i);
1689         return FALSE;
1690     }
1691
1692     if (himl->cCurImage == 0) {
1693         ERR("image list is already empty!\n");
1694         return FALSE;
1695     }
1696
1697     if (i == -1) {
1698         /* remove all */
1699         TRACE("remove all!\n");
1700
1701         himl->cMaxImage = himl->cInitial + himl->cGrow;
1702         himl->cCurImage = 0;
1703         for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
1704              himl->nOvlIdx[nCount] = -1;
1705
1706         DeleteObject (himl->hbmImage);
1707         himl->hbmImage =
1708             CreateBitmap (himl->cMaxImage * himl->cx, himl->cy,
1709                             1, himl->uBitsPixel, NULL);
1710
1711         if (himl->hbmMask) {
1712             DeleteObject (himl->hbmMask);
1713             himl->hbmMask =
1714                 CreateBitmap (himl->cMaxImage * himl->cx, himl->cy,
1715                                 1, 1, NULL);
1716         }
1717     }
1718     else {
1719         /* delete one image */
1720         TRACE("Remove single image! %d\n", i);
1721
1722         /* create new bitmap(s) */
1723         cxNew = (himl->cCurImage + himl->cGrow - 1) * himl->cx;
1724
1725         TRACE(" - Number of images: %d / %d (Old/New)\n",
1726                  himl->cCurImage, himl->cCurImage - 1);
1727         TRACE(" - Max. number of images: %d / %d (Old/New)\n",
1728                  himl->cMaxImage, himl->cCurImage + himl->cGrow - 1);
1729         
1730         hbmNewImage =
1731             CreateBitmap (cxNew, himl->cy, 1, himl->uBitsPixel, NULL);
1732
1733         if (himl->hbmMask)
1734             hbmNewMask = CreateBitmap (cxNew, himl->cy, 1, 1, NULL);
1735         else
1736             hbmNewMask = 0;  /* Just to keep compiler happy! */
1737
1738         hdcSrc = CreateCompatibleDC (0);
1739         hdcDst = CreateCompatibleDC (0);
1740
1741         /* copy all images and masks prior to the "removed" image */
1742         if (i > 0) {
1743             TRACE("Pre image copy: Copy %d images\n", i);
1744        
1745             SelectObject (hdcSrc, himl->hbmImage);
1746             SelectObject (hdcDst, hbmNewImage);
1747             BitBlt (hdcDst, 0, 0, i * himl->cx, himl->cy,
1748                       hdcSrc, 0, 0, SRCCOPY);
1749
1750             if (himl->hbmMask) {
1751                 SelectObject (hdcSrc, himl->hbmMask);
1752                 SelectObject (hdcDst, hbmNewMask);
1753                 BitBlt (hdcDst, 0, 0, i * himl->cx, himl->cy,
1754                           hdcSrc, 0, 0, SRCCOPY);
1755             }
1756         }
1757
1758         /* copy all images and masks behind the removed image */
1759         if (i < himl->cCurImage - 1) {
1760             TRACE("Post image copy!\n");
1761             SelectObject (hdcSrc, himl->hbmImage);
1762             SelectObject (hdcDst, hbmNewImage);
1763             BitBlt (hdcDst, i * himl->cx, 0, (himl->cCurImage - i - 1) * himl->cx,
1764                       himl->cy, hdcSrc, (i + 1) * himl->cx, 0, SRCCOPY);
1765
1766             if (himl->hbmMask) {
1767                 SelectObject (hdcSrc, himl->hbmMask);
1768                 SelectObject (hdcDst, hbmNewMask);
1769                 BitBlt (hdcDst, i * himl->cx, 0,
1770                           (himl->cCurImage - i - 1) * himl->cx,
1771                           himl->cy, hdcSrc, (i + 1) * himl->cx, 0, SRCCOPY);
1772             }
1773         }
1774
1775         DeleteDC (hdcSrc);
1776         DeleteDC (hdcDst);
1777
1778         /* delete old images and insert new ones */
1779         DeleteObject (himl->hbmImage);
1780         himl->hbmImage = hbmNewImage;
1781         if (himl->hbmMask) {
1782             DeleteObject (himl->hbmMask);
1783             himl->hbmMask = hbmNewMask;
1784         }
1785
1786         himl->cCurImage--;
1787         himl->cMaxImage = himl->cCurImage + himl->cGrow;
1788     }
1789
1790     return TRUE;
1791 }
1792
1793
1794 /*************************************************************************
1795  * ImageList_Replace [COMCTL32.68] 
1796  *
1797  * Replaces an image in an image list with a new image.
1798  *
1799  * PARAMS
1800  *     himl     [I] handle to image list
1801  *     i        [I] image index
1802  *     hbmImage [I] handle to image bitmap
1803  *     hbmMask  [I] handle to mask bitmap. Can be NULL.
1804  *
1805  * RETURNS
1806  *     Success: TRUE
1807  *     Failure: FALSE
1808  */
1809
1810 BOOL WINAPI
1811 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
1812                    HBITMAP hbmMask)
1813 {
1814     HDC hdcImageList, hdcImage;
1815     BITMAP bmp;
1816
1817     if (himl == NULL) {
1818         ERR("Invalid image list handle!\n");
1819         return FALSE;
1820     }
1821     
1822     if ((i >= himl->cCurImage) || (i < 0)) {
1823         ERR("Invalid image index!\n");
1824         return FALSE;
1825     }
1826
1827     hdcImageList = CreateCompatibleDC (0);
1828     hdcImage = CreateCompatibleDC (0);
1829     GetObjectA (hbmImage, sizeof(BITMAP), (LPVOID)&bmp);
1830
1831     /* Replace Image */
1832     SelectObject (hdcImageList, himl->hbmImage);
1833     SelectObject (hdcImage, hbmImage);
1834
1835     StretchBlt (hdcImageList, i * himl->cx, 0, himl->cx, himl->cy,
1836                   hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
1837
1838     if (himl->hbmMask)
1839     {
1840         /* Replace Mask */
1841         SelectObject (hdcImageList, himl->hbmMask);
1842         SelectObject (hdcImage, hbmMask);
1843
1844         StretchBlt (hdcImageList, i * himl->cx, 0, himl->cx, himl->cy,
1845                       hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
1846     }
1847
1848     DeleteDC (hdcImage);
1849     DeleteDC (hdcImageList);
1850
1851     return TRUE;
1852 }
1853
1854
1855 /*************************************************************************
1856  * ImageList_ReplaceIcon [COMCTL32.69]
1857  *
1858  * Replaces an image in an image list using an icon.
1859  *
1860  * PARAMS
1861  *     himl  [I] handle to image list
1862  *     i     [I] image index
1863  *     hIcon [I] handle to icon
1864  *
1865  * RETURNS
1866  *     Success: index of the replaced image
1867  *     Failure: -1
1868  */
1869
1870 INT WINAPI
1871 ImageList_ReplaceIcon (HIMAGELIST himl, INT i, HICON hIcon)
1872 {
1873     HDC     hdcImageList, hdcImage;
1874     INT     nIndex;
1875     HBITMAP hbmOldSrc, hbmOldDst;
1876     ICONINFO  ii;
1877     BITMAP  bmp;
1878
1879     TRACE("(0x%lx 0x%x 0x%x)\n", (DWORD)himl, i, hIcon);
1880
1881     if (himl == NULL)
1882         return -1;
1883     if ((i >= himl->cCurImage) || (i < -1))
1884         return -1;
1885
1886     GetIconInfo (hIcon, &ii);
1887     if (ii.hbmMask == 0)
1888         ERR("no mask!\n");
1889     if (ii.hbmColor == 0)
1890         ERR("no color!\n");
1891     GetObjectA (ii.hbmMask, sizeof(BITMAP), (LPVOID)&bmp);
1892
1893     if (i == -1) {
1894         if (himl->cCurImage + 1 >= himl->cMaxImage)
1895             IMAGELIST_InternalExpandBitmaps (himl, 1);
1896
1897         nIndex = himl->cCurImage;
1898         himl->cCurImage++;
1899     }
1900     else
1901         nIndex = i;
1902
1903     hdcImageList = CreateCompatibleDC (0);
1904     TRACE("hdcImageList=0x%x!\n", hdcImageList);
1905     if (hdcImageList == 0)
1906         ERR("invalid hdcImageList!\n");
1907
1908     hdcImage = CreateCompatibleDC (0);
1909     TRACE("hdcImage=0x%x!\n", hdcImage);
1910     if (hdcImage == 0)
1911         ERR("invalid hdcImage!\n");
1912
1913     hbmOldDst = SelectObject (hdcImageList, himl->hbmImage);
1914     SetTextColor( hdcImageList, RGB(0,0,0));
1915     SetBkColor( hdcImageList, RGB(255,255,255));
1916     hbmOldSrc = SelectObject (hdcImage, ii.hbmColor);
1917     StretchBlt (hdcImageList, nIndex * himl->cx, 0, himl->cx, himl->cy,
1918                   hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
1919
1920     if (himl->hbmMask) {
1921         SelectObject (hdcImageList, himl->hbmMask);
1922         SelectObject (hdcImage, ii.hbmMask);
1923         StretchBlt (hdcImageList, nIndex * himl->cx, 0, himl->cx, himl->cy,
1924                       hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
1925     }
1926
1927     SelectObject (hdcImage, hbmOldSrc);
1928     SelectObject (hdcImageList, hbmOldDst);
1929
1930     if (hdcImageList)
1931         DeleteDC (hdcImageList);
1932     if (hdcImage)
1933         DeleteDC (hdcImage);
1934     if (ii.hbmColor)
1935         DeleteObject (ii.hbmColor);
1936     if (ii.hbmMask)
1937         DeleteObject (ii.hbmMask);
1938
1939     return nIndex;
1940 }
1941
1942
1943 /*************************************************************************
1944  * ImageList_SetBkColor [COMCTL32.70] 
1945  *
1946  * Sets the background color of an image list.
1947  *
1948  * PARAMS
1949  *     himl  [I] handle to image list
1950  *     clrBk [I] background color
1951  *
1952  * RETURNS
1953  *     Success: previous background color
1954  *     Failure: CLR_NONE
1955  */
1956
1957 COLORREF WINAPI
1958 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
1959 {
1960     COLORREF clrOldBk;
1961
1962     if (himl == NULL)
1963         return CLR_NONE;
1964
1965     clrOldBk = himl->clrBk;
1966     himl->clrBk = clrBk;
1967     return clrOldBk;
1968 }
1969
1970
1971 /*************************************************************************
1972  * ImageList_SetDragCursorImage [COMCTL32.75]
1973  *
1974  * Combines the specified image with the current drag image
1975  *
1976  * PARAMS
1977  *     himlDrag  [I] handle to drag image list
1978  *     iDrag     [I] drag image index
1979  *     dxHotspot [I] X position of the hot spot
1980  *     dyHotspot [I] Y position of the hot spot
1981  *
1982  * RETURNS
1983  *     Success: TRUE
1984  *     Failure: FALSE
1985  *
1986  * BUGS
1987  *     semi-stub.
1988  */
1989
1990 BOOL WINAPI
1991 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
1992                               INT dxHotspot, INT dyHotspot)
1993 {
1994     HIMAGELIST himlTemp;
1995
1996     FIXME("semi-stub!\n");
1997
1998     if (himlInternalDrag == NULL)
1999         return FALSE;
2000
2001     TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2002            dxHotspot, dyHotspot, nInternalDragHotspotX, nInternalDragHotspotY);
2003
2004     himlTemp = ImageList_Merge (himlInternalDrag, 0, himlDrag, iDrag,
2005                                 dxHotspot, dyHotspot);
2006
2007     ImageList_Destroy (himlInternalDrag);
2008     himlInternalDrag = himlTemp;
2009
2010     nInternalDragHotspotX = dxHotspot;
2011     nInternalDragHotspotY = dyHotspot;
2012
2013     return FALSE;
2014 }
2015
2016
2017 /*************************************************************************
2018  * ImageList_SetFilter [COMCTL32.76] 
2019  *
2020  * Sets a filter (or does something completely different)!!???
2021  *
2022  * PARAMS
2023  *     himl     [I] handle to image list
2024  *     i        [I] ???
2025  *     dwFilter [I] ???
2026  *
2027  * RETURNS
2028  *     Success: TRUE ???
2029  *     Failure: FALSE ???
2030  *
2031  * BUGS
2032  *     This is an UNDOCUMENTED function!!!!
2033  *     empty stub.
2034  */
2035
2036 BOOL WINAPI
2037 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2038 {
2039     FIXME("(%p 0x%x 0x%lx):empty stub!\n",
2040            himl, i, dwFilter);
2041
2042     return FALSE;
2043 }
2044
2045
2046 /*************************************************************************
2047  * ImageList_SetIconSize [COMCTL32.77]
2048  *
2049  * Sets the image size of the bitmap and deletes all images.
2050  *
2051  * PARAMS
2052  *     himl [I] handle to image list
2053  *     cx   [I] image width
2054  *     cy   [I] image height
2055  *
2056  * RETURNS
2057  *     Success: TRUE
2058  *     Failure: FALSE
2059  */
2060
2061 BOOL WINAPI
2062 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2063 {
2064     INT nCount;
2065
2066     if (!himl)
2067         return FALSE;
2068
2069     /* remove all images*/
2070     himl->cMaxImage = himl->cInitial + himl->cGrow;
2071     himl->cCurImage = 0;
2072     himl->cx        = cx;
2073     himl->cy        = cy;
2074
2075     /* initialize overlay mask indices */
2076     for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2077         himl->nOvlIdx[nCount] = -1;
2078
2079     DeleteObject (himl->hbmImage);
2080     himl->hbmImage =
2081         CreateBitmap (himl->cMaxImage * himl->cx, himl->cy,
2082                         1, himl->uBitsPixel, NULL);
2083
2084     if (himl->hbmMask) {
2085         DeleteObject (himl->hbmMask);
2086         himl->hbmMask =
2087             CreateBitmap (himl->cMaxImage * himl->cx, himl->cy,
2088                             1, 1, NULL);
2089     }
2090
2091     return TRUE;
2092 }
2093
2094
2095 /*************************************************************************
2096  * ImageList_SetImageCount [COMCTL32.78]
2097  *
2098  * Resizes an image list to the specified number of images.
2099  *
2100  * PARAMS
2101  *     himl        [I] handle to image list
2102  *     iImageCount [I] number of images in the image list
2103  *
2104  * RETURNS
2105  *     Success: TRUE
2106  *     Failure: FALSE
2107  */
2108
2109 BOOL WINAPI
2110 ImageList_SetImageCount (HIMAGELIST himl, INT iImageCount)
2111 {
2112     HDC     hdcImageList, hdcBitmap;
2113     HBITMAP hbmNewBitmap;
2114     INT     nNewCount, nCopyCount;
2115
2116     if (!himl)
2117         return FALSE;
2118     if (himl->cCurImage >= iImageCount)
2119         return FALSE;
2120     if (himl->cMaxImage > iImageCount)
2121         return TRUE;
2122
2123     nNewCount = iImageCount + himl->cGrow;
2124     nCopyCount = _MIN(himl->cCurImage, iImageCount);
2125
2126     hdcImageList = CreateCompatibleDC (0);
2127     hdcBitmap = CreateCompatibleDC (0);
2128
2129     hbmNewBitmap = CreateBitmap (nNewCount * himl->cx, himl->cy,
2130                                    1, himl->uBitsPixel, NULL);
2131     if (hbmNewBitmap != 0)
2132     {
2133         SelectObject (hdcImageList, himl->hbmImage);
2134         SelectObject (hdcBitmap, hbmNewBitmap);
2135
2136         /* copy images */
2137         BitBlt (hdcBitmap, 0, 0, nCopyCount * himl->cx, himl->cy,
2138                   hdcImageList, 0, 0, SRCCOPY);
2139
2140         /* delete 'empty' image space */
2141         SetBkColor (hdcBitmap, RGB(255, 255, 255));
2142         SetTextColor (hdcBitmap, RGB(0, 0, 0));
2143         PatBlt (hdcBitmap,  nCopyCount * himl->cx, 0, 
2144                   (nNewCount - nCopyCount) * himl->cx, himl->cy, BLACKNESS);
2145
2146         DeleteObject (himl->hbmImage);
2147         himl->hbmImage = hbmNewBitmap;
2148     }
2149     else
2150         ERR("Could not create new image bitmap !\n");
2151
2152     if (himl->hbmMask)
2153     {
2154         hbmNewBitmap = CreateBitmap (nNewCount * himl->cx, himl->cy,
2155                                        1, 1, NULL);
2156         if (hbmNewBitmap != 0)
2157         {
2158             SelectObject (hdcImageList, himl->hbmMask);
2159             SelectObject (hdcBitmap, hbmNewBitmap);
2160
2161             /* copy images */
2162             BitBlt (hdcBitmap, 0, 0, nCopyCount * himl->cx, himl->cy,
2163                       hdcImageList, 0, 0, SRCCOPY);
2164
2165             /* delete 'empty' image space */
2166             SetBkColor (hdcBitmap, RGB(255, 255, 255));
2167             SetTextColor (hdcBitmap, RGB(0, 0, 0));
2168             PatBlt (hdcBitmap,  nCopyCount * himl->cx, 0, 
2169                       (nNewCount - nCopyCount) * himl->cx, himl->cy, BLACKNESS);
2170
2171             DeleteObject (himl->hbmMask);
2172             himl->hbmMask = hbmNewBitmap;
2173         }
2174         else
2175             ERR("Could not create new mask bitmap!\n");
2176     }
2177
2178     DeleteDC (hdcImageList);
2179     DeleteDC (hdcBitmap);
2180
2181     /* Update max image count and current image count */
2182     himl->cMaxImage = nNewCount;
2183     if (himl->cCurImage > nCopyCount)
2184         himl->cCurImage = nCopyCount;
2185
2186     return TRUE;
2187 }
2188
2189
2190 /*************************************************************************
2191  * ImageList_SetOverlayImage [COMCTL32.79]
2192  *
2193  * Assigns an overlay mask index to an existing image in an image list.
2194  *
2195  * PARAMS
2196  *     himl     [I] handle to image list
2197  *     iImage   [I] image index
2198  *     iOverlay [I] overlay mask index
2199  *
2200  * RETURNS
2201  *     Success: TRUE
2202  *     Failure: FALSE
2203  */
2204
2205 BOOL WINAPI
2206 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2207 {
2208     if (!himl)
2209         return FALSE;
2210     if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2211         return FALSE;
2212     if ((iImage < 0) || (iImage > himl->cCurImage))
2213         return FALSE;
2214     
2215     himl->nOvlIdx[iOverlay - 1] = iImage;
2216     return TRUE;
2217 }
2218
2219
2220 /*************************************************************************
2221  * ImageList_Write [COMCTL32.80]
2222  *
2223  * Writes an image list to a stream.
2224  *
2225  * PARAMS
2226  *     himl [I] handle to image list
2227  *     pstm [O] Pointer to a stream.
2228  *
2229  * RETURNS
2230  *     Success: TRUE
2231  *     Failure: FALSE
2232  *
2233  * NOTES
2234  *     This function can not be implemented yet, because
2235  *     IStream32::Write is not implemented.
2236  *
2237  * BUGS
2238  *     empty stub.
2239  */
2240
2241 BOOL WINAPI
2242 ImageList_Write (HIMAGELIST himl, LPSTREAM pstm)
2243 {
2244     if (!himl)
2245         return FALSE;
2246
2247     FIXME("empty stub!\n");
2248
2249     return FALSE;
2250 }
2251