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"
58 #include "user_private.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
61 WINE_DECLARE_DEBUG_CHANNEL(icon);
62 WINE_DECLARE_DEBUG_CHANNEL(resource);
75 } CURSORICONFILEDIRENTRY;
82 CURSORICONFILEDIRENTRY idEntries[1];
87 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
91 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
93 static HICON alloc_icon_handle( unsigned int size )
95 HGLOBAL16 handle = GlobalAlloc16( GMEM_MOVEABLE, size );
96 FarSetOwner16( handle, 0 );
97 return HICON_32( handle );
100 static CURSORICONINFO *get_icon_ptr( HICON handle )
102 return GlobalLock16( HICON_16(handle) );
105 static void release_icon_ptr( HICON handle, CURSORICONINFO *ptr )
107 GlobalUnlock16( HICON_16(handle) );
110 static int free_icon_handle( HICON handle )
112 return GlobalFree16( HICON_16(handle) );
116 /**********************************************************************
117 * ICONCACHE for cursors/icons loaded with LR_SHARED.
119 * FIXME: This should not be allocated on the system heap, but on a
120 * subsystem-global heap (i.e. one for all Win16 processes,
121 * and one for each Win32 process).
123 typedef struct tagICONCACHE
125 struct tagICONCACHE *next;
136 static ICONCACHE *IconAnchor = NULL;
138 static CRITICAL_SECTION IconCrst;
139 static CRITICAL_SECTION_DEBUG critsect_debug =
142 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
143 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
145 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
147 static const WORD ICON_HOTSPOT = 0x4242;
150 /***********************************************************************
153 * Helper function to map a file to memory:
155 * [RETURN] ptr - pointer to mapped file
156 * [RETURN] filesize - pointer size of file to be stored if not NULL
158 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
160 HANDLE hFile, hMapping;
163 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
164 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
165 if (hFile != INVALID_HANDLE_VALUE)
167 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
170 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
171 CloseHandle( hMapping );
173 *filesize = GetFileSize( hFile, NULL );
175 CloseHandle( hFile );
181 /***********************************************************************
182 * get_bitmap_width_bytes
184 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
187 static int get_bitmap_width_bytes( int width, int bpp )
192 return 2 * ((width+15) / 16);
194 return 2 * ((width+3) / 4);
199 return width + (width & 1);
206 WARN("Unknown depth %d, please report.\n", bpp );
212 /***********************************************************************
213 * get_dib_width_bytes
215 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
217 static int get_dib_width_bytes( int width, int depth )
223 case 1: words = (width + 31) / 32; break;
224 case 4: words = (width + 7) / 8; break;
225 case 8: words = (width + 3) / 4; break;
227 case 16: words = (width + 1) / 2; break;
228 case 24: words = (width * 3 + 3)/4; break;
230 WARN("(%d): Unsupported depth\n", depth );
239 /***********************************************************************
242 * Return the size of the bitmap info structure including color table.
244 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
246 int colors, masks = 0;
248 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
250 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
251 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
252 return sizeof(BITMAPCOREHEADER) + colors *
253 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
255 else /* assume BITMAPINFOHEADER */
257 colors = info->bmiHeader.biClrUsed;
258 if (colors > 256) /* buffer overflow otherwise */
260 if (!colors && (info->bmiHeader.biBitCount <= 8))
261 colors = 1 << info->bmiHeader.biBitCount;
262 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
263 return sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) + colors *
264 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
269 /***********************************************************************
272 * Returns whether a DIB can be converted to a monochrome DDB.
274 * A DIB can be converted if its color table contains only black and
275 * white. Black must be the first color in the color table.
277 * Note : If the first color in the color table is white followed by
278 * black, we can't convert it to a monochrome DDB with
279 * SetDIBits, because black and white would be inverted.
281 static BOOL is_dib_monochrome( const BITMAPINFO* info )
283 if (info->bmiHeader.biBitCount != 1) return FALSE;
285 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
287 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
289 /* Check if the first color is black */
290 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
294 /* Check if the second color is white */
295 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
296 && (rgb->rgbtBlue == 0xff));
300 else /* assume BITMAPINFOHEADER */
302 const RGBQUAD *rgb = info->bmiColors;
304 /* Check if the first color is black */
305 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
306 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
310 /* Check if the second color is white */
311 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
312 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
318 /***********************************************************************
321 * Get the info from a bitmap header.
322 * Return 1 for INFOHEADER, 0 for COREHEADER,
323 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
325 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
326 LONG *height, WORD *bpp, DWORD *compr )
328 if (header->biSize == sizeof(BITMAPINFOHEADER))
330 *width = header->biWidth;
331 *height = header->biHeight;
332 *bpp = header->biBitCount;
333 *compr = header->biCompression;
336 if (header->biSize == sizeof(BITMAPCOREHEADER))
338 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
339 *width = core->bcWidth;
340 *height = core->bcHeight;
341 *bpp = core->bcBitCount;
345 if (header->biSize == sizeof(BITMAPV4HEADER))
347 const BITMAPV4HEADER *v4hdr = (const BITMAPV4HEADER *)header;
348 *width = v4hdr->bV4Width;
349 *height = v4hdr->bV4Height;
350 *bpp = v4hdr->bV4BitCount;
351 *compr = v4hdr->bV4V4Compression;
354 if (header->biSize == sizeof(BITMAPV5HEADER))
356 const BITMAPV5HEADER *v5hdr = (const BITMAPV5HEADER *)header;
357 *width = v5hdr->bV5Width;
358 *height = v5hdr->bV5Height;
359 *bpp = v5hdr->bV5BitCount;
360 *compr = v5hdr->bV5Compression;
363 ERR("(%d): unknown/wrong size for header\n", header->biSize );
367 /**********************************************************************
368 * CURSORICON_FindSharedIcon
370 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
375 EnterCriticalSection( &IconCrst );
377 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
378 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
385 LeaveCriticalSection( &IconCrst );
390 /*************************************************************************
391 * CURSORICON_FindCache
393 * Given a handle, find the corresponding cache element
396 * Handle [I] handle to an Image
399 * Success: The cache entry
403 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
406 ICONCACHE *pRet=NULL;
407 BOOL IsFound = FALSE;
409 EnterCriticalSection( &IconCrst );
411 for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
413 if ( hIcon == ptr->hIcon )
420 LeaveCriticalSection( &IconCrst );
425 /**********************************************************************
426 * CURSORICON_AddSharedIcon
428 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
430 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
433 ptr->hModule = hModule;
436 ptr->hGroupRsrc = hGroupRsrc;
439 EnterCriticalSection( &IconCrst );
440 ptr->next = IconAnchor;
442 LeaveCriticalSection( &IconCrst );
445 /**********************************************************************
446 * CURSORICON_DelSharedIcon
448 static INT CURSORICON_DelSharedIcon( HICON hIcon )
453 EnterCriticalSection( &IconCrst );
455 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
456 if ( ptr->hIcon == hIcon )
458 if ( ptr->count > 0 ) ptr->count--;
463 LeaveCriticalSection( &IconCrst );
468 /**********************************************************************
471 BOOL get_icon_size( HICON handle, SIZE *size )
473 CURSORICONINFO *info;
475 if (!(info = get_icon_ptr( handle ))) return FALSE;
476 size->cx = info->nWidth;
477 size->cy = info->nHeight;
478 release_icon_ptr( handle, info );
483 * The following macro functions account for the irregularities of
484 * accessing cursor and icon resources in files and resource entries.
486 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
487 int *width, int *height, int *bits );
489 /**********************************************************************
490 * CURSORICON_FindBestIcon
492 * Find the icon closest to the requested size and number of colors.
494 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
495 int width, int height, int colors )
497 int i, cx, cy, bits, bestEntry = -1;
498 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
499 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
502 iTotalDiff = 0xFFFFFFFF;
503 iColorDiff = 0xFFFFFFFF;
504 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
506 iTempXDiff = abs(width - cx);
507 iTempYDiff = abs(height - cy);
509 if(iTotalDiff > (iTempXDiff + iTempYDiff))
513 iTotalDiff = iXDiff + iYDiff;
517 /* Find Best Colors for Best Fit */
518 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
520 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
522 iTempColorDiff = abs(colors - (1<<bits));
523 if(iColorDiff > iTempColorDiff)
526 iColorDiff = iTempColorDiff;
534 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
535 int *width, int *height, int *bits )
537 CURSORICONDIR *resdir = dir;
540 if ( resdir->idCount <= n )
542 icon = &resdir->idEntries[n].ResInfo.icon;
543 *width = icon->bWidth;
544 *height = icon->bHeight;
545 *bits = resdir->idEntries[n].wBitCount;
549 /**********************************************************************
550 * CURSORICON_FindBestCursor
552 * Find the cursor closest to the requested size.
554 * FIXME: parameter 'color' ignored.
556 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
557 int width, int height, int color )
559 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
561 /* Double height to account for AND and XOR masks */
565 /* First find the largest one smaller than or equal to the requested size*/
567 maxwidth = maxheight = 0;
568 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
570 if ((cx <= width) && (cy <= height) &&
571 (cx > maxwidth) && (cy > maxheight))
578 if (bestEntry != -1) return bestEntry;
580 /* Now find the smallest one larger than the requested size */
582 maxwidth = maxheight = 255;
583 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
585 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
596 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
597 int *width, int *height, int *bits )
599 CURSORICONDIR *resdir = dir;
602 if ( resdir->idCount <= n )
604 cursor = &resdir->idEntries[n].ResInfo.cursor;
605 *width = cursor->wWidth;
606 *height = cursor->wHeight;
607 *bits = resdir->idEntries[n].wBitCount;
611 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
612 int width, int height, int colors )
616 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
617 width, height, colors );
620 return &dir->idEntries[n];
623 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
624 int width, int height, int color )
626 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
627 width, height, color );
630 return &dir->idEntries[n];
633 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
634 int *width, int *height, int *bits )
636 CURSORICONFILEDIR *filedir = dir;
637 CURSORICONFILEDIRENTRY *entry;
639 if ( filedir->idCount <= n )
641 entry = &filedir->idEntries[n];
642 *width = entry->bWidth;
643 *height = entry->bHeight;
644 *bits = entry->bColorCount;
648 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
649 int width, int height, int color )
651 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
652 width, height, color );
655 return &dir->idEntries[n];
658 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
659 int width, int height, int color )
661 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
662 width, height, color );
665 return &dir->idEntries[n];
668 /***********************************************************************
671 * A helper function that stretches a bitmap buffer into an HBITMAP.
674 * hDest [I] The handle of the destination bitmap.
675 * pDestInfo [I] The BITMAPINFO of the destination bitmap.
676 * pSrcInfo [I] The BITMAPINFO of the source bitmap.
677 * pSrcBits [I] A pointer to the source bitmap buffer.
679 static BOOL stretch_blt_icon(HBITMAP hDest, BITMAPINFO *pDestInfo, BITMAPINFO *pSrcInfo, char *pSrcBits)
683 HDC hdcMem = CreateCompatibleDC(screen_dc);
687 hOld = SelectObject(hdcMem, hDest);
688 res = StretchDIBits(hdcMem,
689 0, 0, pDestInfo->bmiHeader.biWidth, pDestInfo->bmiHeader.biHeight,
690 0, 0, pSrcInfo->bmiHeader.biWidth, pSrcInfo->bmiHeader.biHeight,
691 pSrcBits, pSrcInfo, DIB_RGB_COLORS, SRCCOPY);
692 SelectObject(hdcMem, hOld);
699 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi,
700 POINT16 hotspot, BOOL bIcon,
702 INT width, INT height,
706 int sizeAnd, sizeXor;
707 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
708 BITMAP bmpXor, bmpAnd;
710 BITMAPINFO *pSrcInfo, *pDestInfo;
712 if (dwVersion == 0x00020000)
714 FIXME_(cursor)("\t2.xx resources are not supported\n");
718 /* Check bitmap header */
720 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
721 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
722 bmi->bmiHeader.biCompression != BI_RGB) )
724 WARN_(cursor)("\tinvalid resource bitmap header.\n");
728 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
730 if (!width) width = bmi->bmiHeader.biWidth;
731 if (!height) height = bmi->bmiHeader.biHeight/2;
733 /* Scale the hotspot */
734 if (((bmi->bmiHeader.biHeight/2 != height) || (bmi->bmiHeader.biWidth != width)) &&
735 hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
737 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
738 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
741 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
744 /* Make sure we have room for the monochrome bitmap later on.
745 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
746 * up to and including the biBitCount. In-memory icon resource
747 * format is as follows:
749 * BITMAPINFOHEADER icHeader // DIB header
750 * RGBQUAD icColors[] // Color table
751 * BYTE icXOR[] // DIB bits for XOR mask
752 * BYTE icAND[] // DIB bits for AND mask
755 pSrcInfo = HeapAlloc( GetProcessHeap(), 0,
756 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
757 pDestInfo = HeapAlloc( GetProcessHeap(), 0,
758 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
759 if (pSrcInfo && pDestInfo)
761 memcpy( pSrcInfo, bmi, size );
762 pSrcInfo->bmiHeader.biHeight /= 2;
764 memcpy( pDestInfo, bmi, size );
765 pDestInfo->bmiHeader.biWidth = width;
766 pDestInfo->bmiHeader.biHeight = height;
767 pDestInfo->bmiHeader.biSizeImage = 0;
769 /* Create the XOR bitmap */
770 if(pSrcInfo->bmiHeader.biBitCount == 32)
772 void *pDIBBuffer = NULL;
773 hXorBits = CreateDIBSection(screen_dc, pDestInfo, DIB_RGB_COLORS, &pDIBBuffer, NULL, 0);
777 if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
779 DeleteObject(hXorBits);
786 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
790 if(!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
792 DeleteObject(hXorBits);
800 char* xbits = (char *)bmi + size +
801 get_dib_width_bytes( bmi->bmiHeader.biWidth,
802 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
804 pSrcInfo->bmiHeader.biBitCount = 1;
805 if (pSrcInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
807 RGBQUAD *rgb = pSrcInfo->bmiColors;
809 pSrcInfo->bmiHeader.biClrUsed = pSrcInfo->bmiHeader.biClrImportant = 2;
810 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
811 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
812 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
816 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pSrcInfo) + 1);
818 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
819 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
822 /* Create the AND bitmap */
823 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
825 if(!stretch_blt_icon(hAndBits, pDestInfo, pSrcInfo, xbits))
827 DeleteObject(hAndBits);
833 DeleteObject( hXorBits );
839 HeapFree( GetProcessHeap(), 0, pSrcInfo );
840 HeapFree( GetProcessHeap(), 0, pDestInfo );
843 if( !hXorBits || !hAndBits )
845 WARN_(cursor)("\tunable to create an icon bitmap.\n");
849 /* Now create the CURSORICONINFO structure */
850 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
851 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
852 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
853 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
855 hObj = alloc_icon_handle( sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
858 CURSORICONINFO *info = get_icon_ptr( hObj );
860 info->ptHotSpot.x = hotspot.x;
861 info->ptHotSpot.y = hotspot.y;
862 info->nWidth = bmpXor.bmWidth;
863 info->nHeight = bmpXor.bmHeight;
864 info->nWidthBytes = bmpXor.bmWidthBytes;
865 info->bPlanes = bmpXor.bmPlanes;
866 info->bBitsPerPixel = bmpXor.bmBitsPixel;
868 /* Transfer the bitmap bits to the CURSORICONINFO structure */
870 GetBitmapBits( hAndBits, sizeAnd, info + 1 );
871 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
872 release_icon_ptr( hObj, info );
875 DeleteObject( hAndBits );
876 DeleteObject( hXorBits );
881 /**********************************************************************
882 * .ANI cursor support
884 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
885 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
886 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
888 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
889 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
890 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
891 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
892 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
893 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
895 #define ANI_FLAG_ICON 0x1
896 #define ANI_FLAG_SEQUENCE 0x2
912 const unsigned char *data;
915 static void dump_ani_header( const ani_header *header )
917 TRACE(" header size: %d\n", header->header_size);
918 TRACE(" frames: %d\n", header->num_frames);
919 TRACE(" steps: %d\n", header->num_steps);
920 TRACE(" width: %d\n", header->width);
921 TRACE(" height: %d\n", header->height);
922 TRACE(" bpp: %d\n", header->bpp);
923 TRACE(" planes: %d\n", header->num_planes);
924 TRACE(" display rate: %d\n", header->display_rate);
925 TRACE(" flags: 0x%08x\n", header->flags);
947 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
949 const unsigned char *ptr = parent_chunk->data;
950 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
952 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
956 if ((!chunk_type && *(DWORD *)ptr == chunk_id )
957 || (chunk_type && *(DWORD *)ptr == chunk_type && *((DWORD *)ptr + 2) == chunk_id ))
959 ptr += sizeof(DWORD);
960 chunk->data_size = (*(DWORD *)ptr + 1) & ~1;
961 ptr += sizeof(DWORD);
962 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
968 ptr += sizeof(DWORD);
969 ptr += (*(DWORD *)ptr + 1) & ~1;
970 ptr += sizeof(DWORD);
978 * RIFF:'ACON' RIFF chunk
979 * |- CHUNK:'anih' Header
980 * |- CHUNK:'seq ' Sequence information (optional)
981 * \- LIST:'fram' Frame list
982 * |- CHUNK:icon Cursor frames
987 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
988 INT width, INT height, INT colors )
991 ani_header header = {0};
992 LPBYTE frame_bits = 0;
994 CURSORICONFILEDIRENTRY *entry;
996 riff_chunk_t root_chunk = { bits_size, bits };
997 riff_chunk_t ACON_chunk = {0};
998 riff_chunk_t anih_chunk = {0};
999 riff_chunk_t fram_chunk = {0};
1000 const unsigned char *icon_data;
1002 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1004 if (!bits) return 0;
1006 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1007 if (!ACON_chunk.data)
1009 ERR("Failed to get root chunk.\n");
1013 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1014 if (!anih_chunk.data)
1016 ERR("Failed to get 'anih' chunk.\n");
1019 memcpy( &header, anih_chunk.data, sizeof(header) );
1020 dump_ani_header( &header );
1022 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1023 if (!fram_chunk.data)
1025 ERR("Failed to get icon list.\n");
1029 /* FIXME: For now, just load the first frame. Before we can load all the
1030 * frames, we need to write the needed code in wineserver, etc. to handle
1031 * cursors. Once this code is written, we can extend it to support .ani
1032 * cursors and then update user32 and winex11.drv to load all frames.
1034 * Hopefully this will at least make some games (C&C3, etc.) more playable
1037 FIXME("Loading all frames for .ani cursors not implemented.\n");
1038 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1040 entry = CURSORICON_FindBestIconFile( (CURSORICONFILEDIR *) icon_data,
1041 width, height, colors );
1043 frame_bits = HeapAlloc( GetProcessHeap(), 0, entry->dwDIBSize );
1044 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1046 if (!header.width || !header.height)
1048 header.width = entry->bWidth;
1049 header.height = entry->bHeight;
1052 hotspot.x = entry->xHotspot;
1053 hotspot.y = entry->yHotspot;
1055 cursor = CURSORICON_CreateIconFromBMI( (BITMAPINFO *) frame_bits, hotspot,
1056 FALSE, 0x00030000, header.width, header.height, 0 );
1058 HeapFree( GetProcessHeap(), 0, frame_bits );
1064 /**********************************************************************
1065 * CreateIconFromResourceEx (USER32.@)
1067 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1068 * with cbSize parameter as well.
1070 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1071 BOOL bIcon, DWORD dwVersion,
1072 INT width, INT height,
1078 hotspot.x = ICON_HOTSPOT;
1079 hotspot.y = ICON_HOTSPOT;
1081 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1082 bits, cbSize, dwVersion, width, height,
1083 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1086 bmi = (BITMAPINFO *)bits;
1087 else /* get the hotspot */
1089 POINT16 *pt = (POINT16 *)bits;
1091 bmi = (BITMAPINFO *)(pt + 1);
1094 return CURSORICON_CreateIconFromBMI( bmi, hotspot, bIcon, dwVersion,
1095 width, height, cFlag );
1099 /**********************************************************************
1100 * CreateIconFromResource (USER32.@)
1102 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1103 BOOL bIcon, DWORD dwVersion)
1105 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1109 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1110 INT width, INT height, INT colors,
1111 BOOL fCursor, UINT loadflags)
1113 CURSORICONFILEDIRENTRY *entry;
1114 CURSORICONFILEDIR *dir;
1120 TRACE("loading %s\n", debugstr_w( filename ));
1122 bits = map_fileW( filename, &filesize );
1126 /* Check for .ani. */
1127 if (memcmp( bits, "RIFF", 4 ) == 0)
1129 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height,
1134 dir = (CURSORICONFILEDIR*) bits;
1135 if ( filesize < sizeof(*dir) )
1138 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1142 entry = CURSORICON_FindBestCursorFile( dir, width, height, colors );
1144 entry = CURSORICON_FindBestIconFile( dir, width, height, colors );
1149 /* check that we don't run off the end of the file */
1150 if ( entry->dwDIBOffset > filesize )
1152 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1155 /* Set the actual hotspot for cursors and ICON_HOTSPOT for icons. */
1158 hotspot.x = entry->xHotspot;
1159 hotspot.y = entry->yHotspot;
1163 hotspot.x = ICON_HOTSPOT;
1164 hotspot.y = ICON_HOTSPOT;
1166 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset],
1167 hotspot, !fCursor, 0x00030000,
1168 width, height, loadflags );
1170 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1171 UnmapViewOfFile( bits );
1175 /**********************************************************************
1178 * Load a cursor or icon from resource or file.
1180 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1181 INT width, INT height, INT colors,
1182 BOOL fCursor, UINT loadflags)
1186 HRSRC hRsrc, hGroupRsrc;
1188 CURSORICONDIRENTRY *dirEntry;
1193 TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
1194 hInstance, debugstr_w(name), width, height, colors, fCursor, loadflags);
1196 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1197 return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
1199 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1201 /* Normalize hInstance (must be uniquely represented for icon cache) */
1203 if (!HIWORD( hInstance ))
1204 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
1206 /* Get directory resource ID */
1208 if (!(hRsrc = FindResourceW( hInstance, name,
1209 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1213 /* Find the best entry in the directory */
1215 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1216 if (!(dir = LockResource( handle ))) return 0;
1218 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, colors );
1220 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1221 if (!dirEntry) return 0;
1222 wResId = dirEntry->wResId;
1223 dwBytesInRes = dirEntry->dwBytesInRes;
1224 FreeResource( handle );
1226 /* Load the resource */
1228 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1229 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1231 /* If shared icon, check whether it was already loaded */
1232 if ( (loadflags & LR_SHARED)
1233 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1236 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1237 bits = LockResource( handle );
1238 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1239 !fCursor, 0x00030000, width, height, loadflags);
1240 FreeResource( handle );
1242 /* If shared icon, add to icon cache */
1244 if ( hIcon && (loadflags & LR_SHARED) )
1245 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1251 /*************************************************************************
1252 * CURSORICON_ExtCopy
1254 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1257 * Handle [I] handle to an Image
1258 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1259 * iDesiredCX [I] The Desired width of the Image
1260 * iDesiredCY [I] The desired height of the Image
1261 * nFlags [I] The flags from CopyImage
1264 * Success: The new handle of the Image
1267 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1268 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1269 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1274 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1275 INT iDesiredCX, INT iDesiredCY,
1280 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1281 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1288 /* Best Fit or Monochrome */
1289 if( (nFlags & LR_COPYFROMRESOURCE
1290 && (iDesiredCX > 0 || iDesiredCY > 0))
1291 || nFlags & LR_MONOCHROME)
1293 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1295 /* Not Found in Cache, then do a straight copy
1297 if(pIconCache == NULL)
1299 hNew = CopyIcon( hIcon );
1300 if(nFlags & LR_COPYFROMRESOURCE)
1302 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1307 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1313 CURSORICONDIR *pDir;
1314 CURSORICONDIRENTRY *pDirEntry;
1315 BOOL bIsIcon = (nType == IMAGE_ICON);
1317 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1319 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1320 || (iDesiredCX == 0 && iDesiredCY == 0))
1322 iDesiredCY = GetSystemMetrics(bIsIcon ?
1323 SM_CYICON : SM_CYCURSOR);
1324 iDesiredCX = GetSystemMetrics(bIsIcon ?
1325 SM_CXICON : SM_CXCURSOR);
1328 /* Retrieve the CURSORICONDIRENTRY
1330 if (!(hMem = LoadResource( pIconCache->hModule ,
1331 pIconCache->hGroupRsrc)))
1335 if (!(pDir = LockResource( hMem )))
1344 pDirEntry = CURSORICON_FindBestIconRes(
1345 pDir, iDesiredCX, iDesiredCY, 256 );
1349 pDirEntry = CURSORICON_FindBestCursorRes(
1350 pDir, iDesiredCX, iDesiredCY, 1);
1353 wResId = pDirEntry->wResId;
1354 dwBytesInRes = pDirEntry->dwBytesInRes;
1357 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1358 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1359 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1363 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1364 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1368 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1373 pBits = LockResource( hMem );
1375 if(nFlags & LR_DEFAULTSIZE)
1377 iTargetCY = GetSystemMetrics(SM_CYICON);
1378 iTargetCX = GetSystemMetrics(SM_CXICON);
1381 /* Create a New Icon with the proper dimension
1383 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1384 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1388 else hNew = CopyIcon( hIcon );
1393 /***********************************************************************
1394 * CreateCursor (USER32.@)
1396 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1397 INT xHotSpot, INT yHotSpot,
1398 INT nWidth, INT nHeight,
1399 LPCVOID lpANDbits, LPCVOID lpXORbits )
1404 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1405 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1408 info.xHotspot = xHotSpot;
1409 info.yHotspot = yHotSpot;
1410 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1411 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1412 hCursor = CreateIconIndirect( &info );
1413 DeleteObject( info.hbmMask );
1414 DeleteObject( info.hbmColor );
1419 /***********************************************************************
1420 * CreateIcon (USER32.@)
1422 * Creates an icon based on the specified bitmaps. The bitmaps must be
1423 * provided in a device dependent format and will be resized to
1424 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1425 * depth. The provided bitmaps must be top-down bitmaps.
1426 * Although Windows does not support 15bpp(*) this API must support it
1427 * for Winelib applications.
1429 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1433 * Success: handle to an icon
1436 * FIXME: Do we need to resize the bitmaps?
1438 HICON WINAPI CreateIcon(
1439 HINSTANCE hInstance, /* [in] the application's hInstance */
1440 INT nWidth, /* [in] the width of the provided bitmaps */
1441 INT nHeight, /* [in] the height of the provided bitmaps */
1442 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1443 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1444 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1445 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1450 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1451 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1454 iinfo.xHotspot = ICON_HOTSPOT;
1455 iinfo.yHotspot = ICON_HOTSPOT;
1456 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1457 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1459 hIcon = CreateIconIndirect( &iinfo );
1461 DeleteObject( iinfo.hbmMask );
1462 DeleteObject( iinfo.hbmColor );
1468 /***********************************************************************
1469 * CopyIcon (USER32.@)
1471 HICON WINAPI CopyIcon( HICON hIcon )
1473 CURSORICONINFO *ptrOld, *ptrNew;
1475 HICON16 hOld = HICON_16(hIcon);
1478 if (!(ptrOld = get_icon_ptr( hIcon ))) return 0;
1479 size = GlobalSize16( hOld );
1480 hNew = alloc_icon_handle( size );
1481 ptrNew = get_icon_ptr( hNew );
1482 memcpy( ptrNew, ptrOld, size );
1483 release_icon_ptr( hIcon, ptrOld );
1484 release_icon_ptr( hNew, ptrNew );
1489 /***********************************************************************
1490 * DestroyIcon (USER32.@)
1492 BOOL WINAPI DestroyIcon( HICON hIcon )
1494 TRACE_(icon)("%p\n", hIcon );
1496 if (CURSORICON_DelSharedIcon( hIcon ) == -1)
1497 free_icon_handle( hIcon );
1502 /***********************************************************************
1503 * DestroyCursor (USER32.@)
1505 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1507 if (get_user_thread_info()->cursor == hCursor)
1509 WARN_(cursor)("Destroying active cursor!\n" );
1512 return DestroyIcon( hCursor );
1515 /***********************************************************************
1516 * bitmap_has_alpha_channel
1518 * Analyses bits bitmap to determine if alpha data is present.
1521 * bpp [I] The bits-per-pixel of the bitmap
1522 * bitmapBits [I] A pointer to the bitmap data
1523 * bitmapLength [I] The length of the bitmap in bytes
1526 * TRUE if an alpha channel is discovered, FALSE
1529 * Windows' behaviour is that if the icon bitmap is 32-bit and at
1530 * least one pixel has a non-zero alpha, then the bitmap is a
1531 * treated as having an alpha channel transparentcy. Otherwise,
1532 * it's treated as being completely opaque.
1535 static BOOL bitmap_has_alpha_channel( int bpp, unsigned char *bitmapBits,
1536 unsigned int bitmapLength )
1538 /* Detect an alpha channel by looking for non-zero alpha pixels */
1541 unsigned int offset;
1542 for(offset = 3; offset < bitmapLength; offset += 4)
1544 if(bitmapBits[offset] != 0)
1553 /***********************************************************************
1554 * premultiply_alpha_channel
1556 * Premultiplies the color channels of a 32-bit bitmap by the alpha
1557 * channel. This is a necessary step that must be carried out on
1558 * the image before it is passed to GdiAlphaBlend
1561 * destBitmap [I] The destination bitmap buffer
1562 * srcBitmap [I] The source bitmap buffer
1563 * bitmapLength [I] The length of the bitmap in bytes
1566 static void premultiply_alpha_channel( unsigned char *destBitmap,
1567 unsigned char *srcBitmap,
1568 unsigned int bitmapLength )
1570 unsigned char *destPixel = destBitmap;
1571 unsigned char *srcPixel = srcBitmap;
1573 while(destPixel < destBitmap + bitmapLength)
1575 unsigned char alpha = srcPixel[3];
1576 *(destPixel++) = *(srcPixel++) * alpha / 255;
1577 *(destPixel++) = *(srcPixel++) * alpha / 255;
1578 *(destPixel++) = *(srcPixel++) * alpha / 255;
1579 *(destPixel++) = *(srcPixel++);
1583 /***********************************************************************
1584 * DrawIcon (USER32.@)
1586 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1588 CURSORICONINFO *ptr;
1590 HBITMAP hXorBits = NULL, hAndBits = NULL, hBitTemp = NULL;
1591 COLORREF oldFg, oldBg;
1592 unsigned char *xorBitmapBits;
1593 unsigned int dibLength;
1595 TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);
1597 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
1598 if (!(hMemDC = CreateCompatibleDC( hdc )))
1600 release_icon_ptr( hIcon, ptr );
1604 dibLength = ptr->nHeight * get_bitmap_width_bytes(
1605 ptr->nWidth, ptr->bBitsPerPixel);
1607 xorBitmapBits = (unsigned char *)(ptr + 1) + ptr->nHeight *
1608 get_bitmap_width_bytes(ptr->nWidth, 1);
1610 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1611 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1613 if(bitmap_has_alpha_channel(ptr->bBitsPerPixel, xorBitmapBits, dibLength))
1615 BITMAPINFOHEADER bmih;
1616 unsigned char *dibBits;
1618 memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
1619 bmih.biSize = sizeof(BITMAPINFOHEADER);
1620 bmih.biWidth = ptr->nWidth;
1621 bmih.biHeight = -ptr->nHeight;
1622 bmih.biPlanes = ptr->bPlanes;
1623 bmih.biBitCount = 32;
1624 bmih.biCompression = BI_RGB;
1626 hXorBits = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
1627 (void*)&dibBits, NULL, 0);
1629 if (hXorBits && dibBits)
1631 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
1633 /* Do the alpha blending render */
1634 premultiply_alpha_channel(dibBits, xorBitmapBits, dibLength);
1635 hBitTemp = SelectObject( hMemDC, hXorBits );
1636 /* Destination width/height has to be "System Large" size */
1637 GdiAlphaBlend(hdc, x, y, GetSystemMetrics(SM_CXICON),
1638 GetSystemMetrics(SM_CYICON), hMemDC,
1639 0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
1640 SelectObject( hMemDC, hBitTemp );
1645 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1, ptr + 1 );
1646 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1647 ptr->bBitsPerPixel, xorBitmapBits);
1649 if (hXorBits && hAndBits)
1651 hBitTemp = SelectObject( hMemDC, hAndBits );
1652 StretchBlt( hdc, x, y, GetSystemMetrics(SM_CXICON),
1653 GetSystemMetrics(SM_CYICON), hMemDC, 0, 0,
1654 ptr->nWidth, ptr->nHeight, SRCAND );
1655 SelectObject( hMemDC, hXorBits );
1656 StretchBlt( hdc, x, y, GetSystemMetrics(SM_CXICON),
1657 GetSystemMetrics(SM_CYICON), hMemDC, 0, 0,
1658 ptr->nWidth, ptr->nHeight, SRCINVERT );
1659 SelectObject( hMemDC, hBitTemp );
1664 if (hXorBits) DeleteObject( hXorBits );
1665 if (hAndBits) DeleteObject( hAndBits );
1666 release_icon_ptr( hIcon, ptr );
1667 SetTextColor( hdc, oldFg );
1668 SetBkColor( hdc, oldBg );
1672 /***********************************************************************
1673 * SetCursor (USER32.@)
1675 * Set the cursor shape.
1678 * A handle to the previous cursor shape.
1680 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1682 struct user_thread_info *thread_info = get_user_thread_info();
1685 if (hCursor == thread_info->cursor) return hCursor; /* No change */
1686 TRACE("%p\n", hCursor);
1687 hOldCursor = thread_info->cursor;
1688 thread_info->cursor = hCursor;
1689 /* Change the cursor shape only if it is visible */
1690 if (thread_info->cursor_count >= 0)
1692 CURSORICONINFO *info = get_icon_ptr( hCursor );
1693 /* release before calling driver (FIXME) */
1694 if (info) release_icon_ptr( hCursor, info );
1695 USER_Driver->pSetCursor( info );
1700 /***********************************************************************
1701 * ShowCursor (USER32.@)
1703 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1705 struct user_thread_info *thread_info = get_user_thread_info();
1707 TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
1711 if (++thread_info->cursor_count == 0) /* Show it */
1713 CURSORICONINFO *info = get_icon_ptr( thread_info->cursor );
1714 /* release before calling driver (FIXME) */
1715 if (info) release_icon_ptr( thread_info->cursor, info );
1716 USER_Driver->pSetCursor( info );
1721 if (--thread_info->cursor_count == -1) /* Hide it */
1722 USER_Driver->pSetCursor( NULL );
1724 return thread_info->cursor_count;
1727 /***********************************************************************
1728 * GetCursor (USER32.@)
1730 HCURSOR WINAPI GetCursor(void)
1732 return get_user_thread_info()->cursor;
1736 /***********************************************************************
1737 * ClipCursor (USER32.@)
1739 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1743 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1744 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1745 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1746 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1748 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1749 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1751 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1752 CURSOR_ClipRect = virt;
1754 USER_Driver->pClipCursor( rect );
1759 /***********************************************************************
1760 * GetClipCursor (USER32.@)
1762 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1764 /* If this is first time - initialize the rect */
1765 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1767 return CopyRect( rect, &CURSOR_ClipRect );
1771 /***********************************************************************
1772 * SetSystemCursor (USER32.@)
1774 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1776 FIXME("(%p,%08x),stub!\n", hcur, id);
1781 /**********************************************************************
1782 * LookupIconIdFromDirectoryEx (USER32.@)
1784 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1785 INT width, INT height, UINT cFlag )
1787 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1789 if( dir && !dir->idReserved && (dir->idType & 3) )
1791 CURSORICONDIRENTRY* entry;
1796 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1799 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1804 entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1806 entry = CURSORICON_FindBestCursorRes( dir, width, height, colors );
1808 if( entry ) retVal = entry->wResId;
1810 else WARN_(cursor)("invalid resource directory\n");
1814 /**********************************************************************
1815 * LookupIconIdFromDirectory (USER32.@)
1817 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1819 return LookupIconIdFromDirectoryEx( dir, bIcon,
1820 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1821 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1824 /***********************************************************************
1825 * LoadCursorW (USER32.@)
1827 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1829 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1831 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1832 LR_SHARED | LR_DEFAULTSIZE );
1835 /***********************************************************************
1836 * LoadCursorA (USER32.@)
1838 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1840 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1842 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1843 LR_SHARED | LR_DEFAULTSIZE );
1846 /***********************************************************************
1847 * LoadCursorFromFileW (USER32.@)
1849 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1851 TRACE("%s\n", debugstr_w(name));
1853 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1854 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1857 /***********************************************************************
1858 * LoadCursorFromFileA (USER32.@)
1860 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1862 TRACE("%s\n", debugstr_a(name));
1864 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1865 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1868 /***********************************************************************
1869 * LoadIconW (USER32.@)
1871 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1873 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1875 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1876 LR_SHARED | LR_DEFAULTSIZE );
1879 /***********************************************************************
1880 * LoadIconA (USER32.@)
1882 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1884 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1886 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1887 LR_SHARED | LR_DEFAULTSIZE );
1890 /**********************************************************************
1891 * GetIconInfo (USER32.@)
1893 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1895 CURSORICONINFO *ciconinfo;
1898 if (!(ciconinfo = get_icon_ptr( hIcon ))) return FALSE;
1900 TRACE("%p => %dx%d, %d bpp\n", hIcon,
1901 ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
1903 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1904 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1906 iconinfo->fIcon = TRUE;
1907 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1908 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1912 iconinfo->fIcon = FALSE;
1913 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1914 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1917 height = ciconinfo->nHeight;
1919 if (ciconinfo->bBitsPerPixel > 1)
1921 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
1922 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1923 (char *)(ciconinfo + 1)
1924 + ciconinfo->nHeight *
1925 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1929 iconinfo->hbmColor = 0;
1933 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
1934 1, 1, ciconinfo + 1);
1935 release_icon_ptr( hIcon, ciconinfo );
1940 /**********************************************************************
1941 * CreateIconIndirect (USER32.@)
1943 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1948 int xor_objsize = 0, sizeXor = 0, sizeAnd, planes, bpp;
1950 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1951 iconinfo->hbmColor, iconinfo->hbmMask,
1952 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1954 if (!iconinfo->hbmMask) return 0;
1956 planes = GetDeviceCaps( screen_dc, PLANES );
1957 bpp = GetDeviceCaps( screen_dc, BITSPIXEL );
1959 if (iconinfo->hbmColor)
1961 xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1962 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1963 bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
1964 bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
1965 /* we can use either depth 1 or screen depth for xor bitmap */
1966 if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
1967 sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
1969 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1970 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1971 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1972 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1974 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
1976 hObj = alloc_icon_handle( sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1979 CURSORICONINFO *info = get_icon_ptr( hObj );
1981 /* If we are creating an icon, the hotspot is unused */
1982 if (iconinfo->fIcon)
1984 info->ptHotSpot.x = ICON_HOTSPOT;
1985 info->ptHotSpot.y = ICON_HOTSPOT;
1989 info->ptHotSpot.x = iconinfo->xHotspot;
1990 info->ptHotSpot.y = iconinfo->yHotspot;
1993 if (iconinfo->hbmColor)
1995 info->nWidth = bmpXor.dsBm.bmWidth;
1996 info->nHeight = bmpXor.dsBm.bmHeight;
1997 info->nWidthBytes = bmpXor.dsBm.bmWidthBytes;
1998 info->bPlanes = planes;
1999 info->bBitsPerPixel = bpp;
2003 info->nWidth = bmpAnd.bmWidth;
2004 info->nHeight = bmpAnd.bmHeight / 2;
2005 info->nWidthBytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2007 info->bBitsPerPixel = 1;
2010 /* Transfer the bitmap bits to the CURSORICONINFO structure */
2012 /* Some apps pass a color bitmap as a mask, convert it to b/w */
2013 if (bmpAnd.bmBitsPixel == 1)
2015 GetBitmapBits( iconinfo->hbmMask, sizeAnd, info + 1 );
2019 HDC hdc_mem, hdc_mem2;
2020 HBITMAP hbmp_mem_old, hbmp_mem2_old, hbmp_mono;
2022 hdc_mem = CreateCompatibleDC( 0 );
2023 hdc_mem2 = CreateCompatibleDC( 0 );
2025 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
2027 hbmp_mem_old = SelectObject( hdc_mem, iconinfo->hbmMask );
2028 hbmp_mem2_old = SelectObject( hdc_mem2, hbmp_mono );
2030 BitBlt( hdc_mem2, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc_mem, 0, 0, SRCCOPY );
2032 SelectObject( hdc_mem, hbmp_mem_old );
2033 SelectObject( hdc_mem2, hbmp_mem2_old );
2035 DeleteDC( hdc_mem );
2036 DeleteDC( hdc_mem2 );
2038 GetBitmapBits( hbmp_mono, sizeAnd, info + 1 );
2039 DeleteObject( hbmp_mono );
2042 if (iconinfo->hbmColor)
2044 char *dst_bits = (char*)(info + 1) + sizeAnd;
2046 if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
2047 GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
2051 int dib_width = get_dib_width_bytes( info->nWidth, info->bBitsPerPixel );
2052 int bitmap_width = get_bitmap_width_bytes( info->nWidth, info->bBitsPerPixel );
2054 bminfo.bmiHeader.biSize = sizeof(bminfo);
2055 bminfo.bmiHeader.biWidth = info->nWidth;
2056 bminfo.bmiHeader.biHeight = info->nHeight;
2057 bminfo.bmiHeader.biPlanes = info->bPlanes;
2058 bminfo.bmiHeader.biBitCount = info->bBitsPerPixel;
2059 bminfo.bmiHeader.biCompression = BI_RGB;
2060 bminfo.bmiHeader.biSizeImage = info->nHeight * dib_width;
2061 bminfo.bmiHeader.biXPelsPerMeter = 0;
2062 bminfo.bmiHeader.biYPelsPerMeter = 0;
2063 bminfo.bmiHeader.biClrUsed = 0;
2064 bminfo.bmiHeader.biClrImportant = 0;
2066 /* swap lines for dib sections */
2067 if (xor_objsize == sizeof(DIBSECTION))
2068 bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;
2070 if (dib_width != bitmap_width) /* need to fixup alignment */
2072 char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );
2074 if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2075 src_bits, &bminfo, DIB_RGB_COLORS ))
2078 for (y = 0; y < info->nHeight; y++)
2079 memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
2081 HeapFree( GetProcessHeap(), 0, src_bits );
2084 GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2085 dst_bits, &bminfo, DIB_RGB_COLORS );
2088 release_icon_ptr( hObj, info );
2093 /******************************************************************************
2094 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2097 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2100 * hdc [I] Handle to device context
2101 * x0 [I] X coordinate of upper left corner
2102 * y0 [I] Y coordinate of upper left corner
2103 * hIcon [I] Handle to icon to draw
2104 * cxWidth [I] Width of icon
2105 * cyWidth [I] Height of icon
2106 * istep [I] Index of frame in animated cursor
2107 * hbr [I] Handle to background brush
2108 * flags [I] Icon-drawing flags
2114 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2115 INT cxWidth, INT cyWidth, UINT istep,
2116 HBRUSH hbr, UINT flags )
2118 CURSORICONINFO *ptr;
2119 HDC hDC_off = 0, hMemDC;
2120 BOOL result = FALSE, DoOffscreen;
2121 HBITMAP hB_off = 0, hOld = 0;
2122 unsigned char *xorBitmapBits;
2123 unsigned int xorLength;
2124 BOOL has_alpha = FALSE;
2126 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2127 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2129 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2130 if (!(hMemDC = CreateCompatibleDC( hdc )))
2132 release_icon_ptr( hIcon, ptr );
2137 FIXME_(icon)("Ignoring istep=%d\n", istep);
2138 if (flags & DI_NOMIRROR)
2139 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2141 xorLength = ptr->nHeight * get_bitmap_width_bytes(
2142 ptr->nWidth, ptr->bBitsPerPixel);
2143 xorBitmapBits = (unsigned char *)(ptr + 1) + ptr->nHeight *
2144 get_bitmap_width_bytes(ptr->nWidth, 1);
2146 if (flags & DI_IMAGE)
2147 has_alpha = bitmap_has_alpha_channel(
2148 ptr->bBitsPerPixel, xorBitmapBits, xorLength);
2150 /* Calculate the size of the destination image. */
2153 if (flags & DI_DEFAULTSIZE)
2154 cxWidth = GetSystemMetrics (SM_CXICON);
2156 cxWidth = ptr->nWidth;
2160 if (flags & DI_DEFAULTSIZE)
2161 cyWidth = GetSystemMetrics (SM_CYICON);
2163 cyWidth = ptr->nHeight;
2166 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2176 hDC_off = CreateCompatibleDC(hdc);
2177 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2178 if (hDC_off && hB_off) {
2179 hOld = SelectObject(hDC_off, hB_off);
2180 FillRect(hDC_off, &r, hbr);
2184 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2187 HBITMAP hXorBits = NULL, hAndBits = NULL;
2188 COLORREF oldFg, oldBg;
2191 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2193 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2194 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2196 if (((flags & DI_MASK) && !(flags & DI_IMAGE)) ||
2197 ((flags & DI_MASK) && !has_alpha))
2199 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight, 1, 1, ptr + 1 );
2202 hBitTemp = SelectObject( hMemDC, hAndBits );
2204 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2205 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2207 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2208 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2209 SelectObject( hMemDC, hBitTemp );
2213 if (flags & DI_IMAGE)
2215 BITMAPINFOHEADER bmih;
2216 unsigned char *dibBits;
2218 memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
2219 bmih.biSize = sizeof(BITMAPINFOHEADER);
2220 bmih.biWidth = ptr->nWidth;
2221 bmih.biHeight = -ptr->nHeight;
2222 bmih.biPlanes = ptr->bPlanes;
2223 bmih.biBitCount = ptr->bBitsPerPixel;
2224 bmih.biCompression = BI_RGB;
2226 hXorBits = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
2227 (void*)&dibBits, NULL, 0);
2229 if (hXorBits && dibBits)
2233 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2235 /* Do the alpha blending render */
2236 premultiply_alpha_channel(dibBits, xorBitmapBits, xorLength);
2237 hBitTemp = SelectObject( hMemDC, hXorBits );
2240 GdiAlphaBlend(hDC_off, 0, 0, cxWidth, cyWidth, hMemDC,
2241 0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
2243 GdiAlphaBlend(hdc, x0, y0, cxWidth, cyWidth, hMemDC,
2244 0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
2246 SelectObject( hMemDC, hBitTemp );
2250 memcpy(dibBits, xorBitmapBits, xorLength);
2251 hBitTemp = SelectObject( hMemDC, hXorBits );
2253 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2254 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2256 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2257 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2258 SelectObject( hMemDC, hBitTemp );
2261 DeleteObject( hXorBits );
2267 SetTextColor( hdc, oldFg );
2268 SetBkColor( hdc, oldBg );
2270 if (hAndBits) DeleteObject( hAndBits );
2271 SetStretchBltMode (hdc, nStretchMode);
2273 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2274 SelectObject(hDC_off, hOld);
2277 if (hMemDC) DeleteDC( hMemDC );
2278 if (hDC_off) DeleteDC(hDC_off);
2279 if (hB_off) DeleteObject(hB_off);
2280 release_icon_ptr( hIcon, ptr );
2284 /***********************************************************************
2285 * DIB_FixColorsToLoadflags
2287 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2290 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2293 COLORREF c_W, c_S, c_F, c_L, c_C;
2302 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2304 WARN_(resource)("Invalid bitmap\n");
2308 if (bpp > 8) return;
2310 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2318 colors = bmi->bmiHeader.biClrUsed;
2319 if (colors > 256) colors = 256;
2320 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2323 c_W = GetSysColor(COLOR_WINDOW);
2324 c_S = GetSysColor(COLOR_3DSHADOW);
2325 c_F = GetSysColor(COLOR_3DFACE);
2326 c_L = GetSysColor(COLOR_3DLIGHT);
2328 if (loadflags & LR_LOADTRANSPARENT) {
2330 case 1: pix = pix >> 7; break;
2331 case 4: pix = pix >> 4; break;
2334 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2337 if (pix >= colors) {
2338 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2341 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2342 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2343 ptr->rgbBlue = GetBValue(c_W);
2344 ptr->rgbGreen = GetGValue(c_W);
2345 ptr->rgbRed = GetRValue(c_W);
2347 if (loadflags & LR_LOADMAP3DCOLORS)
2348 for (i=0; i<colors; i++) {
2349 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2350 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2351 if (c_C == RGB(128, 128, 128)) {
2352 ptr->rgbRed = GetRValue(c_S);
2353 ptr->rgbGreen = GetGValue(c_S);
2354 ptr->rgbBlue = GetBValue(c_S);
2355 } else if (c_C == RGB(192, 192, 192)) {
2356 ptr->rgbRed = GetRValue(c_F);
2357 ptr->rgbGreen = GetGValue(c_F);
2358 ptr->rgbBlue = GetBValue(c_F);
2359 } else if (c_C == RGB(223, 223, 223)) {
2360 ptr->rgbRed = GetRValue(c_L);
2361 ptr->rgbGreen = GetGValue(c_L);
2362 ptr->rgbBlue = GetBValue(c_L);
2368 /**********************************************************************
2371 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2372 INT desiredx, INT desiredy, UINT loadflags )
2374 HBITMAP hbitmap = 0, orig_bm;
2378 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2382 LONG width, height, new_width, new_height;
2386 HDC screen_mem_dc = NULL;
2388 if (!(loadflags & LR_LOADFROMFILE))
2392 /* OEM bitmap: try to load the resource from user32.dll */
2393 instance = user32_module;
2396 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2397 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2399 if ((info = LockResource( handle )) == NULL) return 0;
2403 BITMAPFILEHEADER * bmfh;
2405 if (!(ptr = map_fileW( name, NULL ))) return 0;
2406 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2407 bmfh = (BITMAPFILEHEADER *)ptr;
2408 if (!( bmfh->bfType == 0x4d42 /* 'BM' */ &&
2409 bmfh->bfReserved1 == 0 &&
2410 bmfh->bfReserved2 == 0))
2412 WARN("Invalid/unsupported bitmap format!\n");
2413 UnmapViewOfFile( ptr );
2418 size = bitmap_info_size(info, DIB_RGB_COLORS);
2419 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2420 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2422 if (!fix_info || !scaled_info) goto end;
2423 memcpy(fix_info, info, size);
2425 pix = *((LPBYTE)info + size);
2426 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2428 memcpy(scaled_info, fix_info, size);
2429 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2430 &bpp_dummy, &compr_dummy);
2432 new_width = desiredx;
2437 new_height = height > 0 ? desiredy : -desiredy;
2439 new_height = height;
2443 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2444 core->bcWidth = new_width;
2445 core->bcHeight = new_height;
2449 scaled_info->bmiHeader.biWidth = new_width;
2450 scaled_info->bmiHeader.biHeight = new_height;
2453 if (new_height < 0) new_height = -new_height;
2455 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2456 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2458 bits = (char *)info + size;
2460 if (loadflags & LR_CREATEDIBSECTION)
2462 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2463 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2467 if (is_dib_monochrome(fix_info))
2468 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2470 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2473 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2474 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2475 SelectObject(screen_mem_dc, orig_bm);
2478 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2479 HeapFree(GetProcessHeap(), 0, scaled_info);
2480 HeapFree(GetProcessHeap(), 0, fix_info);
2481 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2486 /**********************************************************************
2487 * LoadImageA (USER32.@)
2491 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2492 INT desiredx, INT desiredy, UINT loadflags)
2498 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2501 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2502 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2503 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2505 __EXCEPT_PAGE_FAULT {
2506 SetLastError( ERROR_INVALID_PARAMETER );
2510 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2511 HeapFree(GetProcessHeap(), 0, u_name);
2516 /******************************************************************************
2517 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2520 * hinst [I] Handle of instance that contains image
2521 * name [I] Name of image
2522 * type [I] Type of image
2523 * desiredx [I] Desired width
2524 * desiredy [I] Desired height
2525 * loadflags [I] Load flags
2528 * Success: Handle to newly loaded image
2531 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2533 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2534 INT desiredx, INT desiredy, UINT loadflags )
2536 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2537 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2539 if (loadflags & LR_DEFAULTSIZE) {
2540 if (type == IMAGE_ICON) {
2541 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2542 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2543 } else if (type == IMAGE_CURSOR) {
2544 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2545 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2548 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2551 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2554 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2557 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2558 if (palEnts == 0) palEnts = 256;
2559 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2560 palEnts, FALSE, loadflags);
2565 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2566 1, TRUE, loadflags);
2571 /******************************************************************************
2572 * CopyImage (USER32.@) Creates new image and copies attributes to it
2575 * hnd [I] Handle to image to copy
2576 * type [I] Type of image to copy
2577 * desiredx [I] Desired width of new image
2578 * desiredy [I] Desired height of new image
2579 * flags [I] Copy flags
2582 * Success: Handle to newly created image
2586 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2587 * all other versions (95/2000/XP have been tested) ignore it.
2590 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2591 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2592 * the copy will have the same depth as the screen.
2593 * The content of the image will only be copied if the bit depth of the
2594 * original image is compatible with the bit depth of the screen, or
2595 * if the source is a DIB section.
2596 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2598 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2599 INT desiredy, UINT flags )
2601 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2602 hnd, type, desiredx, desiredy, flags);
2613 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2614 if (!objSize) return 0;
2615 if ((desiredx < 0) || (desiredy < 0)) return 0;
2617 if (flags & LR_COPYFROMRESOURCE)
2619 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2622 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2623 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2625 /* Allocate memory for a BITMAPINFOHEADER structure and a
2626 color table. The maximum number of colors in a color table
2627 is 256 which corresponds to a bitmap with depth 8.
2628 Bitmaps with higher depths don't have color tables. */
2629 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2632 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2633 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2634 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2635 bi->bmiHeader.biCompression = BI_RGB;
2637 if (flags & LR_CREATEDIBSECTION)
2639 /* Create a DIB section. LR_MONOCHROME is ignored */
2641 HDC dc = CreateCompatibleDC(NULL);
2643 if (objSize == sizeof(DIBSECTION))
2645 /* The source bitmap is a DIB.
2646 Get its attributes to create an exact copy */
2647 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2650 /* Get the color table or the color masks */
2651 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2653 bi->bmiHeader.biWidth = desiredx;
2654 bi->bmiHeader.biHeight = desiredy;
2655 bi->bmiHeader.biSizeImage = 0;
2657 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2662 /* Create a device-dependent bitmap */
2664 BOOL monochrome = (flags & LR_MONOCHROME);
2666 if (objSize == sizeof(DIBSECTION))
2668 /* The source bitmap is a DIB section.
2669 Get its attributes */
2670 HDC dc = CreateCompatibleDC(NULL);
2671 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2672 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2673 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2676 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2678 /* Look if the colors of the DIB are black and white */
2681 (bi->bmiColors[0].rgbRed == 0xff
2682 && bi->bmiColors[0].rgbGreen == 0xff
2683 && bi->bmiColors[0].rgbBlue == 0xff
2684 && bi->bmiColors[0].rgbReserved == 0
2685 && bi->bmiColors[1].rgbRed == 0
2686 && bi->bmiColors[1].rgbGreen == 0
2687 && bi->bmiColors[1].rgbBlue == 0
2688 && bi->bmiColors[1].rgbReserved == 0)
2690 (bi->bmiColors[0].rgbRed == 0
2691 && bi->bmiColors[0].rgbGreen == 0
2692 && bi->bmiColors[0].rgbBlue == 0
2693 && bi->bmiColors[0].rgbReserved == 0
2694 && bi->bmiColors[1].rgbRed == 0xff
2695 && bi->bmiColors[1].rgbGreen == 0xff
2696 && bi->bmiColors[1].rgbBlue == 0xff
2697 && bi->bmiColors[1].rgbReserved == 0);
2700 else if (!monochrome)
2702 monochrome = ds.dsBm.bmBitsPixel == 1;
2707 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2711 HDC screenDC = GetDC(NULL);
2712 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2713 ReleaseDC(NULL, screenDC);
2719 /* Only copy the bitmap if it's a DIB section or if it's
2720 compatible to the screen */
2723 if (objSize == sizeof(DIBSECTION))
2725 copyContents = TRUE;
2729 HDC screenDC = GetDC(NULL);
2730 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2731 ReleaseDC(NULL, screenDC);
2733 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2738 /* The source bitmap may already be selected in a device context,
2739 use GetDIBits/StretchDIBits and not StretchBlt */
2744 dc = CreateCompatibleDC(NULL);
2746 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2747 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2748 bi->bmiHeader.biSizeImage = 0;
2749 bi->bmiHeader.biClrUsed = 0;
2750 bi->bmiHeader.biClrImportant = 0;
2752 /* Fill in biSizeImage */
2753 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2754 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2760 /* Get the image bits of the source bitmap */
2761 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2763 /* Copy it to the destination bitmap */
2764 oldBmp = SelectObject(dc, res);
2765 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2766 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2767 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2768 SelectObject(dc, oldBmp);
2770 HeapFree(GetProcessHeap(), 0, bits);
2776 if (flags & LR_COPYDELETEORG)
2781 HeapFree(GetProcessHeap(), 0, bi);
2785 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2787 /* Should call CURSORICON_ExtCopy but more testing
2788 * needs to be done before we change this
2790 if (flags) FIXME("Flags are ignored\n");
2791 return CopyCursor(hnd);
2797 /******************************************************************************
2798 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2801 * Success: Handle to specified bitmap
2804 HBITMAP WINAPI LoadBitmapW(
2805 HINSTANCE instance, /* [in] Handle to application instance */
2806 LPCWSTR name) /* [in] Address of bitmap resource name */
2808 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2811 /**********************************************************************
2812 * LoadBitmapA (USER32.@)
2816 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2818 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );