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 :-(
43 * FIXME: what are we going to do with animation and color (bpp > 1) cursors ?!
47 #include "wine/port.h"
59 #include "wine/winbase16.h"
60 #include "wine/winuser16.h"
61 #include "wine/exception.h"
62 #include "wine/debug.h"
63 #include "user_private.h"
65 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
66 WINE_DECLARE_DEBUG_CHANNEL(icon);
67 WINE_DECLARE_DEBUG_CHANNEL(resource);
80 } CURSORICONFILEDIRENTRY;
87 CURSORICONFILEDIRENTRY idEntries[1];
92 #define CID_RESOURCE 0x0001
93 #define CID_WIN32 0x0004
94 #define CID_NONSHARED 0x0008
96 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
100 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
102 /**********************************************************************
103 * ICONCACHE for cursors/icons loaded with LR_SHARED.
105 * FIXME: This should not be allocated on the system heap, but on a
106 * subsystem-global heap (i.e. one for all Win16 processes,
107 * and one for each Win32 process).
109 typedef struct tagICONCACHE
111 struct tagICONCACHE *next;
122 static ICONCACHE *IconAnchor = NULL;
124 static CRITICAL_SECTION IconCrst;
125 static CRITICAL_SECTION_DEBUG critsect_debug =
128 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
129 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
131 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
133 static const WORD ICON_HOTSPOT = 0x4242;
136 /***********************************************************************
139 * Helper function to map a file to memory:
141 * [RETURN] ptr - pointer to mapped file
142 * [RETURN] filesize - pointer size of file to be stored if not NULL
144 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
146 HANDLE hFile, hMapping;
149 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
150 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
151 if (hFile != INVALID_HANDLE_VALUE)
153 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
156 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
157 CloseHandle( hMapping );
159 *filesize = GetFileSize( hFile, NULL );
161 CloseHandle( hFile );
167 /***********************************************************************
168 * get_bitmap_width_bytes
170 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
173 static int get_bitmap_width_bytes( int width, int bpp )
178 return 2 * ((width+15) / 16);
180 return 2 * ((width+3) / 4);
185 return width + (width & 1);
192 WARN("Unknown depth %d, please report.\n", bpp );
198 /***********************************************************************
199 * get_dib_width_bytes
201 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
203 static int get_dib_width_bytes( int width, int depth )
209 case 1: words = (width + 31) / 32; break;
210 case 4: words = (width + 7) / 8; break;
211 case 8: words = (width + 3) / 4; break;
213 case 16: words = (width + 1) / 2; break;
214 case 24: words = (width * 3 + 3)/4; break;
216 WARN("(%d): Unsupported depth\n", depth );
225 /***********************************************************************
228 * Return the size of the bitmap info structure including color table.
230 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
234 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
236 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
237 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
238 return sizeof(BITMAPCOREHEADER) + colors *
239 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
241 else /* assume BITMAPINFOHEADER */
243 colors = info->bmiHeader.biClrUsed;
244 if (colors > 256) /* buffer overflow otherwise */
246 if (!colors && (info->bmiHeader.biBitCount <= 8))
247 colors = 1 << info->bmiHeader.biBitCount;
248 return sizeof(BITMAPINFOHEADER) + colors *
249 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
254 /***********************************************************************
257 * Returns whether a DIB can be converted to a monochrome DDB.
259 * A DIB can be converted if its color table contains only black and
260 * white. Black must be the first color in the color table.
262 * Note : If the first color in the color table is white followed by
263 * black, we can't convert it to a monochrome DDB with
264 * SetDIBits, because black and white would be inverted.
266 static BOOL is_dib_monochrome( const BITMAPINFO* info )
268 if (info->bmiHeader.biBitCount != 1) return FALSE;
270 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
272 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
274 /* Check if the first color is black */
275 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
279 /* Check if the second color is white */
280 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
281 && (rgb->rgbtBlue == 0xff));
285 else /* assume BITMAPINFOHEADER */
287 const RGBQUAD *rgb = info->bmiColors;
289 /* Check if the first color is black */
290 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
291 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
295 /* Check if the second color is white */
296 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
297 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
303 /***********************************************************************
306 * Get the info from a bitmap header.
307 * Return 1 for INFOHEADER, 0 for COREHEADER,
308 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
310 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
311 LONG *height, WORD *bpp, DWORD *compr )
313 if (header->biSize == sizeof(BITMAPINFOHEADER))
315 *width = header->biWidth;
316 *height = header->biHeight;
317 *bpp = header->biBitCount;
318 *compr = header->biCompression;
321 if (header->biSize == sizeof(BITMAPCOREHEADER))
323 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
324 *width = core->bcWidth;
325 *height = core->bcHeight;
326 *bpp = core->bcBitCount;
330 if (header->biSize == sizeof(BITMAPV4HEADER))
332 const BITMAPV4HEADER *v4hdr = (const BITMAPV4HEADER *)header;
333 *width = v4hdr->bV4Width;
334 *height = v4hdr->bV4Height;
335 *bpp = v4hdr->bV4BitCount;
336 *compr = v4hdr->bV4V4Compression;
339 if (header->biSize == sizeof(BITMAPV5HEADER))
341 const BITMAPV5HEADER *v5hdr = (const BITMAPV5HEADER *)header;
342 *width = v5hdr->bV5Width;
343 *height = v5hdr->bV5Height;
344 *bpp = v5hdr->bV5BitCount;
345 *compr = v5hdr->bV5Compression;
348 ERR("(%d): unknown/wrong size for header\n", header->biSize );
352 /**********************************************************************
353 * CURSORICON_FindSharedIcon
355 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
360 EnterCriticalSection( &IconCrst );
362 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
363 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
370 LeaveCriticalSection( &IconCrst );
375 /*************************************************************************
376 * CURSORICON_FindCache
378 * Given a handle, find the corresponding cache element
381 * Handle [I] handle to an Image
384 * Success: The cache entry
388 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
391 ICONCACHE *pRet=NULL;
392 BOOL IsFound = FALSE;
395 EnterCriticalSection( &IconCrst );
397 for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
399 if ( hIcon == ptr->hIcon )
406 LeaveCriticalSection( &IconCrst );
411 /**********************************************************************
412 * CURSORICON_AddSharedIcon
414 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
416 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
419 ptr->hModule = hModule;
422 ptr->hGroupRsrc = hGroupRsrc;
425 EnterCriticalSection( &IconCrst );
426 ptr->next = IconAnchor;
428 LeaveCriticalSection( &IconCrst );
431 /**********************************************************************
432 * CURSORICON_DelSharedIcon
434 static INT CURSORICON_DelSharedIcon( HICON hIcon )
439 EnterCriticalSection( &IconCrst );
441 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
442 if ( ptr->hIcon == hIcon )
444 if ( ptr->count > 0 ) ptr->count--;
449 LeaveCriticalSection( &IconCrst );
454 /**********************************************************************
455 * CURSORICON_FreeModuleIcons
457 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
459 ICONCACHE **ptr = &IconAnchor;
460 HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
462 EnterCriticalSection( &IconCrst );
466 if ( (*ptr)->hModule == hModule )
468 ICONCACHE *freePtr = *ptr;
469 *ptr = freePtr->next;
471 GlobalFree16(HICON_16(freePtr->hIcon));
472 HeapFree( GetProcessHeap(), 0, freePtr );
478 LeaveCriticalSection( &IconCrst );
482 * The following macro functions account for the irregularities of
483 * accessing cursor and icon resources in files and resource entries.
485 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
486 int *width, int *height, int *bits );
488 /**********************************************************************
489 * CURSORICON_FindBestIcon
491 * Find the icon closest to the requested size and number of colors.
493 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
494 int width, int height, int colors )
496 int i, cx, cy, bits, bestEntry = -1;
497 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
498 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
501 iTotalDiff = 0xFFFFFFFF;
502 iColorDiff = 0xFFFFFFFF;
503 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
505 iTempXDiff = abs(width - cx);
506 iTempYDiff = abs(height - cy);
508 if(iTotalDiff > (iTempXDiff + iTempYDiff))
512 iTotalDiff = iXDiff + iYDiff;
516 /* Find Best Colors for Best Fit */
517 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
519 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
521 iTempColorDiff = abs(colors - (1<<bits));
522 if(iColorDiff > iTempColorDiff)
525 iColorDiff = iTempColorDiff;
533 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
534 int *width, int *height, int *bits )
536 CURSORICONDIR *resdir = dir;
539 if ( resdir->idCount <= n )
541 icon = &resdir->idEntries[n].ResInfo.icon;
542 *width = icon->bWidth;
543 *height = icon->bHeight;
544 *bits = resdir->idEntries[n].wBitCount;
548 /**********************************************************************
549 * CURSORICON_FindBestCursor
551 * Find the cursor closest to the requested size.
552 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
555 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
556 int width, int height, int color )
558 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
560 /* Double height to account for AND and XOR masks */
564 /* First find the largest one smaller than or equal to the requested size*/
566 maxwidth = maxheight = 0;
567 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
569 if ((cx <= width) && (cy <= height) &&
570 (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) && (bits == 1)) ||
597 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
598 int *width, int *height, int *bits )
600 CURSORICONDIR *resdir = dir;
603 if ( resdir->idCount <= n )
605 cursor = &resdir->idEntries[n].ResInfo.cursor;
606 *width = cursor->wWidth;
607 *height = cursor->wHeight;
608 *bits = resdir->idEntries[n].wBitCount;
612 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
613 int width, int height, int colors )
617 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
618 width, height, colors );
621 return &dir->idEntries[n];
624 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
625 int width, int height, int color )
627 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
628 width, height, color );
631 return &dir->idEntries[n];
634 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
635 int *width, int *height, int *bits )
637 CURSORICONFILEDIR *filedir = dir;
638 CURSORICONFILEDIRENTRY *entry;
640 if ( filedir->idCount <= n )
642 entry = &filedir->idEntries[n];
643 *width = entry->bWidth;
644 *height = entry->bHeight;
645 *bits = entry->bColorCount;
649 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
650 int width, int height, int color )
652 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
653 width, height, color );
656 return &dir->idEntries[n];
659 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
660 int width, int height, int color )
662 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
663 width, height, color );
666 return &dir->idEntries[n];
669 /**********************************************************************
670 * CreateIconFromResourceEx (USER32.@)
672 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
673 * with cbSize parameter as well.
675 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
676 BOOL bIcon, DWORD dwVersion,
677 INT width, INT height,
682 int sizeAnd, sizeXor;
683 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
684 BITMAP bmpXor, bmpAnd;
690 hotspot.x = ICON_HOTSPOT;
691 hotspot.y = ICON_HOTSPOT;
693 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
694 bits, cbSize, (unsigned)dwVersion, width, height,
695 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
696 if (dwVersion == 0x00020000)
698 FIXME_(cursor)("\t2.xx resources are not supported\n");
703 bmi = (BITMAPINFO *)bits;
704 else /* get the hotspot */
706 POINT16 *pt = (POINT16 *)bits;
708 bmi = (BITMAPINFO *)(pt + 1);
710 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
712 if (!width) width = bmi->bmiHeader.biWidth;
713 if (!height) height = bmi->bmiHeader.biHeight/2;
714 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
715 (bmi->bmiHeader.biWidth != width);
717 /* Check bitmap header */
719 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
720 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
721 bmi->bmiHeader.biCompression != BI_RGB) )
723 WARN_(cursor)("\tinvalid resource bitmap header.\n");
727 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
732 /* Make sure we have room for the monochrome bitmap later on.
733 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
734 * up to and including the biBitCount. In-memory icon resource
735 * format is as follows:
737 * BITMAPINFOHEADER icHeader // DIB header
738 * RGBQUAD icColors[] // Color table
739 * BYTE icXOR[] // DIB bits for XOR mask
740 * BYTE icAND[] // DIB bits for AND mask
743 if ((pInfo = HeapAlloc( GetProcessHeap(), 0,
744 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
746 memcpy( pInfo, bmi, size );
747 pInfo->bmiHeader.biHeight /= 2;
749 /* Create the XOR bitmap */
754 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
758 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
765 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
767 hOld = SelectObject(hdcMem, hXorBits);
768 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
769 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
770 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
771 SelectObject(hdcMem, hOld);
773 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
776 if (is_dib_monochrome(bmi)) {
777 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
778 SetDIBits(screen_dc, hXorBits, 0, height,
779 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
782 hXorBits = CreateDIBitmap(screen_dc, &pInfo->bmiHeader,
783 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS);
788 char* xbits = (char *)bmi + size +
789 get_dib_width_bytes( bmi->bmiHeader.biWidth,
790 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
792 pInfo->bmiHeader.biBitCount = 1;
793 if (pInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
795 RGBQUAD *rgb = pInfo->bmiColors;
797 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
798 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
799 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
800 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
804 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
806 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
807 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
810 /* Create the AND bitmap */
813 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
817 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
819 hOld = SelectObject(hdcMem, hAndBits);
820 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
821 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
822 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
823 SelectObject(hdcMem, hOld);
825 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
828 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
830 if (hAndBits) SetDIBits(screen_dc, hAndBits, 0, height,
831 xbits, pInfo, DIB_RGB_COLORS);
834 if( !hAndBits ) DeleteObject( hXorBits );
836 HeapFree( GetProcessHeap(), 0, pInfo );
840 if( !hXorBits || !hAndBits )
842 WARN_(cursor)("\tunable to create an icon bitmap.\n");
846 /* Now create the CURSORICONINFO structure */
847 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
848 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
849 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
850 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
852 hObj = GlobalAlloc16( GMEM_MOVEABLE,
853 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
856 CURSORICONINFO *info;
858 info = (CURSORICONINFO *)GlobalLock16( hObj );
859 info->ptHotSpot.x = hotspot.x;
860 info->ptHotSpot.y = hotspot.y;
861 info->nWidth = bmpXor.bmWidth;
862 info->nHeight = bmpXor.bmHeight;
863 info->nWidthBytes = bmpXor.bmWidthBytes;
864 info->bPlanes = bmpXor.bmPlanes;
865 info->bBitsPerPixel = bmpXor.bmBitsPixel;
867 /* Transfer the bitmap bits to the CURSORICONINFO structure */
869 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
870 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
871 GlobalUnlock16( hObj );
874 DeleteObject( hAndBits );
875 DeleteObject( hXorBits );
876 return HICON_32((HICON16)hObj);
880 /**********************************************************************
881 * CreateIconFromResource (USER32.@)
883 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
884 BOOL bIcon, DWORD dwVersion)
886 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
890 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
891 INT width, INT height, INT colors,
892 BOOL fCursor, UINT loadflags)
894 CURSORICONFILEDIRENTRY *entry;
895 CURSORICONFILEDIR *dir;
900 TRACE("loading %s\n", debugstr_w( filename ));
902 bits = map_fileW( filename, &filesize );
906 dir = (CURSORICONFILEDIR*) bits;
907 if ( filesize < sizeof(*dir) )
910 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
914 entry = CURSORICON_FindBestCursorFile( dir, width, height, 1 );
916 entry = CURSORICON_FindBestIconFile( dir, width, height, colors );
921 /* check that we don't run off the end of the file */
922 if ( entry->dwDIBOffset > filesize )
924 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
927 hIcon = CreateIconFromResourceEx( &bits[entry->dwDIBOffset], entry->dwDIBSize,
928 !fCursor, 0x00030000, width, height, loadflags );
930 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
931 UnmapViewOfFile( bits );
935 /**********************************************************************
938 * Load a cursor or icon from resource or file.
940 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
941 INT width, INT height, INT colors,
942 BOOL fCursor, UINT loadflags)
946 HRSRC hRsrc, hGroupRsrc;
948 CURSORICONDIRENTRY *dirEntry;
953 TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
954 hInstance, debugstr_w(name), width, height, colors, fCursor, loadflags);
956 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
957 return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
959 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
961 /* Normalize hInstance (must be uniquely represented for icon cache) */
963 if (!HIWORD( hInstance ))
964 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
966 /* Get directory resource ID */
968 if (!(hRsrc = FindResourceW( hInstance, name,
969 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
973 /* Find the best entry in the directory */
975 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
976 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
978 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
980 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
981 if (!dirEntry) return 0;
982 wResId = dirEntry->wResId;
983 dwBytesInRes = dirEntry->dwBytesInRes;
984 FreeResource( handle );
986 /* Load the resource */
988 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
989 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
991 /* If shared icon, check whether it was already loaded */
992 if ( (loadflags & LR_SHARED)
993 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
996 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
997 bits = (LPBYTE)LockResource( handle );
998 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
999 !fCursor, 0x00030000, width, height, loadflags);
1000 FreeResource( handle );
1002 /* If shared icon, add to icon cache */
1004 if ( hIcon && (loadflags & LR_SHARED) )
1005 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1010 /***********************************************************************
1013 * Make a copy of a cursor or icon.
1015 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
1017 char *ptrOld, *ptrNew;
1019 HICON16 hOld = HICON_16(hIcon);
1022 if (!(ptrOld = (char *)GlobalLock16( hOld ))) return 0;
1023 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
1024 size = GlobalSize16( hOld );
1025 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
1026 FarSetOwner16( hNew, hInst16 );
1027 ptrNew = (char *)GlobalLock16( hNew );
1028 memcpy( ptrNew, ptrOld, size );
1029 GlobalUnlock16( hOld );
1030 GlobalUnlock16( hNew );
1031 return HICON_32(hNew);
1034 /*************************************************************************
1035 * CURSORICON_ExtCopy
1037 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1040 * Handle [I] handle to an Image
1041 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1042 * iDesiredCX [I] The Desired width of the Image
1043 * iDesiredCY [I] The desired height of the Image
1044 * nFlags [I] The flags from CopyImage
1047 * Success: The new handle of the Image
1050 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1051 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1052 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1057 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1058 INT iDesiredCX, INT iDesiredCY,
1063 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1064 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1071 /* Best Fit or Monochrome */
1072 if( (nFlags & LR_COPYFROMRESOURCE
1073 && (iDesiredCX > 0 || iDesiredCY > 0))
1074 || nFlags & LR_MONOCHROME)
1076 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1078 /* Not Found in Cache, then do a straight copy
1080 if(pIconCache == NULL)
1082 hNew = CURSORICON_Copy(0, hIcon);
1083 if(nFlags & LR_COPYFROMRESOURCE)
1085 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1090 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1096 CURSORICONDIR *pDir;
1097 CURSORICONDIRENTRY *pDirEntry;
1098 BOOL bIsIcon = (nType == IMAGE_ICON);
1100 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1102 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1103 || (iDesiredCX == 0 && iDesiredCY == 0))
1105 iDesiredCY = GetSystemMetrics(bIsIcon ?
1106 SM_CYICON : SM_CYCURSOR);
1107 iDesiredCX = GetSystemMetrics(bIsIcon ?
1108 SM_CXICON : SM_CXCURSOR);
1111 /* Retrieve the CURSORICONDIRENTRY
1113 if (!(hMem = LoadResource( pIconCache->hModule ,
1114 pIconCache->hGroupRsrc)))
1118 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1127 pDirEntry = CURSORICON_FindBestIconRes(
1128 pDir, iDesiredCX, iDesiredCY, 256 );
1132 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursorRes(
1133 pDir, iDesiredCX, iDesiredCY, 1);
1136 wResId = pDirEntry->wResId;
1137 dwBytesInRes = pDirEntry->dwBytesInRes;
1140 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1141 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1142 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1146 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1147 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1151 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1156 pBits = (LPBYTE)LockResource( hMem );
1158 if(nFlags & LR_DEFAULTSIZE)
1160 iTargetCY = GetSystemMetrics(SM_CYICON);
1161 iTargetCX = GetSystemMetrics(SM_CXICON);
1164 /* Create a New Icon with the proper dimension
1166 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1167 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1171 else hNew = CURSORICON_Copy(0, hIcon);
1176 /***********************************************************************
1177 * CreateCursor (USER32.@)
1179 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1180 INT xHotSpot, INT yHotSpot,
1181 INT nWidth, INT nHeight,
1182 LPCVOID lpANDbits, LPCVOID lpXORbits )
1184 CURSORICONINFO info;
1186 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1187 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1189 info.ptHotSpot.x = xHotSpot;
1190 info.ptHotSpot.y = yHotSpot;
1191 info.nWidth = nWidth;
1192 info.nHeight = nHeight;
1193 info.nWidthBytes = 0;
1195 info.bBitsPerPixel = 1;
1197 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1201 /***********************************************************************
1202 * CreateIcon (USER.407)
1204 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1205 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1206 LPCVOID lpANDbits, LPCVOID lpXORbits )
1208 CURSORICONINFO info;
1210 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1211 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1213 info.ptHotSpot.x = ICON_HOTSPOT;
1214 info.ptHotSpot.y = ICON_HOTSPOT;
1215 info.nWidth = nWidth;
1216 info.nHeight = nHeight;
1217 info.nWidthBytes = 0;
1218 info.bPlanes = bPlanes;
1219 info.bBitsPerPixel = bBitsPixel;
1221 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1225 /***********************************************************************
1226 * CreateIcon (USER32.@)
1228 * Creates an icon based on the specified bitmaps. The bitmaps must be
1229 * provided in a device dependent format and will be resized to
1230 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1231 * depth. The provided bitmaps must be top-down bitmaps.
1232 * Although Windows does not support 15bpp(*) this API must support it
1233 * for Winelib applications.
1235 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1239 * Success: handle to an icon
1242 * FIXME: Do we need to resize the bitmaps?
1244 HICON WINAPI CreateIcon(
1245 HINSTANCE hInstance, /* [in] the application's hInstance */
1246 INT nWidth, /* [in] the width of the provided bitmaps */
1247 INT nHeight, /* [in] the height of the provided bitmaps */
1248 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1249 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1250 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1251 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1256 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1257 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1260 iinfo.xHotspot = ICON_HOTSPOT;
1261 iinfo.yHotspot = ICON_HOTSPOT;
1262 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1263 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1265 hIcon = CreateIconIndirect( &iinfo );
1267 DeleteObject( iinfo.hbmMask );
1268 DeleteObject( iinfo.hbmColor );
1274 /***********************************************************************
1275 * CreateCursorIconIndirect (USER.408)
1277 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1278 CURSORICONINFO *info,
1284 int sizeAnd, sizeXor;
1286 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1287 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1288 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1289 sizeXor = info->nHeight * info->nWidthBytes;
1290 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1291 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1292 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1294 FarSetOwner16( handle, hInstance );
1295 ptr = (char *)GlobalLock16( handle );
1296 memcpy( ptr, info, sizeof(*info) );
1297 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1298 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1299 GlobalUnlock16( handle );
1304 /***********************************************************************
1305 * CopyIcon (USER.368)
1307 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1309 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1310 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1314 /***********************************************************************
1315 * CopyIcon (USER32.@)
1317 HICON WINAPI CopyIcon( HICON hIcon )
1319 TRACE_(icon)("%p\n", hIcon );
1320 return CURSORICON_Copy( 0, hIcon );
1324 /***********************************************************************
1325 * CopyCursor (USER.369)
1327 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1329 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1330 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1333 /**********************************************************************
1334 * DestroyIcon32 (USER.610)
1336 * This routine is actually exported from Win95 USER under the name
1337 * DestroyIcon32 ... The behaviour implemented here should mimic
1338 * the Win95 one exactly, especially the return values, which
1339 * depend on the setting of various flags.
1341 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1345 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1347 /* Check whether destroying active cursor */
1349 if ( get_user_thread_info()->cursor == HICON_32(handle) )
1351 WARN_(cursor)("Destroying active cursor!\n" );
1355 /* Try shared cursor/icon first */
1357 if ( !(flags & CID_NONSHARED) )
1359 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1362 return (flags & CID_WIN32)? TRUE : (count == 0);
1364 /* FIXME: OEM cursors/icons should be recognized */
1367 /* Now assume non-shared cursor/icon */
1369 retv = GlobalFree16( handle );
1370 return (flags & CID_RESOURCE)? retv : TRUE;
1373 /***********************************************************************
1374 * DestroyIcon (USER32.@)
1376 BOOL WINAPI DestroyIcon( HICON hIcon )
1378 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1382 /***********************************************************************
1383 * DestroyCursor (USER32.@)
1385 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1387 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1391 /***********************************************************************
1392 * DrawIcon (USER32.@)
1394 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1396 CURSORICONINFO *ptr;
1398 HBITMAP hXorBits, hAndBits;
1399 COLORREF oldFg, oldBg;
1401 TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);
1403 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
1404 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1405 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
1407 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1408 ptr->bBitsPerPixel, (char *)(ptr + 1)
1409 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
1410 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1411 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1413 if (hXorBits && hAndBits)
1415 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1416 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
1417 SelectObject( hMemDC, hXorBits );
1418 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
1419 SelectObject( hMemDC, hBitTemp );
1422 if (hXorBits) DeleteObject( hXorBits );
1423 if (hAndBits) DeleteObject( hAndBits );
1424 GlobalUnlock16(HICON_16(hIcon));
1425 SetTextColor( hdc, oldFg );
1426 SetBkColor( hdc, oldBg );
1430 /***********************************************************************
1431 * DumpIcon (USER.459)
1433 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1434 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1436 CURSORICONINFO *info = MapSL( pInfo );
1437 int sizeAnd, sizeXor;
1439 if (!info) return 0;
1440 sizeXor = info->nHeight * info->nWidthBytes;
1441 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1442 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1443 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1444 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1445 return MAKELONG( sizeXor, sizeXor );
1449 /***********************************************************************
1450 * SetCursor (USER32.@)
1452 * Set the cursor shape.
1455 * A handle to the previous cursor shape.
1457 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1459 struct user_thread_info *thread_info = get_user_thread_info();
1462 if (hCursor == thread_info->cursor) return hCursor; /* No change */
1463 TRACE("%p\n", hCursor);
1464 hOldCursor = thread_info->cursor;
1465 thread_info->cursor = hCursor;
1466 /* Change the cursor shape only if it is visible */
1467 if (thread_info->cursor_count >= 0)
1469 USER_Driver->pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
1470 GlobalUnlock16(HCURSOR_16(hCursor));
1475 /***********************************************************************
1476 * ShowCursor (USER32.@)
1478 INT WINAPI ShowCursor( BOOL bShow )
1480 struct user_thread_info *thread_info = get_user_thread_info();
1482 TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
1486 if (++thread_info->cursor_count == 0) /* Show it */
1488 USER_Driver->pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(thread_info->cursor)));
1489 GlobalUnlock16(HCURSOR_16(thread_info->cursor));
1494 if (--thread_info->cursor_count == -1) /* Hide it */
1495 USER_Driver->pSetCursor( NULL );
1497 return thread_info->cursor_count;
1500 /***********************************************************************
1501 * GetCursor (USER32.@)
1503 HCURSOR WINAPI GetCursor(void)
1505 return get_user_thread_info()->cursor;
1509 /***********************************************************************
1510 * ClipCursor (USER32.@)
1512 BOOL WINAPI ClipCursor( const RECT *rect )
1516 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1517 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1518 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1519 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1521 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1522 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1524 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1525 CURSOR_ClipRect = virt;
1527 USER_Driver->pClipCursor( rect );
1532 /***********************************************************************
1533 * GetClipCursor (USER32.@)
1535 BOOL WINAPI GetClipCursor( RECT *rect )
1537 /* If this is first time - initialize the rect */
1538 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1540 return CopyRect( rect, &CURSOR_ClipRect );
1544 /***********************************************************************
1545 * SetSystemCursor (USER32.@)
1547 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1549 FIXME("(%p,%08x),stub!\n", hcur, id);
1554 /**********************************************************************
1555 * LookupIconIdFromDirectoryEx (USER.364)
1557 * FIXME: exact parameter sizes
1559 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1560 INT16 width, INT16 height, UINT16 cFlag )
1562 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1565 /**********************************************************************
1566 * LookupIconIdFromDirectoryEx (USER32.@)
1568 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1569 INT width, INT height, UINT cFlag )
1571 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1573 if( dir && !dir->idReserved && (dir->idType & 3) )
1575 CURSORICONDIRENTRY* entry;
1580 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1583 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1588 entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1590 entry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
1592 if( entry ) retVal = entry->wResId;
1594 else WARN_(cursor)("invalid resource directory\n");
1598 /**********************************************************************
1599 * LookupIconIdFromDirectory (USER.?)
1601 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
1603 return LookupIconIdFromDirectoryEx16( dir, bIcon,
1604 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1605 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1608 /**********************************************************************
1609 * LookupIconIdFromDirectory (USER32.@)
1611 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1613 return LookupIconIdFromDirectoryEx( dir, bIcon,
1614 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1615 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1618 /**********************************************************************
1619 * GetIconID (USER.455)
1621 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1623 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
1625 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1626 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1631 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1632 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1634 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1635 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1637 WARN_(cursor)("invalid res type %d\n", resType );
1642 /**********************************************************************
1643 * LoadCursorIconHandler (USER.336)
1645 * Supposed to load resources of Windows 2.x applications.
1647 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1649 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1650 hResource, hModule, hRsrc);
1651 return (HGLOBAL16)0;
1654 /**********************************************************************
1655 * LoadIconHandler (USER.456)
1657 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
1659 LPBYTE bits = (LPBYTE)LockResource16( hResource );
1661 TRACE_(cursor)("hRes=%04x\n",hResource);
1663 return HICON_16(CreateIconFromResourceEx( bits, 0, TRUE,
1664 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
1667 /***********************************************************************
1668 * LoadCursorW (USER32.@)
1670 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1672 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1674 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1675 LR_SHARED | LR_DEFAULTSIZE );
1678 /***********************************************************************
1679 * LoadCursorA (USER32.@)
1681 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1683 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1685 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1686 LR_SHARED | LR_DEFAULTSIZE );
1689 /***********************************************************************
1690 * LoadCursorFromFileW (USER32.@)
1692 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1694 TRACE("%s\n", debugstr_w(name));
1696 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1697 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1700 /***********************************************************************
1701 * LoadCursorFromFileA (USER32.@)
1703 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1705 TRACE("%s\n", debugstr_a(name));
1707 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1708 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1711 /***********************************************************************
1712 * LoadIconW (USER32.@)
1714 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1716 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1718 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1719 LR_SHARED | LR_DEFAULTSIZE );
1722 /***********************************************************************
1723 * LoadIconA (USER32.@)
1725 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1727 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1729 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1730 LR_SHARED | LR_DEFAULTSIZE );
1733 /**********************************************************************
1734 * GetIconInfo (USER32.@)
1736 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1738 CURSORICONINFO *ciconinfo;
1741 ciconinfo = GlobalLock16(HICON_16(hIcon));
1745 TRACE("%p => %dx%d, %d bpp\n", hIcon,
1746 ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
1748 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1749 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1751 iconinfo->fIcon = TRUE;
1752 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1753 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1757 iconinfo->fIcon = FALSE;
1758 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1759 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1762 height = ciconinfo->nHeight;
1764 if (ciconinfo->bBitsPerPixel > 1)
1766 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
1767 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1768 (char *)(ciconinfo + 1)
1769 + ciconinfo->nHeight *
1770 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1774 iconinfo->hbmColor = 0;
1778 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
1779 1, 1, (char *)(ciconinfo + 1));
1781 GlobalUnlock16(HICON_16(hIcon));
1786 /**********************************************************************
1787 * CreateIconIndirect (USER32.@)
1789 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1791 BITMAP bmpXor,bmpAnd;
1793 int sizeXor,sizeAnd;
1795 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1796 iconinfo->hbmColor, iconinfo->hbmMask,
1797 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1799 if (!iconinfo->hbmMask) return 0;
1801 if (iconinfo->hbmColor)
1803 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1804 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1805 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
1806 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
1808 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1809 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1810 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1811 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1813 sizeXor = iconinfo->hbmColor ? (bmpXor.bmHeight * bmpXor.bmWidthBytes) : 0;
1814 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
1816 hObj = GlobalAlloc16( GMEM_MOVEABLE,
1817 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1820 CURSORICONINFO *info;
1822 info = (CURSORICONINFO *)GlobalLock16( hObj );
1824 /* If we are creating an icon, the hotspot is unused */
1825 if (iconinfo->fIcon)
1827 info->ptHotSpot.x = ICON_HOTSPOT;
1828 info->ptHotSpot.y = ICON_HOTSPOT;
1832 info->ptHotSpot.x = iconinfo->xHotspot;
1833 info->ptHotSpot.y = iconinfo->yHotspot;
1836 if (iconinfo->hbmColor)
1838 info->nWidth = bmpXor.bmWidth;
1839 info->nHeight = bmpXor.bmHeight;
1840 info->nWidthBytes = bmpXor.bmWidthBytes;
1841 info->bPlanes = bmpXor.bmPlanes;
1842 info->bBitsPerPixel = bmpXor.bmBitsPixel;
1846 info->nWidth = bmpAnd.bmWidth;
1847 info->nHeight = bmpAnd.bmHeight / 2;
1848 info->nWidthBytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
1850 info->bBitsPerPixel = 1;
1853 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1855 /* Some apps pass a color bitmap as a mask, convert it to b/w */
1856 if (bmpAnd.bmBitsPixel == 1)
1858 GetBitmapBits( iconinfo->hbmMask, sizeAnd, (char*)(info + 1) );
1863 HBITMAP hbmp_old, hbmp_mem_old, hbmp_mono;
1866 hdc_mem = CreateCompatibleDC( hdc );
1868 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
1870 hbmp_old = SelectObject( hdc, iconinfo->hbmMask );
1871 hbmp_mem_old = SelectObject( hdc_mem, hbmp_mono );
1873 BitBlt( hdc_mem, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc, 0, 0, SRCCOPY );
1875 SelectObject( hdc, hbmp_old );
1876 SelectObject( hdc_mem, hbmp_mem_old );
1878 DeleteDC( hdc_mem );
1879 ReleaseDC( 0, hdc );
1881 GetBitmapBits( hbmp_mono, sizeAnd, (char*)(info + 1) );
1882 DeleteObject( hbmp_mono );
1884 if (iconinfo->hbmColor) GetBitmapBits( iconinfo->hbmColor, sizeXor, (char*)(info + 1) + sizeAnd );
1885 GlobalUnlock16( hObj );
1887 return HICON_32(hObj);
1890 /******************************************************************************
1891 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1894 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1897 * hdc [I] Handle to device context
1898 * x0 [I] X coordinate of upper left corner
1899 * y0 [I] Y coordinate of upper left corner
1900 * hIcon [I] Handle to icon to draw
1901 * cxWidth [I] Width of icon
1902 * cyWidth [I] Height of icon
1903 * istep [I] Index of frame in animated cursor
1904 * hbr [I] Handle to background brush
1905 * flags [I] Icon-drawing flags
1911 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1912 INT cxWidth, INT cyWidth, UINT istep,
1913 HBRUSH hbr, UINT flags )
1915 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
1916 HDC hDC_off = 0, hMemDC;
1917 BOOL result = FALSE, DoOffscreen;
1918 HBITMAP hB_off = 0, hOld = 0;
1920 if (!ptr) return FALSE;
1921 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1922 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1924 hMemDC = CreateCompatibleDC (hdc);
1926 FIXME_(icon)("Ignoring istep=%d\n", istep);
1927 if (flags & DI_COMPAT)
1928 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
1931 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
1935 /* Calculate the size of the destination image. */
1938 if (flags & DI_DEFAULTSIZE)
1939 cxWidth = GetSystemMetrics (SM_CXICON);
1941 cxWidth = ptr->nWidth;
1945 if (flags & DI_DEFAULTSIZE)
1946 cyWidth = GetSystemMetrics (SM_CYICON);
1948 cyWidth = ptr->nHeight;
1951 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1961 hDC_off = CreateCompatibleDC(hdc);
1962 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
1963 if (hDC_off && hB_off) {
1964 hOld = SelectObject(hDC_off, hB_off);
1965 FillRect(hDC_off, &r, hbr);
1969 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
1971 HBITMAP hXorBits, hAndBits;
1972 COLORREF oldFg, oldBg;
1975 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
1977 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1978 ptr->bPlanes, ptr->bBitsPerPixel,
1981 get_bitmap_width_bytes(ptr->nWidth,1) );
1982 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1983 1, 1, (char *)(ptr+1) );
1984 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1985 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1987 if (hXorBits && hAndBits)
1989 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1990 if (flags & DI_MASK)
1993 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1994 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1996 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1997 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1999 SelectObject( hMemDC, hXorBits );
2000 if (flags & DI_IMAGE)
2003 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2004 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2006 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2007 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2009 SelectObject( hMemDC, hBitTemp );
2013 SetTextColor( hdc, oldFg );
2014 SetBkColor( hdc, oldBg );
2015 if (hXorBits) DeleteObject( hXorBits );
2016 if (hAndBits) DeleteObject( hAndBits );
2017 SetStretchBltMode (hdc, nStretchMode);
2019 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2020 SelectObject(hDC_off, hOld);
2023 if (hMemDC) DeleteDC( hMemDC );
2024 if (hDC_off) DeleteDC(hDC_off);
2025 if (hB_off) DeleteObject(hB_off);
2026 GlobalUnlock16(HICON_16(hIcon));
2030 /***********************************************************************
2031 * DIB_FixColorsToLoadflags
2033 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2036 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2039 COLORREF c_W, c_S, c_F, c_L, c_C;
2048 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2050 WARN_(resource)("Invalid bitmap\n");
2054 if (bpp > 8) return;
2056 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2064 colors = bmi->bmiHeader.biClrUsed;
2065 if (colors > 256) colors = 256;
2066 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2069 c_W = GetSysColor(COLOR_WINDOW);
2070 c_S = GetSysColor(COLOR_3DSHADOW);
2071 c_F = GetSysColor(COLOR_3DFACE);
2072 c_L = GetSysColor(COLOR_3DLIGHT);
2074 if (loadflags & LR_LOADTRANSPARENT) {
2076 case 1: pix = pix >> 7; break;
2077 case 4: pix = pix >> 4; break;
2080 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2083 if (pix >= colors) {
2084 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2087 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2088 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2089 ptr->rgbBlue = GetBValue(c_W);
2090 ptr->rgbGreen = GetGValue(c_W);
2091 ptr->rgbRed = GetRValue(c_W);
2093 if (loadflags & LR_LOADMAP3DCOLORS)
2094 for (i=0; i<colors; i++) {
2095 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2096 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2097 if (c_C == RGB(128, 128, 128)) {
2098 ptr->rgbRed = GetRValue(c_S);
2099 ptr->rgbGreen = GetGValue(c_S);
2100 ptr->rgbBlue = GetBValue(c_S);
2101 } else if (c_C == RGB(192, 192, 192)) {
2102 ptr->rgbRed = GetRValue(c_F);
2103 ptr->rgbGreen = GetGValue(c_F);
2104 ptr->rgbBlue = GetBValue(c_F);
2105 } else if (c_C == RGB(223, 223, 223)) {
2106 ptr->rgbRed = GetRValue(c_L);
2107 ptr->rgbGreen = GetGValue(c_L);
2108 ptr->rgbBlue = GetBValue(c_L);
2114 /**********************************************************************
2117 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2118 INT desiredx, INT desiredy, UINT loadflags )
2120 HBITMAP hbitmap = 0, orig_bm;
2124 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2128 LONG width, height, new_width, new_height;
2132 HDC screen_mem_dc = NULL;
2134 if (!(loadflags & LR_LOADFROMFILE))
2138 /* OEM bitmap: try to load the resource from user32.dll */
2139 instance = user32_module;
2142 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2143 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2145 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
2149 if (!(ptr = map_fileW( name, NULL ))) return 0;
2150 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2153 size = bitmap_info_size(info, DIB_RGB_COLORS);
2154 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2155 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2157 if (!fix_info || !scaled_info) goto end;
2158 memcpy(fix_info, info, size);
2160 pix = *((LPBYTE)info + size);
2161 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2163 memcpy(scaled_info, fix_info, size);
2164 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2165 &bpp_dummy, &compr_dummy);
2167 new_width = desiredx;
2172 new_height = height > 0 ? desiredy : -desiredy;
2174 new_height = height;
2178 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2179 core->bcWidth = new_width;
2180 core->bcHeight = new_height;
2184 scaled_info->bmiHeader.biWidth = new_width;
2185 scaled_info->bmiHeader.biHeight = new_height;
2188 if (new_height < 0) new_height = -new_height;
2190 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2191 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2193 bits = (char *)info + size;
2195 if (loadflags & LR_CREATEDIBSECTION)
2197 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2198 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2202 if (is_dib_monochrome(fix_info))
2203 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2205 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2208 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2209 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2210 SelectObject(screen_mem_dc, orig_bm);
2213 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2214 HeapFree(GetProcessHeap(), 0, scaled_info);
2215 HeapFree(GetProcessHeap(), 0, fix_info);
2216 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2221 /**********************************************************************
2222 * LoadImageA (USER32.@)
2226 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2227 INT desiredx, INT desiredy, UINT loadflags)
2233 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2236 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2237 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2238 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2240 __EXCEPT_PAGE_FAULT {
2241 SetLastError( ERROR_INVALID_PARAMETER );
2245 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2246 HeapFree(GetProcessHeap(), 0, u_name);
2251 /******************************************************************************
2252 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2255 * hinst [I] Handle of instance that contains image
2256 * name [I] Name of image
2257 * type [I] Type of image
2258 * desiredx [I] Desired width
2259 * desiredy [I] Desired height
2260 * loadflags [I] Load flags
2263 * Success: Handle to newly loaded image
2266 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2268 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2269 INT desiredx, INT desiredy, UINT loadflags )
2271 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2272 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2274 if (loadflags & LR_DEFAULTSIZE) {
2275 if (type == IMAGE_ICON) {
2276 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2277 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2278 } else if (type == IMAGE_CURSOR) {
2279 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2280 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2283 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2286 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2289 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2292 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2293 if (palEnts == 0) palEnts = 256;
2294 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2295 palEnts, FALSE, loadflags);
2300 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2301 1, TRUE, loadflags);
2306 /******************************************************************************
2307 * CopyImage (USER32.@) Creates new image and copies attributes to it
2310 * hnd [I] Handle to image to copy
2311 * type [I] Type of image to copy
2312 * desiredx [I] Desired width of new image
2313 * desiredy [I] Desired height of new image
2314 * flags [I] Copy flags
2317 * Success: Handle to newly created image
2321 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2322 * all other versions (95/2000/XP have been tested) ignore it.
2325 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2326 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2327 * the copy will have the same depth as the screen.
2328 * The content of the image will only be copied if the bit depth of the
2329 * original image is compatible with the bit depth of the screen, or
2330 * if the source is a DIB section.
2331 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2333 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2334 INT desiredy, UINT flags )
2336 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2337 hnd, type, desiredx, desiredy, flags);
2348 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2349 if (!objSize) return 0;
2350 if ((desiredx < 0) || (desiredy < 0)) return 0;
2352 if (flags & LR_COPYFROMRESOURCE)
2354 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2357 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2358 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2360 /* Allocate memory for a BITMAPINFOHEADER structure and a
2361 color table. The maximum number of colors in a color table
2362 is 256 which corresponds to a bitmap with depth 8.
2363 Bitmaps with higher depths don't have color tables. */
2364 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2367 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2368 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2369 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2370 bi->bmiHeader.biCompression = BI_RGB;
2372 if (flags & LR_CREATEDIBSECTION)
2374 /* Create a DIB section. LR_MONOCHROME is ignored */
2376 HDC dc = CreateCompatibleDC(NULL);
2378 if (objSize == sizeof(DIBSECTION))
2380 /* The source bitmap is a DIB.
2381 Get its attributes to create an exact copy */
2382 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2385 /* Get the color table or the color masks */
2386 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2388 bi->bmiHeader.biWidth = desiredx;
2389 bi->bmiHeader.biHeight = desiredy;
2390 bi->bmiHeader.biSizeImage = 0;
2392 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2397 /* Create a device-dependent bitmap */
2399 BOOL monochrome = (flags & LR_MONOCHROME);
2401 if (objSize == sizeof(DIBSECTION))
2403 /* The source bitmap is a DIB section.
2404 Get its attributes */
2405 HDC dc = CreateCompatibleDC(NULL);
2406 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2407 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2408 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2411 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2413 /* Look if the colors of the DIB are black and white */
2416 (bi->bmiColors[0].rgbRed == 0xff
2417 && bi->bmiColors[0].rgbGreen == 0xff
2418 && bi->bmiColors[0].rgbBlue == 0xff
2419 && bi->bmiColors[0].rgbReserved == 0
2420 && bi->bmiColors[1].rgbRed == 0
2421 && bi->bmiColors[1].rgbGreen == 0
2422 && bi->bmiColors[1].rgbBlue == 0
2423 && bi->bmiColors[1].rgbReserved == 0)
2425 (bi->bmiColors[0].rgbRed == 0
2426 && bi->bmiColors[0].rgbGreen == 0
2427 && bi->bmiColors[0].rgbBlue == 0
2428 && bi->bmiColors[0].rgbReserved == 0
2429 && bi->bmiColors[1].rgbRed == 0xff
2430 && bi->bmiColors[1].rgbGreen == 0xff
2431 && bi->bmiColors[1].rgbBlue == 0xff
2432 && bi->bmiColors[1].rgbReserved == 0);
2435 else if (!monochrome)
2437 monochrome = ds.dsBm.bmBitsPixel == 1;
2442 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2446 HDC screenDC = GetDC(NULL);
2447 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2448 ReleaseDC(NULL, screenDC);
2454 /* Only copy the bitmap if it's a DIB section or if it's
2455 compatible to the screen */
2458 if (objSize == sizeof(DIBSECTION))
2460 copyContents = TRUE;
2464 HDC screenDC = GetDC(NULL);
2465 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2466 ReleaseDC(NULL, screenDC);
2468 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2473 /* The source bitmap may already be selected in a device context,
2474 use GetDIBits/StretchDIBits and not StretchBlt */
2479 dc = CreateCompatibleDC(NULL);
2481 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2482 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2483 bi->bmiHeader.biSizeImage = 0;
2484 bi->bmiHeader.biClrUsed = 0;
2485 bi->bmiHeader.biClrImportant = 0;
2487 /* Fill in biSizeImage */
2488 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2489 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2495 /* Get the image bits of the source bitmap */
2496 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2498 /* Copy it to the destination bitmap */
2499 oldBmp = SelectObject(dc, res);
2500 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2501 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2502 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2503 SelectObject(dc, oldBmp);
2505 HeapFree(GetProcessHeap(), 0, bits);
2511 if (flags & LR_COPYDELETEORG)
2516 HeapFree(GetProcessHeap(), 0, bi);
2520 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2522 /* Should call CURSORICON_ExtCopy but more testing
2523 * needs to be done before we change this
2525 if (flags) FIXME("Flags are ignored\n");
2526 return CopyCursor(hnd);
2532 /******************************************************************************
2533 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2536 * Success: Handle to specified bitmap
2539 HBITMAP WINAPI LoadBitmapW(
2540 HINSTANCE instance, /* [in] Handle to application instance */
2541 LPCWSTR name) /* [in] Address of bitmap resource name */
2543 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2546 /**********************************************************************
2547 * LoadBitmapA (USER32.@)
2551 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2553 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );