2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
7 * 1998 Turchanov Sergey
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_icons.asp
29 * Cursors and icons are stored in a global heap block, with the
32 * CURSORICONINFO info;
36 * The bits structures are in the format of a device-dependent bitmap.
38 * This layout is very sub-optimal, as the bitmap bits are stored in
39 * the X client instead of in the server like other bitmaps; however,
40 * some programs (notably Paint Brush) expect to be able to manipulate
41 * the bits directly :-(
45 #include "wine/port.h"
55 #include "wine/winbase16.h"
56 #include "wine/winuser16.h"
57 #include "wine/exception.h"
58 #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 #define CID_RESOURCE 0x0001
89 #define CID_WIN32 0x0004
90 #define CID_NONSHARED 0x0008
92 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
96 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
98 /**********************************************************************
99 * ICONCACHE for cursors/icons loaded with LR_SHARED.
101 * FIXME: This should not be allocated on the system heap, but on a
102 * subsystem-global heap (i.e. one for all Win16 processes,
103 * and one for each Win32 process).
105 typedef struct tagICONCACHE
107 struct tagICONCACHE *next;
118 static ICONCACHE *IconAnchor = NULL;
120 static CRITICAL_SECTION IconCrst;
121 static CRITICAL_SECTION_DEBUG critsect_debug =
124 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
125 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
127 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
129 static const WORD ICON_HOTSPOT = 0x4242;
132 /***********************************************************************
135 * Helper function to map a file to memory:
137 * [RETURN] ptr - pointer to mapped file
138 * [RETURN] filesize - pointer size of file to be stored if not NULL
140 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
142 HANDLE hFile, hMapping;
145 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
146 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
147 if (hFile != INVALID_HANDLE_VALUE)
149 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
152 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
153 CloseHandle( hMapping );
155 *filesize = GetFileSize( hFile, NULL );
157 CloseHandle( hFile );
163 /***********************************************************************
164 * get_bitmap_width_bytes
166 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
169 static int get_bitmap_width_bytes( int width, int bpp )
174 return 2 * ((width+15) / 16);
176 return 2 * ((width+3) / 4);
181 return width + (width & 1);
188 WARN("Unknown depth %d, please report.\n", bpp );
194 /***********************************************************************
195 * get_dib_width_bytes
197 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
199 static int get_dib_width_bytes( int width, int depth )
205 case 1: words = (width + 31) / 32; break;
206 case 4: words = (width + 7) / 8; break;
207 case 8: words = (width + 3) / 4; break;
209 case 16: words = (width + 1) / 2; break;
210 case 24: words = (width * 3 + 3)/4; break;
212 WARN("(%d): Unsupported depth\n", depth );
221 /***********************************************************************
224 * Return the size of the bitmap info structure including color table.
226 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
230 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
232 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
233 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
234 return sizeof(BITMAPCOREHEADER) + colors *
235 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
237 else /* assume BITMAPINFOHEADER */
239 colors = info->bmiHeader.biClrUsed;
240 if (colors > 256) /* buffer overflow otherwise */
242 if (!colors && (info->bmiHeader.biBitCount <= 8))
243 colors = 1 << info->bmiHeader.biBitCount;
244 return sizeof(BITMAPINFOHEADER) + colors *
245 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
250 /***********************************************************************
253 * Returns whether a DIB can be converted to a monochrome DDB.
255 * A DIB can be converted if its color table contains only black and
256 * white. Black must be the first color in the color table.
258 * Note : If the first color in the color table is white followed by
259 * black, we can't convert it to a monochrome DDB with
260 * SetDIBits, because black and white would be inverted.
262 static BOOL is_dib_monochrome( const BITMAPINFO* info )
264 if (info->bmiHeader.biBitCount != 1) return FALSE;
266 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
268 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
270 /* Check if the first color is black */
271 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
275 /* Check if the second color is white */
276 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
277 && (rgb->rgbtBlue == 0xff));
281 else /* assume BITMAPINFOHEADER */
283 const RGBQUAD *rgb = info->bmiColors;
285 /* Check if the first color is black */
286 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
287 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
291 /* Check if the second color is white */
292 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
293 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
299 /***********************************************************************
302 * Get the info from a bitmap header.
303 * Return 1 for INFOHEADER, 0 for COREHEADER,
304 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
306 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
307 LONG *height, WORD *bpp, DWORD *compr )
309 if (header->biSize == sizeof(BITMAPINFOHEADER))
311 *width = header->biWidth;
312 *height = header->biHeight;
313 *bpp = header->biBitCount;
314 *compr = header->biCompression;
317 if (header->biSize == sizeof(BITMAPCOREHEADER))
319 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
320 *width = core->bcWidth;
321 *height = core->bcHeight;
322 *bpp = core->bcBitCount;
326 if (header->biSize == sizeof(BITMAPV4HEADER))
328 const BITMAPV4HEADER *v4hdr = (const BITMAPV4HEADER *)header;
329 *width = v4hdr->bV4Width;
330 *height = v4hdr->bV4Height;
331 *bpp = v4hdr->bV4BitCount;
332 *compr = v4hdr->bV4V4Compression;
335 if (header->biSize == sizeof(BITMAPV5HEADER))
337 const BITMAPV5HEADER *v5hdr = (const BITMAPV5HEADER *)header;
338 *width = v5hdr->bV5Width;
339 *height = v5hdr->bV5Height;
340 *bpp = v5hdr->bV5BitCount;
341 *compr = v5hdr->bV5Compression;
344 ERR("(%d): unknown/wrong size for header\n", header->biSize );
348 /**********************************************************************
349 * CURSORICON_FindSharedIcon
351 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
356 EnterCriticalSection( &IconCrst );
358 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
359 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
366 LeaveCriticalSection( &IconCrst );
371 /*************************************************************************
372 * CURSORICON_FindCache
374 * Given a handle, find the corresponding cache element
377 * Handle [I] handle to an Image
380 * Success: The cache entry
384 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
387 ICONCACHE *pRet=NULL;
388 BOOL IsFound = FALSE;
391 EnterCriticalSection( &IconCrst );
393 for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
395 if ( hIcon == ptr->hIcon )
402 LeaveCriticalSection( &IconCrst );
407 /**********************************************************************
408 * CURSORICON_AddSharedIcon
410 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
412 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
415 ptr->hModule = hModule;
418 ptr->hGroupRsrc = hGroupRsrc;
421 EnterCriticalSection( &IconCrst );
422 ptr->next = IconAnchor;
424 LeaveCriticalSection( &IconCrst );
427 /**********************************************************************
428 * CURSORICON_DelSharedIcon
430 static INT CURSORICON_DelSharedIcon( HICON hIcon )
435 EnterCriticalSection( &IconCrst );
437 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
438 if ( ptr->hIcon == hIcon )
440 if ( ptr->count > 0 ) ptr->count--;
445 LeaveCriticalSection( &IconCrst );
450 /**********************************************************************
451 * CURSORICON_FreeModuleIcons
453 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
455 ICONCACHE **ptr = &IconAnchor;
456 HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
458 EnterCriticalSection( &IconCrst );
462 if ( (*ptr)->hModule == hModule )
464 ICONCACHE *freePtr = *ptr;
465 *ptr = freePtr->next;
467 GlobalFree16(HICON_16(freePtr->hIcon));
468 HeapFree( GetProcessHeap(), 0, freePtr );
474 LeaveCriticalSection( &IconCrst );
478 * The following macro functions account for the irregularities of
479 * accessing cursor and icon resources in files and resource entries.
481 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
482 int *width, int *height, int *bits );
484 /**********************************************************************
485 * CURSORICON_FindBestIcon
487 * Find the icon closest to the requested size and number of colors.
489 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
490 int width, int height, int colors )
492 int i, cx, cy, bits, bestEntry = -1;
493 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
494 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
497 iTotalDiff = 0xFFFFFFFF;
498 iColorDiff = 0xFFFFFFFF;
499 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
501 iTempXDiff = abs(width - cx);
502 iTempYDiff = abs(height - cy);
504 if(iTotalDiff > (iTempXDiff + iTempYDiff))
508 iTotalDiff = iXDiff + iYDiff;
512 /* Find Best Colors for Best Fit */
513 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
515 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
517 iTempColorDiff = abs(colors - (1<<bits));
518 if(iColorDiff > iTempColorDiff)
521 iColorDiff = iTempColorDiff;
529 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
530 int *width, int *height, int *bits )
532 CURSORICONDIR *resdir = dir;
535 if ( resdir->idCount <= n )
537 icon = &resdir->idEntries[n].ResInfo.icon;
538 *width = icon->bWidth;
539 *height = icon->bHeight;
540 *bits = resdir->idEntries[n].wBitCount;
544 /**********************************************************************
545 * CURSORICON_FindBestCursor
547 * Find the cursor closest to the requested size.
548 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
551 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
552 int width, int height, int color )
554 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
556 /* Double height to account for AND and XOR masks */
560 /* First find the largest one smaller than or equal to the requested size*/
562 maxwidth = maxheight = 0;
563 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
565 if ((cx <= width) && (cy <= height) &&
566 (cx > maxwidth) && (cy > maxheight) &&
574 if (bestEntry != -1) return bestEntry;
576 /* Now find the smallest one larger than the requested size */
578 maxwidth = maxheight = 255;
579 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
581 if (((cx < maxwidth) && (cy < maxheight) && (bits == 1)) ||
593 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
594 int *width, int *height, int *bits )
596 CURSORICONDIR *resdir = dir;
599 if ( resdir->idCount <= n )
601 cursor = &resdir->idEntries[n].ResInfo.cursor;
602 *width = cursor->wWidth;
603 *height = cursor->wHeight;
604 *bits = resdir->idEntries[n].wBitCount;
608 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
609 int width, int height, int colors )
613 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
614 width, height, colors );
617 return &dir->idEntries[n];
620 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
621 int width, int height, int color )
623 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
624 width, height, color );
627 return &dir->idEntries[n];
630 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
631 int *width, int *height, int *bits )
633 CURSORICONFILEDIR *filedir = dir;
634 CURSORICONFILEDIRENTRY *entry;
636 if ( filedir->idCount <= n )
638 entry = &filedir->idEntries[n];
639 *width = entry->bWidth;
640 *height = entry->bHeight;
641 *bits = entry->bColorCount;
645 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
646 int width, int height, int color )
648 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
649 width, height, color );
652 return &dir->idEntries[n];
655 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
656 int width, int height, int color )
658 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
659 width, height, color );
662 return &dir->idEntries[n];
665 /**********************************************************************
666 * CreateIconFromResourceEx (USER32.@)
668 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
669 * with cbSize parameter as well.
671 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
672 BOOL bIcon, DWORD dwVersion,
673 INT width, INT height,
678 int sizeAnd, sizeXor;
679 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
680 BITMAP bmpXor, bmpAnd;
686 hotspot.x = ICON_HOTSPOT;
687 hotspot.y = ICON_HOTSPOT;
689 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
690 bits, cbSize, dwVersion, width, height,
691 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
692 if (dwVersion == 0x00020000)
694 FIXME_(cursor)("\t2.xx resources are not supported\n");
699 bmi = (BITMAPINFO *)bits;
700 else /* get the hotspot */
702 POINT16 *pt = (POINT16 *)bits;
704 bmi = (BITMAPINFO *)(pt + 1);
706 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
708 if (!width) width = bmi->bmiHeader.biWidth;
709 if (!height) height = bmi->bmiHeader.biHeight/2;
710 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
711 (bmi->bmiHeader.biWidth != width);
713 /* Check bitmap header */
715 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
716 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
717 bmi->bmiHeader.biCompression != BI_RGB) )
719 WARN_(cursor)("\tinvalid resource bitmap header.\n");
723 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
728 /* Make sure we have room for the monochrome bitmap later on.
729 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
730 * up to and including the biBitCount. In-memory icon resource
731 * format is as follows:
733 * BITMAPINFOHEADER icHeader // DIB header
734 * RGBQUAD icColors[] // Color table
735 * BYTE icXOR[] // DIB bits for XOR mask
736 * BYTE icAND[] // DIB bits for AND mask
739 if ((pInfo = HeapAlloc( GetProcessHeap(), 0,
740 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
742 memcpy( pInfo, bmi, size );
743 pInfo->bmiHeader.biHeight /= 2;
745 /* Create the XOR bitmap */
750 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
754 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
761 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
763 hOld = SelectObject(hdcMem, hXorBits);
764 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
765 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
766 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
767 SelectObject(hdcMem, hOld);
769 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
772 if (is_dib_monochrome(bmi)) {
773 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
774 SetDIBits(screen_dc, hXorBits, 0, height,
775 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
778 hXorBits = CreateDIBitmap(screen_dc, &pInfo->bmiHeader,
779 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS);
784 char* xbits = (char *)bmi + size +
785 get_dib_width_bytes( bmi->bmiHeader.biWidth,
786 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
788 pInfo->bmiHeader.biBitCount = 1;
789 if (pInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
791 RGBQUAD *rgb = pInfo->bmiColors;
793 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
794 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
795 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
796 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
800 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
802 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
803 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
806 /* Create the AND bitmap */
809 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
813 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
815 hOld = SelectObject(hdcMem, hAndBits);
816 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
817 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
818 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
819 SelectObject(hdcMem, hOld);
821 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
824 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
826 if (hAndBits) SetDIBits(screen_dc, hAndBits, 0, height,
827 xbits, pInfo, DIB_RGB_COLORS);
830 if( !hAndBits ) DeleteObject( hXorBits );
832 HeapFree( GetProcessHeap(), 0, pInfo );
836 if( !hXorBits || !hAndBits )
838 WARN_(cursor)("\tunable to create an icon bitmap.\n");
842 /* Now create the CURSORICONINFO structure */
843 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
844 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
845 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
846 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
848 hObj = GlobalAlloc16( GMEM_MOVEABLE,
849 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
852 CURSORICONINFO *info;
854 info = (CURSORICONINFO *)GlobalLock16( hObj );
855 info->ptHotSpot.x = hotspot.x;
856 info->ptHotSpot.y = hotspot.y;
857 info->nWidth = bmpXor.bmWidth;
858 info->nHeight = bmpXor.bmHeight;
859 info->nWidthBytes = bmpXor.bmWidthBytes;
860 info->bPlanes = bmpXor.bmPlanes;
861 info->bBitsPerPixel = bmpXor.bmBitsPixel;
863 /* Transfer the bitmap bits to the CURSORICONINFO structure */
865 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
866 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
867 GlobalUnlock16( hObj );
870 DeleteObject( hAndBits );
871 DeleteObject( hXorBits );
872 return HICON_32(hObj);
876 /**********************************************************************
877 * CreateIconFromResource (USER32.@)
879 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
880 BOOL bIcon, DWORD dwVersion)
882 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
886 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
887 INT width, INT height, INT colors,
888 BOOL fCursor, UINT loadflags)
890 CURSORICONFILEDIRENTRY *entry;
891 CURSORICONFILEDIR *dir;
896 TRACE("loading %s\n", debugstr_w( filename ));
898 bits = map_fileW( filename, &filesize );
902 /* Check for .ani. */
903 if (memcmp( bits, "RIFF", 4 ) == 0)
905 FIXME("No support for .ani cursors.\n");
909 dir = (CURSORICONFILEDIR*) bits;
910 if ( filesize < sizeof(*dir) )
913 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
917 entry = CURSORICON_FindBestCursorFile( dir, width, height, 1 );
919 entry = CURSORICON_FindBestIconFile( dir, width, height, colors );
924 /* check that we don't run off the end of the file */
925 if ( entry->dwDIBOffset > filesize )
927 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
930 hIcon = CreateIconFromResourceEx( &bits[entry->dwDIBOffset], entry->dwDIBSize,
931 !fCursor, 0x00030000, width, height, loadflags );
933 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
934 UnmapViewOfFile( bits );
938 /**********************************************************************
941 * Load a cursor or icon from resource or file.
943 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
944 INT width, INT height, INT colors,
945 BOOL fCursor, UINT loadflags)
949 HRSRC hRsrc, hGroupRsrc;
951 CURSORICONDIRENTRY *dirEntry;
956 TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
957 hInstance, debugstr_w(name), width, height, colors, fCursor, loadflags);
959 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
960 return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
962 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
964 /* Normalize hInstance (must be uniquely represented for icon cache) */
966 if (!HIWORD( hInstance ))
967 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
969 /* Get directory resource ID */
971 if (!(hRsrc = FindResourceW( hInstance, name,
972 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
976 /* Find the best entry in the directory */
978 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
979 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
981 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
983 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
984 if (!dirEntry) return 0;
985 wResId = dirEntry->wResId;
986 dwBytesInRes = dirEntry->dwBytesInRes;
987 FreeResource( handle );
989 /* Load the resource */
991 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
992 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
994 /* If shared icon, check whether it was already loaded */
995 if ( (loadflags & LR_SHARED)
996 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
999 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1000 bits = (LPBYTE)LockResource( handle );
1001 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1002 !fCursor, 0x00030000, width, height, loadflags);
1003 FreeResource( handle );
1005 /* If shared icon, add to icon cache */
1007 if ( hIcon && (loadflags & LR_SHARED) )
1008 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1013 /***********************************************************************
1016 * Make a copy of a cursor or icon.
1018 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
1020 char *ptrOld, *ptrNew;
1022 HICON16 hOld = HICON_16(hIcon);
1025 if (!(ptrOld = (char *)GlobalLock16( hOld ))) return 0;
1026 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
1027 size = GlobalSize16( hOld );
1028 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
1029 FarSetOwner16( hNew, hInst16 );
1030 ptrNew = (char *)GlobalLock16( hNew );
1031 memcpy( ptrNew, ptrOld, size );
1032 GlobalUnlock16( hOld );
1033 GlobalUnlock16( hNew );
1034 return HICON_32(hNew);
1037 /*************************************************************************
1038 * CURSORICON_ExtCopy
1040 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1043 * Handle [I] handle to an Image
1044 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1045 * iDesiredCX [I] The Desired width of the Image
1046 * iDesiredCY [I] The desired height of the Image
1047 * nFlags [I] The flags from CopyImage
1050 * Success: The new handle of the Image
1053 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1054 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1055 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1060 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1061 INT iDesiredCX, INT iDesiredCY,
1066 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1067 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1074 /* Best Fit or Monochrome */
1075 if( (nFlags & LR_COPYFROMRESOURCE
1076 && (iDesiredCX > 0 || iDesiredCY > 0))
1077 || nFlags & LR_MONOCHROME)
1079 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1081 /* Not Found in Cache, then do a straight copy
1083 if(pIconCache == NULL)
1085 hNew = CURSORICON_Copy(0, hIcon);
1086 if(nFlags & LR_COPYFROMRESOURCE)
1088 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1093 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1099 CURSORICONDIR *pDir;
1100 CURSORICONDIRENTRY *pDirEntry;
1101 BOOL bIsIcon = (nType == IMAGE_ICON);
1103 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1105 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1106 || (iDesiredCX == 0 && iDesiredCY == 0))
1108 iDesiredCY = GetSystemMetrics(bIsIcon ?
1109 SM_CYICON : SM_CYCURSOR);
1110 iDesiredCX = GetSystemMetrics(bIsIcon ?
1111 SM_CXICON : SM_CXCURSOR);
1114 /* Retrieve the CURSORICONDIRENTRY
1116 if (!(hMem = LoadResource( pIconCache->hModule ,
1117 pIconCache->hGroupRsrc)))
1121 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1130 pDirEntry = CURSORICON_FindBestIconRes(
1131 pDir, iDesiredCX, iDesiredCY, 256 );
1135 pDirEntry = CURSORICON_FindBestCursorRes(
1136 pDir, iDesiredCX, iDesiredCY, 1);
1139 wResId = pDirEntry->wResId;
1140 dwBytesInRes = pDirEntry->dwBytesInRes;
1143 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1144 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1145 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1149 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1150 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1154 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1159 pBits = (LPBYTE)LockResource( hMem );
1161 if(nFlags & LR_DEFAULTSIZE)
1163 iTargetCY = GetSystemMetrics(SM_CYICON);
1164 iTargetCX = GetSystemMetrics(SM_CXICON);
1167 /* Create a New Icon with the proper dimension
1169 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1170 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1174 else hNew = CURSORICON_Copy(0, hIcon);
1179 /***********************************************************************
1180 * CreateCursor (USER32.@)
1182 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1183 INT xHotSpot, INT yHotSpot,
1184 INT nWidth, INT nHeight,
1185 LPCVOID lpANDbits, LPCVOID lpXORbits )
1187 CURSORICONINFO info;
1189 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1190 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1192 info.ptHotSpot.x = xHotSpot;
1193 info.ptHotSpot.y = yHotSpot;
1194 info.nWidth = nWidth;
1195 info.nHeight = nHeight;
1196 info.nWidthBytes = 0;
1198 info.bBitsPerPixel = 1;
1200 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1204 /***********************************************************************
1205 * CreateIcon (USER.407)
1207 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1208 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1209 LPCVOID lpANDbits, LPCVOID lpXORbits )
1211 CURSORICONINFO info;
1213 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1214 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1216 info.ptHotSpot.x = ICON_HOTSPOT;
1217 info.ptHotSpot.y = ICON_HOTSPOT;
1218 info.nWidth = nWidth;
1219 info.nHeight = nHeight;
1220 info.nWidthBytes = 0;
1221 info.bPlanes = bPlanes;
1222 info.bBitsPerPixel = bBitsPixel;
1224 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1228 /***********************************************************************
1229 * CreateIcon (USER32.@)
1231 * Creates an icon based on the specified bitmaps. The bitmaps must be
1232 * provided in a device dependent format and will be resized to
1233 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1234 * depth. The provided bitmaps must be top-down bitmaps.
1235 * Although Windows does not support 15bpp(*) this API must support it
1236 * for Winelib applications.
1238 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1242 * Success: handle to an icon
1245 * FIXME: Do we need to resize the bitmaps?
1247 HICON WINAPI CreateIcon(
1248 HINSTANCE hInstance, /* [in] the application's hInstance */
1249 INT nWidth, /* [in] the width of the provided bitmaps */
1250 INT nHeight, /* [in] the height of the provided bitmaps */
1251 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1252 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1253 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1254 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1259 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1260 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1263 iinfo.xHotspot = ICON_HOTSPOT;
1264 iinfo.yHotspot = ICON_HOTSPOT;
1265 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1266 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1268 hIcon = CreateIconIndirect( &iinfo );
1270 DeleteObject( iinfo.hbmMask );
1271 DeleteObject( iinfo.hbmColor );
1277 /***********************************************************************
1278 * CreateCursorIconIndirect (USER.408)
1280 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1281 CURSORICONINFO *info,
1287 int sizeAnd, sizeXor;
1289 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1290 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1291 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1292 sizeXor = info->nHeight * info->nWidthBytes;
1293 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1294 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1295 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1297 FarSetOwner16( handle, hInstance );
1298 ptr = (char *)GlobalLock16( handle );
1299 memcpy( ptr, info, sizeof(*info) );
1300 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1301 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1302 GlobalUnlock16( handle );
1307 /***********************************************************************
1308 * CopyIcon (USER.368)
1310 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1312 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1313 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1317 /***********************************************************************
1318 * CopyIcon (USER32.@)
1320 HICON WINAPI CopyIcon( HICON hIcon )
1322 TRACE_(icon)("%p\n", hIcon );
1323 return CURSORICON_Copy( 0, hIcon );
1327 /***********************************************************************
1328 * CopyCursor (USER.369)
1330 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1332 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1333 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1336 /**********************************************************************
1337 * DestroyIcon32 (USER.610)
1339 * This routine is actually exported from Win95 USER under the name
1340 * DestroyIcon32 ... The behaviour implemented here should mimic
1341 * the Win95 one exactly, especially the return values, which
1342 * depend on the setting of various flags.
1344 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1348 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1350 /* Check whether destroying active cursor */
1352 if ( get_user_thread_info()->cursor == HICON_32(handle) )
1354 WARN_(cursor)("Destroying active cursor!\n" );
1358 /* Try shared cursor/icon first */
1360 if ( !(flags & CID_NONSHARED) )
1362 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1365 return (flags & CID_WIN32)? TRUE : (count == 0);
1367 /* FIXME: OEM cursors/icons should be recognized */
1370 /* Now assume non-shared cursor/icon */
1372 retv = GlobalFree16( handle );
1373 return (flags & CID_RESOURCE)? retv : TRUE;
1376 /***********************************************************************
1377 * DestroyIcon (USER32.@)
1379 BOOL WINAPI DestroyIcon( HICON hIcon )
1381 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1385 /***********************************************************************
1386 * DestroyCursor (USER32.@)
1388 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1390 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1394 /***********************************************************************
1395 * DrawIcon (USER32.@)
1397 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1399 CURSORICONINFO *ptr;
1401 HBITMAP hXorBits, hAndBits;
1402 COLORREF oldFg, oldBg;
1404 TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);
1406 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
1407 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1408 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
1410 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1411 ptr->bBitsPerPixel, (char *)(ptr + 1)
1412 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
1413 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1414 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1416 if (hXorBits && hAndBits)
1418 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1419 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
1420 SelectObject( hMemDC, hXorBits );
1421 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
1422 SelectObject( hMemDC, hBitTemp );
1425 if (hXorBits) DeleteObject( hXorBits );
1426 if (hAndBits) DeleteObject( hAndBits );
1427 GlobalUnlock16(HICON_16(hIcon));
1428 SetTextColor( hdc, oldFg );
1429 SetBkColor( hdc, oldBg );
1433 /***********************************************************************
1434 * DumpIcon (USER.459)
1436 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1437 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1439 CURSORICONINFO *info = MapSL( pInfo );
1440 int sizeAnd, sizeXor;
1442 if (!info) return 0;
1443 sizeXor = info->nHeight * info->nWidthBytes;
1444 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1445 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1446 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1447 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1448 return MAKELONG( sizeXor, sizeXor );
1452 /***********************************************************************
1453 * SetCursor (USER32.@)
1455 * Set the cursor shape.
1458 * A handle to the previous cursor shape.
1460 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1462 struct user_thread_info *thread_info = get_user_thread_info();
1465 if (hCursor == thread_info->cursor) return hCursor; /* No change */
1466 TRACE("%p\n", hCursor);
1467 hOldCursor = thread_info->cursor;
1468 thread_info->cursor = hCursor;
1469 /* Change the cursor shape only if it is visible */
1470 if (thread_info->cursor_count >= 0)
1472 USER_Driver->pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
1473 GlobalUnlock16(HCURSOR_16(hCursor));
1478 /***********************************************************************
1479 * ShowCursor (USER32.@)
1481 INT WINAPI ShowCursor( BOOL bShow )
1483 struct user_thread_info *thread_info = get_user_thread_info();
1485 TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
1489 if (++thread_info->cursor_count == 0) /* Show it */
1491 USER_Driver->pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(thread_info->cursor)));
1492 GlobalUnlock16(HCURSOR_16(thread_info->cursor));
1497 if (--thread_info->cursor_count == -1) /* Hide it */
1498 USER_Driver->pSetCursor( NULL );
1500 return thread_info->cursor_count;
1503 /***********************************************************************
1504 * GetCursor (USER32.@)
1506 HCURSOR WINAPI GetCursor(void)
1508 return get_user_thread_info()->cursor;
1512 /***********************************************************************
1513 * ClipCursor (USER32.@)
1515 BOOL WINAPI ClipCursor( const RECT *rect )
1519 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1520 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1521 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1522 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1524 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1525 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1527 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1528 CURSOR_ClipRect = virt;
1530 USER_Driver->pClipCursor( rect );
1535 /***********************************************************************
1536 * GetClipCursor (USER32.@)
1538 BOOL WINAPI GetClipCursor( RECT *rect )
1540 /* If this is first time - initialize the rect */
1541 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1543 return CopyRect( rect, &CURSOR_ClipRect );
1547 /***********************************************************************
1548 * SetSystemCursor (USER32.@)
1550 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1552 FIXME("(%p,%08x),stub!\n", hcur, id);
1557 /**********************************************************************
1558 * LookupIconIdFromDirectoryEx (USER.364)
1560 * FIXME: exact parameter sizes
1562 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1563 INT16 width, INT16 height, UINT16 cFlag )
1565 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1568 /**********************************************************************
1569 * LookupIconIdFromDirectoryEx (USER32.@)
1571 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1572 INT width, INT height, UINT cFlag )
1574 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1576 if( dir && !dir->idReserved && (dir->idType & 3) )
1578 CURSORICONDIRENTRY* entry;
1583 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1586 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1591 entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1593 entry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
1595 if( entry ) retVal = entry->wResId;
1597 else WARN_(cursor)("invalid resource directory\n");
1601 /**********************************************************************
1602 * LookupIconIdFromDirectory (USER.?)
1604 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
1606 return LookupIconIdFromDirectoryEx16( dir, bIcon,
1607 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1608 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1611 /**********************************************************************
1612 * LookupIconIdFromDirectory (USER32.@)
1614 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1616 return LookupIconIdFromDirectoryEx( dir, bIcon,
1617 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1618 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1621 /**********************************************************************
1622 * GetIconID (USER.455)
1624 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1626 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
1628 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1629 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1634 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1635 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1637 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1638 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1640 WARN_(cursor)("invalid res type %d\n", resType );
1645 /**********************************************************************
1646 * LoadCursorIconHandler (USER.336)
1648 * Supposed to load resources of Windows 2.x applications.
1650 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1652 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1653 hResource, hModule, hRsrc);
1654 return (HGLOBAL16)0;
1657 /**********************************************************************
1658 * LoadIconHandler (USER.456)
1660 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
1662 LPBYTE bits = (LPBYTE)LockResource16( hResource );
1664 TRACE_(cursor)("hRes=%04x\n",hResource);
1666 return HICON_16(CreateIconFromResourceEx( bits, 0, TRUE,
1667 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
1670 /***********************************************************************
1671 * LoadCursorW (USER32.@)
1673 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1675 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1677 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1678 LR_SHARED | LR_DEFAULTSIZE );
1681 /***********************************************************************
1682 * LoadCursorA (USER32.@)
1684 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1686 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1688 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1689 LR_SHARED | LR_DEFAULTSIZE );
1692 /***********************************************************************
1693 * LoadCursorFromFileW (USER32.@)
1695 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1697 TRACE("%s\n", debugstr_w(name));
1699 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1700 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1703 /***********************************************************************
1704 * LoadCursorFromFileA (USER32.@)
1706 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1708 TRACE("%s\n", debugstr_a(name));
1710 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1711 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1714 /***********************************************************************
1715 * LoadIconW (USER32.@)
1717 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1719 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1721 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1722 LR_SHARED | LR_DEFAULTSIZE );
1725 /***********************************************************************
1726 * LoadIconA (USER32.@)
1728 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1730 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1732 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1733 LR_SHARED | LR_DEFAULTSIZE );
1736 /**********************************************************************
1737 * GetIconInfo (USER32.@)
1739 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1741 CURSORICONINFO *ciconinfo;
1744 ciconinfo = GlobalLock16(HICON_16(hIcon));
1748 TRACE("%p => %dx%d, %d bpp\n", hIcon,
1749 ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
1751 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1752 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1754 iconinfo->fIcon = TRUE;
1755 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1756 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1760 iconinfo->fIcon = FALSE;
1761 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1762 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1765 height = ciconinfo->nHeight;
1767 if (ciconinfo->bBitsPerPixel > 1)
1769 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
1770 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1771 (char *)(ciconinfo + 1)
1772 + ciconinfo->nHeight *
1773 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1777 iconinfo->hbmColor = 0;
1781 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
1782 1, 1, (char *)(ciconinfo + 1));
1784 GlobalUnlock16(HICON_16(hIcon));
1789 /**********************************************************************
1790 * CreateIconIndirect (USER32.@)
1792 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1794 BITMAP bmpXor,bmpAnd;
1796 int sizeXor,sizeAnd;
1798 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1799 iconinfo->hbmColor, iconinfo->hbmMask,
1800 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1802 if (!iconinfo->hbmMask) return 0;
1804 if (iconinfo->hbmColor)
1806 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1807 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1808 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
1809 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
1811 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1812 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1813 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1814 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1816 sizeXor = iconinfo->hbmColor ? (bmpXor.bmHeight * bmpXor.bmWidthBytes) : 0;
1817 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
1819 hObj = GlobalAlloc16( GMEM_MOVEABLE,
1820 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1823 CURSORICONINFO *info;
1825 info = (CURSORICONINFO *)GlobalLock16( hObj );
1827 /* If we are creating an icon, the hotspot is unused */
1828 if (iconinfo->fIcon)
1830 info->ptHotSpot.x = ICON_HOTSPOT;
1831 info->ptHotSpot.y = ICON_HOTSPOT;
1835 info->ptHotSpot.x = iconinfo->xHotspot;
1836 info->ptHotSpot.y = iconinfo->yHotspot;
1839 if (iconinfo->hbmColor)
1841 info->nWidth = bmpXor.bmWidth;
1842 info->nHeight = bmpXor.bmHeight;
1843 info->nWidthBytes = bmpXor.bmWidthBytes;
1844 info->bPlanes = bmpXor.bmPlanes;
1845 info->bBitsPerPixel = bmpXor.bmBitsPixel;
1849 info->nWidth = bmpAnd.bmWidth;
1850 info->nHeight = bmpAnd.bmHeight / 2;
1851 info->nWidthBytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
1853 info->bBitsPerPixel = 1;
1856 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1858 /* Some apps pass a color bitmap as a mask, convert it to b/w */
1859 if (bmpAnd.bmBitsPixel == 1)
1861 GetBitmapBits( iconinfo->hbmMask, sizeAnd, (char*)(info + 1) );
1866 HBITMAP hbmp_old, hbmp_mem_old, hbmp_mono;
1869 hdc_mem = CreateCompatibleDC( hdc );
1871 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
1873 hbmp_old = SelectObject( hdc, iconinfo->hbmMask );
1874 hbmp_mem_old = SelectObject( hdc_mem, hbmp_mono );
1876 BitBlt( hdc_mem, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc, 0, 0, SRCCOPY );
1878 SelectObject( hdc, hbmp_old );
1879 SelectObject( hdc_mem, hbmp_mem_old );
1881 DeleteDC( hdc_mem );
1882 ReleaseDC( 0, hdc );
1884 GetBitmapBits( hbmp_mono, sizeAnd, (char*)(info + 1) );
1885 DeleteObject( hbmp_mono );
1887 if (iconinfo->hbmColor) GetBitmapBits( iconinfo->hbmColor, sizeXor, (char*)(info + 1) + sizeAnd );
1888 GlobalUnlock16( hObj );
1890 return HICON_32(hObj);
1893 /******************************************************************************
1894 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1897 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1900 * hdc [I] Handle to device context
1901 * x0 [I] X coordinate of upper left corner
1902 * y0 [I] Y coordinate of upper left corner
1903 * hIcon [I] Handle to icon to draw
1904 * cxWidth [I] Width of icon
1905 * cyWidth [I] Height of icon
1906 * istep [I] Index of frame in animated cursor
1907 * hbr [I] Handle to background brush
1908 * flags [I] Icon-drawing flags
1914 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1915 INT cxWidth, INT cyWidth, UINT istep,
1916 HBRUSH hbr, UINT flags )
1918 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
1919 HDC hDC_off = 0, hMemDC;
1920 BOOL result = FALSE, DoOffscreen;
1921 HBITMAP hB_off = 0, hOld = 0;
1923 if (!ptr) return FALSE;
1924 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1925 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1927 hMemDC = CreateCompatibleDC (hdc);
1929 FIXME_(icon)("Ignoring istep=%d\n", istep);
1930 if (flags & DI_COMPAT)
1931 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
1934 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
1938 /* Calculate the size of the destination image. */
1941 if (flags & DI_DEFAULTSIZE)
1942 cxWidth = GetSystemMetrics (SM_CXICON);
1944 cxWidth = ptr->nWidth;
1948 if (flags & DI_DEFAULTSIZE)
1949 cyWidth = GetSystemMetrics (SM_CYICON);
1951 cyWidth = ptr->nHeight;
1954 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1964 hDC_off = CreateCompatibleDC(hdc);
1965 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
1966 if (hDC_off && hB_off) {
1967 hOld = SelectObject(hDC_off, hB_off);
1968 FillRect(hDC_off, &r, hbr);
1972 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
1974 HBITMAP hXorBits, hAndBits;
1975 COLORREF oldFg, oldBg;
1978 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
1980 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1981 ptr->bPlanes, ptr->bBitsPerPixel,
1984 get_bitmap_width_bytes(ptr->nWidth,1) );
1985 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1986 1, 1, (char *)(ptr+1) );
1987 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1988 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1990 if (hXorBits && hAndBits)
1992 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1993 if (flags & DI_MASK)
1996 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1997 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1999 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2000 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2002 SelectObject( hMemDC, hXorBits );
2003 if (flags & DI_IMAGE)
2006 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2007 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2009 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2010 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2012 SelectObject( hMemDC, hBitTemp );
2016 SetTextColor( hdc, oldFg );
2017 SetBkColor( hdc, oldBg );
2018 if (hXorBits) DeleteObject( hXorBits );
2019 if (hAndBits) DeleteObject( hAndBits );
2020 SetStretchBltMode (hdc, nStretchMode);
2022 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2023 SelectObject(hDC_off, hOld);
2026 if (hMemDC) DeleteDC( hMemDC );
2027 if (hDC_off) DeleteDC(hDC_off);
2028 if (hB_off) DeleteObject(hB_off);
2029 GlobalUnlock16(HICON_16(hIcon));
2033 /***********************************************************************
2034 * DIB_FixColorsToLoadflags
2036 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2039 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2042 COLORREF c_W, c_S, c_F, c_L, c_C;
2051 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2053 WARN_(resource)("Invalid bitmap\n");
2057 if (bpp > 8) return;
2059 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2067 colors = bmi->bmiHeader.biClrUsed;
2068 if (colors > 256) colors = 256;
2069 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2072 c_W = GetSysColor(COLOR_WINDOW);
2073 c_S = GetSysColor(COLOR_3DSHADOW);
2074 c_F = GetSysColor(COLOR_3DFACE);
2075 c_L = GetSysColor(COLOR_3DLIGHT);
2077 if (loadflags & LR_LOADTRANSPARENT) {
2079 case 1: pix = pix >> 7; break;
2080 case 4: pix = pix >> 4; break;
2083 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2086 if (pix >= colors) {
2087 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2090 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2091 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2092 ptr->rgbBlue = GetBValue(c_W);
2093 ptr->rgbGreen = GetGValue(c_W);
2094 ptr->rgbRed = GetRValue(c_W);
2096 if (loadflags & LR_LOADMAP3DCOLORS)
2097 for (i=0; i<colors; i++) {
2098 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2099 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2100 if (c_C == RGB(128, 128, 128)) {
2101 ptr->rgbRed = GetRValue(c_S);
2102 ptr->rgbGreen = GetGValue(c_S);
2103 ptr->rgbBlue = GetBValue(c_S);
2104 } else if (c_C == RGB(192, 192, 192)) {
2105 ptr->rgbRed = GetRValue(c_F);
2106 ptr->rgbGreen = GetGValue(c_F);
2107 ptr->rgbBlue = GetBValue(c_F);
2108 } else if (c_C == RGB(223, 223, 223)) {
2109 ptr->rgbRed = GetRValue(c_L);
2110 ptr->rgbGreen = GetGValue(c_L);
2111 ptr->rgbBlue = GetBValue(c_L);
2117 /**********************************************************************
2120 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2121 INT desiredx, INT desiredy, UINT loadflags )
2123 HBITMAP hbitmap = 0, orig_bm;
2127 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2131 LONG width, height, new_width, new_height;
2135 HDC screen_mem_dc = NULL;
2137 if (!(loadflags & LR_LOADFROMFILE))
2141 /* OEM bitmap: try to load the resource from user32.dll */
2142 instance = user32_module;
2145 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2146 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2148 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
2152 if (!(ptr = map_fileW( name, NULL ))) return 0;
2153 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2156 size = bitmap_info_size(info, DIB_RGB_COLORS);
2157 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2158 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2160 if (!fix_info || !scaled_info) goto end;
2161 memcpy(fix_info, info, size);
2163 pix = *((LPBYTE)info + size);
2164 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2166 memcpy(scaled_info, fix_info, size);
2167 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2168 &bpp_dummy, &compr_dummy);
2170 new_width = desiredx;
2175 new_height = height > 0 ? desiredy : -desiredy;
2177 new_height = height;
2181 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2182 core->bcWidth = new_width;
2183 core->bcHeight = new_height;
2187 scaled_info->bmiHeader.biWidth = new_width;
2188 scaled_info->bmiHeader.biHeight = new_height;
2191 if (new_height < 0) new_height = -new_height;
2193 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2194 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2196 bits = (char *)info + size;
2198 if (loadflags & LR_CREATEDIBSECTION)
2200 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2201 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2205 if (is_dib_monochrome(fix_info))
2206 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2208 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2211 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2212 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2213 SelectObject(screen_mem_dc, orig_bm);
2216 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2217 HeapFree(GetProcessHeap(), 0, scaled_info);
2218 HeapFree(GetProcessHeap(), 0, fix_info);
2219 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2224 /**********************************************************************
2225 * LoadImageA (USER32.@)
2229 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2230 INT desiredx, INT desiredy, UINT loadflags)
2236 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2239 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2240 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2241 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2243 __EXCEPT_PAGE_FAULT {
2244 SetLastError( ERROR_INVALID_PARAMETER );
2248 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2249 HeapFree(GetProcessHeap(), 0, u_name);
2254 /******************************************************************************
2255 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2258 * hinst [I] Handle of instance that contains image
2259 * name [I] Name of image
2260 * type [I] Type of image
2261 * desiredx [I] Desired width
2262 * desiredy [I] Desired height
2263 * loadflags [I] Load flags
2266 * Success: Handle to newly loaded image
2269 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2271 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2272 INT desiredx, INT desiredy, UINT loadflags )
2274 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2275 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2277 if (loadflags & LR_DEFAULTSIZE) {
2278 if (type == IMAGE_ICON) {
2279 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2280 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2281 } else if (type == IMAGE_CURSOR) {
2282 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2283 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2286 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2289 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2292 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2295 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2296 if (palEnts == 0) palEnts = 256;
2297 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2298 palEnts, FALSE, loadflags);
2303 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2304 1, TRUE, loadflags);
2309 /******************************************************************************
2310 * CopyImage (USER32.@) Creates new image and copies attributes to it
2313 * hnd [I] Handle to image to copy
2314 * type [I] Type of image to copy
2315 * desiredx [I] Desired width of new image
2316 * desiredy [I] Desired height of new image
2317 * flags [I] Copy flags
2320 * Success: Handle to newly created image
2324 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2325 * all other versions (95/2000/XP have been tested) ignore it.
2328 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2329 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2330 * the copy will have the same depth as the screen.
2331 * The content of the image will only be copied if the bit depth of the
2332 * original image is compatible with the bit depth of the screen, or
2333 * if the source is a DIB section.
2334 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2336 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2337 INT desiredy, UINT flags )
2339 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2340 hnd, type, desiredx, desiredy, flags);
2351 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2352 if (!objSize) return 0;
2353 if ((desiredx < 0) || (desiredy < 0)) return 0;
2355 if (flags & LR_COPYFROMRESOURCE)
2357 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2360 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2361 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2363 /* Allocate memory for a BITMAPINFOHEADER structure and a
2364 color table. The maximum number of colors in a color table
2365 is 256 which corresponds to a bitmap with depth 8.
2366 Bitmaps with higher depths don't have color tables. */
2367 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2370 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2371 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2372 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2373 bi->bmiHeader.biCompression = BI_RGB;
2375 if (flags & LR_CREATEDIBSECTION)
2377 /* Create a DIB section. LR_MONOCHROME is ignored */
2379 HDC dc = CreateCompatibleDC(NULL);
2381 if (objSize == sizeof(DIBSECTION))
2383 /* The source bitmap is a DIB.
2384 Get its attributes to create an exact copy */
2385 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2388 /* Get the color table or the color masks */
2389 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2391 bi->bmiHeader.biWidth = desiredx;
2392 bi->bmiHeader.biHeight = desiredy;
2393 bi->bmiHeader.biSizeImage = 0;
2395 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2400 /* Create a device-dependent bitmap */
2402 BOOL monochrome = (flags & LR_MONOCHROME);
2404 if (objSize == sizeof(DIBSECTION))
2406 /* The source bitmap is a DIB section.
2407 Get its attributes */
2408 HDC dc = CreateCompatibleDC(NULL);
2409 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2410 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2411 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2414 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2416 /* Look if the colors of the DIB are black and white */
2419 (bi->bmiColors[0].rgbRed == 0xff
2420 && bi->bmiColors[0].rgbGreen == 0xff
2421 && bi->bmiColors[0].rgbBlue == 0xff
2422 && bi->bmiColors[0].rgbReserved == 0
2423 && bi->bmiColors[1].rgbRed == 0
2424 && bi->bmiColors[1].rgbGreen == 0
2425 && bi->bmiColors[1].rgbBlue == 0
2426 && bi->bmiColors[1].rgbReserved == 0)
2428 (bi->bmiColors[0].rgbRed == 0
2429 && bi->bmiColors[0].rgbGreen == 0
2430 && bi->bmiColors[0].rgbBlue == 0
2431 && bi->bmiColors[0].rgbReserved == 0
2432 && bi->bmiColors[1].rgbRed == 0xff
2433 && bi->bmiColors[1].rgbGreen == 0xff
2434 && bi->bmiColors[1].rgbBlue == 0xff
2435 && bi->bmiColors[1].rgbReserved == 0);
2438 else if (!monochrome)
2440 monochrome = ds.dsBm.bmBitsPixel == 1;
2445 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2449 HDC screenDC = GetDC(NULL);
2450 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2451 ReleaseDC(NULL, screenDC);
2457 /* Only copy the bitmap if it's a DIB section or if it's
2458 compatible to the screen */
2461 if (objSize == sizeof(DIBSECTION))
2463 copyContents = TRUE;
2467 HDC screenDC = GetDC(NULL);
2468 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2469 ReleaseDC(NULL, screenDC);
2471 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2476 /* The source bitmap may already be selected in a device context,
2477 use GetDIBits/StretchDIBits and not StretchBlt */
2482 dc = CreateCompatibleDC(NULL);
2484 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2485 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2486 bi->bmiHeader.biSizeImage = 0;
2487 bi->bmiHeader.biClrUsed = 0;
2488 bi->bmiHeader.biClrImportant = 0;
2490 /* Fill in biSizeImage */
2491 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2492 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2498 /* Get the image bits of the source bitmap */
2499 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2501 /* Copy it to the destination bitmap */
2502 oldBmp = SelectObject(dc, res);
2503 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2504 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2505 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2506 SelectObject(dc, oldBmp);
2508 HeapFree(GetProcessHeap(), 0, bits);
2514 if (flags & LR_COPYDELETEORG)
2519 HeapFree(GetProcessHeap(), 0, bi);
2523 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2525 /* Should call CURSORICON_ExtCopy but more testing
2526 * needs to be done before we change this
2528 if (flags) FIXME("Flags are ignored\n");
2529 return CopyCursor(hnd);
2535 /******************************************************************************
2536 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2539 * Success: Handle to specified bitmap
2542 HBITMAP WINAPI LoadBitmapW(
2543 HINSTANCE instance, /* [in] Handle to application instance */
2544 LPCWSTR name) /* [in] Address of bitmap resource name */
2546 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2549 /**********************************************************************
2550 * LoadBitmapA (USER32.@)
2554 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2556 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );