user32: Data chunks in RIFF file must be word-aligned.
[wine] / dlls / user32 / cursoricon.c
1 /*
2  * Cursor and icon support
3  *
4  * Copyright 1995 Alexandre Julliard
5  *           1996 Martin Von Loewis
6  *           1997 Alex Korobka
7  *           1998 Turchanov Sergey
8  *           2007 Henri Verbeet
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 /*
26  * Theory:
27  *
28  * Cursors and icons are stored in a global heap block, with the
29  * following layout:
30  *
31  * CURSORICONINFO info;
32  * BYTE[]         ANDbits;
33  * BYTE[]         XORbits;
34  *
35  * The bits structures are in the format of a device-dependent bitmap.
36  *
37  * This layout is very sub-optimal, as the bitmap bits are stored in
38  * the X client instead of in the server like other bitmaps; however,
39  * some programs (notably Paint Brush) expect to be able to manipulate
40  * the bits directly :-(
41  */
42
43 #include "config.h"
44 #include "wine/port.h"
45
46 #include <stdarg.h>
47 #include <string.h>
48 #include <stdlib.h>
49
50 #include "windef.h"
51 #include "winbase.h"
52 #include "wingdi.h"
53 #include "winerror.h"
54 #include "wine/winbase16.h"
55 #include "wine/winuser16.h"
56 #include "wine/exception.h"
57 #include "wine/debug.h"
58 #include "user_private.h"
59
60 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
61 WINE_DECLARE_DEBUG_CHANNEL(icon);
62 WINE_DECLARE_DEBUG_CHANNEL(resource);
63
64 #include "pshpack1.h"
65
66 typedef struct {
67     BYTE bWidth;
68     BYTE bHeight;
69     BYTE bColorCount;
70     BYTE bReserved;
71     WORD xHotspot;
72     WORD yHotspot;
73     DWORD dwDIBSize;
74     DWORD dwDIBOffset;
75 } CURSORICONFILEDIRENTRY;
76
77 typedef struct
78 {
79     WORD                idReserved;
80     WORD                idType;
81     WORD                idCount;
82     CURSORICONFILEDIRENTRY  idEntries[1];
83 } CURSORICONFILEDIR;
84
85 #include "poppack.h"
86
87 #define CID_RESOURCE  0x0001
88 #define CID_WIN32     0x0004
89 #define CID_NONSHARED 0x0008
90
91 static RECT CURSOR_ClipRect;       /* Cursor clipping rect */
92
93 static HDC screen_dc;
94
95 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
96
97 /**********************************************************************
98  * ICONCACHE for cursors/icons loaded with LR_SHARED.
99  *
100  * FIXME: This should not be allocated on the system heap, but on a
101  *        subsystem-global heap (i.e. one for all Win16 processes,
102  *        and one for each Win32 process).
103  */
104 typedef struct tagICONCACHE
105 {
106     struct tagICONCACHE *next;
107
108     HMODULE              hModule;
109     HRSRC                hRsrc;
110     HRSRC                hGroupRsrc;
111     HICON                hIcon;
112
113     INT                  count;
114
115 } ICONCACHE;
116
117 static ICONCACHE *IconAnchor = NULL;
118
119 static CRITICAL_SECTION IconCrst;
120 static CRITICAL_SECTION_DEBUG critsect_debug =
121 {
122     0, 0, &IconCrst,
123     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
124       0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
125 };
126 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
127
128 static const WORD ICON_HOTSPOT = 0x4242;
129
130
131 /***********************************************************************
132  *             map_fileW
133  *
134  * Helper function to map a file to memory:
135  *  name                        -       file name
136  *  [RETURN] ptr                -       pointer to mapped file
137  *  [RETURN] filesize           -       pointer size of file to be stored if not NULL
138  */
139 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
140 {
141     HANDLE hFile, hMapping;
142     LPVOID ptr = NULL;
143
144     hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
145                          OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
146     if (hFile != INVALID_HANDLE_VALUE)
147     {
148         hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
149         if (hMapping)
150         {
151             ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
152             CloseHandle( hMapping );
153             if (filesize)
154                 *filesize = GetFileSize( hFile, NULL );
155         }
156         CloseHandle( hFile );
157     }
158     return ptr;
159 }
160
161
162 /***********************************************************************
163  *           get_bitmap_width_bytes
164  *
165  * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
166  * data.
167  */
168 static int get_bitmap_width_bytes( int width, int bpp )
169 {
170     switch(bpp)
171     {
172     case 1:
173         return 2 * ((width+15) / 16);
174     case 4:
175         return 2 * ((width+3) / 4);
176     case 24:
177         width *= 3;
178         /* fall through */
179     case 8:
180         return width + (width & 1);
181     case 16:
182     case 15:
183         return width * 2;
184     case 32:
185         return width * 4;
186     default:
187         WARN("Unknown depth %d, please report.\n", bpp );
188     }
189     return -1;
190 }
191
192
193 /***********************************************************************
194  *          get_dib_width_bytes
195  *
196  * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
197  */
198 static int get_dib_width_bytes( int width, int depth )
199 {
200     int words;
201
202     switch(depth)
203     {
204     case 1:  words = (width + 31) / 32; break;
205     case 4:  words = (width + 7) / 8; break;
206     case 8:  words = (width + 3) / 4; break;
207     case 15:
208     case 16: words = (width + 1) / 2; break;
209     case 24: words = (width * 3 + 3)/4; break;
210     default:
211         WARN("(%d): Unsupported depth\n", depth );
212         /* fall through */
213     case 32:
214         words = width;
215     }
216     return 4 * words;
217 }
218
219
220 /***********************************************************************
221  *           bitmap_info_size
222  *
223  * Return the size of the bitmap info structure including color table.
224  */
225 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
226 {
227     int colors, masks = 0;
228
229     if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
230     {
231         const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
232         colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
233         return sizeof(BITMAPCOREHEADER) + colors *
234              ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
235     }
236     else  /* assume BITMAPINFOHEADER */
237     {
238         colors = info->bmiHeader.biClrUsed;
239         if (colors > 256) /* buffer overflow otherwise */
240                 colors = 256;
241         if (!colors && (info->bmiHeader.biBitCount <= 8))
242             colors = 1 << info->bmiHeader.biBitCount;
243         if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
244         return sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) + colors *
245                ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
246     }
247 }
248
249
250 /***********************************************************************
251  *          is_dib_monochrome
252  *
253  * Returns whether a DIB can be converted to a monochrome DDB.
254  *
255  * A DIB can be converted if its color table contains only black and
256  * white. Black must be the first color in the color table.
257  *
258  * Note : If the first color in the color table is white followed by
259  *        black, we can't convert it to a monochrome DDB with
260  *        SetDIBits, because black and white would be inverted.
261  */
262 static BOOL is_dib_monochrome( const BITMAPINFO* info )
263 {
264     if (info->bmiHeader.biBitCount != 1) return FALSE;
265
266     if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
267     {
268         const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
269
270         /* Check if the first color is black */
271         if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
272         {
273             rgb++;
274
275             /* Check if the second color is white */
276             return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
277                  && (rgb->rgbtBlue == 0xff));
278         }
279         else return FALSE;
280     }
281     else  /* assume BITMAPINFOHEADER */
282     {
283         const RGBQUAD *rgb = info->bmiColors;
284
285         /* Check if the first color is black */
286         if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
287             (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
288         {
289             rgb++;
290
291             /* Check if the second color is white */
292             return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
293                  && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
294         }
295         else return FALSE;
296     }
297 }
298
299 /***********************************************************************
300  *           DIB_GetBitmapInfo
301  *
302  * Get the info from a bitmap header.
303  * Return 1 for INFOHEADER, 0 for COREHEADER,
304  * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
305  */
306 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
307                               LONG *height, WORD *bpp, DWORD *compr )
308 {
309     if (header->biSize == sizeof(BITMAPINFOHEADER))
310     {
311         *width  = header->biWidth;
312         *height = header->biHeight;
313         *bpp    = header->biBitCount;
314         *compr  = header->biCompression;
315         return 1;
316     }
317     if (header->biSize == sizeof(BITMAPCOREHEADER))
318     {
319         const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
320         *width  = core->bcWidth;
321         *height = core->bcHeight;
322         *bpp    = core->bcBitCount;
323         *compr  = 0;
324         return 0;
325     }
326     if (header->biSize == sizeof(BITMAPV4HEADER))
327     {
328         const BITMAPV4HEADER *v4hdr = (const BITMAPV4HEADER *)header;
329         *width  = v4hdr->bV4Width;
330         *height = v4hdr->bV4Height;
331         *bpp    = v4hdr->bV4BitCount;
332         *compr  = v4hdr->bV4V4Compression;
333         return 4;
334     }
335     if (header->biSize == sizeof(BITMAPV5HEADER))
336     {
337         const BITMAPV5HEADER *v5hdr = (const BITMAPV5HEADER *)header;
338         *width  = v5hdr->bV5Width;
339         *height = v5hdr->bV5Height;
340         *bpp    = v5hdr->bV5BitCount;
341         *compr  = v5hdr->bV5Compression;
342         return 5;
343     }
344     ERR("(%d): unknown/wrong size for header\n", header->biSize );
345     return -1;
346 }
347
348 /**********************************************************************
349  *          CURSORICON_FindSharedIcon
350  */
351 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
352 {
353     HICON hIcon = 0;
354     ICONCACHE *ptr;
355
356     EnterCriticalSection( &IconCrst );
357
358     for ( ptr = IconAnchor; ptr; ptr = ptr->next )
359         if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
360         {
361             ptr->count++;
362             hIcon = ptr->hIcon;
363             break;
364         }
365
366     LeaveCriticalSection( &IconCrst );
367
368     return hIcon;
369 }
370
371 /*************************************************************************
372  * CURSORICON_FindCache
373  *
374  * Given a handle, find the corresponding cache element
375  *
376  * PARAMS
377  *      Handle     [I] handle to an Image
378  *
379  * RETURNS
380  *     Success: The cache entry
381  *     Failure: NULL
382  *
383  */
384 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
385 {
386     ICONCACHE *ptr;
387     ICONCACHE *pRet=NULL;
388     BOOL IsFound = FALSE;
389
390     EnterCriticalSection( &IconCrst );
391
392     for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
393     {
394         if ( hIcon == ptr->hIcon )
395         {
396             IsFound = TRUE;
397             pRet = ptr;
398         }
399     }
400
401     LeaveCriticalSection( &IconCrst );
402
403     return pRet;
404 }
405
406 /**********************************************************************
407  *          CURSORICON_AddSharedIcon
408  */
409 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
410 {
411     ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
412     if ( !ptr ) return;
413
414     ptr->hModule = hModule;
415     ptr->hRsrc   = hRsrc;
416     ptr->hIcon  = hIcon;
417     ptr->hGroupRsrc = hGroupRsrc;
418     ptr->count   = 1;
419
420     EnterCriticalSection( &IconCrst );
421     ptr->next    = IconAnchor;
422     IconAnchor   = ptr;
423     LeaveCriticalSection( &IconCrst );
424 }
425
426 /**********************************************************************
427  *          CURSORICON_DelSharedIcon
428  */
429 static INT CURSORICON_DelSharedIcon( HICON hIcon )
430 {
431     INT count = -1;
432     ICONCACHE *ptr;
433
434     EnterCriticalSection( &IconCrst );
435
436     for ( ptr = IconAnchor; ptr; ptr = ptr->next )
437         if ( ptr->hIcon == hIcon )
438         {
439             if ( ptr->count > 0 ) ptr->count--;
440             count = ptr->count;
441             break;
442         }
443
444     LeaveCriticalSection( &IconCrst );
445
446     return count;
447 }
448
449 /**********************************************************************
450  *          CURSORICON_FreeModuleIcons
451  */
452 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
453 {
454     ICONCACHE **ptr = &IconAnchor;
455     HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
456
457     EnterCriticalSection( &IconCrst );
458
459     while ( *ptr )
460     {
461         if ( (*ptr)->hModule == hModule )
462         {
463             ICONCACHE *freePtr = *ptr;
464             *ptr = freePtr->next;
465
466             GlobalFree16(HICON_16(freePtr->hIcon));
467             HeapFree( GetProcessHeap(), 0, freePtr );
468             continue;
469         }
470         ptr = &(*ptr)->next;
471     }
472
473     LeaveCriticalSection( &IconCrst );
474 }
475
476 /**********************************************************************
477  *              get_icon_size
478  */
479 BOOL get_icon_size( HICON handle, SIZE *size )
480 {
481     CURSORICONINFO *info;
482
483     if (!(info = GlobalLock16( HICON_16(handle) ))) return FALSE;
484     size->cx = info->nWidth;
485     size->cy = info->nHeight;
486     GlobalUnlock16( HICON_16(handle) );
487     return TRUE;
488 }
489
490 /*
491  *  The following macro functions account for the irregularities of
492  *   accessing cursor and icon resources in files and resource entries.
493  */
494 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
495                               int *width, int *height, int *bits );
496
497 /**********************************************************************
498  *          CURSORICON_FindBestIcon
499  *
500  * Find the icon closest to the requested size and number of colors.
501  */
502 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
503                                     int width, int height, int colors )
504 {
505     int i, cx, cy, bits, bestEntry = -1;
506     UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
507     UINT iTempXDiff, iTempYDiff, iTempColorDiff;
508
509     /* Find Best Fit */
510     iTotalDiff = 0xFFFFFFFF;
511     iColorDiff = 0xFFFFFFFF;
512     for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
513     {
514         iTempXDiff = abs(width - cx);
515         iTempYDiff = abs(height - cy);
516
517         if(iTotalDiff > (iTempXDiff + iTempYDiff))
518         {
519             iXDiff = iTempXDiff;
520             iYDiff = iTempYDiff;
521             iTotalDiff = iXDiff + iYDiff;
522         }
523     }
524
525     /* Find Best Colors for Best Fit */
526     for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
527     {
528         if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
529         {
530             iTempColorDiff = abs(colors - (1<<bits));
531             if(iColorDiff > iTempColorDiff)
532             {
533                 bestEntry = i;
534                 iColorDiff = iTempColorDiff;
535             }
536         }
537     }
538
539     return bestEntry;
540 }
541
542 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
543                                         int *width, int *height, int *bits )
544 {
545     CURSORICONDIR *resdir = dir;
546     ICONRESDIR *icon;
547
548     if ( resdir->idCount <= n )
549         return FALSE;
550     icon = &resdir->idEntries[n].ResInfo.icon;
551     *width = icon->bWidth;
552     *height = icon->bHeight;
553     *bits = resdir->idEntries[n].wBitCount;
554     return TRUE;
555 }
556
557 /**********************************************************************
558  *          CURSORICON_FindBestCursor
559  *
560  * Find the cursor closest to the requested size.
561  *
562  * FIXME: parameter 'color' ignored.
563  */
564 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
565                                       int width, int height, int color )
566 {
567     int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
568
569     /* Double height to account for AND and XOR masks */
570
571     height *= 2;
572
573     /* First find the largest one smaller than or equal to the requested size*/
574
575     maxwidth = maxheight = 0;
576     for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
577     {
578         if ((cx <= width) && (cy <= height) &&
579             (cx > maxwidth) && (cy > maxheight))
580         {
581             bestEntry = i;
582             maxwidth  = cx;
583             maxheight = cy;
584         }
585     }
586     if (bestEntry != -1) return bestEntry;
587
588     /* Now find the smallest one larger than the requested size */
589
590     maxwidth = maxheight = 255;
591     for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
592     {
593         if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
594         {
595             bestEntry = i;
596             maxwidth  = cx;
597             maxheight = cy;
598         }
599     }
600
601     return bestEntry;
602 }
603
604 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
605                                           int *width, int *height, int *bits )
606 {
607     CURSORICONDIR *resdir = dir;
608     CURSORDIR *cursor;
609
610     if ( resdir->idCount <= n )
611         return FALSE;
612     cursor = &resdir->idEntries[n].ResInfo.cursor;
613     *width = cursor->wWidth;
614     *height = cursor->wHeight;
615     *bits = resdir->idEntries[n].wBitCount;
616     return TRUE;
617 }
618
619 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
620                                       int width, int height, int colors )
621 {
622     int n;
623
624     n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
625                                  width, height, colors );
626     if ( n < 0 )
627         return NULL;
628     return &dir->idEntries[n];
629 }
630
631 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
632                                       int width, int height, int color )
633 {
634     int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
635                                    width, height, color );
636     if ( n < 0 )
637         return NULL;
638     return &dir->idEntries[n];
639 }
640
641 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
642                                      int *width, int *height, int *bits )
643 {
644     CURSORICONFILEDIR *filedir = dir;
645     CURSORICONFILEDIRENTRY *entry;
646
647     if ( filedir->idCount <= n )
648         return FALSE;
649     entry = &filedir->idEntries[n];
650     *width = entry->bWidth;
651     *height = entry->bHeight;
652     *bits = entry->bColorCount;
653     return TRUE;
654 }
655
656 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
657                                       int width, int height, int color )
658 {
659     int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
660                                        width, height, color );
661     if ( n < 0 )
662         return NULL;
663     return &dir->idEntries[n];
664 }
665
666 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
667                                       int width, int height, int color )
668 {
669     int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
670                                      width, height, color );
671     if ( n < 0 )
672         return NULL;
673     return &dir->idEntries[n];
674 }
675
676 /***********************************************************************
677  *          stretch_blt_icon
678  *
679  * A helper function that stretches a bitmap buffer into an HBITMAP.
680  *
681  * PARAMS
682  *      hDest       [I] The handle of the destination bitmap.
683  *      pDestInfo   [I] The BITMAPINFO of the destination bitmap.
684  *      pSrcInfo    [I] The BITMAPINFO of the source bitmap.
685  *      pSrcBits    [I] A pointer to the source bitmap buffer.
686  **/
687 static BOOL stretch_blt_icon(HBITMAP hDest, BITMAPINFO *pDestInfo, BITMAPINFO *pSrcInfo, char *pSrcBits)
688 {
689     HBITMAP hOld;
690     BOOL res = FALSE;
691     HDC hdcMem = CreateCompatibleDC(screen_dc);
692
693     if (hdcMem)
694     {
695         hOld = SelectObject(hdcMem, hDest);
696         res = StretchDIBits(hdcMem,
697                             0, 0, pDestInfo->bmiHeader.biWidth, pDestInfo->bmiHeader.biHeight,
698                             0, 0, pSrcInfo->bmiHeader.biWidth, pSrcInfo->bmiHeader.biHeight,
699                             pSrcBits, pSrcInfo, DIB_RGB_COLORS, SRCCOPY);
700         SelectObject(hdcMem, hOld);
701         DeleteDC( hdcMem );
702     }
703
704     return res;
705 }
706
707 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi,
708                                            POINT16 hotspot, BOOL bIcon,
709                                            DWORD dwVersion,
710                                            INT width, INT height,
711                                            UINT cFlag )
712 {
713     HGLOBAL16 hObj;
714     int sizeAnd, sizeXor;
715     HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
716     BITMAP bmpXor, bmpAnd;
717     INT size;
718     BITMAPINFO *pSrcInfo, *pDestInfo;
719
720     if (dwVersion == 0x00020000)
721     {
722         FIXME_(cursor)("\t2.xx resources are not supported\n");
723         return 0;
724     }
725
726     /* Check bitmap header */
727
728     if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
729          (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER)  ||
730           bmi->bmiHeader.biCompression != BI_RGB) )
731     {
732           WARN_(cursor)("\tinvalid resource bitmap header.\n");
733           return 0;
734     }
735
736     size = bitmap_info_size( bmi, DIB_RGB_COLORS );
737
738     if (!width) width = bmi->bmiHeader.biWidth;
739     if (!height) height = bmi->bmiHeader.biHeight/2;
740
741     /* Scale the hotspot */
742     if (((bmi->bmiHeader.biHeight/2 != height) || (bmi->bmiHeader.biWidth != width)) &&
743         hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
744     {
745         hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
746         hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
747     }
748
749     if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
750     if (screen_dc)
751     {
752         /* Make sure we have room for the monochrome bitmap later on.
753          * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
754          * up to and including the biBitCount. In-memory icon resource
755          * format is as follows:
756          *
757          *   BITMAPINFOHEADER   icHeader  // DIB header
758          *   RGBQUAD         icColors[]   // Color table
759          *   BYTE            icXOR[]      // DIB bits for XOR mask
760          *   BYTE            icAND[]      // DIB bits for AND mask
761          */
762
763         pSrcInfo = HeapAlloc( GetProcessHeap(), 0,
764                               max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
765         pDestInfo = HeapAlloc( GetProcessHeap(), 0,
766                               max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
767         if (pSrcInfo && pDestInfo)
768         {
769             memcpy( pSrcInfo, bmi, size );
770             pSrcInfo->bmiHeader.biHeight /= 2;
771
772             memcpy( pDestInfo, bmi, size );
773             pDestInfo->bmiHeader.biWidth = width;
774             pDestInfo->bmiHeader.biHeight = height;
775             pDestInfo->bmiHeader.biSizeImage = 0;
776
777             /* Create the XOR bitmap */
778             if(pSrcInfo->bmiHeader.biBitCount == 32)
779             {
780                 void *pDIBBuffer = NULL;
781                 hXorBits = CreateDIBSection(screen_dc, pDestInfo, DIB_RGB_COLORS, &pDIBBuffer, NULL, 0);
782
783                 if(hXorBits)
784                 {
785                     if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
786                     {
787                         DeleteObject(hXorBits);
788                         hXorBits = 0;
789                     }
790                 }
791             }
792             else
793             {
794                 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
795
796                 if(hXorBits)
797                 {
798                     if(!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
799                     {
800                         DeleteObject(hXorBits);
801                         hXorBits = 0;
802                     }
803                 }
804             }
805
806             if( hXorBits )
807             {
808                 char* xbits = (char *)bmi + size +
809                     get_dib_width_bytes( bmi->bmiHeader.biWidth,
810                                          bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
811
812                 pSrcInfo->bmiHeader.biBitCount = 1;
813                 if (pSrcInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
814                 {
815                     RGBQUAD *rgb = pSrcInfo->bmiColors;
816
817                     pSrcInfo->bmiHeader.biClrUsed = pSrcInfo->bmiHeader.biClrImportant = 2;
818                     rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
819                     rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
820                     rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
821                 }
822                 else
823                 {
824                     RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pSrcInfo) + 1);
825
826                     rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
827                     rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
828                 }
829
830                 /* Create the AND bitmap */
831                 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
832
833                 if(!stretch_blt_icon(hAndBits, pDestInfo, pSrcInfo, xbits))
834                 {
835                     DeleteObject(hAndBits);
836                     hAndBits = 0;
837                 }
838
839                 if( !hAndBits )
840                 {
841                     DeleteObject( hXorBits );
842                     hXorBits = 0;
843                 }
844             }
845         }
846
847         HeapFree( GetProcessHeap(), 0, pSrcInfo );
848         HeapFree( GetProcessHeap(), 0, pDestInfo );
849     }
850
851     if( !hXorBits || !hAndBits )
852     {
853         WARN_(cursor)("\tunable to create an icon bitmap.\n");
854         return 0;
855     }
856
857     /* Now create the CURSORICONINFO structure */
858     GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
859     GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
860     sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
861     sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
862
863     hObj = GlobalAlloc16( GMEM_MOVEABLE,
864                      sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
865     if (hObj)
866     {
867         CURSORICONINFO *info;
868
869         info = GlobalLock16( hObj );
870         info->ptHotSpot.x   = hotspot.x;
871         info->ptHotSpot.y   = hotspot.y;
872         info->nWidth        = bmpXor.bmWidth;
873         info->nHeight       = bmpXor.bmHeight;
874         info->nWidthBytes   = bmpXor.bmWidthBytes;
875         info->bPlanes       = bmpXor.bmPlanes;
876         info->bBitsPerPixel = bmpXor.bmBitsPixel;
877
878         /* Transfer the bitmap bits to the CURSORICONINFO structure */
879
880         GetBitmapBits( hAndBits, sizeAnd, info + 1 );
881         GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
882         GlobalUnlock16( hObj );
883     }
884
885     DeleteObject( hAndBits );
886     DeleteObject( hXorBits );
887     return HICON_32(hObj);
888 }
889
890
891 /**********************************************************************
892  *          .ANI cursor support
893  */
894 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
895         ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
896         ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
897
898 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
899 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
900 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
901 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
902 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
903 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
904
905 #define ANI_FLAG_ICON       0x1
906 #define ANI_FLAG_SEQUENCE   0x2
907
908 typedef struct {
909     DWORD header_size;
910     DWORD num_frames;
911     DWORD num_steps;
912     DWORD width;
913     DWORD height;
914     DWORD bpp;
915     DWORD num_planes;
916     DWORD display_rate;
917     DWORD flags;
918 } ani_header;
919
920 typedef struct {
921     DWORD           data_size;
922     const unsigned char   *data;
923 } riff_chunk_t;
924
925 static void dump_ani_header( const ani_header *header )
926 {
927     TRACE("     header size: %d\n", header->header_size);
928     TRACE("          frames: %d\n", header->num_frames);
929     TRACE("           steps: %d\n", header->num_steps);
930     TRACE("           width: %d\n", header->width);
931     TRACE("          height: %d\n", header->height);
932     TRACE("             bpp: %d\n", header->bpp);
933     TRACE("          planes: %d\n", header->num_planes);
934     TRACE("    display rate: %d\n", header->display_rate);
935     TRACE("           flags: 0x%08x\n", header->flags);
936 }
937
938
939 /*
940  * RIFF:
941  * DWORD "RIFF"
942  * DWORD size
943  * DWORD riff_id
944  * BYTE[] data
945  *
946  * LIST:
947  * DWORD "LIST"
948  * DWORD size
949  * DWORD list_id
950  * BYTE[] data
951  *
952  * CHUNK:
953  * DWORD chunk_id
954  * DWORD size
955  * BYTE[] data
956  */
957 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
958 {
959     const unsigned char *ptr = parent_chunk->data;
960     const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
961
962     if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
963
964     while (ptr < end)
965     {
966         if ((!chunk_type && *(DWORD *)ptr == chunk_id )
967                 || (chunk_type && *(DWORD *)ptr == chunk_type && *((DWORD *)ptr + 2) == chunk_id ))
968         {
969             ptr += sizeof(DWORD);
970             chunk->data_size = (*(DWORD *)ptr + 1) & ~1;
971             ptr += sizeof(DWORD);
972             if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
973             chunk->data = ptr;
974
975             return;
976         }
977
978         ptr += sizeof(DWORD);
979         ptr += (*(DWORD *)ptr + 1) & ~1;
980         ptr += sizeof(DWORD);
981     }
982 }
983
984
985 /*
986  * .ANI layout:
987  *
988  * RIFF:'ACON'                  RIFF chunk
989  *     |- CHUNK:'anih'          Header
990  *     |- CHUNK:'seq '          Sequence information (optional)
991  *     \- LIST:'fram'           Frame list
992  *            |- CHUNK:icon     Cursor frames
993  *            |- CHUNK:icon
994  *            |- ...
995  *            \- CHUNK:icon
996  */
997 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
998     INT width, INT height, INT colors )
999 {
1000     HCURSOR cursor;
1001     ani_header header = {0};
1002     LPBYTE frame_bits = 0;
1003     POINT16 hotspot;
1004     CURSORICONFILEDIRENTRY *entry;
1005
1006     riff_chunk_t root_chunk = { bits_size, bits };
1007     riff_chunk_t ACON_chunk = {0};
1008     riff_chunk_t anih_chunk = {0};
1009     riff_chunk_t fram_chunk = {0};
1010     const unsigned char *icon_data;
1011
1012     TRACE("bits %p, bits_size %d\n", bits, bits_size);
1013
1014     if (!bits) return 0;
1015
1016     riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1017     if (!ACON_chunk.data)
1018     {
1019         ERR("Failed to get root chunk.\n");
1020         return 0;
1021     }
1022
1023     riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1024     if (!anih_chunk.data)
1025     {
1026         ERR("Failed to get 'anih' chunk.\n");
1027         return 0;
1028     }
1029     memcpy( &header, anih_chunk.data, sizeof(header) );
1030     dump_ani_header( &header );
1031
1032     riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1033     if (!fram_chunk.data)
1034     {
1035         ERR("Failed to get icon list.\n");
1036         return 0;
1037     }
1038
1039     /* FIXME: For now, just load the first frame.  Before we can load all the
1040      * frames, we need to write the needed code in wineserver, etc. to handle
1041      * cursors.  Once this code is written, we can extend it to support .ani
1042      * cursors and then update user32 and winex11.drv to load all frames.
1043      *
1044      * Hopefully this will at least make some games (C&C3, etc.) more playable
1045      * in the meantime.
1046      */
1047     FIXME("Loading all frames for .ani cursors not implemented.\n");
1048     icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1049
1050     entry = CURSORICON_FindBestIconFile( (CURSORICONFILEDIR *) icon_data,
1051         width, height, colors );
1052
1053     frame_bits = HeapAlloc( GetProcessHeap(), 0, entry->dwDIBSize );
1054     memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1055
1056     if (!header.width || !header.height)
1057     {
1058         header.width = entry->bWidth;
1059         header.height = entry->bHeight;
1060     }
1061
1062     hotspot.x = entry->xHotspot;
1063     hotspot.y = entry->yHotspot;
1064
1065     cursor = CURSORICON_CreateIconFromBMI( (BITMAPINFO *) frame_bits, hotspot,
1066         FALSE, 0x00030000, header.width, header.height, 0 );
1067
1068     HeapFree( GetProcessHeap(), 0, frame_bits );
1069
1070     return cursor;
1071 }
1072
1073
1074 /**********************************************************************
1075  *              CreateIconFromResourceEx (USER32.@)
1076  *
1077  * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1078  *        with cbSize parameter as well.
1079  */
1080 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1081                                        BOOL bIcon, DWORD dwVersion,
1082                                        INT width, INT height,
1083                                        UINT cFlag )
1084 {
1085     POINT16 hotspot;
1086     BITMAPINFO *bmi;
1087
1088     hotspot.x = ICON_HOTSPOT;
1089     hotspot.y = ICON_HOTSPOT;
1090
1091     TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1092                    bits, cbSize, dwVersion, width, height,
1093                                   bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1094
1095     if (bIcon)
1096         bmi = (BITMAPINFO *)bits;
1097     else /* get the hotspot */
1098     {
1099         POINT16 *pt = (POINT16 *)bits;
1100         hotspot = *pt;
1101         bmi = (BITMAPINFO *)(pt + 1);
1102     }
1103
1104     return CURSORICON_CreateIconFromBMI( bmi, hotspot, bIcon, dwVersion,
1105                                          width, height, cFlag );
1106 }
1107
1108
1109 /**********************************************************************
1110  *              CreateIconFromResource (USER32.@)
1111  */
1112 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1113                                            BOOL bIcon, DWORD dwVersion)
1114 {
1115     return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1116 }
1117
1118
1119 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1120                              INT width, INT height, INT colors,
1121                              BOOL fCursor, UINT loadflags)
1122 {
1123     CURSORICONFILEDIRENTRY *entry;
1124     CURSORICONFILEDIR *dir;
1125     DWORD filesize = 0;
1126     HICON hIcon = 0;
1127     LPBYTE bits;
1128     POINT16 hotspot;
1129
1130     TRACE("loading %s\n", debugstr_w( filename ));
1131
1132     bits = map_fileW( filename, &filesize );
1133     if (!bits)
1134         return hIcon;
1135
1136     /* Check for .ani. */
1137     if (memcmp( bits, "RIFF", 4 ) == 0)
1138     {
1139         hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height,
1140             colors );
1141         goto end;
1142     }
1143
1144     dir = (CURSORICONFILEDIR*) bits;
1145     if ( filesize < sizeof(*dir) )
1146         goto end;
1147
1148     if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1149         goto end;
1150
1151     if ( fCursor )
1152         entry = CURSORICON_FindBestCursorFile( dir, width, height, colors );
1153     else
1154         entry = CURSORICON_FindBestIconFile( dir, width, height, colors );
1155
1156     if ( !entry )
1157         goto end;
1158
1159     /* check that we don't run off the end of the file */
1160     if ( entry->dwDIBOffset > filesize )
1161         goto end;
1162     if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1163         goto end;
1164
1165     /* Set the actual hotspot for cursors and ICON_HOTSPOT for icons. */
1166     if ( fCursor )
1167     {
1168         hotspot.x = entry->xHotspot;
1169         hotspot.y = entry->yHotspot;
1170     }
1171     else
1172     {
1173         hotspot.x = ICON_HOTSPOT;
1174         hotspot.y = ICON_HOTSPOT;
1175     }
1176     hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset],
1177                                           hotspot, !fCursor, 0x00030000,
1178                                           width, height, loadflags );
1179 end:
1180     TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1181     UnmapViewOfFile( bits );
1182     return hIcon;
1183 }
1184
1185 /**********************************************************************
1186  *          CURSORICON_Load
1187  *
1188  * Load a cursor or icon from resource or file.
1189  */
1190 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1191                              INT width, INT height, INT colors,
1192                              BOOL fCursor, UINT loadflags)
1193 {
1194     HANDLE handle = 0;
1195     HICON hIcon = 0;
1196     HRSRC hRsrc, hGroupRsrc;
1197     CURSORICONDIR *dir;
1198     CURSORICONDIRENTRY *dirEntry;
1199     LPBYTE bits;
1200     WORD wResId;
1201     DWORD dwBytesInRes;
1202
1203     TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
1204           hInstance, debugstr_w(name), width, height, colors, fCursor, loadflags);
1205
1206     if ( loadflags & LR_LOADFROMFILE )    /* Load from file */
1207         return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
1208
1209     if (!hInstance) hInstance = user32_module;  /* Load OEM cursor/icon */
1210
1211     /* Normalize hInstance (must be uniquely represented for icon cache) */
1212
1213     if (!HIWORD( hInstance ))
1214         hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
1215
1216     /* Get directory resource ID */
1217
1218     if (!(hRsrc = FindResourceW( hInstance, name,
1219                                  (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1220         return 0;
1221     hGroupRsrc = hRsrc;
1222
1223     /* Find the best entry in the directory */
1224
1225     if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1226     if (!(dir = LockResource( handle ))) return 0;
1227     if (fCursor)
1228         dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, colors );
1229     else
1230         dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1231     if (!dirEntry) return 0;
1232     wResId = dirEntry->wResId;
1233     dwBytesInRes = dirEntry->dwBytesInRes;
1234     FreeResource( handle );
1235
1236     /* Load the resource */
1237
1238     if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1239                                 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1240
1241     /* If shared icon, check whether it was already loaded */
1242     if (    (loadflags & LR_SHARED)
1243          && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1244         return hIcon;
1245
1246     if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1247     bits = LockResource( handle );
1248     hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1249                                       !fCursor, 0x00030000, width, height, loadflags);
1250     FreeResource( handle );
1251
1252     /* If shared icon, add to icon cache */
1253
1254     if ( hIcon && (loadflags & LR_SHARED) )
1255         CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1256
1257     return hIcon;
1258 }
1259
1260 /***********************************************************************
1261  *           CURSORICON_Copy
1262  *
1263  * Make a copy of a cursor or icon.
1264  */
1265 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
1266 {
1267     char *ptrOld, *ptrNew;
1268     int size;
1269     HICON16 hOld = HICON_16(hIcon);
1270     HICON16 hNew;
1271
1272     if (!(ptrOld = GlobalLock16( hOld ))) return 0;
1273     if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
1274     size = GlobalSize16( hOld );
1275     hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
1276     FarSetOwner16( hNew, hInst16 );
1277     ptrNew = GlobalLock16( hNew );
1278     memcpy( ptrNew, ptrOld, size );
1279     GlobalUnlock16( hOld );
1280     GlobalUnlock16( hNew );
1281     return HICON_32(hNew);
1282 }
1283
1284 /*************************************************************************
1285  * CURSORICON_ExtCopy
1286  *
1287  * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1288  *
1289  * PARAMS
1290  *      Handle     [I] handle to an Image
1291  *      nType      [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1292  *      iDesiredCX [I] The Desired width of the Image
1293  *      iDesiredCY [I] The desired height of the Image
1294  *      nFlags     [I] The flags from CopyImage
1295  *
1296  * RETURNS
1297  *     Success: The new handle of the Image
1298  *
1299  * NOTES
1300  *     LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1301  *     LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1302  *     LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1303  *
1304  *
1305  */
1306
1307 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1308                                 INT iDesiredCX, INT iDesiredCY,
1309                                 UINT nFlags)
1310 {
1311     HICON hNew=0;
1312
1313     TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1314                  hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1315
1316     if(hIcon == 0)
1317     {
1318         return 0;
1319     }
1320
1321     /* Best Fit or Monochrome */
1322     if( (nFlags & LR_COPYFROMRESOURCE
1323         && (iDesiredCX > 0 || iDesiredCY > 0))
1324         || nFlags & LR_MONOCHROME)
1325     {
1326         ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1327
1328         /* Not Found in Cache, then do a straight copy
1329         */
1330         if(pIconCache == NULL)
1331         {
1332             hNew = CURSORICON_Copy(0, hIcon);
1333             if(nFlags & LR_COPYFROMRESOURCE)
1334             {
1335                 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1336             }
1337         }
1338         else
1339         {
1340             int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1341             LPBYTE pBits;
1342             HANDLE hMem;
1343             HRSRC hRsrc;
1344             DWORD dwBytesInRes;
1345             WORD wResId;
1346             CURSORICONDIR *pDir;
1347             CURSORICONDIRENTRY *pDirEntry;
1348             BOOL bIsIcon = (nType == IMAGE_ICON);
1349
1350             /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1351             */
1352             if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1353                 || (iDesiredCX == 0 && iDesiredCY == 0))
1354             {
1355                 iDesiredCY = GetSystemMetrics(bIsIcon ?
1356                     SM_CYICON : SM_CYCURSOR);
1357                 iDesiredCX = GetSystemMetrics(bIsIcon ?
1358                     SM_CXICON : SM_CXCURSOR);
1359             }
1360
1361             /* Retrieve the CURSORICONDIRENTRY
1362             */
1363             if (!(hMem = LoadResource( pIconCache->hModule ,
1364                             pIconCache->hGroupRsrc)))
1365             {
1366                 return 0;
1367             }
1368             if (!(pDir = LockResource( hMem )))
1369             {
1370                 return 0;
1371             }
1372
1373             /* Find Best Fit
1374             */
1375             if(bIsIcon)
1376             {
1377                 pDirEntry = CURSORICON_FindBestIconRes(
1378                                 pDir, iDesiredCX, iDesiredCY, 256 );
1379             }
1380             else
1381             {
1382                 pDirEntry = CURSORICON_FindBestCursorRes(
1383                                 pDir, iDesiredCX, iDesiredCY, 1);
1384             }
1385
1386             wResId = pDirEntry->wResId;
1387             dwBytesInRes = pDirEntry->dwBytesInRes;
1388             FreeResource(hMem);
1389
1390             TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1391                 wResId, dwBytesInRes,  pDirEntry->ResInfo.icon.bWidth,
1392                 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1393
1394             /* Get the Best Fit
1395             */
1396             if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1397                 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1398             {
1399                 return 0;
1400             }
1401             if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1402             {
1403                 return 0;
1404             }
1405
1406             pBits = LockResource( hMem );
1407
1408             if(nFlags & LR_DEFAULTSIZE)
1409             {
1410                 iTargetCY = GetSystemMetrics(SM_CYICON);
1411                 iTargetCX = GetSystemMetrics(SM_CXICON);
1412             }
1413
1414             /* Create a New Icon with the proper dimension
1415             */
1416             hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1417                        bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1418             FreeResource(hMem);
1419         }
1420     }
1421     else hNew = CURSORICON_Copy(0, hIcon);
1422     return hNew;
1423 }
1424
1425
1426 /***********************************************************************
1427  *              CreateCursor (USER32.@)
1428  */
1429 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1430                                  INT xHotSpot, INT yHotSpot,
1431                                  INT nWidth, INT nHeight,
1432                                  LPCVOID lpANDbits, LPCVOID lpXORbits )
1433 {
1434     CURSORICONINFO info;
1435
1436     TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1437                     nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1438
1439     info.ptHotSpot.x = xHotSpot;
1440     info.ptHotSpot.y = yHotSpot;
1441     info.nWidth = nWidth;
1442     info.nHeight = nHeight;
1443     info.nWidthBytes = 0;
1444     info.bPlanes = 1;
1445     info.bBitsPerPixel = 1;
1446
1447     return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1448 }
1449
1450
1451 /***********************************************************************
1452  *              CreateIcon (USER.407)
1453  */
1454 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1455                              INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1456                              LPCVOID lpANDbits, LPCVOID lpXORbits )
1457 {
1458     CURSORICONINFO info;
1459
1460     TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1461                   nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1462
1463     info.ptHotSpot.x = ICON_HOTSPOT;
1464     info.ptHotSpot.y = ICON_HOTSPOT;
1465     info.nWidth = nWidth;
1466     info.nHeight = nHeight;
1467     info.nWidthBytes = 0;
1468     info.bPlanes = bPlanes;
1469     info.bBitsPerPixel = bBitsPixel;
1470
1471     return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1472 }
1473
1474
1475 /***********************************************************************
1476  *              CreateIcon (USER32.@)
1477  *
1478  *  Creates an icon based on the specified bitmaps. The bitmaps must be
1479  *  provided in a device dependent format and will be resized to
1480  *  (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1481  *  depth. The provided bitmaps must be top-down bitmaps.
1482  *  Although Windows does not support 15bpp(*) this API must support it
1483  *  for Winelib applications.
1484  *
1485  *  (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1486  *      format!
1487  *
1488  * RETURNS
1489  *  Success: handle to an icon
1490  *  Failure: NULL
1491  *
1492  * FIXME: Do we need to resize the bitmaps?
1493  */
1494 HICON WINAPI CreateIcon(
1495     HINSTANCE hInstance,  /* [in] the application's hInstance */
1496     INT       nWidth,     /* [in] the width of the provided bitmaps */
1497     INT       nHeight,    /* [in] the height of the provided bitmaps */
1498     BYTE      bPlanes,    /* [in] the number of planes in the provided bitmaps */
1499     BYTE      bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1500     LPCVOID   lpANDbits,  /* [in] a monochrome bitmap representing the icon's mask */
1501     LPCVOID   lpXORbits)  /* [in] the icon's 'color' bitmap */
1502 {
1503     ICONINFO iinfo;
1504     HICON hIcon;
1505
1506     TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1507                  nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1508
1509     iinfo.fIcon = TRUE;
1510     iinfo.xHotspot = ICON_HOTSPOT;
1511     iinfo.yHotspot = ICON_HOTSPOT;
1512     iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1513     iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1514
1515     hIcon = CreateIconIndirect( &iinfo );
1516
1517     DeleteObject( iinfo.hbmMask );
1518     DeleteObject( iinfo.hbmColor );
1519
1520     return hIcon;
1521 }
1522
1523
1524 /***********************************************************************
1525  *              CreateCursorIconIndirect (USER.408)
1526  */
1527 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1528                                            CURSORICONINFO *info,
1529                                            LPCVOID lpANDbits,
1530                                            LPCVOID lpXORbits )
1531 {
1532     HGLOBAL16 handle;
1533     char *ptr;
1534     int sizeAnd, sizeXor;
1535
1536     hInstance = GetExePtr( hInstance );  /* Make it a module handle */
1537     if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1538     info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1539     sizeXor = info->nHeight * info->nWidthBytes;
1540     sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1541     if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1542                                   sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1543         return 0;
1544     FarSetOwner16( handle, hInstance );
1545     ptr = GlobalLock16( handle );
1546     memcpy( ptr, info, sizeof(*info) );
1547     memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1548     memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1549     GlobalUnlock16( handle );
1550     return handle;
1551 }
1552
1553
1554 /***********************************************************************
1555  *              CopyIcon (USER.368)
1556  */
1557 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1558 {
1559     TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1560     return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1561 }
1562
1563
1564 /***********************************************************************
1565  *              CopyIcon (USER32.@)
1566  */
1567 HICON WINAPI CopyIcon( HICON hIcon )
1568 {
1569     TRACE_(icon)("%p\n", hIcon );
1570     return CURSORICON_Copy( 0, hIcon );
1571 }
1572
1573
1574 /***********************************************************************
1575  *              CopyCursor (USER.369)
1576  */
1577 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1578 {
1579     TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1580     return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1581 }
1582
1583 /**********************************************************************
1584  *              DestroyIcon32 (USER.610)
1585  *
1586  * This routine is actually exported from Win95 USER under the name
1587  * DestroyIcon32 ...  The behaviour implemented here should mimic
1588  * the Win95 one exactly, especially the return values, which
1589  * depend on the setting of various flags.
1590  */
1591 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1592 {
1593     WORD retv;
1594
1595     TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1596
1597     /* Check whether destroying active cursor */
1598
1599     if ( get_user_thread_info()->cursor == HICON_32(handle) )
1600     {
1601         WARN_(cursor)("Destroying active cursor!\n" );
1602         return FALSE;
1603     }
1604
1605     /* Try shared cursor/icon first */
1606
1607     if ( !(flags & CID_NONSHARED) )
1608     {
1609         INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1610
1611         if ( count != -1 )
1612             return (flags & CID_WIN32)? TRUE : (count == 0);
1613
1614         /* FIXME: OEM cursors/icons should be recognized */
1615     }
1616
1617     /* Now assume non-shared cursor/icon */
1618
1619     retv = GlobalFree16( handle );
1620     return (flags & CID_RESOURCE)? retv : TRUE;
1621 }
1622
1623 /***********************************************************************
1624  *              DestroyIcon (USER32.@)
1625  */
1626 BOOL WINAPI DestroyIcon( HICON hIcon )
1627 {
1628     return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1629 }
1630
1631
1632 /***********************************************************************
1633  *              DestroyCursor (USER32.@)
1634  */
1635 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1636 {
1637     return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1638 }
1639
1640 /***********************************************************************
1641  *      bitmap_has_alpha_channel
1642  *
1643  * Analyses bits bitmap to determine if alpha data is present.
1644  *
1645  * PARAMS
1646  *      bpp          [I] The bits-per-pixel of the bitmap
1647  *      bitmapBits   [I] A pointer to the bitmap data
1648  *      bitmapLength [I] The length of the bitmap in bytes
1649  *
1650  * RETURNS
1651  *      TRUE if an alpha channel is discovered, FALSE
1652  *
1653  * NOTE
1654  *      Windows' behaviour is that if the icon bitmap is 32-bit and at
1655  *      least one pixel has a non-zero alpha, then the bitmap is a
1656  *      treated as having an alpha channel transparentcy. Otherwise,
1657  *      it's treated as being completely opaque.
1658  *
1659  */
1660 static BOOL bitmap_has_alpha_channel( int bpp, unsigned char *bitmapBits,
1661                                       unsigned int bitmapLength )
1662 {
1663     /* Detect an alpha channel by looking for non-zero alpha pixels */
1664     if(bpp == 32)
1665     {
1666         unsigned int offset;
1667         for(offset = 3; offset < bitmapLength; offset += 4)
1668         {
1669             if(bitmapBits[offset] != 0)
1670             {
1671                 return TRUE;
1672             }
1673         }
1674     }
1675     return FALSE;
1676 }
1677
1678 /***********************************************************************
1679  *          premultiply_alpha_channel
1680  *
1681  * Premultiplies the color channels of a 32-bit bitmap by the alpha
1682  * channel. This is a necessary step that must be carried out on
1683  * the image before it is passed to GdiAlphaBlend
1684  *
1685  * PARAMS
1686  *      destBitmap   [I] The destination bitmap buffer
1687  *      srcBitmap    [I] The source bitmap buffer
1688  *      bitmapLength [I] The length of the bitmap in bytes
1689  *
1690  */
1691 static void premultiply_alpha_channel( unsigned char *destBitmap,
1692                                        unsigned char *srcBitmap,
1693                                        unsigned int bitmapLength )
1694 {
1695     unsigned char *destPixel = destBitmap;
1696     unsigned char *srcPixel = srcBitmap;
1697
1698     while(destPixel < destBitmap + bitmapLength)
1699     {
1700         unsigned char alpha = srcPixel[3];
1701         *(destPixel++) = *(srcPixel++) * alpha / 255;
1702         *(destPixel++) = *(srcPixel++) * alpha / 255;
1703         *(destPixel++) = *(srcPixel++) * alpha / 255;
1704         *(destPixel++) = *(srcPixel++);
1705     }
1706 }
1707
1708 /***********************************************************************
1709  *              DrawIcon (USER32.@)
1710  */
1711 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1712 {
1713     CURSORICONINFO *ptr;
1714     HDC hMemDC;
1715     HBITMAP hXorBits = NULL, hAndBits = NULL, hBitTemp = NULL;
1716     COLORREF oldFg, oldBg;
1717     unsigned char *xorBitmapBits;
1718     unsigned int dibLength;
1719
1720     TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);
1721
1722     if (!(ptr = GlobalLock16(HICON_16(hIcon)))) return FALSE;
1723     if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1724
1725     dibLength = ptr->nHeight * get_bitmap_width_bytes(
1726         ptr->nWidth, ptr->bBitsPerPixel);
1727
1728     xorBitmapBits = (unsigned char *)(ptr + 1) + ptr->nHeight *
1729                     get_bitmap_width_bytes(ptr->nWidth, 1);
1730
1731     oldFg = SetTextColor( hdc, RGB(0,0,0) );
1732     oldBg = SetBkColor( hdc, RGB(255,255,255) );
1733
1734     if(bitmap_has_alpha_channel(ptr->bBitsPerPixel, xorBitmapBits, dibLength))
1735     {
1736         BITMAPINFOHEADER bmih;
1737         unsigned char *dibBits;
1738
1739         memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
1740         bmih.biSize = sizeof(BITMAPINFOHEADER);
1741         bmih.biWidth = ptr->nWidth;
1742         bmih.biHeight = -ptr->nHeight;
1743         bmih.biPlanes = ptr->bPlanes;
1744         bmih.biBitCount = 32;
1745         bmih.biCompression = BI_RGB;
1746
1747         hXorBits = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
1748                                     (void*)&dibBits, NULL, 0);
1749
1750         if (hXorBits && dibBits)
1751         {
1752             BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
1753
1754             /* Do the alpha blending render */
1755             premultiply_alpha_channel(dibBits, xorBitmapBits, dibLength);
1756             hBitTemp = SelectObject( hMemDC, hXorBits );
1757             /* Destination width/height has to be "System Large" size */
1758             GdiAlphaBlend(hdc, x, y, GetSystemMetrics(SM_CXICON),
1759                             GetSystemMetrics(SM_CYICON), hMemDC,
1760                             0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
1761             SelectObject( hMemDC, hBitTemp );
1762         }
1763     }
1764     else
1765     {
1766         hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1, ptr + 1 );
1767         hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1768                                ptr->bBitsPerPixel, xorBitmapBits);
1769
1770         if (hXorBits && hAndBits)
1771         {
1772             hBitTemp = SelectObject( hMemDC, hAndBits );
1773             StretchBlt( hdc, x, y, GetSystemMetrics(SM_CXICON),
1774                             GetSystemMetrics(SM_CYICON), hMemDC, 0, 0,
1775                             ptr->nWidth, ptr->nHeight, SRCAND );
1776             SelectObject( hMemDC, hXorBits );
1777             StretchBlt( hdc, x, y, GetSystemMetrics(SM_CXICON),
1778                             GetSystemMetrics(SM_CYICON), hMemDC, 0, 0,
1779                             ptr->nWidth, ptr->nHeight, SRCINVERT );
1780             SelectObject( hMemDC, hBitTemp );
1781         }
1782     }
1783
1784     DeleteDC( hMemDC );
1785     if (hXorBits) DeleteObject( hXorBits );
1786     if (hAndBits) DeleteObject( hAndBits );
1787     GlobalUnlock16(HICON_16(hIcon));
1788     SetTextColor( hdc, oldFg );
1789     SetBkColor( hdc, oldBg );
1790     return TRUE;
1791 }
1792
1793 /***********************************************************************
1794  *              DumpIcon (USER.459)
1795  */
1796 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1797                        SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1798 {
1799     CURSORICONINFO *info = MapSL( pInfo );
1800     int sizeAnd, sizeXor;
1801
1802     if (!info) return 0;
1803     sizeXor = info->nHeight * info->nWidthBytes;
1804     sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1805     if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1806     if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1807     if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1808     return MAKELONG( sizeXor, sizeXor );
1809 }
1810
1811
1812 /***********************************************************************
1813  *              SetCursor (USER32.@)
1814  *
1815  * Set the cursor shape.
1816  *
1817  * RETURNS
1818  *      A handle to the previous cursor shape.
1819  */
1820 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1821 {
1822     struct user_thread_info *thread_info = get_user_thread_info();
1823     HCURSOR hOldCursor;
1824
1825     if (hCursor == thread_info->cursor) return hCursor;  /* No change */
1826     TRACE("%p\n", hCursor);
1827     hOldCursor = thread_info->cursor;
1828     thread_info->cursor = hCursor;
1829     /* Change the cursor shape only if it is visible */
1830     if (thread_info->cursor_count >= 0)
1831     {
1832         USER_Driver->pSetCursor(GlobalLock16(HCURSOR_16(hCursor)));
1833         GlobalUnlock16(HCURSOR_16(hCursor));
1834     }
1835     return hOldCursor;
1836 }
1837
1838 /***********************************************************************
1839  *              ShowCursor (USER32.@)
1840  */
1841 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1842 {
1843     struct user_thread_info *thread_info = get_user_thread_info();
1844
1845     TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
1846
1847     if (bShow)
1848     {
1849         if (++thread_info->cursor_count == 0) /* Show it */
1850         {
1851             USER_Driver->pSetCursor(GlobalLock16(HCURSOR_16(thread_info->cursor)));
1852             GlobalUnlock16(HCURSOR_16(thread_info->cursor));
1853         }
1854     }
1855     else
1856     {
1857         if (--thread_info->cursor_count == -1) /* Hide it */
1858             USER_Driver->pSetCursor( NULL );
1859     }
1860     return thread_info->cursor_count;
1861 }
1862
1863 /***********************************************************************
1864  *              GetCursor (USER32.@)
1865  */
1866 HCURSOR WINAPI GetCursor(void)
1867 {
1868     return get_user_thread_info()->cursor;
1869 }
1870
1871
1872 /***********************************************************************
1873  *              ClipCursor (USER32.@)
1874  */
1875 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1876 {
1877     RECT virt;
1878
1879     SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1880                           GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1881     OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1882                        GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1883
1884     TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1885            wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1886
1887     if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1888         CURSOR_ClipRect = virt;
1889
1890     USER_Driver->pClipCursor( rect );
1891     return TRUE;
1892 }
1893
1894
1895 /***********************************************************************
1896  *              GetClipCursor (USER32.@)
1897  */
1898 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1899 {
1900     /* If this is first time - initialize the rect */
1901     if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1902
1903     return CopyRect( rect, &CURSOR_ClipRect );
1904 }
1905
1906
1907 /***********************************************************************
1908  *              SetSystemCursor (USER32.@)
1909  */
1910 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1911 {
1912     FIXME("(%p,%08x),stub!\n",  hcur, id);
1913     return TRUE;
1914 }
1915
1916
1917 /**********************************************************************
1918  *              LookupIconIdFromDirectoryEx (USER.364)
1919  *
1920  * FIXME: exact parameter sizes
1921  */
1922 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1923                                             INT16 width, INT16 height, UINT16 cFlag )
1924 {
1925     return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1926 }
1927
1928 /**********************************************************************
1929  *              LookupIconIdFromDirectoryEx (USER32.@)
1930  */
1931 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1932              INT width, INT height, UINT cFlag )
1933 {
1934     CURSORICONDIR       *dir = (CURSORICONDIR*)xdir;
1935     UINT retVal = 0;
1936     if( dir && !dir->idReserved && (dir->idType & 3) )
1937     {
1938         CURSORICONDIRENTRY* entry;
1939         HDC hdc;
1940         UINT palEnts;
1941         int colors;
1942         hdc = GetDC(0);
1943         palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1944         if (palEnts == 0)
1945             palEnts = 256;
1946         colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1947
1948         ReleaseDC(0, hdc);
1949
1950         if( bIcon )
1951             entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1952         else
1953             entry = CURSORICON_FindBestCursorRes( dir, width, height, colors );
1954
1955         if( entry ) retVal = entry->wResId;
1956     }
1957     else WARN_(cursor)("invalid resource directory\n");
1958     return retVal;
1959 }
1960
1961 /**********************************************************************
1962  *              LookupIconIdFromDirectory (USER32.@)
1963  */
1964 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1965 {
1966     return LookupIconIdFromDirectoryEx( dir, bIcon,
1967            bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1968            bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1969 }
1970
1971 /**********************************************************************
1972  *              GetIconID (USER.455)
1973  */
1974 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1975 {
1976     LPBYTE lpDir = GlobalLock16(hResource);
1977
1978     TRACE_(cursor)("hRes=%04x, entries=%i\n",
1979                     hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1980
1981     switch(resType)
1982     {
1983         case RT_CURSOR:
1984              return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1985                           GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1986         case RT_ICON:
1987              return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1988                           GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1989         default:
1990              WARN_(cursor)("invalid res type %d\n", resType );
1991     }
1992     return 0;
1993 }
1994
1995 /**********************************************************************
1996  *              LoadCursorIconHandler (USER.336)
1997  *
1998  * Supposed to load resources of Windows 2.x applications.
1999  */
2000 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
2001 {
2002     FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
2003           hResource, hModule, hRsrc);
2004     return 0;
2005 }
2006
2007 /**********************************************************************
2008  *              LoadIconHandler (USER.456)
2009  */
2010 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
2011 {
2012     LPBYTE bits = LockResource16( hResource );
2013
2014     TRACE_(cursor)("hRes=%04x\n",hResource);
2015
2016     return HICON_16(CreateIconFromResourceEx( bits, 0, TRUE,
2017                       bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
2018 }
2019
2020 /***********************************************************************
2021  *              LoadCursorW (USER32.@)
2022  */
2023 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
2024 {
2025     TRACE("%p, %s\n", hInstance, debugstr_w(name));
2026
2027     return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
2028                        LR_SHARED | LR_DEFAULTSIZE );
2029 }
2030
2031 /***********************************************************************
2032  *              LoadCursorA (USER32.@)
2033  */
2034 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
2035 {
2036     TRACE("%p, %s\n", hInstance, debugstr_a(name));
2037
2038     return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
2039                        LR_SHARED | LR_DEFAULTSIZE );
2040 }
2041
2042 /***********************************************************************
2043  *              LoadCursorFromFileW (USER32.@)
2044  */
2045 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
2046 {
2047     TRACE("%s\n", debugstr_w(name));
2048
2049     return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
2050                        LR_LOADFROMFILE | LR_DEFAULTSIZE );
2051 }
2052
2053 /***********************************************************************
2054  *              LoadCursorFromFileA (USER32.@)
2055  */
2056 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
2057 {
2058     TRACE("%s\n", debugstr_a(name));
2059
2060     return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
2061                        LR_LOADFROMFILE | LR_DEFAULTSIZE );
2062 }
2063
2064 /***********************************************************************
2065  *              LoadIconW (USER32.@)
2066  */
2067 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
2068 {
2069     TRACE("%p, %s\n", hInstance, debugstr_w(name));
2070
2071     return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
2072                        LR_SHARED | LR_DEFAULTSIZE );
2073 }
2074
2075 /***********************************************************************
2076  *              LoadIconA (USER32.@)
2077  */
2078 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
2079 {
2080     TRACE("%p, %s\n", hInstance, debugstr_a(name));
2081
2082     return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
2083                        LR_SHARED | LR_DEFAULTSIZE );
2084 }
2085
2086 /**********************************************************************
2087  *              GetIconInfo (USER32.@)
2088  */
2089 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
2090 {
2091     CURSORICONINFO *ciconinfo;
2092     INT height;
2093
2094     ciconinfo = GlobalLock16(HICON_16(hIcon));
2095     if (!ciconinfo)
2096         return FALSE;
2097
2098     TRACE("%p => %dx%d, %d bpp\n", hIcon,
2099           ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
2100
2101     if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
2102          (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
2103     {
2104       iconinfo->fIcon    = TRUE;
2105       iconinfo->xHotspot = ciconinfo->nWidth / 2;
2106       iconinfo->yHotspot = ciconinfo->nHeight / 2;
2107     }
2108     else
2109     {
2110       iconinfo->fIcon    = FALSE;
2111       iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
2112       iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
2113     }
2114
2115     height = ciconinfo->nHeight;
2116
2117     if (ciconinfo->bBitsPerPixel > 1)
2118     {
2119         iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
2120                                 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
2121                                 (char *)(ciconinfo + 1)
2122                                 + ciconinfo->nHeight *
2123                                 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
2124     }
2125     else
2126     {
2127         iconinfo->hbmColor = 0;
2128         height *= 2;
2129     }
2130
2131     iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
2132                                 1, 1, ciconinfo + 1);
2133
2134     GlobalUnlock16(HICON_16(hIcon));
2135
2136     return TRUE;
2137 }
2138
2139 /**********************************************************************
2140  *              CreateIconIndirect (USER32.@)
2141  */
2142 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2143 {
2144     DIBSECTION bmpXor;
2145     BITMAP bmpAnd;
2146     HICON16 hObj;
2147     int xor_objsize = 0, sizeXor = 0, sizeAnd, planes, bpp;
2148
2149     TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2150            iconinfo->hbmColor, iconinfo->hbmMask,
2151            iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2152
2153     if (!iconinfo->hbmMask) return 0;
2154
2155     planes = GetDeviceCaps( screen_dc, PLANES );
2156     bpp = GetDeviceCaps( screen_dc, BITSPIXEL );
2157
2158     if (iconinfo->hbmColor)
2159     {
2160         xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2161         TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2162                bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
2163                bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
2164         /* we can use either depth 1 or screen depth for xor bitmap */
2165         if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
2166         sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
2167     }
2168     GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2169     TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2170            bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2171            bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2172
2173     sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2174
2175     hObj = GlobalAlloc16( GMEM_MOVEABLE,
2176                           sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
2177     if (hObj)
2178     {
2179         CURSORICONINFO *info;
2180
2181         info = GlobalLock16( hObj );
2182
2183         /* If we are creating an icon, the hotspot is unused */
2184         if (iconinfo->fIcon)
2185         {
2186             info->ptHotSpot.x   = ICON_HOTSPOT;
2187             info->ptHotSpot.y   = ICON_HOTSPOT;
2188         }
2189         else
2190         {
2191             info->ptHotSpot.x   = iconinfo->xHotspot;
2192             info->ptHotSpot.y   = iconinfo->yHotspot;
2193         }
2194
2195         if (iconinfo->hbmColor)
2196         {
2197             info->nWidth        = bmpXor.dsBm.bmWidth;
2198             info->nHeight       = bmpXor.dsBm.bmHeight;
2199             info->nWidthBytes   = bmpXor.dsBm.bmWidthBytes;
2200             info->bPlanes       = planes;
2201             info->bBitsPerPixel = bpp;
2202         }
2203         else
2204         {
2205             info->nWidth        = bmpAnd.bmWidth;
2206             info->nHeight       = bmpAnd.bmHeight / 2;
2207             info->nWidthBytes   = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2208             info->bPlanes       = 1;
2209             info->bBitsPerPixel = 1;
2210         }
2211
2212         /* Transfer the bitmap bits to the CURSORICONINFO structure */
2213
2214         /* Some apps pass a color bitmap as a mask, convert it to b/w */
2215         if (bmpAnd.bmBitsPixel == 1)
2216         {
2217             GetBitmapBits( iconinfo->hbmMask, sizeAnd, info + 1 );
2218         }
2219         else
2220         {
2221             HDC hdc, hdc_mem;
2222             HBITMAP hbmp_old, hbmp_mem_old, hbmp_mono;
2223
2224             hdc = GetDC( 0 );
2225             hdc_mem = CreateCompatibleDC( hdc );
2226
2227             hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
2228
2229             hbmp_old = SelectObject( hdc, iconinfo->hbmMask );
2230             hbmp_mem_old = SelectObject( hdc_mem, hbmp_mono );
2231
2232             BitBlt( hdc_mem, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc, 0, 0, SRCCOPY );
2233
2234             SelectObject( hdc, hbmp_old );
2235             SelectObject( hdc_mem, hbmp_mem_old );
2236
2237             DeleteDC( hdc_mem );
2238             ReleaseDC( 0, hdc );
2239
2240             GetBitmapBits( hbmp_mono, sizeAnd, info + 1 );
2241             DeleteObject( hbmp_mono );
2242         }
2243
2244         if (iconinfo->hbmColor)
2245         {
2246             char *dst_bits = (char*)(info + 1) + sizeAnd;
2247
2248             if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
2249                 GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
2250             else
2251             {
2252                 BITMAPINFO bminfo;
2253                 int dib_width = get_dib_width_bytes( info->nWidth, info->bBitsPerPixel );
2254                 int bitmap_width = get_bitmap_width_bytes( info->nWidth, info->bBitsPerPixel );
2255
2256                 bminfo.bmiHeader.biSize = sizeof(bminfo);
2257                 bminfo.bmiHeader.biWidth = info->nWidth;
2258                 bminfo.bmiHeader.biHeight = info->nHeight;
2259                 bminfo.bmiHeader.biPlanes = info->bPlanes;
2260                 bminfo.bmiHeader.biBitCount = info->bBitsPerPixel;
2261                 bminfo.bmiHeader.biCompression = BI_RGB;
2262                 bminfo.bmiHeader.biSizeImage = info->nHeight * dib_width;
2263                 bminfo.bmiHeader.biXPelsPerMeter = 0;
2264                 bminfo.bmiHeader.biYPelsPerMeter = 0;
2265                 bminfo.bmiHeader.biClrUsed = 0;
2266                 bminfo.bmiHeader.biClrImportant = 0;
2267
2268                 /* swap lines for dib sections */
2269                 if (xor_objsize == sizeof(DIBSECTION))
2270                     bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;
2271
2272                 if (dib_width != bitmap_width)  /* need to fixup alignment */
2273                 {
2274                     char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );
2275
2276                     if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2277                                                src_bits, &bminfo, DIB_RGB_COLORS ))
2278                     {
2279                         int y;
2280                         for (y = 0; y < info->nHeight; y++)
2281                             memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
2282                     }
2283                     HeapFree( GetProcessHeap(), 0, src_bits );
2284                 }
2285                 else
2286                     GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2287                                dst_bits, &bminfo, DIB_RGB_COLORS );
2288             }
2289         }
2290         GlobalUnlock16( hObj );
2291     }
2292     return HICON_32(hObj);
2293 }
2294
2295 /******************************************************************************
2296  *              DrawIconEx (USER32.@) Draws an icon or cursor on device context
2297  *
2298  * NOTES
2299  *    Why is this using SM_CXICON instead of SM_CXCURSOR?
2300  *
2301  * PARAMS
2302  *    hdc     [I] Handle to device context
2303  *    x0      [I] X coordinate of upper left corner
2304  *    y0      [I] Y coordinate of upper left corner
2305  *    hIcon   [I] Handle to icon to draw
2306  *    cxWidth [I] Width of icon
2307  *    cyWidth [I] Height of icon
2308  *    istep   [I] Index of frame in animated cursor
2309  *    hbr     [I] Handle to background brush
2310  *    flags   [I] Icon-drawing flags
2311  *
2312  * RETURNS
2313  *    Success: TRUE
2314  *    Failure: FALSE
2315  */
2316 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2317                             INT cxWidth, INT cyWidth, UINT istep,
2318                             HBRUSH hbr, UINT flags )
2319 {
2320     CURSORICONINFO *ptr;
2321     HDC hDC_off = 0, hMemDC;
2322     BOOL result = FALSE, DoOffscreen;
2323     HBITMAP hB_off = 0, hOld = 0;
2324     unsigned char *xorBitmapBits;
2325     unsigned int xorLength;
2326     BOOL has_alpha = FALSE;
2327
2328     TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2329                  hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2330
2331     if (!(ptr = GlobalLock16(HICON_16(hIcon)))) return FALSE;
2332     if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
2333
2334     if (istep)
2335         FIXME_(icon)("Ignoring istep=%d\n", istep);
2336     if (flags & DI_NOMIRROR)
2337         FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2338
2339     xorLength = ptr->nHeight * get_bitmap_width_bytes(
2340         ptr->nWidth, ptr->bBitsPerPixel);
2341     xorBitmapBits = (unsigned char *)(ptr + 1) + ptr->nHeight *
2342                     get_bitmap_width_bytes(ptr->nWidth, 1);
2343
2344     if (flags & DI_IMAGE)
2345         has_alpha = bitmap_has_alpha_channel(
2346             ptr->bBitsPerPixel, xorBitmapBits, xorLength);
2347
2348     /* Calculate the size of the destination image.  */
2349     if (cxWidth == 0)
2350     {
2351         if (flags & DI_DEFAULTSIZE)
2352             cxWidth = GetSystemMetrics (SM_CXICON);
2353         else
2354             cxWidth = ptr->nWidth;
2355     }
2356     if (cyWidth == 0)
2357     {
2358         if (flags & DI_DEFAULTSIZE)
2359             cyWidth = GetSystemMetrics (SM_CYICON);
2360         else
2361             cyWidth = ptr->nHeight;
2362     }
2363
2364     DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2365
2366     if (DoOffscreen) {
2367         RECT r;
2368
2369         r.left = 0;
2370         r.top = 0;
2371         r.right = cxWidth;
2372         r.bottom = cxWidth;
2373
2374         hDC_off = CreateCompatibleDC(hdc);
2375         hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2376         if (hDC_off && hB_off) {
2377             hOld = SelectObject(hDC_off, hB_off);
2378             FillRect(hDC_off, &r, hbr);
2379         }
2380     }
2381
2382     if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2383     {
2384         HBITMAP hBitTemp;
2385         HBITMAP hXorBits = NULL, hAndBits = NULL;
2386         COLORREF  oldFg, oldBg;
2387         INT     nStretchMode;
2388
2389         nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2390
2391         oldFg = SetTextColor( hdc, RGB(0,0,0) );
2392         oldBg = SetBkColor( hdc, RGB(255,255,255) );
2393
2394         if (((flags & DI_MASK) && !(flags & DI_IMAGE)) ||
2395             ((flags & DI_MASK) && !has_alpha))
2396         {
2397             hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight, 1, 1, ptr + 1 );
2398             if (hAndBits)
2399             {
2400                 hBitTemp = SelectObject( hMemDC, hAndBits );
2401                 if (DoOffscreen)
2402                     StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2403                                 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2404                 else
2405                     StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2406                                 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2407                 SelectObject( hMemDC, hBitTemp );
2408             }
2409         }
2410
2411         if (flags & DI_IMAGE)
2412         {
2413             BITMAPINFOHEADER bmih;
2414             unsigned char *dibBits;
2415
2416             memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
2417             bmih.biSize = sizeof(BITMAPINFOHEADER);
2418             bmih.biWidth = ptr->nWidth;
2419             bmih.biHeight = -ptr->nHeight;
2420             bmih.biPlanes = ptr->bPlanes;
2421             bmih.biBitCount = ptr->bBitsPerPixel;
2422             bmih.biCompression = BI_RGB;
2423
2424             hXorBits = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
2425                                         (void*)&dibBits, NULL, 0);
2426
2427             if (hXorBits && dibBits)
2428             {
2429                 if(has_alpha)
2430                 {
2431                     BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2432
2433                     /* Do the alpha blending render */
2434                     premultiply_alpha_channel(dibBits, xorBitmapBits, xorLength);
2435                     hBitTemp = SelectObject( hMemDC, hXorBits );
2436
2437                     if (DoOffscreen)
2438                         GdiAlphaBlend(hDC_off, 0, 0, cxWidth, cyWidth, hMemDC,
2439                                         0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
2440                     else
2441                         GdiAlphaBlend(hdc, x0, y0, cxWidth, cyWidth, hMemDC,
2442                                         0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
2443
2444                     SelectObject( hMemDC, hBitTemp );
2445                 }
2446                 else
2447                 {
2448                     memcpy(dibBits, xorBitmapBits, xorLength);
2449                     hBitTemp = SelectObject( hMemDC, hXorBits );
2450                     if (DoOffscreen)
2451                         StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2452                                     hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2453                     else
2454                         StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2455                                     hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2456                     SelectObject( hMemDC, hBitTemp );
2457                 }
2458
2459                 DeleteObject( hXorBits );
2460             }
2461         }
2462
2463         result = TRUE;
2464
2465         SetTextColor( hdc, oldFg );
2466         SetBkColor( hdc, oldBg );
2467
2468         if (hAndBits) DeleteObject( hAndBits );
2469         SetStretchBltMode (hdc, nStretchMode);
2470         if (DoOffscreen) {
2471             BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2472             SelectObject(hDC_off, hOld);
2473         }
2474     }
2475     if (hMemDC) DeleteDC( hMemDC );
2476     if (hDC_off) DeleteDC(hDC_off);
2477     if (hB_off) DeleteObject(hB_off);
2478     GlobalUnlock16(HICON_16(hIcon));
2479     return result;
2480 }
2481
2482 /***********************************************************************
2483  *           DIB_FixColorsToLoadflags
2484  *
2485  * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2486  * are in loadflags
2487  */
2488 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2489 {
2490     int colors;
2491     COLORREF c_W, c_S, c_F, c_L, c_C;
2492     int incr,i;
2493     RGBQUAD *ptr;
2494     int bitmap_type;
2495     LONG width;
2496     LONG height;
2497     WORD bpp;
2498     DWORD compr;
2499
2500     if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2501     {
2502         WARN_(resource)("Invalid bitmap\n");
2503         return;
2504     }
2505
2506     if (bpp > 8) return;
2507
2508     if (bitmap_type == 0) /* BITMAPCOREHEADER */
2509     {
2510         incr = 3;
2511         colors = 1 << bpp;
2512     }
2513     else
2514     {
2515         incr = 4;
2516         colors = bmi->bmiHeader.biClrUsed;
2517         if (colors > 256) colors = 256;
2518         if (!colors && (bpp <= 8)) colors = 1 << bpp;
2519     }
2520
2521     c_W = GetSysColor(COLOR_WINDOW);
2522     c_S = GetSysColor(COLOR_3DSHADOW);
2523     c_F = GetSysColor(COLOR_3DFACE);
2524     c_L = GetSysColor(COLOR_3DLIGHT);
2525
2526     if (loadflags & LR_LOADTRANSPARENT) {
2527         switch (bpp) {
2528         case 1: pix = pix >> 7; break;
2529         case 4: pix = pix >> 4; break;
2530         case 8: break;
2531         default:
2532             WARN_(resource)("(%d): Unsupported depth\n", bpp);
2533             return;
2534         }
2535         if (pix >= colors) {
2536             WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2537             return;
2538         }
2539         if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2540         ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2541         ptr->rgbBlue = GetBValue(c_W);
2542         ptr->rgbGreen = GetGValue(c_W);
2543         ptr->rgbRed = GetRValue(c_W);
2544     }
2545     if (loadflags & LR_LOADMAP3DCOLORS)
2546         for (i=0; i<colors; i++) {
2547             ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2548             c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2549             if (c_C == RGB(128, 128, 128)) {
2550                 ptr->rgbRed = GetRValue(c_S);
2551                 ptr->rgbGreen = GetGValue(c_S);
2552                 ptr->rgbBlue = GetBValue(c_S);
2553             } else if (c_C == RGB(192, 192, 192)) {
2554                 ptr->rgbRed = GetRValue(c_F);
2555                 ptr->rgbGreen = GetGValue(c_F);
2556                 ptr->rgbBlue = GetBValue(c_F);
2557             } else if (c_C == RGB(223, 223, 223)) {
2558                 ptr->rgbRed = GetRValue(c_L);
2559                 ptr->rgbGreen = GetGValue(c_L);
2560                 ptr->rgbBlue = GetBValue(c_L);
2561             }
2562         }
2563 }
2564
2565
2566 /**********************************************************************
2567  *       BITMAP_Load
2568  */
2569 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2570                             INT desiredx, INT desiredy, UINT loadflags )
2571 {
2572     HBITMAP hbitmap = 0, orig_bm;
2573     HRSRC hRsrc;
2574     HGLOBAL handle;
2575     char *ptr = NULL;
2576     BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2577     int size;
2578     BYTE pix;
2579     char *bits;
2580     LONG width, height, new_width, new_height;
2581     WORD bpp_dummy;
2582     DWORD compr_dummy;
2583     INT bm_type;
2584     HDC screen_mem_dc = NULL;
2585
2586     if (!(loadflags & LR_LOADFROMFILE))
2587     {
2588         if (!instance)
2589         {
2590             /* OEM bitmap: try to load the resource from user32.dll */
2591             instance = user32_module;
2592         }
2593
2594         if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2595         if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2596
2597         if ((info = LockResource( handle )) == NULL) return 0;
2598     }
2599     else
2600     {
2601         BITMAPFILEHEADER * bmfh;
2602
2603         if (!(ptr = map_fileW( name, NULL ))) return 0;
2604         info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2605         bmfh = (BITMAPFILEHEADER *)ptr;
2606         if (!(  bmfh->bfType == 0x4d42 /* 'BM' */ &&
2607                 bmfh->bfReserved1 == 0 &&
2608                 bmfh->bfReserved2 == 0))
2609         {
2610             WARN("Invalid/unsupported bitmap format!\n");
2611             UnmapViewOfFile( ptr );
2612             return 0;
2613         }
2614     }
2615
2616     size = bitmap_info_size(info, DIB_RGB_COLORS);
2617     fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2618     scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2619
2620     if (!fix_info || !scaled_info) goto end;
2621     memcpy(fix_info, info, size);
2622
2623     pix = *((LPBYTE)info + size);
2624     DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2625
2626     memcpy(scaled_info, fix_info, size);
2627     bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2628                                  &bpp_dummy, &compr_dummy);
2629     if(desiredx != 0)
2630         new_width = desiredx;
2631     else
2632         new_width = width;
2633
2634     if(desiredy != 0)
2635         new_height = height > 0 ? desiredy : -desiredy;
2636     else
2637         new_height = height;
2638
2639     if(bm_type == 0)
2640     {
2641         BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2642         core->bcWidth = new_width;
2643         core->bcHeight = new_height;
2644     }
2645     else
2646     {
2647         scaled_info->bmiHeader.biWidth = new_width;
2648         scaled_info->bmiHeader.biHeight = new_height;
2649     }
2650
2651     if (new_height < 0) new_height = -new_height;
2652
2653     if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2654     if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2655
2656     bits = (char *)info + size;
2657
2658     if (loadflags & LR_CREATEDIBSECTION)
2659     {
2660         scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2661         hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2662     }
2663     else
2664     {
2665         if (is_dib_monochrome(fix_info))
2666             hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2667         else
2668             hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);        
2669     }
2670
2671     orig_bm = SelectObject(screen_mem_dc, hbitmap);
2672     StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2673     SelectObject(screen_mem_dc, orig_bm);
2674
2675 end:
2676     if (screen_mem_dc) DeleteDC(screen_mem_dc);
2677     HeapFree(GetProcessHeap(), 0, scaled_info);
2678     HeapFree(GetProcessHeap(), 0, fix_info);
2679     if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2680
2681     return hbitmap;
2682 }
2683
2684 /**********************************************************************
2685  *              LoadImageA (USER32.@)
2686  *
2687  * See LoadImageW.
2688  */
2689 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2690                               INT desiredx, INT desiredy, UINT loadflags)
2691 {
2692     HANDLE res;
2693     LPWSTR u_name;
2694
2695     if (!HIWORD(name))
2696         return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2697
2698     __TRY {
2699         DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2700         u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2701         MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2702     }
2703     __EXCEPT_PAGE_FAULT {
2704         SetLastError( ERROR_INVALID_PARAMETER );
2705         return 0;
2706     }
2707     __ENDTRY
2708     res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2709     HeapFree(GetProcessHeap(), 0, u_name);
2710     return res;
2711 }
2712
2713
2714 /******************************************************************************
2715  *              LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2716  *
2717  * PARAMS
2718  *    hinst     [I] Handle of instance that contains image
2719  *    name      [I] Name of image
2720  *    type      [I] Type of image
2721  *    desiredx  [I] Desired width
2722  *    desiredy  [I] Desired height
2723  *    loadflags [I] Load flags
2724  *
2725  * RETURNS
2726  *    Success: Handle to newly loaded image
2727  *    Failure: NULL
2728  *
2729  * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2730  */
2731 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2732                 INT desiredx, INT desiredy, UINT loadflags )
2733 {
2734     TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2735                      hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2736
2737     if (loadflags & LR_DEFAULTSIZE) {
2738         if (type == IMAGE_ICON) {
2739             if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2740             if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2741         } else if (type == IMAGE_CURSOR) {
2742             if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2743             if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2744         }
2745     }
2746     if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2747     switch (type) {
2748     case IMAGE_BITMAP:
2749         return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2750
2751     case IMAGE_ICON:
2752         if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2753         if (screen_dc)
2754         {
2755             UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2756             if (palEnts == 0) palEnts = 256;
2757             return CURSORICON_Load(hinst, name, desiredx, desiredy,
2758                                    palEnts, FALSE, loadflags);
2759         }
2760         break;
2761
2762     case IMAGE_CURSOR:
2763         return CURSORICON_Load(hinst, name, desiredx, desiredy,
2764                                1, TRUE, loadflags);
2765     }
2766     return 0;
2767 }
2768
2769 /******************************************************************************
2770  *              CopyImage (USER32.@) Creates new image and copies attributes to it
2771  *
2772  * PARAMS
2773  *    hnd      [I] Handle to image to copy
2774  *    type     [I] Type of image to copy
2775  *    desiredx [I] Desired width of new image
2776  *    desiredy [I] Desired height of new image
2777  *    flags    [I] Copy flags
2778  *
2779  * RETURNS
2780  *    Success: Handle to newly created image
2781  *    Failure: NULL
2782  *
2783  * BUGS
2784  *    Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2785  *    all other versions (95/2000/XP have been tested) ignore it.
2786  *
2787  * NOTES
2788  *    If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2789  *    a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2790  *    the copy will have the same depth as the screen.
2791  *    The content of the image will only be copied if the bit depth of the
2792  *    original image is compatible with the bit depth of the screen, or
2793  *    if the source is a DIB section.
2794  *    The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2795  */
2796 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2797                              INT desiredy, UINT flags )
2798 {
2799     TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2800           hnd, type, desiredx, desiredy, flags);
2801
2802     switch (type)
2803     {
2804         case IMAGE_BITMAP:
2805         {
2806             HBITMAP res = NULL;
2807             DIBSECTION ds;
2808             int objSize;
2809             BITMAPINFO * bi;
2810
2811             objSize = GetObjectW( hnd, sizeof(ds), &ds );
2812             if (!objSize) return 0;
2813             if ((desiredx < 0) || (desiredy < 0)) return 0;
2814
2815             if (flags & LR_COPYFROMRESOURCE)
2816             {
2817                 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2818             }
2819
2820             if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2821             if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2822
2823             /* Allocate memory for a BITMAPINFOHEADER structure and a
2824                color table. The maximum number of colors in a color table
2825                is 256 which corresponds to a bitmap with depth 8.
2826                Bitmaps with higher depths don't have color tables. */
2827             bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2828             if (!bi) return 0;
2829
2830             bi->bmiHeader.biSize        = sizeof(bi->bmiHeader);
2831             bi->bmiHeader.biPlanes      = ds.dsBm.bmPlanes;
2832             bi->bmiHeader.biBitCount    = ds.dsBm.bmBitsPixel;
2833             bi->bmiHeader.biCompression = BI_RGB;
2834
2835             if (flags & LR_CREATEDIBSECTION)
2836             {
2837                 /* Create a DIB section. LR_MONOCHROME is ignored */
2838                 void * bits;
2839                 HDC dc = CreateCompatibleDC(NULL);
2840
2841                 if (objSize == sizeof(DIBSECTION))
2842                 {
2843                     /* The source bitmap is a DIB.
2844                        Get its attributes to create an exact copy */
2845                     memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2846                 }
2847
2848                 /* Get the color table or the color masks */
2849                 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2850
2851                 bi->bmiHeader.biWidth  = desiredx;
2852                 bi->bmiHeader.biHeight = desiredy;
2853                 bi->bmiHeader.biSizeImage = 0;
2854
2855                 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2856                 DeleteDC(dc);
2857             }
2858             else
2859             {
2860                 /* Create a device-dependent bitmap */
2861
2862                 BOOL monochrome = (flags & LR_MONOCHROME);
2863
2864                 if (objSize == sizeof(DIBSECTION))
2865                 {
2866                     /* The source bitmap is a DIB section.
2867                        Get its attributes */
2868                     HDC dc = CreateCompatibleDC(NULL);
2869                     bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2870                     bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2871                     GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2872                     DeleteDC(dc);
2873
2874                     if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2875                     {
2876                         /* Look if the colors of the DIB are black and white */
2877
2878                         monochrome = 
2879                               (bi->bmiColors[0].rgbRed == 0xff
2880                             && bi->bmiColors[0].rgbGreen == 0xff
2881                             && bi->bmiColors[0].rgbBlue == 0xff
2882                             && bi->bmiColors[0].rgbReserved == 0
2883                             && bi->bmiColors[1].rgbRed == 0
2884                             && bi->bmiColors[1].rgbGreen == 0
2885                             && bi->bmiColors[1].rgbBlue == 0
2886                             && bi->bmiColors[1].rgbReserved == 0)
2887                             ||
2888                               (bi->bmiColors[0].rgbRed == 0
2889                             && bi->bmiColors[0].rgbGreen == 0
2890                             && bi->bmiColors[0].rgbBlue == 0
2891                             && bi->bmiColors[0].rgbReserved == 0
2892                             && bi->bmiColors[1].rgbRed == 0xff
2893                             && bi->bmiColors[1].rgbGreen == 0xff
2894                             && bi->bmiColors[1].rgbBlue == 0xff
2895                             && bi->bmiColors[1].rgbReserved == 0);
2896                     }
2897                 }
2898                 else if (!monochrome)
2899                 {
2900                     monochrome = ds.dsBm.bmBitsPixel == 1;
2901                 }
2902
2903                 if (monochrome)
2904                 {
2905                     res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2906                 }
2907                 else
2908                 {
2909                     HDC screenDC = GetDC(NULL);
2910                     res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2911                     ReleaseDC(NULL, screenDC);
2912                 }
2913             }
2914
2915             if (res)
2916             {
2917                 /* Only copy the bitmap if it's a DIB section or if it's
2918                    compatible to the screen */
2919                 BOOL copyContents;
2920
2921                 if (objSize == sizeof(DIBSECTION))
2922                 {
2923                     copyContents = TRUE;
2924                 }
2925                 else
2926                 {
2927                     HDC screenDC = GetDC(NULL);
2928                     int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2929                     ReleaseDC(NULL, screenDC);
2930
2931                     copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2932                 }
2933
2934                 if (copyContents)
2935                 {
2936                     /* The source bitmap may already be selected in a device context,
2937                        use GetDIBits/StretchDIBits and not StretchBlt  */
2938
2939                     HDC dc;
2940                     void * bits;
2941
2942                     dc = CreateCompatibleDC(NULL);
2943
2944                     bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2945                     bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2946                     bi->bmiHeader.biSizeImage = 0;
2947                     bi->bmiHeader.biClrUsed = 0;
2948                     bi->bmiHeader.biClrImportant = 0;
2949
2950                     /* Fill in biSizeImage */
2951                     GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2952                     bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2953
2954                     if (bits)
2955                     {
2956                         HBITMAP oldBmp;
2957
2958                         /* Get the image bits of the source bitmap */
2959                         GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2960
2961                         /* Copy it to the destination bitmap */
2962                         oldBmp = SelectObject(dc, res);
2963                         StretchDIBits(dc, 0, 0, desiredx, desiredy,
2964                                       0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2965                                       bits, bi, DIB_RGB_COLORS, SRCCOPY);
2966                         SelectObject(dc, oldBmp);
2967
2968                         HeapFree(GetProcessHeap(), 0, bits);
2969                     }
2970
2971                     DeleteDC(dc);
2972                 }
2973
2974                 if (flags & LR_COPYDELETEORG)
2975                 {
2976                     DeleteObject(hnd);
2977                 }
2978             }
2979             HeapFree(GetProcessHeap(), 0, bi);
2980             return res;
2981         }
2982         case IMAGE_ICON:
2983                 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2984         case IMAGE_CURSOR:
2985                 /* Should call CURSORICON_ExtCopy but more testing
2986                  * needs to be done before we change this
2987                  */
2988                 if (flags) FIXME("Flags are ignored\n");
2989                 return CopyCursor(hnd);
2990     }
2991     return 0;
2992 }
2993
2994
2995 /******************************************************************************
2996  *              LoadBitmapW (USER32.@) Loads bitmap from the executable file
2997  *
2998  * RETURNS
2999  *    Success: Handle to specified bitmap
3000  *    Failure: NULL
3001  */
3002 HBITMAP WINAPI LoadBitmapW(
3003     HINSTANCE instance, /* [in] Handle to application instance */
3004     LPCWSTR name)         /* [in] Address of bitmap resource name */
3005 {
3006     return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
3007 }
3008
3009 /**********************************************************************
3010  *              LoadBitmapA (USER32.@)
3011  *
3012  * See LoadBitmapW.
3013  */
3014 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
3015 {
3016     return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );
3017 }