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 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
954 return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
956 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
958 /* Normalize hInstance (must be uniquely represented for icon cache) */
960 if (!HIWORD( hInstance ))
961 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
963 /* Get directory resource ID */
965 if (!(hRsrc = FindResourceW( hInstance, name,
966 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
970 /* Find the best entry in the directory */
972 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
973 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
975 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
977 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
978 if (!dirEntry) return 0;
979 wResId = dirEntry->wResId;
980 dwBytesInRes = dirEntry->dwBytesInRes;
981 FreeResource( handle );
983 /* Load the resource */
985 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
986 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
988 /* If shared icon, check whether it was already loaded */
989 if ( (loadflags & LR_SHARED)
990 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
993 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
994 bits = (LPBYTE)LockResource( handle );
995 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
996 !fCursor, 0x00030000, width, height, loadflags);
997 FreeResource( handle );
999 /* If shared icon, add to icon cache */
1001 if ( hIcon && (loadflags & LR_SHARED) )
1002 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1007 /***********************************************************************
1010 * Make a copy of a cursor or icon.
1012 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
1014 char *ptrOld, *ptrNew;
1016 HICON16 hOld = HICON_16(hIcon);
1019 if (!(ptrOld = (char *)GlobalLock16( hOld ))) return 0;
1020 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
1021 size = GlobalSize16( hOld );
1022 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
1023 FarSetOwner16( hNew, hInst16 );
1024 ptrNew = (char *)GlobalLock16( hNew );
1025 memcpy( ptrNew, ptrOld, size );
1026 GlobalUnlock16( hOld );
1027 GlobalUnlock16( hNew );
1028 return HICON_32(hNew);
1031 /*************************************************************************
1032 * CURSORICON_ExtCopy
1034 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1037 * Handle [I] handle to an Image
1038 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1039 * iDesiredCX [I] The Desired width of the Image
1040 * iDesiredCY [I] The desired height of the Image
1041 * nFlags [I] The flags from CopyImage
1044 * Success: The new handle of the Image
1047 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1048 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1049 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1054 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1055 INT iDesiredCX, INT iDesiredCY,
1060 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1061 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1068 /* Best Fit or Monochrome */
1069 if( (nFlags & LR_COPYFROMRESOURCE
1070 && (iDesiredCX > 0 || iDesiredCY > 0))
1071 || nFlags & LR_MONOCHROME)
1073 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1075 /* Not Found in Cache, then do a straight copy
1077 if(pIconCache == NULL)
1079 hNew = CURSORICON_Copy(0, hIcon);
1080 if(nFlags & LR_COPYFROMRESOURCE)
1082 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1087 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1093 CURSORICONDIR *pDir;
1094 CURSORICONDIRENTRY *pDirEntry;
1095 BOOL bIsIcon = (nType == IMAGE_ICON);
1097 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1099 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1100 || (iDesiredCX == 0 && iDesiredCY == 0))
1102 iDesiredCY = GetSystemMetrics(bIsIcon ?
1103 SM_CYICON : SM_CYCURSOR);
1104 iDesiredCX = GetSystemMetrics(bIsIcon ?
1105 SM_CXICON : SM_CXCURSOR);
1108 /* Retrieve the CURSORICONDIRENTRY
1110 if (!(hMem = LoadResource( pIconCache->hModule ,
1111 pIconCache->hGroupRsrc)))
1115 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1124 pDirEntry = CURSORICON_FindBestIconRes(
1125 pDir, iDesiredCX, iDesiredCY, 256 );
1129 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursorRes(
1130 pDir, iDesiredCX, iDesiredCY, 1);
1133 wResId = pDirEntry->wResId;
1134 dwBytesInRes = pDirEntry->dwBytesInRes;
1137 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1138 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1139 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1143 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1144 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1148 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1153 pBits = (LPBYTE)LockResource( hMem );
1155 if(nFlags & LR_DEFAULTSIZE)
1157 iTargetCY = GetSystemMetrics(SM_CYICON);
1158 iTargetCX = GetSystemMetrics(SM_CXICON);
1161 /* Create a New Icon with the proper dimension
1163 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1164 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1168 else hNew = CURSORICON_Copy(0, hIcon);
1173 /***********************************************************************
1174 * CreateCursor (USER32.@)
1176 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1177 INT xHotSpot, INT yHotSpot,
1178 INT nWidth, INT nHeight,
1179 LPCVOID lpANDbits, LPCVOID lpXORbits )
1181 CURSORICONINFO info;
1183 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1184 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1186 info.ptHotSpot.x = xHotSpot;
1187 info.ptHotSpot.y = yHotSpot;
1188 info.nWidth = nWidth;
1189 info.nHeight = nHeight;
1190 info.nWidthBytes = 0;
1192 info.bBitsPerPixel = 1;
1194 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1198 /***********************************************************************
1199 * CreateIcon (USER.407)
1201 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1202 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1203 LPCVOID lpANDbits, LPCVOID lpXORbits )
1205 CURSORICONINFO info;
1207 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1208 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1210 info.ptHotSpot.x = ICON_HOTSPOT;
1211 info.ptHotSpot.y = ICON_HOTSPOT;
1212 info.nWidth = nWidth;
1213 info.nHeight = nHeight;
1214 info.nWidthBytes = 0;
1215 info.bPlanes = bPlanes;
1216 info.bBitsPerPixel = bBitsPixel;
1218 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1222 /***********************************************************************
1223 * CreateIcon (USER32.@)
1225 * Creates an icon based on the specified bitmaps. The bitmaps must be
1226 * provided in a device dependent format and will be resized to
1227 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1228 * depth. The provided bitmaps must be top-down bitmaps.
1229 * Although Windows does not support 15bpp(*) this API must support it
1230 * for Winelib applications.
1232 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1236 * Success: handle to an icon
1241 * - The provided bitmaps are not resized!
1242 * - The documentation says the lpXORbits bitmap must be in a device
1243 * dependent format. But we must still resize it and perform depth
1244 * conversions if necessary.
1245 * - I'm a bit unsure about the how the 'device dependent format' thing works.
1246 * I did some tests on windows and found that if you provide a 16bpp bitmap
1247 * in lpXORbits, then its format but be 565 RGB if the screen's bit depth
1248 * is 16bpp but it must be 555 RGB if the screen's bit depth is anything
1249 * else. I don't know if this is part of the GDI specs or if this is a
1250 * quirk of the graphics card driver.
1251 * - You may think that we check whether the bit depths match or not
1252 * as an optimization. But the truth is that the conversion using
1253 * CreateDIBitmap does not work for some bit depth (e.g. 8bpp) and I have
1255 * - I'm pretty sure that all the things we do in CreateIcon should
1256 * also be done in CreateIconIndirect...
1258 HICON WINAPI CreateIcon(
1259 HINSTANCE hInstance, /* [in] the application's hInstance */
1260 INT nWidth, /* [in] the width of the provided bitmaps */
1261 INT nHeight, /* [in] the height of the provided bitmaps */
1262 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1263 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1264 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1265 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1270 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1271 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1277 if (GetDeviceCaps(hdc,BITSPIXEL)==bBitsPixel) {
1278 CURSORICONINFO info;
1280 info.ptHotSpot.x = ICON_HOTSPOT;
1281 info.ptHotSpot.y = ICON_HOTSPOT;
1282 info.nWidth = nWidth;
1283 info.nHeight = nHeight;
1284 info.nWidthBytes = 0;
1285 info.bPlanes = bPlanes;
1286 info.bBitsPerPixel = bBitsPixel;
1288 hIcon=HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1294 iinfo.xHotspot=ICON_HOTSPOT;
1295 iinfo.yHotspot=ICON_HOTSPOT;
1296 iinfo.hbmMask=CreateBitmap(nWidth,nHeight,1,1,lpANDbits);
1298 bmi.bmiHeader.biSize=sizeof(bmi.bmiHeader);
1299 bmi.bmiHeader.biWidth=nWidth;
1300 bmi.bmiHeader.biHeight=-nHeight;
1301 bmi.bmiHeader.biPlanes=bPlanes;
1302 bmi.bmiHeader.biBitCount=bBitsPixel;
1303 bmi.bmiHeader.biCompression=BI_RGB;
1304 bmi.bmiHeader.biSizeImage=0;
1305 bmi.bmiHeader.biXPelsPerMeter=0;
1306 bmi.bmiHeader.biYPelsPerMeter=0;
1307 bmi.bmiHeader.biClrUsed=0;
1308 bmi.bmiHeader.biClrImportant=0;
1310 iinfo.hbmColor = CreateDIBitmap( hdc, &bmi.bmiHeader,
1311 CBM_INIT, lpXORbits,
1312 &bmi, DIB_RGB_COLORS );
1314 hIcon=CreateIconIndirect(&iinfo);
1315 DeleteObject(iinfo.hbmMask);
1316 DeleteObject(iinfo.hbmColor);
1323 /***********************************************************************
1324 * CreateCursorIconIndirect (USER.408)
1326 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1327 CURSORICONINFO *info,
1333 int sizeAnd, sizeXor;
1335 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1336 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1337 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1338 sizeXor = info->nHeight * info->nWidthBytes;
1339 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1340 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1341 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1343 FarSetOwner16( handle, hInstance );
1344 ptr = (char *)GlobalLock16( handle );
1345 memcpy( ptr, info, sizeof(*info) );
1346 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1347 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1348 GlobalUnlock16( handle );
1353 /***********************************************************************
1354 * CopyIcon (USER.368)
1356 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1358 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1359 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1363 /***********************************************************************
1364 * CopyIcon (USER32.@)
1366 HICON WINAPI CopyIcon( HICON hIcon )
1368 TRACE_(icon)("%p\n", hIcon );
1369 return CURSORICON_Copy( 0, hIcon );
1373 /***********************************************************************
1374 * CopyCursor (USER.369)
1376 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1378 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1379 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1382 /**********************************************************************
1383 * DestroyIcon32 (USER.610)
1385 * This routine is actually exported from Win95 USER under the name
1386 * DestroyIcon32 ... The behaviour implemented here should mimic
1387 * the Win95 one exactly, especially the return values, which
1388 * depend on the setting of various flags.
1390 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1394 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1396 /* Check whether destroying active cursor */
1398 if ( get_user_thread_info()->cursor == HICON_32(handle) )
1400 WARN_(cursor)("Destroying active cursor!\n" );
1404 /* Try shared cursor/icon first */
1406 if ( !(flags & CID_NONSHARED) )
1408 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1411 return (flags & CID_WIN32)? TRUE : (count == 0);
1413 /* FIXME: OEM cursors/icons should be recognized */
1416 /* Now assume non-shared cursor/icon */
1418 retv = GlobalFree16( handle );
1419 return (flags & CID_RESOURCE)? retv : TRUE;
1422 /***********************************************************************
1423 * DestroyIcon (USER32.@)
1425 BOOL WINAPI DestroyIcon( HICON hIcon )
1427 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1431 /***********************************************************************
1432 * DestroyCursor (USER32.@)
1434 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1436 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1440 /***********************************************************************
1441 * DrawIcon (USER32.@)
1443 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1445 CURSORICONINFO *ptr;
1447 HBITMAP hXorBits, hAndBits;
1448 COLORREF oldFg, oldBg;
1450 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
1451 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1452 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
1454 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1455 ptr->bBitsPerPixel, (char *)(ptr + 1)
1456 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
1457 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1458 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1460 if (hXorBits && hAndBits)
1462 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1463 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
1464 SelectObject( hMemDC, hXorBits );
1465 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
1466 SelectObject( hMemDC, hBitTemp );
1469 if (hXorBits) DeleteObject( hXorBits );
1470 if (hAndBits) DeleteObject( hAndBits );
1471 GlobalUnlock16(HICON_16(hIcon));
1472 SetTextColor( hdc, oldFg );
1473 SetBkColor( hdc, oldBg );
1477 /***********************************************************************
1478 * DumpIcon (USER.459)
1480 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1481 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1483 CURSORICONINFO *info = MapSL( pInfo );
1484 int sizeAnd, sizeXor;
1486 if (!info) return 0;
1487 sizeXor = info->nHeight * info->nWidthBytes;
1488 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1489 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1490 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1491 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1492 return MAKELONG( sizeXor, sizeXor );
1496 /***********************************************************************
1497 * SetCursor (USER32.@)
1499 * Set the cursor shape.
1502 * A handle to the previous cursor shape.
1504 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1506 struct user_thread_info *thread_info = get_user_thread_info();
1509 if (hCursor == thread_info->cursor) return hCursor; /* No change */
1510 TRACE_(cursor)("%p\n", hCursor );
1511 hOldCursor = thread_info->cursor;
1512 thread_info->cursor = hCursor;
1513 /* Change the cursor shape only if it is visible */
1514 if (thread_info->cursor_count >= 0)
1516 USER_Driver->pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
1517 GlobalUnlock16(HCURSOR_16(hCursor));
1522 /***********************************************************************
1523 * ShowCursor (USER32.@)
1525 INT WINAPI ShowCursor( BOOL bShow )
1527 struct user_thread_info *thread_info = get_user_thread_info();
1529 TRACE_(cursor)("%d, count=%d\n", bShow, thread_info->cursor_count );
1533 if (++thread_info->cursor_count == 0) /* Show it */
1535 USER_Driver->pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(thread_info->cursor)));
1536 GlobalUnlock16(HCURSOR_16(thread_info->cursor));
1541 if (--thread_info->cursor_count == -1) /* Hide it */
1542 USER_Driver->pSetCursor( NULL );
1544 return thread_info->cursor_count;
1547 /***********************************************************************
1548 * GetCursor (USER32.@)
1550 HCURSOR WINAPI GetCursor(void)
1552 return get_user_thread_info()->cursor;
1556 /***********************************************************************
1557 * ClipCursor (USER32.@)
1559 BOOL WINAPI ClipCursor( const RECT *rect )
1563 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1564 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1565 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1566 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1568 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1569 CURSOR_ClipRect = virt;
1575 /***********************************************************************
1576 * GetClipCursor (USER32.@)
1578 BOOL WINAPI GetClipCursor( RECT *rect )
1580 /* If this is first time - initialize the rect */
1581 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1583 return CopyRect( rect, &CURSOR_ClipRect );
1587 /***********************************************************************
1588 * SetSystemCursor (USER32.@)
1590 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1592 FIXME("(%p,%08x),stub!\n", hcur, id);
1597 /**********************************************************************
1598 * LookupIconIdFromDirectoryEx (USER.364)
1600 * FIXME: exact parameter sizes
1602 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1603 INT16 width, INT16 height, UINT16 cFlag )
1605 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1608 /**********************************************************************
1609 * LookupIconIdFromDirectoryEx (USER32.@)
1611 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1612 INT width, INT height, UINT cFlag )
1614 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1616 if( dir && !dir->idReserved && (dir->idType & 3) )
1618 CURSORICONDIRENTRY* entry;
1623 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1626 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1631 entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1633 entry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
1635 if( entry ) retVal = entry->wResId;
1637 else WARN_(cursor)("invalid resource directory\n");
1641 /**********************************************************************
1642 * LookupIconIdFromDirectory (USER.?)
1644 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
1646 return LookupIconIdFromDirectoryEx16( dir, bIcon,
1647 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1648 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1651 /**********************************************************************
1652 * LookupIconIdFromDirectory (USER32.@)
1654 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1656 return LookupIconIdFromDirectoryEx( dir, bIcon,
1657 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1658 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1661 /**********************************************************************
1662 * GetIconID (USER.455)
1664 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1666 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
1668 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1669 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1674 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1675 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1677 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1678 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1680 WARN_(cursor)("invalid res type %d\n", resType );
1685 /**********************************************************************
1686 * LoadCursorIconHandler (USER.336)
1688 * Supposed to load resources of Windows 2.x applications.
1690 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1692 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1693 hResource, hModule, hRsrc);
1694 return (HGLOBAL16)0;
1697 /**********************************************************************
1698 * LoadIconHandler (USER.456)
1700 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
1702 LPBYTE bits = (LPBYTE)LockResource16( hResource );
1704 TRACE_(cursor)("hRes=%04x\n",hResource);
1706 return HICON_16(CreateIconFromResourceEx( bits, 0, TRUE,
1707 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
1710 /***********************************************************************
1711 * LoadCursorW (USER32.@)
1713 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1715 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1716 LR_SHARED | LR_DEFAULTSIZE );
1719 /***********************************************************************
1720 * LoadCursorA (USER32.@)
1722 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1724 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1725 LR_SHARED | LR_DEFAULTSIZE );
1728 /***********************************************************************
1729 * LoadCursorFromFileW (USER32.@)
1731 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1733 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1734 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1737 /***********************************************************************
1738 * LoadCursorFromFileA (USER32.@)
1740 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1742 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1743 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1746 /***********************************************************************
1747 * LoadIconW (USER32.@)
1749 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1751 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1752 LR_SHARED | LR_DEFAULTSIZE );
1755 /***********************************************************************
1756 * LoadIconA (USER32.@)
1758 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1760 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1761 LR_SHARED | LR_DEFAULTSIZE );
1764 /**********************************************************************
1765 * GetIconInfo (USER32.@)
1767 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1769 CURSORICONINFO *ciconinfo;
1772 ciconinfo = GlobalLock16(HICON_16(hIcon));
1776 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1777 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1779 iconinfo->fIcon = TRUE;
1780 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1781 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1785 iconinfo->fIcon = FALSE;
1786 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1787 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1790 if (ciconinfo->bBitsPerPixel > 1)
1792 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
1793 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1794 (char *)(ciconinfo + 1)
1795 + ciconinfo->nHeight *
1796 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1797 height = ciconinfo->nHeight;
1801 iconinfo->hbmColor = 0;
1802 height = ciconinfo->nHeight * 2;
1805 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
1806 1, 1, (char *)(ciconinfo + 1));
1808 GlobalUnlock16(HICON_16(hIcon));
1813 /**********************************************************************
1814 * CreateIconIndirect (USER32.@)
1816 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1818 BITMAP bmpXor,bmpAnd;
1820 int sizeXor,sizeAnd;
1822 if (iconinfo->hbmColor) GetObjectA( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1823 GetObjectA( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1825 sizeXor = iconinfo->hbmColor ? (bmpXor.bmHeight * bmpXor.bmWidthBytes) : 0;
1826 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1828 hObj = GlobalAlloc16( GMEM_MOVEABLE,
1829 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1832 CURSORICONINFO *info;
1834 info = (CURSORICONINFO *)GlobalLock16( hObj );
1836 /* If we are creating an icon, the hotspot is unused */
1837 if (iconinfo->fIcon)
1839 info->ptHotSpot.x = ICON_HOTSPOT;
1840 info->ptHotSpot.y = ICON_HOTSPOT;
1844 info->ptHotSpot.x = iconinfo->xHotspot;
1845 info->ptHotSpot.y = iconinfo->yHotspot;
1848 if (iconinfo->hbmColor)
1850 info->nWidth = bmpXor.bmWidth;
1851 info->nHeight = bmpXor.bmHeight;
1852 info->nWidthBytes = bmpXor.bmWidthBytes;
1853 info->bPlanes = bmpXor.bmPlanes;
1854 info->bBitsPerPixel = bmpXor.bmBitsPixel;
1858 info->nWidth = bmpAnd.bmWidth;
1859 info->nHeight = bmpAnd.bmHeight / 2;
1860 info->nWidthBytes = bmpAnd.bmWidthBytes;
1861 info->bPlanes = bmpAnd.bmPlanes;
1862 info->bBitsPerPixel = bmpAnd.bmBitsPixel;
1865 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1867 GetBitmapBits( iconinfo->hbmMask, sizeAnd, (char*)(info + 1) );
1868 if (iconinfo->hbmColor) GetBitmapBits( iconinfo->hbmColor, sizeXor, (char*)(info + 1) + sizeAnd );
1869 GlobalUnlock16( hObj );
1871 return HICON_32(hObj);
1874 /******************************************************************************
1875 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1878 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1881 * hdc [I] Handle to device context
1882 * x0 [I] X coordinate of upper left corner
1883 * y0 [I] Y coordinate of upper left corner
1884 * hIcon [I] Handle to icon to draw
1885 * cxWidth [I] Width of icon
1886 * cyWidth [I] Height of icon
1887 * istep [I] Index of frame in animated cursor
1888 * hbr [I] Handle to background brush
1889 * flags [I] Icon-drawing flags
1895 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1896 INT cxWidth, INT cyWidth, UINT istep,
1897 HBRUSH hbr, UINT flags )
1899 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
1900 HDC hDC_off = 0, hMemDC;
1901 BOOL result = FALSE, DoOffscreen;
1902 HBITMAP hB_off = 0, hOld = 0;
1904 if (!ptr) return FALSE;
1905 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1906 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1908 hMemDC = CreateCompatibleDC (hdc);
1910 FIXME_(icon)("Ignoring istep=%d\n", istep);
1911 if (flags & DI_COMPAT)
1912 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
1915 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
1919 /* Calculate the size of the destination image. */
1922 if (flags & DI_DEFAULTSIZE)
1923 cxWidth = GetSystemMetrics (SM_CXICON);
1925 cxWidth = ptr->nWidth;
1929 if (flags & DI_DEFAULTSIZE)
1930 cyWidth = GetSystemMetrics (SM_CYICON);
1932 cyWidth = ptr->nHeight;
1935 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1945 hDC_off = CreateCompatibleDC(hdc);
1946 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
1947 if (hDC_off && hB_off) {
1948 hOld = SelectObject(hDC_off, hB_off);
1949 FillRect(hDC_off, &r, hbr);
1953 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
1955 HBITMAP hXorBits, hAndBits;
1956 COLORREF oldFg, oldBg;
1959 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
1961 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1962 ptr->bPlanes, ptr->bBitsPerPixel,
1965 get_bitmap_width_bytes(ptr->nWidth,1) );
1966 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1967 1, 1, (char *)(ptr+1) );
1968 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1969 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1971 if (hXorBits && hAndBits)
1973 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1974 if (flags & DI_MASK)
1977 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1978 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1980 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1981 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1983 SelectObject( hMemDC, hXorBits );
1984 if (flags & DI_IMAGE)
1987 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1988 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1990 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1991 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1993 SelectObject( hMemDC, hBitTemp );
1997 SetTextColor( hdc, oldFg );
1998 SetBkColor( hdc, oldBg );
1999 if (hXorBits) DeleteObject( hXorBits );
2000 if (hAndBits) DeleteObject( hAndBits );
2001 SetStretchBltMode (hdc, nStretchMode);
2003 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2004 SelectObject(hDC_off, hOld);
2007 if (hMemDC) DeleteDC( hMemDC );
2008 if (hDC_off) DeleteDC(hDC_off);
2009 if (hB_off) DeleteObject(hB_off);
2010 GlobalUnlock16(HICON_16(hIcon));
2014 /***********************************************************************
2015 * DIB_FixColorsToLoadflags
2017 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2020 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2023 COLORREF c_W, c_S, c_F, c_L, c_C;
2032 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2034 WARN_(resource)("Invalid bitmap\n");
2038 if (bpp > 8) return;
2040 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2048 colors = bmi->bmiHeader.biClrUsed;
2049 if (colors > 256) colors = 256;
2050 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2053 c_W = GetSysColor(COLOR_WINDOW);
2054 c_S = GetSysColor(COLOR_3DSHADOW);
2055 c_F = GetSysColor(COLOR_3DFACE);
2056 c_L = GetSysColor(COLOR_3DLIGHT);
2058 if (loadflags & LR_LOADTRANSPARENT) {
2060 case 1: pix = pix >> 7; break;
2061 case 4: pix = pix >> 4; break;
2064 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2067 if (pix >= colors) {
2068 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2071 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2072 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2073 ptr->rgbBlue = GetBValue(c_W);
2074 ptr->rgbGreen = GetGValue(c_W);
2075 ptr->rgbRed = GetRValue(c_W);
2077 if (loadflags & LR_LOADMAP3DCOLORS)
2078 for (i=0; i<colors; i++) {
2079 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2080 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2081 if (c_C == RGB(128, 128, 128)) {
2082 ptr->rgbRed = GetRValue(c_S);
2083 ptr->rgbGreen = GetGValue(c_S);
2084 ptr->rgbBlue = GetBValue(c_S);
2085 } else if (c_C == RGB(192, 192, 192)) {
2086 ptr->rgbRed = GetRValue(c_F);
2087 ptr->rgbGreen = GetGValue(c_F);
2088 ptr->rgbBlue = GetBValue(c_F);
2089 } else if (c_C == RGB(223, 223, 223)) {
2090 ptr->rgbRed = GetRValue(c_L);
2091 ptr->rgbGreen = GetGValue(c_L);
2092 ptr->rgbBlue = GetBValue(c_L);
2098 /**********************************************************************
2101 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2102 INT desiredx, INT desiredy, UINT loadflags )
2104 HBITMAP hbitmap = 0, orig_bm;
2108 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2112 LONG width, height, new_width, new_height;
2116 HDC screen_mem_dc = NULL;
2118 if (!(loadflags & LR_LOADFROMFILE))
2122 /* OEM bitmap: try to load the resource from user32.dll */
2123 instance = user32_module;
2126 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2127 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2129 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
2133 if (!(ptr = map_fileW( name, NULL ))) return 0;
2134 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2137 size = bitmap_info_size(info, DIB_RGB_COLORS);
2138 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2139 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2141 if (!fix_info || !scaled_info) goto end;
2142 memcpy(fix_info, info, size);
2144 pix = *((LPBYTE)info + size);
2145 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2147 memcpy(scaled_info, fix_info, size);
2148 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2149 &bpp_dummy, &compr_dummy);
2151 new_width = desiredx;
2156 new_height = height > 0 ? desiredy : -desiredy;
2158 new_height = height;
2162 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2163 core->bcWidth = new_width;
2164 core->bcHeight = new_height;
2168 scaled_info->bmiHeader.biWidth = new_width;
2169 scaled_info->bmiHeader.biHeight = new_height;
2172 if (new_height < 0) new_height = -new_height;
2174 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2175 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2177 bits = (char *)info + size;
2179 if (loadflags & LR_CREATEDIBSECTION)
2181 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2182 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2186 if (is_dib_monochrome(fix_info))
2187 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2189 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2192 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2193 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2194 SelectObject(screen_mem_dc, orig_bm);
2197 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2198 HeapFree(GetProcessHeap(), 0, scaled_info);
2199 HeapFree(GetProcessHeap(), 0, fix_info);
2200 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2205 /**********************************************************************
2206 * LoadImageA (USER32.@)
2210 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2211 INT desiredx, INT desiredy, UINT loadflags)
2217 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2220 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2221 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2222 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2224 __EXCEPT_PAGE_FAULT {
2225 SetLastError( ERROR_INVALID_PARAMETER );
2229 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2230 HeapFree(GetProcessHeap(), 0, u_name);
2235 /******************************************************************************
2236 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2239 * hinst [I] Handle of instance that contains image
2240 * name [I] Name of image
2241 * type [I] Type of image
2242 * desiredx [I] Desired width
2243 * desiredy [I] Desired height
2244 * loadflags [I] Load flags
2247 * Success: Handle to newly loaded image
2250 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2252 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2253 INT desiredx, INT desiredy, UINT loadflags )
2255 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2256 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2258 if (loadflags & LR_DEFAULTSIZE) {
2259 if (type == IMAGE_ICON) {
2260 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2261 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2262 } else if (type == IMAGE_CURSOR) {
2263 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2264 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2267 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2270 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2273 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2276 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2277 if (palEnts == 0) palEnts = 256;
2278 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2279 palEnts, FALSE, loadflags);
2284 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2285 1, TRUE, loadflags);
2290 /******************************************************************************
2291 * CopyImage (USER32.@) Creates new image and copies attributes to it
2294 * hnd [I] Handle to image to copy
2295 * type [I] Type of image to copy
2296 * desiredx [I] Desired width of new image
2297 * desiredy [I] Desired height of new image
2298 * flags [I] Copy flags
2301 * Success: Handle to newly created image
2305 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2306 * all other versions (95/2000/XP have been tested) ignore it.
2309 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2310 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2311 * the copy will have the same depth as the screen.
2312 * The content of the image will only be copied if the bit depth of the
2313 * original image is compatible with the bit depth of the screen, or
2314 * if the source is a DIB section.
2315 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2317 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2318 INT desiredy, UINT flags )
2320 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2321 hnd, type, desiredx, desiredy, flags);
2332 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2333 if (!objSize) return 0;
2334 if ((desiredx < 0) || (desiredy < 0)) return 0;
2336 if (flags & LR_COPYFROMRESOURCE)
2338 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2341 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2342 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2344 /* Allocate memory for a BITMAPINFOHEADER structure and a
2345 color table. The maximum number of colors in a color table
2346 is 256 which corresponds to a bitmap with depth 8.
2347 Bitmaps with higher depths don't have color tables. */
2348 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2351 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2352 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2353 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2354 bi->bmiHeader.biCompression = BI_RGB;
2356 if (flags & LR_CREATEDIBSECTION)
2358 /* Create a DIB section. LR_MONOCHROME is ignored */
2360 HDC dc = CreateCompatibleDC(NULL);
2362 if (objSize == sizeof(DIBSECTION))
2364 /* The source bitmap is a DIB.
2365 Get its attributes to create an exact copy */
2366 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2369 /* Get the color table or the color masks */
2370 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2372 bi->bmiHeader.biWidth = desiredx;
2373 bi->bmiHeader.biHeight = desiredy;
2374 bi->bmiHeader.biSizeImage = 0;
2376 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2381 /* Create a device-dependent bitmap */
2383 BOOL monochrome = (flags & LR_MONOCHROME);
2385 if (objSize == sizeof(DIBSECTION))
2387 /* The source bitmap is a DIB section.
2388 Get its attributes */
2389 HDC dc = CreateCompatibleDC(NULL);
2390 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2391 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2392 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2395 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2397 /* Look if the colors of the DIB are black and white */
2400 (bi->bmiColors[0].rgbRed == 0xff
2401 && bi->bmiColors[0].rgbGreen == 0xff
2402 && bi->bmiColors[0].rgbBlue == 0xff
2403 && bi->bmiColors[0].rgbReserved == 0
2404 && bi->bmiColors[1].rgbRed == 0
2405 && bi->bmiColors[1].rgbGreen == 0
2406 && bi->bmiColors[1].rgbBlue == 0
2407 && bi->bmiColors[1].rgbReserved == 0)
2409 (bi->bmiColors[0].rgbRed == 0
2410 && bi->bmiColors[0].rgbGreen == 0
2411 && bi->bmiColors[0].rgbBlue == 0
2412 && bi->bmiColors[0].rgbReserved == 0
2413 && bi->bmiColors[1].rgbRed == 0xff
2414 && bi->bmiColors[1].rgbGreen == 0xff
2415 && bi->bmiColors[1].rgbBlue == 0xff
2416 && bi->bmiColors[1].rgbReserved == 0);
2419 else if (!monochrome)
2421 monochrome = ds.dsBm.bmBitsPixel == 1;
2426 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2430 HDC screenDC = GetDC(NULL);
2431 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2432 ReleaseDC(NULL, screenDC);
2438 /* Only copy the bitmap if it's a DIB section or if it's
2439 compatible to the screen */
2442 if (objSize == sizeof(DIBSECTION))
2444 copyContents = TRUE;
2448 HDC screenDC = GetDC(NULL);
2449 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2450 ReleaseDC(NULL, screenDC);
2452 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2457 /* The source bitmap may already be selected in a device context,
2458 use GetDIBits/StretchDIBits and not StretchBlt */
2463 dc = CreateCompatibleDC(NULL);
2465 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2466 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2467 bi->bmiHeader.biSizeImage = 0;
2468 bi->bmiHeader.biClrUsed = 0;
2469 bi->bmiHeader.biClrImportant = 0;
2471 /* Fill in biSizeImage */
2472 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2473 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2479 /* Get the image bits of the source bitmap */
2480 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2482 /* Copy it to the destination bitmap */
2483 oldBmp = SelectObject(dc, res);
2484 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2485 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2486 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2487 SelectObject(dc, oldBmp);
2489 HeapFree(GetProcessHeap(), 0, bits);
2495 if (flags & LR_COPYDELETEORG)
2500 HeapFree(GetProcessHeap(), 0, bi);
2504 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2506 /* Should call CURSORICON_ExtCopy but more testing
2507 * needs to be done before we change this
2509 if (flags) FIXME("Flags are ignored\n");
2510 return CopyCursor(hnd);
2516 /******************************************************************************
2517 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2520 * Success: Handle to specified bitmap
2523 HBITMAP WINAPI LoadBitmapW(
2524 HINSTANCE instance, /* [in] Handle to application instance */
2525 LPCWSTR name) /* [in] Address of bitmap resource name */
2527 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2530 /**********************************************************************
2531 * LoadBitmapA (USER32.@)
2535 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2537 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );