2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
7 * 1998 Turchanov Sergey
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.
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.
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
28 * Cursors and icons are stored in a global heap block, with the
31 * CURSORICONINFO info;
35 * The bits structures are in the format of a device-dependent bitmap.
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 :-(
44 #include "wine/port.h"
54 #include "wine/winbase16.h"
55 #include "wine/winuser16.h"
56 #include "wine/exception.h"
57 #include "wine/debug.h"
59 #include "user_private.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
62 WINE_DECLARE_DEBUG_CHANNEL(icon);
63 WINE_DECLARE_DEBUG_CHANNEL(resource);
76 } CURSORICONFILEDIRENTRY;
83 CURSORICONFILEDIRENTRY idEntries[1];
88 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
92 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
95 /**********************************************************************
96 * ICONCACHE for cursors/icons loaded with LR_SHARED.
98 * FIXME: This should not be allocated on the system heap, but on a
99 * subsystem-global heap (i.e. one for all Win16 processes,
100 * and one for each Win32 process).
102 typedef struct tagICONCACHE
104 struct tagICONCACHE *next;
115 static ICONCACHE *IconAnchor = NULL;
117 static CRITICAL_SECTION IconCrst;
118 static CRITICAL_SECTION_DEBUG critsect_debug =
121 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
122 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
124 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
126 static const WORD ICON_HOTSPOT = 0x4242;
129 /***********************************************************************
132 * Helper function to map a file to memory:
134 * [RETURN] ptr - pointer to mapped file
135 * [RETURN] filesize - pointer size of file to be stored if not NULL
137 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
139 HANDLE hFile, hMapping;
142 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
143 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
144 if (hFile != INVALID_HANDLE_VALUE)
146 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
149 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
150 CloseHandle( hMapping );
152 *filesize = GetFileSize( hFile, NULL );
154 CloseHandle( hFile );
160 /***********************************************************************
161 * get_bitmap_width_bytes
163 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
166 static int get_bitmap_width_bytes( int width, int bpp )
171 return 2 * ((width+15) / 16);
173 return 2 * ((width+3) / 4);
178 return width + (width & 1);
185 WARN("Unknown depth %d, please report.\n", bpp );
191 /***********************************************************************
192 * get_dib_width_bytes
194 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
196 static int get_dib_width_bytes( int width, int depth )
202 case 1: words = (width + 31) / 32; break;
203 case 4: words = (width + 7) / 8; break;
204 case 8: words = (width + 3) / 4; break;
206 case 16: words = (width + 1) / 2; break;
207 case 24: words = (width * 3 + 3)/4; break;
209 WARN("(%d): Unsupported depth\n", depth );
218 /***********************************************************************
221 * Return the size of the bitmap info structure including color table.
223 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
225 int colors, masks = 0;
227 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
229 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
230 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
231 return sizeof(BITMAPCOREHEADER) + colors *
232 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
234 else /* assume BITMAPINFOHEADER */
236 colors = info->bmiHeader.biClrUsed;
237 if (colors > 256) /* buffer overflow otherwise */
239 if (!colors && (info->bmiHeader.biBitCount <= 8))
240 colors = 1 << info->bmiHeader.biBitCount;
241 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
242 return sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) + colors *
243 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
248 /***********************************************************************
251 * Returns whether a DIB can be converted to a monochrome DDB.
253 * A DIB can be converted if its color table contains only black and
254 * white. Black must be the first color in the color table.
256 * Note : If the first color in the color table is white followed by
257 * black, we can't convert it to a monochrome DDB with
258 * SetDIBits, because black and white would be inverted.
260 static BOOL is_dib_monochrome( const BITMAPINFO* info )
262 if (info->bmiHeader.biBitCount != 1) return FALSE;
264 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
266 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
268 /* Check if the first color is black */
269 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
273 /* Check if the second color is white */
274 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
275 && (rgb->rgbtBlue == 0xff));
279 else /* assume BITMAPINFOHEADER */
281 const RGBQUAD *rgb = info->bmiColors;
283 /* Check if the first color is black */
284 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
285 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
289 /* Check if the second color is white */
290 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
291 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
297 /***********************************************************************
300 * Get the info from a bitmap header.
301 * Return 1 for INFOHEADER, 0 for COREHEADER,
302 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
304 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
305 LONG *height, WORD *bpp, DWORD *compr )
307 if (header->biSize == sizeof(BITMAPINFOHEADER))
309 *width = header->biWidth;
310 *height = header->biHeight;
311 *bpp = header->biBitCount;
312 *compr = header->biCompression;
315 if (header->biSize == sizeof(BITMAPCOREHEADER))
317 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
318 *width = core->bcWidth;
319 *height = core->bcHeight;
320 *bpp = core->bcBitCount;
324 if (header->biSize == sizeof(BITMAPV4HEADER))
326 const BITMAPV4HEADER *v4hdr = (const BITMAPV4HEADER *)header;
327 *width = v4hdr->bV4Width;
328 *height = v4hdr->bV4Height;
329 *bpp = v4hdr->bV4BitCount;
330 *compr = v4hdr->bV4V4Compression;
333 if (header->biSize == sizeof(BITMAPV5HEADER))
335 const BITMAPV5HEADER *v5hdr = (const BITMAPV5HEADER *)header;
336 *width = v5hdr->bV5Width;
337 *height = v5hdr->bV5Height;
338 *bpp = v5hdr->bV5BitCount;
339 *compr = v5hdr->bV5Compression;
342 ERR("(%d): unknown/wrong size for header\n", header->biSize );
346 /**********************************************************************
347 * CURSORICON_FindSharedIcon
349 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
354 EnterCriticalSection( &IconCrst );
356 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
357 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
364 LeaveCriticalSection( &IconCrst );
369 /*************************************************************************
370 * CURSORICON_FindCache
372 * Given a handle, find the corresponding cache element
375 * Handle [I] handle to an Image
378 * Success: The cache entry
382 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
385 ICONCACHE *pRet=NULL;
386 BOOL IsFound = FALSE;
388 EnterCriticalSection( &IconCrst );
390 for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
392 if ( hIcon == ptr->hIcon )
399 LeaveCriticalSection( &IconCrst );
404 /**********************************************************************
405 * CURSORICON_AddSharedIcon
407 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
409 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
412 ptr->hModule = hModule;
415 ptr->hGroupRsrc = hGroupRsrc;
418 EnterCriticalSection( &IconCrst );
419 ptr->next = IconAnchor;
421 LeaveCriticalSection( &IconCrst );
424 /**********************************************************************
425 * CURSORICON_DelSharedIcon
427 static INT CURSORICON_DelSharedIcon( HICON hIcon )
432 EnterCriticalSection( &IconCrst );
434 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
435 if ( ptr->hIcon == hIcon )
437 if ( ptr->count > 0 ) ptr->count--;
442 LeaveCriticalSection( &IconCrst );
447 /**********************************************************************
450 BOOL get_icon_size( HICON handle, SIZE *size )
452 CURSORICONINFO *info;
454 if (!(info = wow_handlers.get_icon_ptr( handle ))) return FALSE;
455 size->cx = info->nWidth;
456 size->cy = info->nHeight;
457 wow_handlers.release_icon_ptr( handle, info );
462 * The following macro functions account for the irregularities of
463 * accessing cursor and icon resources in files and resource entries.
465 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
466 int *width, int *height, int *bits );
468 /**********************************************************************
469 * CURSORICON_FindBestIcon
471 * Find the icon closest to the requested size and bit depth.
473 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
474 int width, int height, int depth )
476 int i, cx, cy, bits, bestEntry = -1;
477 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
478 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
481 iTotalDiff = 0xFFFFFFFF;
482 iColorDiff = 0xFFFFFFFF;
483 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
485 iTempXDiff = abs(width - cx);
486 iTempYDiff = abs(height - cy);
488 if(iTotalDiff > (iTempXDiff + iTempYDiff))
492 iTotalDiff = iXDiff + iYDiff;
496 /* Find Best Colors for Best Fit */
497 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
499 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
501 iTempColorDiff = abs(depth - bits);
502 if(iColorDiff > iTempColorDiff)
505 iColorDiff = iTempColorDiff;
513 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
514 int *width, int *height, int *bits )
516 CURSORICONDIR *resdir = dir;
519 if ( resdir->idCount <= n )
521 icon = &resdir->idEntries[n].ResInfo.icon;
522 *width = icon->bWidth;
523 *height = icon->bHeight;
524 *bits = resdir->idEntries[n].wBitCount;
528 /**********************************************************************
529 * CURSORICON_FindBestCursor
531 * Find the cursor closest to the requested size.
533 * FIXME: parameter 'color' ignored.
535 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
536 int width, int height, int depth )
538 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
540 /* Double height to account for AND and XOR masks */
544 /* First find the largest one smaller than or equal to the requested size*/
546 maxwidth = maxheight = 0;
547 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
549 if ((cx <= width) && (cy <= height) &&
550 (cx > maxwidth) && (cy > maxheight))
557 if (bestEntry != -1) return bestEntry;
559 /* Now find the smallest one larger than the requested size */
561 maxwidth = maxheight = 255;
562 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
564 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
575 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
576 int *width, int *height, int *bits )
578 CURSORICONDIR *resdir = dir;
581 if ( resdir->idCount <= n )
583 cursor = &resdir->idEntries[n].ResInfo.cursor;
584 *width = cursor->wWidth;
585 *height = cursor->wHeight;
586 *bits = resdir->idEntries[n].wBitCount;
590 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
591 int width, int height, int depth )
595 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
596 width, height, depth );
599 return &dir->idEntries[n];
602 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
603 int width, int height, int depth )
605 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
606 width, height, depth );
609 return &dir->idEntries[n];
612 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
613 int *width, int *height, int *bits )
615 CURSORICONFILEDIR *filedir = dir;
616 CURSORICONFILEDIRENTRY *entry;
618 if ( filedir->idCount <= n )
620 entry = &filedir->idEntries[n];
621 *width = entry->bWidth;
622 *height = entry->bHeight;
623 *bits = entry->bColorCount;
627 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
628 int width, int height, int depth )
630 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
631 width, height, depth );
634 return &dir->idEntries[n];
637 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
638 int width, int height, int depth )
640 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
641 width, height, depth );
644 return &dir->idEntries[n];
647 /***********************************************************************
650 * A helper function that stretches a bitmap buffer into an HBITMAP.
653 * hDest [I] The handle of the destination bitmap.
654 * pDestInfo [I] The BITMAPINFO of the destination bitmap.
655 * pSrcInfo [I] The BITMAPINFO of the source bitmap.
656 * pSrcBits [I] A pointer to the source bitmap buffer.
658 static BOOL stretch_blt_icon(HBITMAP hDest, BITMAPINFO *pDestInfo, BITMAPINFO *pSrcInfo, char *pSrcBits)
662 HDC hdcMem = CreateCompatibleDC(screen_dc);
666 hOld = SelectObject(hdcMem, hDest);
667 res = StretchDIBits(hdcMem,
668 0, 0, pDestInfo->bmiHeader.biWidth, pDestInfo->bmiHeader.biHeight,
669 0, 0, pSrcInfo->bmiHeader.biWidth, pSrcInfo->bmiHeader.biHeight,
670 pSrcBits, pSrcInfo, DIB_RGB_COLORS, SRCCOPY);
671 SelectObject(hdcMem, hOld);
678 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi,
679 POINT16 hotspot, BOOL bIcon,
681 INT width, INT height,
685 int sizeAnd, sizeXor;
686 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
687 BITMAP bmpXor, bmpAnd;
689 BITMAPINFO *pSrcInfo, *pDestInfo;
691 if (dwVersion == 0x00020000)
693 FIXME_(cursor)("\t2.xx resources are not supported\n");
697 /* Check bitmap header */
699 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
700 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
701 bmi->bmiHeader.biCompression != BI_RGB) )
703 WARN_(cursor)("\tinvalid resource bitmap header.\n");
707 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
709 if (!width) width = bmi->bmiHeader.biWidth;
710 if (!height) height = bmi->bmiHeader.biHeight/2;
712 /* Scale the hotspot */
713 if (((bmi->bmiHeader.biHeight/2 != height) || (bmi->bmiHeader.biWidth != width)) &&
714 hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
716 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
717 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
720 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
723 /* Make sure we have room for the monochrome bitmap later on.
724 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
725 * up to and including the biBitCount. In-memory icon resource
726 * format is as follows:
728 * BITMAPINFOHEADER icHeader // DIB header
729 * RGBQUAD icColors[] // Color table
730 * BYTE icXOR[] // DIB bits for XOR mask
731 * BYTE icAND[] // DIB bits for AND mask
734 pSrcInfo = HeapAlloc( GetProcessHeap(), 0,
735 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
736 pDestInfo = HeapAlloc( GetProcessHeap(), 0,
737 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
738 if (pSrcInfo && pDestInfo)
740 memcpy( pSrcInfo, bmi, size );
741 pSrcInfo->bmiHeader.biHeight /= 2;
743 memcpy( pDestInfo, bmi, size );
744 pDestInfo->bmiHeader.biWidth = width;
745 pDestInfo->bmiHeader.biHeight = height;
746 pDestInfo->bmiHeader.biSizeImage = 0;
748 /* Create the XOR bitmap */
749 if(pSrcInfo->bmiHeader.biBitCount == 32)
751 void *pDIBBuffer = NULL;
752 hXorBits = CreateDIBSection(screen_dc, pDestInfo, DIB_RGB_COLORS, &pDIBBuffer, NULL, 0);
756 if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
758 DeleteObject(hXorBits);
765 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
769 if(!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
771 DeleteObject(hXorBits);
779 char* xbits = (char *)bmi + size +
780 get_dib_width_bytes( bmi->bmiHeader.biWidth,
781 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
783 pSrcInfo->bmiHeader.biBitCount = 1;
784 if (pSrcInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
786 RGBQUAD *rgb = pSrcInfo->bmiColors;
788 pSrcInfo->bmiHeader.biClrUsed = pSrcInfo->bmiHeader.biClrImportant = 2;
789 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
790 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
791 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
795 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pSrcInfo) + 1);
797 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
798 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
801 /* Create the AND bitmap */
802 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
804 if(!stretch_blt_icon(hAndBits, pDestInfo, pSrcInfo, xbits))
806 DeleteObject(hAndBits);
812 DeleteObject( hXorBits );
818 HeapFree( GetProcessHeap(), 0, pSrcInfo );
819 HeapFree( GetProcessHeap(), 0, pDestInfo );
822 if( !hXorBits || !hAndBits )
824 WARN_(cursor)("\tunable to create an icon bitmap.\n");
828 /* Now create the CURSORICONINFO structure */
829 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
830 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
831 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
832 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
834 hObj = wow_handlers.alloc_icon_handle( sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
837 CURSORICONINFO *info = wow_handlers.get_icon_ptr( hObj );
839 info->ptHotSpot.x = hotspot.x;
840 info->ptHotSpot.y = hotspot.y;
841 info->nWidth = bmpXor.bmWidth;
842 info->nHeight = bmpXor.bmHeight;
843 info->nWidthBytes = bmpXor.bmWidthBytes;
844 info->bPlanes = bmpXor.bmPlanes;
845 info->bBitsPerPixel = bmpXor.bmBitsPixel;
847 /* Transfer the bitmap bits to the CURSORICONINFO structure */
849 GetBitmapBits( hAndBits, sizeAnd, info + 1 );
850 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
851 wow_handlers.release_icon_ptr( hObj, info );
854 DeleteObject( hAndBits );
855 DeleteObject( hXorBits );
860 /**********************************************************************
861 * .ANI cursor support
863 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
864 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
865 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
867 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
868 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
869 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
870 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
871 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
872 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
874 #define ANI_FLAG_ICON 0x1
875 #define ANI_FLAG_SEQUENCE 0x2
891 const unsigned char *data;
894 static void dump_ani_header( const ani_header *header )
896 TRACE(" header size: %d\n", header->header_size);
897 TRACE(" frames: %d\n", header->num_frames);
898 TRACE(" steps: %d\n", header->num_steps);
899 TRACE(" width: %d\n", header->width);
900 TRACE(" height: %d\n", header->height);
901 TRACE(" bpp: %d\n", header->bpp);
902 TRACE(" planes: %d\n", header->num_planes);
903 TRACE(" display rate: %d\n", header->display_rate);
904 TRACE(" flags: 0x%08x\n", header->flags);
926 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
928 const unsigned char *ptr = parent_chunk->data;
929 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
931 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
935 if ((!chunk_type && *(DWORD *)ptr == chunk_id )
936 || (chunk_type && *(DWORD *)ptr == chunk_type && *((DWORD *)ptr + 2) == chunk_id ))
938 ptr += sizeof(DWORD);
939 chunk->data_size = (*(DWORD *)ptr + 1) & ~1;
940 ptr += sizeof(DWORD);
941 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
947 ptr += sizeof(DWORD);
948 ptr += (*(DWORD *)ptr + 1) & ~1;
949 ptr += sizeof(DWORD);
957 * RIFF:'ACON' RIFF chunk
958 * |- CHUNK:'anih' Header
959 * |- CHUNK:'seq ' Sequence information (optional)
960 * \- LIST:'fram' Frame list
961 * |- CHUNK:icon Cursor frames
966 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
967 INT width, INT height, INT depth )
970 ani_header header = {0};
971 LPBYTE frame_bits = 0;
973 CURSORICONFILEDIRENTRY *entry;
975 riff_chunk_t root_chunk = { bits_size, bits };
976 riff_chunk_t ACON_chunk = {0};
977 riff_chunk_t anih_chunk = {0};
978 riff_chunk_t fram_chunk = {0};
979 const unsigned char *icon_data;
981 TRACE("bits %p, bits_size %d\n", bits, bits_size);
985 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
986 if (!ACON_chunk.data)
988 ERR("Failed to get root chunk.\n");
992 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
993 if (!anih_chunk.data)
995 ERR("Failed to get 'anih' chunk.\n");
998 memcpy( &header, anih_chunk.data, sizeof(header) );
999 dump_ani_header( &header );
1001 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1002 if (!fram_chunk.data)
1004 ERR("Failed to get icon list.\n");
1008 /* FIXME: For now, just load the first frame. Before we can load all the
1009 * frames, we need to write the needed code in wineserver, etc. to handle
1010 * cursors. Once this code is written, we can extend it to support .ani
1011 * cursors and then update user32 and winex11.drv to load all frames.
1013 * Hopefully this will at least make some games (C&C3, etc.) more playable
1016 FIXME("Loading all frames for .ani cursors not implemented.\n");
1017 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1019 entry = CURSORICON_FindBestIconFile( (CURSORICONFILEDIR *) icon_data,
1020 width, height, depth );
1022 frame_bits = HeapAlloc( GetProcessHeap(), 0, entry->dwDIBSize );
1023 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1025 if (!header.width || !header.height)
1027 header.width = entry->bWidth;
1028 header.height = entry->bHeight;
1031 hotspot.x = entry->xHotspot;
1032 hotspot.y = entry->yHotspot;
1034 cursor = CURSORICON_CreateIconFromBMI( (BITMAPINFO *) frame_bits, hotspot,
1035 FALSE, 0x00030000, header.width, header.height, 0 );
1037 HeapFree( GetProcessHeap(), 0, frame_bits );
1043 /**********************************************************************
1044 * CreateIconFromResourceEx (USER32.@)
1046 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1047 * with cbSize parameter as well.
1049 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1050 BOOL bIcon, DWORD dwVersion,
1051 INT width, INT height,
1057 hotspot.x = ICON_HOTSPOT;
1058 hotspot.y = ICON_HOTSPOT;
1060 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1061 bits, cbSize, dwVersion, width, height,
1062 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1065 bmi = (BITMAPINFO *)bits;
1066 else /* get the hotspot */
1068 POINT16 *pt = (POINT16 *)bits;
1070 bmi = (BITMAPINFO *)(pt + 1);
1073 return CURSORICON_CreateIconFromBMI( bmi, hotspot, bIcon, dwVersion,
1074 width, height, cFlag );
1078 /**********************************************************************
1079 * CreateIconFromResource (USER32.@)
1081 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1082 BOOL bIcon, DWORD dwVersion)
1084 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1088 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1089 INT width, INT height, INT depth,
1090 BOOL fCursor, UINT loadflags)
1092 CURSORICONFILEDIRENTRY *entry;
1093 CURSORICONFILEDIR *dir;
1099 TRACE("loading %s\n", debugstr_w( filename ));
1101 bits = map_fileW( filename, &filesize );
1105 /* Check for .ani. */
1106 if (memcmp( bits, "RIFF", 4 ) == 0)
1108 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height,
1113 dir = (CURSORICONFILEDIR*) bits;
1114 if ( filesize < sizeof(*dir) )
1117 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1121 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth );
1123 entry = CURSORICON_FindBestIconFile( dir, width, height, depth );
1128 /* check that we don't run off the end of the file */
1129 if ( entry->dwDIBOffset > filesize )
1131 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1134 /* Set the actual hotspot for cursors and ICON_HOTSPOT for icons. */
1137 hotspot.x = entry->xHotspot;
1138 hotspot.y = entry->yHotspot;
1142 hotspot.x = ICON_HOTSPOT;
1143 hotspot.y = ICON_HOTSPOT;
1145 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset],
1146 hotspot, !fCursor, 0x00030000,
1147 width, height, loadflags );
1149 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1150 UnmapViewOfFile( bits );
1154 /**********************************************************************
1157 * Load a cursor or icon from resource or file.
1159 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1160 INT width, INT height, INT depth,
1161 BOOL fCursor, UINT loadflags)
1165 HRSRC hRsrc, hGroupRsrc;
1167 CURSORICONDIRENTRY *dirEntry;
1172 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1173 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1175 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1176 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1178 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1180 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1181 if (!HIWORD( hInstance )) loadflags &= ~LR_SHARED;
1183 /* Get directory resource ID */
1185 if (!(hRsrc = FindResourceW( hInstance, name,
1186 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1190 /* Find the best entry in the directory */
1192 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1193 if (!(dir = LockResource( handle ))) return 0;
1195 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1197 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1198 if (!dirEntry) return 0;
1199 wResId = dirEntry->wResId;
1200 dwBytesInRes = dirEntry->dwBytesInRes;
1201 FreeResource( handle );
1203 /* Load the resource */
1205 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1206 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1208 /* If shared icon, check whether it was already loaded */
1209 if ( (loadflags & LR_SHARED)
1210 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1213 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1214 bits = LockResource( handle );
1215 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1216 !fCursor, 0x00030000, width, height, loadflags);
1217 FreeResource( handle );
1219 /* If shared icon, add to icon cache */
1221 if ( hIcon && (loadflags & LR_SHARED) )
1222 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1228 /*************************************************************************
1229 * CURSORICON_ExtCopy
1231 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1234 * Handle [I] handle to an Image
1235 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1236 * iDesiredCX [I] The Desired width of the Image
1237 * iDesiredCY [I] The desired height of the Image
1238 * nFlags [I] The flags from CopyImage
1241 * Success: The new handle of the Image
1244 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1245 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1246 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1251 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1252 INT iDesiredCX, INT iDesiredCY,
1257 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1258 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1265 /* Best Fit or Monochrome */
1266 if( (nFlags & LR_COPYFROMRESOURCE
1267 && (iDesiredCX > 0 || iDesiredCY > 0))
1268 || nFlags & LR_MONOCHROME)
1270 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1272 /* Not Found in Cache, then do a straight copy
1274 if(pIconCache == NULL)
1276 hNew = CopyIcon( hIcon );
1277 if(nFlags & LR_COPYFROMRESOURCE)
1279 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1284 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1290 CURSORICONDIR *pDir;
1291 CURSORICONDIRENTRY *pDirEntry;
1292 BOOL bIsIcon = (nType == IMAGE_ICON);
1294 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1296 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1297 || (iDesiredCX == 0 && iDesiredCY == 0))
1299 iDesiredCY = GetSystemMetrics(bIsIcon ?
1300 SM_CYICON : SM_CYCURSOR);
1301 iDesiredCX = GetSystemMetrics(bIsIcon ?
1302 SM_CXICON : SM_CXCURSOR);
1305 /* Retrieve the CURSORICONDIRENTRY
1307 if (!(hMem = LoadResource( pIconCache->hModule ,
1308 pIconCache->hGroupRsrc)))
1312 if (!(pDir = LockResource( hMem )))
1321 pDirEntry = CURSORICON_FindBestIconRes(
1322 pDir, iDesiredCX, iDesiredCY, 256 );
1326 pDirEntry = CURSORICON_FindBestCursorRes(
1327 pDir, iDesiredCX, iDesiredCY, 1);
1330 wResId = pDirEntry->wResId;
1331 dwBytesInRes = pDirEntry->dwBytesInRes;
1334 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1335 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1336 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1340 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1341 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1345 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1350 pBits = LockResource( hMem );
1352 if(nFlags & LR_DEFAULTSIZE)
1354 iTargetCY = GetSystemMetrics(SM_CYICON);
1355 iTargetCX = GetSystemMetrics(SM_CXICON);
1358 /* Create a New Icon with the proper dimension
1360 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1361 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1365 else hNew = CopyIcon( hIcon );
1370 /***********************************************************************
1371 * CreateCursor (USER32.@)
1373 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1374 INT xHotSpot, INT yHotSpot,
1375 INT nWidth, INT nHeight,
1376 LPCVOID lpANDbits, LPCVOID lpXORbits )
1381 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1382 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1385 info.xHotspot = xHotSpot;
1386 info.yHotspot = yHotSpot;
1387 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1388 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1389 hCursor = CreateIconIndirect( &info );
1390 DeleteObject( info.hbmMask );
1391 DeleteObject( info.hbmColor );
1396 /***********************************************************************
1397 * CreateIcon (USER32.@)
1399 * Creates an icon based on the specified bitmaps. The bitmaps must be
1400 * provided in a device dependent format and will be resized to
1401 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1402 * depth. The provided bitmaps must be top-down bitmaps.
1403 * Although Windows does not support 15bpp(*) this API must support it
1404 * for Winelib applications.
1406 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1410 * Success: handle to an icon
1413 * FIXME: Do we need to resize the bitmaps?
1415 HICON WINAPI CreateIcon(
1416 HINSTANCE hInstance, /* [in] the application's hInstance */
1417 INT nWidth, /* [in] the width of the provided bitmaps */
1418 INT nHeight, /* [in] the height of the provided bitmaps */
1419 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1420 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1421 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1422 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1427 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1428 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1431 iinfo.xHotspot = ICON_HOTSPOT;
1432 iinfo.yHotspot = ICON_HOTSPOT;
1433 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1434 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1436 hIcon = CreateIconIndirect( &iinfo );
1438 DeleteObject( iinfo.hbmMask );
1439 DeleteObject( iinfo.hbmColor );
1445 /***********************************************************************
1446 * CopyIcon (USER32.@)
1448 HICON WINAPI CopyIcon( HICON hIcon )
1450 CURSORICONINFO *ptrOld, *ptrNew;
1454 if (!(ptrOld = wow_handlers.get_icon_ptr( hIcon ))) return 0;
1455 size = sizeof(CURSORICONINFO);
1456 size += ptrOld->nHeight * get_bitmap_width_bytes( ptrOld->nWidth, 1 ); /* and bitmap */
1457 size += ptrOld->nHeight * ptrOld->nWidthBytes; /* xor bitmap */
1458 hNew = wow_handlers.alloc_icon_handle( size );
1459 ptrNew = wow_handlers.get_icon_ptr( hNew );
1460 memcpy( ptrNew, ptrOld, size );
1461 wow_handlers.release_icon_ptr( hIcon, ptrOld );
1462 wow_handlers.release_icon_ptr( hNew, ptrNew );
1467 /***********************************************************************
1468 * DestroyIcon (USER32.@)
1470 BOOL WINAPI DestroyIcon( HICON hIcon )
1472 TRACE_(icon)("%p\n", hIcon );
1474 if (CURSORICON_DelSharedIcon( hIcon ) == -1)
1475 wow_handlers.free_icon_handle( hIcon );
1480 /***********************************************************************
1481 * DestroyCursor (USER32.@)
1483 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1485 if (get_user_thread_info()->cursor == hCursor)
1487 WARN_(cursor)("Destroying active cursor!\n" );
1490 return DestroyIcon( hCursor );
1493 /***********************************************************************
1494 * bitmap_has_alpha_channel
1496 * Analyses bits bitmap to determine if alpha data is present.
1499 * bpp [I] The bits-per-pixel of the bitmap
1500 * bitmapBits [I] A pointer to the bitmap data
1501 * bitmapLength [I] The length of the bitmap in bytes
1504 * TRUE if an alpha channel is discovered, FALSE
1507 * Windows' behaviour is that if the icon bitmap is 32-bit and at
1508 * least one pixel has a non-zero alpha, then the bitmap is a
1509 * treated as having an alpha channel transparentcy. Otherwise,
1510 * it's treated as being completely opaque.
1513 static BOOL bitmap_has_alpha_channel( int bpp, unsigned char *bitmapBits,
1514 unsigned int bitmapLength )
1516 /* Detect an alpha channel by looking for non-zero alpha pixels */
1519 unsigned int offset;
1520 for(offset = 3; offset < bitmapLength; offset += 4)
1522 if(bitmapBits[offset] != 0)
1531 /***********************************************************************
1532 * premultiply_alpha_channel
1534 * Premultiplies the color channels of a 32-bit bitmap by the alpha
1535 * channel. This is a necessary step that must be carried out on
1536 * the image before it is passed to GdiAlphaBlend
1539 * destBitmap [I] The destination bitmap buffer
1540 * srcBitmap [I] The source bitmap buffer
1541 * bitmapLength [I] The length of the bitmap in bytes
1544 static void premultiply_alpha_channel( unsigned char *destBitmap,
1545 unsigned char *srcBitmap,
1546 unsigned int bitmapLength )
1548 unsigned char *destPixel = destBitmap;
1549 unsigned char *srcPixel = srcBitmap;
1551 while(destPixel < destBitmap + bitmapLength)
1553 unsigned char alpha = srcPixel[3];
1554 *(destPixel++) = *(srcPixel++) * alpha / 255;
1555 *(destPixel++) = *(srcPixel++) * alpha / 255;
1556 *(destPixel++) = *(srcPixel++) * alpha / 255;
1557 *(destPixel++) = *(srcPixel++);
1561 /***********************************************************************
1562 * DrawIcon (USER32.@)
1564 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1566 CURSORICONINFO *ptr;
1568 HBITMAP hXorBits = NULL, hAndBits = NULL, hBitTemp = NULL;
1569 COLORREF oldFg, oldBg;
1570 unsigned char *xorBitmapBits;
1571 unsigned int dibLength;
1573 TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);
1575 if (!(ptr = wow_handlers.get_icon_ptr( hIcon ))) return FALSE;
1576 if (!(hMemDC = CreateCompatibleDC( hdc )))
1578 wow_handlers.release_icon_ptr( hIcon, ptr );
1582 dibLength = ptr->nHeight * get_bitmap_width_bytes(
1583 ptr->nWidth, ptr->bBitsPerPixel);
1585 xorBitmapBits = (unsigned char *)(ptr + 1) + ptr->nHeight *
1586 get_bitmap_width_bytes(ptr->nWidth, 1);
1588 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1589 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1591 if(bitmap_has_alpha_channel(ptr->bBitsPerPixel, xorBitmapBits, dibLength))
1593 BITMAPINFOHEADER bmih;
1594 unsigned char *dibBits;
1596 memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
1597 bmih.biSize = sizeof(BITMAPINFOHEADER);
1598 bmih.biWidth = ptr->nWidth;
1599 bmih.biHeight = -ptr->nHeight;
1600 bmih.biPlanes = ptr->bPlanes;
1601 bmih.biBitCount = 32;
1602 bmih.biCompression = BI_RGB;
1604 hXorBits = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
1605 (void*)&dibBits, NULL, 0);
1607 if (hXorBits && dibBits)
1609 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
1611 /* Do the alpha blending render */
1612 premultiply_alpha_channel(dibBits, xorBitmapBits, dibLength);
1613 hBitTemp = SelectObject( hMemDC, hXorBits );
1614 /* Destination width/height has to be "System Large" size */
1615 GdiAlphaBlend(hdc, x, y, GetSystemMetrics(SM_CXICON),
1616 GetSystemMetrics(SM_CYICON), hMemDC,
1617 0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
1618 SelectObject( hMemDC, hBitTemp );
1623 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1, ptr + 1 );
1624 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1625 ptr->bBitsPerPixel, xorBitmapBits);
1627 if (hXorBits && hAndBits)
1629 hBitTemp = SelectObject( hMemDC, hAndBits );
1630 StretchBlt( hdc, x, y, GetSystemMetrics(SM_CXICON),
1631 GetSystemMetrics(SM_CYICON), hMemDC, 0, 0,
1632 ptr->nWidth, ptr->nHeight, SRCAND );
1633 SelectObject( hMemDC, hXorBits );
1634 StretchBlt( hdc, x, y, GetSystemMetrics(SM_CXICON),
1635 GetSystemMetrics(SM_CYICON), hMemDC, 0, 0,
1636 ptr->nWidth, ptr->nHeight, SRCINVERT );
1637 SelectObject( hMemDC, hBitTemp );
1642 if (hXorBits) DeleteObject( hXorBits );
1643 if (hAndBits) DeleteObject( hAndBits );
1644 wow_handlers.release_icon_ptr( hIcon, ptr );
1645 SetTextColor( hdc, oldFg );
1646 SetBkColor( hdc, oldBg );
1650 /***********************************************************************
1651 * SetCursor (USER32.@)
1653 * Set the cursor shape.
1656 * A handle to the previous cursor shape.
1658 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1660 struct user_thread_info *thread_info = get_user_thread_info();
1663 if (hCursor == thread_info->cursor) return hCursor; /* No change */
1664 TRACE("%p\n", hCursor);
1665 hOldCursor = thread_info->cursor;
1666 thread_info->cursor = hCursor;
1667 /* Change the cursor shape only if it is visible */
1668 if (thread_info->cursor_count >= 0)
1670 CURSORICONINFO *info = wow_handlers.get_icon_ptr( hCursor );
1671 /* release before calling driver (FIXME) */
1672 if (info) wow_handlers.release_icon_ptr( hCursor, info );
1673 USER_Driver->pSetCursor( info );
1678 /***********************************************************************
1679 * ShowCursor (USER32.@)
1681 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1683 struct user_thread_info *thread_info = get_user_thread_info();
1685 TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
1689 if (++thread_info->cursor_count == 0) /* Show it */
1691 CURSORICONINFO *info = wow_handlers.get_icon_ptr( thread_info->cursor );
1692 /* release before calling driver (FIXME) */
1693 if (info) wow_handlers.release_icon_ptr( thread_info->cursor, info );
1694 USER_Driver->pSetCursor( info );
1699 if (--thread_info->cursor_count == -1) /* Hide it */
1700 USER_Driver->pSetCursor( NULL );
1702 return thread_info->cursor_count;
1705 /***********************************************************************
1706 * GetCursor (USER32.@)
1708 HCURSOR WINAPI GetCursor(void)
1710 return get_user_thread_info()->cursor;
1714 /***********************************************************************
1715 * ClipCursor (USER32.@)
1717 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1721 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1722 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1723 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1724 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1726 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1727 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1729 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1730 CURSOR_ClipRect = virt;
1732 USER_Driver->pClipCursor( rect );
1737 /***********************************************************************
1738 * GetClipCursor (USER32.@)
1740 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1742 /* If this is first time - initialize the rect */
1743 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1745 return CopyRect( rect, &CURSOR_ClipRect );
1749 /***********************************************************************
1750 * SetSystemCursor (USER32.@)
1752 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1754 FIXME("(%p,%08x),stub!\n", hcur, id);
1759 /**********************************************************************
1760 * LookupIconIdFromDirectoryEx (USER32.@)
1762 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1763 INT width, INT height, UINT cFlag )
1765 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1767 if( dir && !dir->idReserved && (dir->idType & 3) )
1769 CURSORICONDIRENTRY* entry;
1771 const HDC hdc = GetDC(0);
1772 const int depth = (cFlag & LR_MONOCHROME) ?
1773 1 : GetDeviceCaps(hdc, BITSPIXEL);
1777 entry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1779 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1781 if( entry ) retVal = entry->wResId;
1783 else WARN_(cursor)("invalid resource directory\n");
1787 /**********************************************************************
1788 * LookupIconIdFromDirectory (USER32.@)
1790 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1792 return LookupIconIdFromDirectoryEx( dir, bIcon,
1793 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1794 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1797 /***********************************************************************
1798 * LoadCursorW (USER32.@)
1800 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1802 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1804 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1805 LR_SHARED | LR_DEFAULTSIZE );
1808 /***********************************************************************
1809 * LoadCursorA (USER32.@)
1811 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1813 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1815 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1816 LR_SHARED | LR_DEFAULTSIZE );
1819 /***********************************************************************
1820 * LoadCursorFromFileW (USER32.@)
1822 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1824 TRACE("%s\n", debugstr_w(name));
1826 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1827 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1830 /***********************************************************************
1831 * LoadCursorFromFileA (USER32.@)
1833 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1835 TRACE("%s\n", debugstr_a(name));
1837 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1838 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1841 /***********************************************************************
1842 * LoadIconW (USER32.@)
1844 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1846 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1848 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1849 LR_SHARED | LR_DEFAULTSIZE );
1852 /***********************************************************************
1853 * LoadIconA (USER32.@)
1855 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1857 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1859 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1860 LR_SHARED | LR_DEFAULTSIZE );
1863 /**********************************************************************
1864 * GetIconInfo (USER32.@)
1866 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1868 CURSORICONINFO *ciconinfo;
1871 if (!(ciconinfo = wow_handlers.get_icon_ptr( hIcon ))) return FALSE;
1873 TRACE("%p => %dx%d, %d bpp\n", hIcon,
1874 ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
1876 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1877 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1879 iconinfo->fIcon = TRUE;
1880 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1881 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1885 iconinfo->fIcon = FALSE;
1886 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1887 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1890 height = ciconinfo->nHeight;
1892 if (ciconinfo->bBitsPerPixel > 1)
1894 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
1895 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1896 (char *)(ciconinfo + 1)
1897 + ciconinfo->nHeight *
1898 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1902 iconinfo->hbmColor = 0;
1906 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
1907 1, 1, ciconinfo + 1);
1908 wow_handlers.release_icon_ptr( hIcon, ciconinfo );
1913 /**********************************************************************
1914 * CreateIconIndirect (USER32.@)
1916 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1921 int xor_objsize = 0, sizeXor = 0, sizeAnd, planes, bpp;
1923 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1924 iconinfo->hbmColor, iconinfo->hbmMask,
1925 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1927 if (!iconinfo->hbmMask) return 0;
1929 planes = GetDeviceCaps( screen_dc, PLANES );
1930 bpp = GetDeviceCaps( screen_dc, BITSPIXEL );
1932 if (iconinfo->hbmColor)
1934 xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1935 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1936 bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
1937 bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
1938 /* we can use either depth 1 or screen depth for xor bitmap */
1939 if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
1940 sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
1942 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1943 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1944 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1945 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1947 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
1949 hObj = wow_handlers.alloc_icon_handle( sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1952 CURSORICONINFO *info = wow_handlers.get_icon_ptr( hObj );
1954 /* If we are creating an icon, the hotspot is unused */
1955 if (iconinfo->fIcon)
1957 info->ptHotSpot.x = ICON_HOTSPOT;
1958 info->ptHotSpot.y = ICON_HOTSPOT;
1962 info->ptHotSpot.x = iconinfo->xHotspot;
1963 info->ptHotSpot.y = iconinfo->yHotspot;
1966 if (iconinfo->hbmColor)
1968 info->nWidth = bmpXor.dsBm.bmWidth;
1969 info->nHeight = bmpXor.dsBm.bmHeight;
1970 info->nWidthBytes = bmpXor.dsBm.bmWidthBytes;
1971 info->bPlanes = planes;
1972 info->bBitsPerPixel = bpp;
1976 info->nWidth = bmpAnd.bmWidth;
1977 info->nHeight = bmpAnd.bmHeight / 2;
1978 info->nWidthBytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
1980 info->bBitsPerPixel = 1;
1983 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1985 /* Some apps pass a color bitmap as a mask, convert it to b/w */
1986 if (bmpAnd.bmBitsPixel == 1)
1988 GetBitmapBits( iconinfo->hbmMask, sizeAnd, info + 1 );
1992 HDC hdc_mem, hdc_mem2;
1993 HBITMAP hbmp_mem_old, hbmp_mem2_old, hbmp_mono;
1995 hdc_mem = CreateCompatibleDC( 0 );
1996 hdc_mem2 = CreateCompatibleDC( 0 );
1998 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
2000 hbmp_mem_old = SelectObject( hdc_mem, iconinfo->hbmMask );
2001 hbmp_mem2_old = SelectObject( hdc_mem2, hbmp_mono );
2003 BitBlt( hdc_mem2, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc_mem, 0, 0, SRCCOPY );
2005 SelectObject( hdc_mem, hbmp_mem_old );
2006 SelectObject( hdc_mem2, hbmp_mem2_old );
2008 DeleteDC( hdc_mem );
2009 DeleteDC( hdc_mem2 );
2011 GetBitmapBits( hbmp_mono, sizeAnd, info + 1 );
2012 DeleteObject( hbmp_mono );
2015 if (iconinfo->hbmColor)
2017 char *dst_bits = (char*)(info + 1) + sizeAnd;
2019 if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
2020 GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
2024 int dib_width = get_dib_width_bytes( info->nWidth, info->bBitsPerPixel );
2025 int bitmap_width = get_bitmap_width_bytes( info->nWidth, info->bBitsPerPixel );
2027 bminfo.bmiHeader.biSize = sizeof(bminfo);
2028 bminfo.bmiHeader.biWidth = info->nWidth;
2029 bminfo.bmiHeader.biHeight = info->nHeight;
2030 bminfo.bmiHeader.biPlanes = info->bPlanes;
2031 bminfo.bmiHeader.biBitCount = info->bBitsPerPixel;
2032 bminfo.bmiHeader.biCompression = BI_RGB;
2033 bminfo.bmiHeader.biSizeImage = info->nHeight * dib_width;
2034 bminfo.bmiHeader.biXPelsPerMeter = 0;
2035 bminfo.bmiHeader.biYPelsPerMeter = 0;
2036 bminfo.bmiHeader.biClrUsed = 0;
2037 bminfo.bmiHeader.biClrImportant = 0;
2039 /* swap lines for dib sections */
2040 if (xor_objsize == sizeof(DIBSECTION))
2041 bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;
2043 if (dib_width != bitmap_width) /* need to fixup alignment */
2045 char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );
2047 if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2048 src_bits, &bminfo, DIB_RGB_COLORS ))
2051 for (y = 0; y < info->nHeight; y++)
2052 memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
2054 HeapFree( GetProcessHeap(), 0, src_bits );
2057 GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2058 dst_bits, &bminfo, DIB_RGB_COLORS );
2061 wow_handlers.release_icon_ptr( hObj, info );
2066 /******************************************************************************
2067 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2070 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2073 * hdc [I] Handle to device context
2074 * x0 [I] X coordinate of upper left corner
2075 * y0 [I] Y coordinate of upper left corner
2076 * hIcon [I] Handle to icon to draw
2077 * cxWidth [I] Width of icon
2078 * cyWidth [I] Height of icon
2079 * istep [I] Index of frame in animated cursor
2080 * hbr [I] Handle to background brush
2081 * flags [I] Icon-drawing flags
2087 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2088 INT cxWidth, INT cyWidth, UINT istep,
2089 HBRUSH hbr, UINT flags )
2091 CURSORICONINFO *ptr;
2092 HDC hDC_off = 0, hMemDC;
2093 BOOL result = FALSE, DoOffscreen;
2094 HBITMAP hB_off = 0, hOld = 0;
2095 unsigned char *xorBitmapBits;
2096 unsigned int xorLength;
2097 BOOL has_alpha = FALSE;
2099 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2100 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2102 if (!(ptr = wow_handlers.get_icon_ptr( hIcon ))) return FALSE;
2103 if (!(hMemDC = CreateCompatibleDC( hdc )))
2105 wow_handlers.release_icon_ptr( hIcon, ptr );
2110 FIXME_(icon)("Ignoring istep=%d\n", istep);
2111 if (flags & DI_NOMIRROR)
2112 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2114 xorLength = ptr->nHeight * get_bitmap_width_bytes(
2115 ptr->nWidth, ptr->bBitsPerPixel);
2116 xorBitmapBits = (unsigned char *)(ptr + 1) + ptr->nHeight *
2117 get_bitmap_width_bytes(ptr->nWidth, 1);
2119 if (flags & DI_IMAGE)
2120 has_alpha = bitmap_has_alpha_channel(
2121 ptr->bBitsPerPixel, xorBitmapBits, xorLength);
2123 /* Calculate the size of the destination image. */
2126 if (flags & DI_DEFAULTSIZE)
2127 cxWidth = GetSystemMetrics (SM_CXICON);
2129 cxWidth = ptr->nWidth;
2133 if (flags & DI_DEFAULTSIZE)
2134 cyWidth = GetSystemMetrics (SM_CYICON);
2136 cyWidth = ptr->nHeight;
2139 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2149 hDC_off = CreateCompatibleDC(hdc);
2150 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2151 if (hDC_off && hB_off) {
2152 hOld = SelectObject(hDC_off, hB_off);
2153 FillRect(hDC_off, &r, hbr);
2157 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2160 HBITMAP hXorBits = NULL, hAndBits = NULL;
2161 COLORREF oldFg, oldBg;
2164 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2166 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2167 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2169 if (((flags & DI_MASK) && !(flags & DI_IMAGE)) ||
2170 ((flags & DI_MASK) && !has_alpha))
2172 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight, 1, 1, ptr + 1 );
2175 hBitTemp = SelectObject( hMemDC, hAndBits );
2177 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2178 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2180 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2181 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2182 SelectObject( hMemDC, hBitTemp );
2186 if (flags & DI_IMAGE)
2188 BITMAPINFOHEADER bmih;
2189 unsigned char *dibBits;
2191 memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
2192 bmih.biSize = sizeof(BITMAPINFOHEADER);
2193 bmih.biWidth = ptr->nWidth;
2194 bmih.biHeight = -ptr->nHeight;
2195 bmih.biPlanes = ptr->bPlanes;
2196 bmih.biBitCount = ptr->bBitsPerPixel;
2197 bmih.biCompression = BI_RGB;
2199 hXorBits = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
2200 (void*)&dibBits, NULL, 0);
2202 if (hXorBits && dibBits)
2206 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2208 /* Do the alpha blending render */
2209 premultiply_alpha_channel(dibBits, xorBitmapBits, xorLength);
2210 hBitTemp = SelectObject( hMemDC, hXorBits );
2213 GdiAlphaBlend(hDC_off, 0, 0, cxWidth, cyWidth, hMemDC,
2214 0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
2216 GdiAlphaBlend(hdc, x0, y0, cxWidth, cyWidth, hMemDC,
2217 0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
2219 SelectObject( hMemDC, hBitTemp );
2223 memcpy(dibBits, xorBitmapBits, xorLength);
2224 hBitTemp = SelectObject( hMemDC, hXorBits );
2226 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2227 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2229 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2230 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2231 SelectObject( hMemDC, hBitTemp );
2234 DeleteObject( hXorBits );
2240 SetTextColor( hdc, oldFg );
2241 SetBkColor( hdc, oldBg );
2243 if (hAndBits) DeleteObject( hAndBits );
2244 SetStretchBltMode (hdc, nStretchMode);
2246 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2247 SelectObject(hDC_off, hOld);
2250 if (hMemDC) DeleteDC( hMemDC );
2251 if (hDC_off) DeleteDC(hDC_off);
2252 if (hB_off) DeleteObject(hB_off);
2253 wow_handlers.release_icon_ptr( hIcon, ptr );
2257 /***********************************************************************
2258 * DIB_FixColorsToLoadflags
2260 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2263 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2266 COLORREF c_W, c_S, c_F, c_L, c_C;
2275 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2277 WARN_(resource)("Invalid bitmap\n");
2281 if (bpp > 8) return;
2283 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2291 colors = bmi->bmiHeader.biClrUsed;
2292 if (colors > 256) colors = 256;
2293 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2296 c_W = GetSysColor(COLOR_WINDOW);
2297 c_S = GetSysColor(COLOR_3DSHADOW);
2298 c_F = GetSysColor(COLOR_3DFACE);
2299 c_L = GetSysColor(COLOR_3DLIGHT);
2301 if (loadflags & LR_LOADTRANSPARENT) {
2303 case 1: pix = pix >> 7; break;
2304 case 4: pix = pix >> 4; break;
2307 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2310 if (pix >= colors) {
2311 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2314 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2315 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2316 ptr->rgbBlue = GetBValue(c_W);
2317 ptr->rgbGreen = GetGValue(c_W);
2318 ptr->rgbRed = GetRValue(c_W);
2320 if (loadflags & LR_LOADMAP3DCOLORS)
2321 for (i=0; i<colors; i++) {
2322 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2323 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2324 if (c_C == RGB(128, 128, 128)) {
2325 ptr->rgbRed = GetRValue(c_S);
2326 ptr->rgbGreen = GetGValue(c_S);
2327 ptr->rgbBlue = GetBValue(c_S);
2328 } else if (c_C == RGB(192, 192, 192)) {
2329 ptr->rgbRed = GetRValue(c_F);
2330 ptr->rgbGreen = GetGValue(c_F);
2331 ptr->rgbBlue = GetBValue(c_F);
2332 } else if (c_C == RGB(223, 223, 223)) {
2333 ptr->rgbRed = GetRValue(c_L);
2334 ptr->rgbGreen = GetGValue(c_L);
2335 ptr->rgbBlue = GetBValue(c_L);
2341 /**********************************************************************
2344 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2345 INT desiredx, INT desiredy, UINT loadflags )
2347 HBITMAP hbitmap = 0, orig_bm;
2351 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2355 LONG width, height, new_width, new_height;
2359 HDC screen_mem_dc = NULL;
2361 if (!(loadflags & LR_LOADFROMFILE))
2365 /* OEM bitmap: try to load the resource from user32.dll */
2366 instance = user32_module;
2369 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2370 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2372 if ((info = LockResource( handle )) == NULL) return 0;
2376 BITMAPFILEHEADER * bmfh;
2378 if (!(ptr = map_fileW( name, NULL ))) return 0;
2379 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2380 bmfh = (BITMAPFILEHEADER *)ptr;
2381 if (!( bmfh->bfType == 0x4d42 /* 'BM' */ &&
2382 bmfh->bfReserved1 == 0 &&
2383 bmfh->bfReserved2 == 0))
2385 WARN("Invalid/unsupported bitmap format!\n");
2386 UnmapViewOfFile( ptr );
2391 size = bitmap_info_size(info, DIB_RGB_COLORS);
2392 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2393 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2395 if (!fix_info || !scaled_info) goto end;
2396 memcpy(fix_info, info, size);
2398 pix = *((LPBYTE)info + size);
2399 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2401 memcpy(scaled_info, fix_info, size);
2402 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2403 &bpp_dummy, &compr_dummy);
2405 new_width = desiredx;
2410 new_height = height > 0 ? desiredy : -desiredy;
2412 new_height = height;
2416 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2417 core->bcWidth = new_width;
2418 core->bcHeight = new_height;
2422 scaled_info->bmiHeader.biWidth = new_width;
2423 scaled_info->bmiHeader.biHeight = new_height;
2426 if (new_height < 0) new_height = -new_height;
2428 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2429 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2431 bits = (char *)info + size;
2433 if (loadflags & LR_CREATEDIBSECTION)
2435 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2436 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2440 if (is_dib_monochrome(fix_info))
2441 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2443 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2446 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2447 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2448 SelectObject(screen_mem_dc, orig_bm);
2451 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2452 HeapFree(GetProcessHeap(), 0, scaled_info);
2453 HeapFree(GetProcessHeap(), 0, fix_info);
2454 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2459 /**********************************************************************
2460 * LoadImageA (USER32.@)
2464 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2465 INT desiredx, INT desiredy, UINT loadflags)
2471 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2474 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2475 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2476 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2478 __EXCEPT_PAGE_FAULT {
2479 SetLastError( ERROR_INVALID_PARAMETER );
2483 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2484 HeapFree(GetProcessHeap(), 0, u_name);
2489 /******************************************************************************
2490 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2493 * hinst [I] Handle of instance that contains image
2494 * name [I] Name of image
2495 * type [I] Type of image
2496 * desiredx [I] Desired width
2497 * desiredy [I] Desired height
2498 * loadflags [I] Load flags
2501 * Success: Handle to newly loaded image
2504 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2506 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2507 INT desiredx, INT desiredy, UINT loadflags )
2509 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2510 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2512 if (loadflags & LR_DEFAULTSIZE) {
2513 if (type == IMAGE_ICON) {
2514 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2515 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2516 } else if (type == IMAGE_CURSOR) {
2517 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2518 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2521 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2524 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2527 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2530 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2531 GetDeviceCaps(screen_dc, BITSPIXEL),
2537 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2538 1, TRUE, loadflags);
2543 /******************************************************************************
2544 * CopyImage (USER32.@) Creates new image and copies attributes to it
2547 * hnd [I] Handle to image to copy
2548 * type [I] Type of image to copy
2549 * desiredx [I] Desired width of new image
2550 * desiredy [I] Desired height of new image
2551 * flags [I] Copy flags
2554 * Success: Handle to newly created image
2558 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2559 * all other versions (95/2000/XP have been tested) ignore it.
2562 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2563 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2564 * the copy will have the same depth as the screen.
2565 * The content of the image will only be copied if the bit depth of the
2566 * original image is compatible with the bit depth of the screen, or
2567 * if the source is a DIB section.
2568 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2570 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2571 INT desiredy, UINT flags )
2573 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2574 hnd, type, desiredx, desiredy, flags);
2585 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2586 if (!objSize) return 0;
2587 if ((desiredx < 0) || (desiredy < 0)) return 0;
2589 if (flags & LR_COPYFROMRESOURCE)
2591 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2594 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2595 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2597 /* Allocate memory for a BITMAPINFOHEADER structure and a
2598 color table. The maximum number of colors in a color table
2599 is 256 which corresponds to a bitmap with depth 8.
2600 Bitmaps with higher depths don't have color tables. */
2601 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2604 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2605 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2606 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2607 bi->bmiHeader.biCompression = BI_RGB;
2609 if (flags & LR_CREATEDIBSECTION)
2611 /* Create a DIB section. LR_MONOCHROME is ignored */
2613 HDC dc = CreateCompatibleDC(NULL);
2615 if (objSize == sizeof(DIBSECTION))
2617 /* The source bitmap is a DIB.
2618 Get its attributes to create an exact copy */
2619 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2622 /* Get the color table or the color masks */
2623 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2625 bi->bmiHeader.biWidth = desiredx;
2626 bi->bmiHeader.biHeight = desiredy;
2627 bi->bmiHeader.biSizeImage = 0;
2629 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2634 /* Create a device-dependent bitmap */
2636 BOOL monochrome = (flags & LR_MONOCHROME);
2638 if (objSize == sizeof(DIBSECTION))
2640 /* The source bitmap is a DIB section.
2641 Get its attributes */
2642 HDC dc = CreateCompatibleDC(NULL);
2643 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2644 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2645 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2648 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2650 /* Look if the colors of the DIB are black and white */
2653 (bi->bmiColors[0].rgbRed == 0xff
2654 && bi->bmiColors[0].rgbGreen == 0xff
2655 && bi->bmiColors[0].rgbBlue == 0xff
2656 && bi->bmiColors[0].rgbReserved == 0
2657 && bi->bmiColors[1].rgbRed == 0
2658 && bi->bmiColors[1].rgbGreen == 0
2659 && bi->bmiColors[1].rgbBlue == 0
2660 && bi->bmiColors[1].rgbReserved == 0)
2662 (bi->bmiColors[0].rgbRed == 0
2663 && bi->bmiColors[0].rgbGreen == 0
2664 && bi->bmiColors[0].rgbBlue == 0
2665 && bi->bmiColors[0].rgbReserved == 0
2666 && bi->bmiColors[1].rgbRed == 0xff
2667 && bi->bmiColors[1].rgbGreen == 0xff
2668 && bi->bmiColors[1].rgbBlue == 0xff
2669 && bi->bmiColors[1].rgbReserved == 0);
2672 else if (!monochrome)
2674 monochrome = ds.dsBm.bmBitsPixel == 1;
2679 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2683 HDC screenDC = GetDC(NULL);
2684 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2685 ReleaseDC(NULL, screenDC);
2691 /* Only copy the bitmap if it's a DIB section or if it's
2692 compatible to the screen */
2695 if (objSize == sizeof(DIBSECTION))
2697 copyContents = TRUE;
2701 HDC screenDC = GetDC(NULL);
2702 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2703 ReleaseDC(NULL, screenDC);
2705 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2710 /* The source bitmap may already be selected in a device context,
2711 use GetDIBits/StretchDIBits and not StretchBlt */
2716 dc = CreateCompatibleDC(NULL);
2718 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2719 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2720 bi->bmiHeader.biSizeImage = 0;
2721 bi->bmiHeader.biClrUsed = 0;
2722 bi->bmiHeader.biClrImportant = 0;
2724 /* Fill in biSizeImage */
2725 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2726 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2732 /* Get the image bits of the source bitmap */
2733 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2735 /* Copy it to the destination bitmap */
2736 oldBmp = SelectObject(dc, res);
2737 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2738 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2739 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2740 SelectObject(dc, oldBmp);
2742 HeapFree(GetProcessHeap(), 0, bits);
2748 if (flags & LR_COPYDELETEORG)
2753 HeapFree(GetProcessHeap(), 0, bi);
2757 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2759 /* Should call CURSORICON_ExtCopy but more testing
2760 * needs to be done before we change this
2762 if (flags) FIXME("Flags are ignored\n");
2763 return CopyCursor(hnd);
2769 /******************************************************************************
2770 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2773 * Success: Handle to specified bitmap
2776 HBITMAP WINAPI LoadBitmapW(
2777 HINSTANCE instance, /* [in] Handle to application instance */
2778 LPCWSTR name) /* [in] Address of bitmap resource name */
2780 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2783 /**********************************************************************
2784 * LoadBitmapA (USER32.@)
2788 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2790 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );