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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * http://www.microsoft.com/win32dev/ui/icons.htm
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 ?!
52 #include "wine/winbase16.h"
53 #include "wine/winuser16.h"
54 #include "wine/exception.h"
56 #include "cursoricon.h"
58 #include "wine/debug.h"
64 #include "msvcrt/excpt.h"
66 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
67 WINE_DECLARE_DEBUG_CHANNEL(icon);
68 WINE_DECLARE_DEBUG_CHANNEL(resource);
71 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
75 /**********************************************************************
76 * ICONCACHE for cursors/icons loaded with LR_SHARED.
78 * FIXME: This should not be allocated on the system heap, but on a
79 * subsystem-global heap (i.e. one for all Win16 processes,
80 * and one for each Win32 process).
82 typedef struct tagICONCACHE
84 struct tagICONCACHE *next;
95 static ICONCACHE *IconAnchor = NULL;
96 static CRITICAL_SECTION IconCrst = CRITICAL_SECTION_INIT("IconCrst");
97 static WORD ICON_HOTSPOT = 0x4242;
100 /***********************************************************************
103 * Helper function to map a file to memory:
105 * [RETURN] ptr - pointer to mapped file
107 static void *map_fileW( LPCWSTR name )
109 HANDLE hFile, hMapping;
112 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
113 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
114 if (hFile != INVALID_HANDLE_VALUE)
116 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
117 CloseHandle( hFile );
120 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
121 CloseHandle( hMapping );
128 /***********************************************************************
129 * get_bitmap_width_bytes
131 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
134 static int get_bitmap_width_bytes( int width, int bpp )
139 return 2 * ((width+15) / 16);
141 return 2 * ((width+3) / 4);
146 return width + (width & 1);
153 WARN("Unknown depth %d, please report.\n", bpp );
159 /**********************************************************************
160 * CURSORICON_FindSharedIcon
162 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
167 EnterCriticalSection( &IconCrst );
169 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
170 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
177 LeaveCriticalSection( &IconCrst );
182 /*************************************************************************
183 * CURSORICON_FindCache
185 * Given a handle, find the corresponding cache element
188 * Handle [I] handle to an Image
191 * Success: The cache entry
195 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
198 ICONCACHE *pRet=NULL;
199 BOOL IsFound = FALSE;
202 EnterCriticalSection( &IconCrst );
204 for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
206 if ( hIcon == ptr->hIcon )
213 LeaveCriticalSection( &IconCrst );
218 /**********************************************************************
219 * CURSORICON_AddSharedIcon
221 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
223 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
226 ptr->hModule = hModule;
229 ptr->hGroupRsrc = hGroupRsrc;
232 EnterCriticalSection( &IconCrst );
233 ptr->next = IconAnchor;
235 LeaveCriticalSection( &IconCrst );
238 /**********************************************************************
239 * CURSORICON_DelSharedIcon
241 static INT CURSORICON_DelSharedIcon( HICON hIcon )
246 EnterCriticalSection( &IconCrst );
248 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
249 if ( ptr->hIcon == hIcon )
251 if ( ptr->count > 0 ) ptr->count--;
256 LeaveCriticalSection( &IconCrst );
261 /**********************************************************************
262 * CURSORICON_FreeModuleIcons
264 void CURSORICON_FreeModuleIcons( HMODULE hModule )
266 ICONCACHE **ptr = &IconAnchor;
268 if ( HIWORD( hModule ) )
269 hModule = MapHModuleLS( hModule );
271 hModule = GetExePtr( hModule );
273 EnterCriticalSection( &IconCrst );
277 if ( (*ptr)->hModule == hModule )
279 ICONCACHE *freePtr = *ptr;
280 *ptr = freePtr->next;
282 GlobalFree16(HICON_16(freePtr->hIcon));
283 HeapFree( GetProcessHeap(), 0, freePtr );
289 LeaveCriticalSection( &IconCrst );
292 /**********************************************************************
293 * CURSORICON_FindBestIcon
295 * Find the icon closest to the requested size and number of colors.
297 static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
298 int height, int colors )
301 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
302 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
303 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
305 if (dir->idCount < 1)
307 WARN_(icon)("Empty directory!\n" );
310 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
313 iTotalDiff = 0xFFFFFFFF;
314 iColorDiff = 0xFFFFFFFF;
315 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
317 iTempXDiff = abs(width - entry->ResInfo.icon.bWidth);
318 iTempYDiff = abs(height - entry->ResInfo.icon.bHeight);
320 if(iTotalDiff > (iTempXDiff + iTempYDiff))
324 iTotalDiff = iXDiff + iYDiff;
328 /* Find Best Colors for Best Fit */
329 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
331 if(abs(width - entry->ResInfo.icon.bWidth) == iXDiff &&
332 abs(height - entry->ResInfo.icon.bHeight) == iYDiff)
334 iTempColorDiff = abs(colors - entry->ResInfo.icon.bColorCount);
335 if(iColorDiff > iTempColorDiff)
338 iColorDiff = iTempColorDiff;
347 /**********************************************************************
348 * CURSORICON_FindBestCursor
350 * Find the cursor closest to the requested size.
351 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
354 static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
355 int width, int height, int color)
357 int i, maxwidth, maxheight;
358 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
360 if (dir->idCount < 1)
362 WARN_(cursor)("Empty directory!\n" );
365 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
367 /* Double height to account for AND and XOR masks */
371 /* First find the largest one smaller than or equal to the requested size*/
373 maxwidth = maxheight = 0;
374 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
375 if ((entry->ResInfo.cursor.wWidth <= width) && (entry->ResInfo.cursor.wHeight <= height) &&
376 (entry->ResInfo.cursor.wWidth > maxwidth) && (entry->ResInfo.cursor.wHeight > maxheight) &&
377 (entry->wBitCount == 1))
380 maxwidth = entry->ResInfo.cursor.wWidth;
381 maxheight = entry->ResInfo.cursor.wHeight;
383 if (bestEntry) return bestEntry;
385 /* Now find the smallest one larger than the requested size */
387 maxwidth = maxheight = 255;
388 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
389 if ((entry->ResInfo.cursor.wWidth < maxwidth) && (entry->ResInfo.cursor.wHeight < maxheight) &&
390 (entry->wBitCount == 1))
393 maxwidth = entry->ResInfo.cursor.wWidth;
394 maxheight = entry->ResInfo.cursor.wHeight;
400 /*********************************************************************
401 * The main purpose of this function is to create fake resource directory
402 * and fake resource entries. There are several reasons for this:
403 * - CURSORICONDIR and CURSORICONFILEDIR differ in sizes and their
405 * There are some "bad" cursor files which do not have
406 * bColorCount initialized but instead one must read this info
407 * directly from corresponding DIB sections
408 * Note: wResId is index to array of pointer returned in ptrs (origin is 1)
410 static BOOL CURSORICON_SimulateLoadingFromResourceW( LPWSTR filename, BOOL fCursor,
411 CURSORICONDIR **res, LPBYTE **ptr)
414 CURSORICONFILEDIR *bits;
415 int entries, size, i;
419 if (!(bits = map_fileW( filename ))) return FALSE;
421 /* FIXME: test for inimated icons
422 * hack to load the first icon from the *.ani file
424 if ( *(LPDWORD)bits==0x46464952 ) /* "RIFF" */
425 { LPBYTE pos = (LPBYTE) bits;
426 FIXME_(cursor)("Animated icons not correctly implemented! %p \n", bits);
429 { if (*(LPDWORD)pos==0x6e6f6369) /* "icon" */
430 { FIXME_(cursor)("icon entry found! %p\n", bits);
432 if ( !*(LPWORD) pos==0x2fe) /* iconsize */
435 bits=(CURSORICONFILEDIR*)(pos+4);
436 FIXME_(cursor)("icon size ok. offset=%p \n", bits);
440 if (pos>=(LPBYTE)bits+766) goto fail;
443 if (!(entries = bits->idCount)) goto fail;
444 size = sizeof(CURSORICONDIR) + sizeof(CURSORICONDIRENTRY) * (entries - 1);
445 _free = (LPBYTE) size;
447 for (i=0; i < entries; i++)
448 size += bits->idEntries[i].dwDIBSize + (fCursor ? sizeof(POINT16): 0);
450 if (!(*ptr = HeapAlloc( GetProcessHeap(), 0,
451 entries * sizeof (CURSORICONDIRENTRY*)))) goto fail;
452 if (!(*res = HeapAlloc( GetProcessHeap(), 0, size))) goto fail;
454 _free = (LPBYTE)(*res) + (int)_free;
455 memcpy((*res), bits, 6);
456 for (i=0; i<entries; i++)
458 ((LPBYTE*)(*ptr))[i] = _free;
460 (*res)->idEntries[i].ResInfo.cursor.wWidth=bits->idEntries[i].bWidth;
461 (*res)->idEntries[i].ResInfo.cursor.wHeight=bits->idEntries[i].bHeight;
462 ((LPPOINT16)_free)->x=bits->idEntries[i].xHotspot;
463 ((LPPOINT16)_free)->y=bits->idEntries[i].yHotspot;
464 _free+=sizeof(POINT16);
466 (*res)->idEntries[i].ResInfo.icon.bWidth=bits->idEntries[i].bWidth;
467 (*res)->idEntries[i].ResInfo.icon.bHeight=bits->idEntries[i].bHeight;
468 (*res)->idEntries[i].ResInfo.icon.bColorCount = bits->idEntries[i].bColorCount;
470 (*res)->idEntries[i].wPlanes=1;
471 (*res)->idEntries[i].wBitCount = ((LPBITMAPINFOHEADER)((LPBYTE)bits +
472 bits->idEntries[i].dwDIBOffset))->biBitCount;
473 (*res)->idEntries[i].dwBytesInRes = bits->idEntries[i].dwDIBSize;
474 (*res)->idEntries[i].wResId=i+1;
476 memcpy(_free,(LPBYTE)bits +bits->idEntries[i].dwDIBOffset,
477 (*res)->idEntries[i].dwBytesInRes);
478 _free += (*res)->idEntries[i].dwBytesInRes;
480 UnmapViewOfFile( bits );
483 if (*res) HeapFree( GetProcessHeap(), 0, *res );
484 if (*ptr) HeapFree( GetProcessHeap(), 0, *ptr );
485 UnmapViewOfFile( bits );
490 /**********************************************************************
491 * CURSORICON_CreateFromResource
493 * Create a cursor or icon from in-memory resource template.
495 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
496 * with cbSize parameter as well.
498 static HICON CURSORICON_CreateFromResource( HMODULE16 hModule, HGLOBAL16 hObj, LPBYTE bits,
499 UINT cbSize, BOOL bIcon, DWORD dwVersion,
500 INT width, INT height, UINT loadflags )
503 int sizeAnd, sizeXor;
504 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
505 BITMAP bmpXor, bmpAnd;
511 hotspot.x = ICON_HOTSPOT;
512 hotspot.y = ICON_HOTSPOT;
514 TRACE_(cursor)("%08x (%u bytes), ver %08x, %ix%i %s %s\n",
515 (unsigned)bits, cbSize, (unsigned)dwVersion, width, height,
516 bIcon ? "icon" : "cursor", (loadflags & LR_MONOCHROME) ? "mono" : "" );
517 if (dwVersion == 0x00020000)
519 FIXME_(cursor)("\t2.xx resources are not supported\n");
524 bmi = (BITMAPINFO *)bits;
525 else /* get the hotspot */
527 POINT16 *pt = (POINT16 *)bits;
529 bmi = (BITMAPINFO *)(pt + 1);
531 size = DIB_BitmapInfoSize( bmi, DIB_RGB_COLORS );
533 if (!width) width = bmi->bmiHeader.biWidth;
534 if (!height) height = bmi->bmiHeader.biHeight/2;
535 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
536 (bmi->bmiHeader.biWidth != width);
538 /* Check bitmap header */
540 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
541 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
542 bmi->bmiHeader.biCompression != BI_RGB) )
544 WARN_(cursor)("\tinvalid resource bitmap header.\n");
548 if (!screen_dc) screen_dc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
553 /* Make sure we have room for the monochrome bitmap later on.
554 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
555 * up to and including the biBitCount. In-memory icon resource
556 * format is as follows:
558 * BITMAPINFOHEADER icHeader // DIB header
559 * RGBQUAD icColors[] // Color table
560 * BYTE icXOR[] // DIB bits for XOR mask
561 * BYTE icAND[] // DIB bits for AND mask
564 if ((pInfo = (BITMAPINFO *)HeapAlloc( GetProcessHeap(), 0,
565 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
567 memcpy( pInfo, bmi, size );
568 pInfo->bmiHeader.biHeight /= 2;
570 /* Create the XOR bitmap */
575 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
579 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
586 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
588 hOld = SelectObject(hdcMem, hXorBits);
589 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
590 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
591 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
592 SelectObject(hdcMem, hOld);
594 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
596 } else hXorBits = CreateDIBitmap( screen_dc, &pInfo->bmiHeader,
597 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS );
600 char* xbits = (char *)bmi + size +
601 DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth,
602 bmi->bmiHeader.biHeight,
603 bmi->bmiHeader.biBitCount) / 2;
605 pInfo->bmiHeader.biBitCount = 1;
606 if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
608 RGBQUAD *rgb = pInfo->bmiColors;
610 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
611 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
612 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
613 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
617 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
619 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
620 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
623 /* Create the AND bitmap */
626 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
630 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
632 hOld = SelectObject(hdcMem, hAndBits);
633 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
634 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
635 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
636 SelectObject(hdcMem, hOld);
638 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
640 } else hAndBits = CreateDIBitmap( screen_dc, &pInfo->bmiHeader,
641 CBM_INIT, xbits, pInfo, DIB_RGB_COLORS );
643 if( !hAndBits ) DeleteObject( hXorBits );
645 HeapFree( GetProcessHeap(), 0, pInfo );
649 if( !hXorBits || !hAndBits )
651 WARN_(cursor)("\tunable to create an icon bitmap.\n");
655 /* Now create the CURSORICONINFO structure */
656 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
657 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
658 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
659 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
661 if (hObj) hObj = GlobalReAlloc16( hObj,
662 sizeof(CURSORICONINFO) + sizeXor + sizeAnd, GMEM_MOVEABLE );
663 if (!hObj) hObj = GlobalAlloc16( GMEM_MOVEABLE,
664 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
667 CURSORICONINFO *info;
669 /* Make it owned by the module */
670 if (hModule) hModule = GetExePtr(hModule);
671 FarSetOwner16( hObj, hModule );
673 info = (CURSORICONINFO *)GlobalLock16( hObj );
674 info->ptHotSpot.x = hotspot.x;
675 info->ptHotSpot.y = hotspot.y;
676 info->nWidth = bmpXor.bmWidth;
677 info->nHeight = bmpXor.bmHeight;
678 info->nWidthBytes = bmpXor.bmWidthBytes;
679 info->bPlanes = bmpXor.bmPlanes;
680 info->bBitsPerPixel = bmpXor.bmBitsPixel;
682 /* Transfer the bitmap bits to the CURSORICONINFO structure */
684 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
685 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
686 GlobalUnlock16( hObj );
689 DeleteObject( hAndBits );
690 DeleteObject( hXorBits );
691 return HICON_32((HICON16)hObj);
695 /**********************************************************************
696 * CreateIconFromResource (USER32.@)
698 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
699 BOOL bIcon, DWORD dwVersion)
701 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
705 /**********************************************************************
706 * CreateIconFromResourceEx (USER32.@)
708 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
709 BOOL bIcon, DWORD dwVersion,
710 INT width, INT height,
713 return CURSORICON_CreateFromResource( 0, 0, bits, cbSize, bIcon, dwVersion,
714 width, height, cFlag );
717 /**********************************************************************
720 * Load a cursor or icon from resource or file.
722 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
723 INT width, INT height, INT colors,
724 BOOL fCursor, UINT loadflags)
730 CURSORICONDIRENTRY *dirEntry;
733 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
736 if (!CURSORICON_SimulateLoadingFromResourceW((LPWSTR)name, fCursor, &dir, &ptr))
739 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(dir, width, height, 1);
741 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(dir, width, height, colors);
742 bits = ptr[dirEntry->wResId-1];
743 hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dirEntry->dwBytesInRes,
744 !fCursor, 0x00030000, width, height, loadflags);
745 HeapFree( GetProcessHeap(), 0, dir );
746 HeapFree( GetProcessHeap(), 0, ptr );
748 else /* Load from resource */
754 if (!hInstance) /* Load OEM cursor/icon */
756 if (!(hInstance = GetModuleHandleA( "user32.dll" ))) return 0;
759 /* Normalize hInstance (must be uniquely represented for icon cache) */
761 if ( HIWORD( hInstance ) )
762 hInstance = MapHModuleLS( hInstance );
764 hInstance = GetExePtr( hInstance );
766 /* Get directory resource ID */
768 if (!(hRsrc = FindResourceW( hInstance, name,
769 fCursor ? RT_GROUP_CURSORW : RT_GROUP_ICONW )))
773 /* Find the best entry in the directory */
775 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
776 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
778 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
781 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon( dir,
782 width, height, colors );
783 if (!dirEntry) return 0;
784 wResId = dirEntry->wResId;
785 dwBytesInRes = dirEntry->dwBytesInRes;
786 FreeResource( handle );
788 /* Load the resource */
790 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
791 fCursor ? RT_CURSORW : RT_ICONW ))) return 0;
793 /* If shared icon, check whether it was already loaded */
794 if ( (loadflags & LR_SHARED)
795 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
798 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
799 bits = (LPBYTE)LockResource( handle );
800 hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dwBytesInRes,
801 !fCursor, 0x00030000, width, height, loadflags);
802 FreeResource( handle );
804 /* If shared icon, add to icon cache */
806 if ( hIcon && (loadflags & LR_SHARED) )
807 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
813 /***********************************************************************
816 * Make a copy of a cursor or icon.
818 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
820 char *ptrOld, *ptrNew;
822 HICON16 hOld = HICON_16(hIcon);
825 if (!(ptrOld = (char *)GlobalLock16( hOld ))) return 0;
826 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
827 size = GlobalSize16( hOld );
828 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
829 FarSetOwner16( hNew, hInst16 );
830 ptrNew = (char *)GlobalLock16( hNew );
831 memcpy( ptrNew, ptrOld, size );
832 GlobalUnlock16( hOld );
833 GlobalUnlock16( hNew );
834 return HICON_32(hNew);
837 /*************************************************************************
840 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
843 * Handle [I] handle to an Image
844 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
845 * iDesiredCX [I] The Desired width of the Image
846 * iDesiredCY [I] The desired height of the Image
847 * nFlags [I] The flags from CopyImage
850 * Success: The new handle of the Image
853 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
854 * LR_MONOCHROME should be implemented by CURSORICON_CreateFromResource.
855 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
860 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
861 INT iDesiredCX, INT iDesiredCY,
866 TRACE_(icon)("hIcon %u, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
867 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
874 /* Best Fit or Monochrome */
875 if( (nFlags & LR_COPYFROMRESOURCE
876 && (iDesiredCX > 0 || iDesiredCY > 0))
877 || nFlags & LR_MONOCHROME)
879 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
881 /* Not Found in Cache, then do a straight copy
883 if(pIconCache == NULL)
885 hNew = CURSORICON_Copy(0, hIcon);
886 if(nFlags & LR_COPYFROMRESOURCE)
888 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
893 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
900 CURSORICONDIRENTRY *pDirEntry;
901 BOOL bIsIcon = (nType == IMAGE_ICON);
903 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
905 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
906 || (iDesiredCX == 0 && iDesiredCY == 0))
908 iDesiredCY = GetSystemMetrics(bIsIcon ?
909 SM_CYICON : SM_CYCURSOR);
910 iDesiredCX = GetSystemMetrics(bIsIcon ?
911 SM_CXICON : SM_CXCURSOR);
914 /* Retrieve the CURSORICONDIRENTRY
916 if (!(hMem = LoadResource( pIconCache->hModule ,
917 pIconCache->hGroupRsrc)))
921 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
930 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
931 pDir, iDesiredCX, iDesiredCY, 256);
935 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
936 pDir, iDesiredCX, iDesiredCY, 1);
939 wResId = pDirEntry->wResId;
940 dwBytesInRes = pDirEntry->dwBytesInRes;
943 TRACE_(icon)("ResID %u, BytesInRes %lu, Width %d, Height %d DX %d, DY %d\n",
944 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
945 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
949 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
950 MAKEINTRESOURCEW(wResId), bIsIcon ? RT_ICONW : RT_CURSORW)))
954 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
959 pBits = (LPBYTE)LockResource( hMem );
961 if(nFlags & LR_DEFAULTSIZE)
963 iTargetCY = GetSystemMetrics(SM_CYICON);
964 iTargetCX = GetSystemMetrics(SM_CXICON);
967 /* Create a New Icon with the proper dimension
969 hNew = CURSORICON_CreateFromResource( 0, 0, pBits, dwBytesInRes,
970 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
974 else hNew = CURSORICON_Copy(0, hIcon);
979 /***********************************************************************
980 * CreateCursor (USER32.@)
982 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
983 INT xHotSpot, INT yHotSpot,
984 INT nWidth, INT nHeight,
985 LPCVOID lpANDbits, LPCVOID lpXORbits )
989 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
990 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
992 info.ptHotSpot.x = xHotSpot;
993 info.ptHotSpot.y = yHotSpot;
994 info.nWidth = nWidth;
995 info.nHeight = nHeight;
996 info.nWidthBytes = 0;
998 info.bBitsPerPixel = 1;
1000 return HICON_32(CreateCursorIconIndirect16(MapHModuleLS(hInstance), &info,
1001 lpANDbits, lpXORbits));
1005 /***********************************************************************
1006 * CreateIcon (USER.407)
1008 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1009 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1010 LPCVOID lpANDbits, LPCVOID lpXORbits )
1012 CURSORICONINFO info;
1014 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1015 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1017 info.ptHotSpot.x = ICON_HOTSPOT;
1018 info.ptHotSpot.y = ICON_HOTSPOT;
1019 info.nWidth = nWidth;
1020 info.nHeight = nHeight;
1021 info.nWidthBytes = 0;
1022 info.bPlanes = bPlanes;
1023 info.bBitsPerPixel = bBitsPixel;
1025 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1029 /***********************************************************************
1030 * CreateIcon (USER32.@)
1032 * Creates an icon based on the specified bitmaps. The bitmaps must be
1033 * provided in a device dependent format and will be resized to
1034 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1035 * depth. The provided bitmaps must be top-down bitmaps.
1036 * Although Windows does not support 15bpp(*) this API must support it
1037 * for Winelib applications.
1039 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1044 * - The provided bitmaps are not resized!
1045 * - The documentation says the lpXORbits bitmap must be in a device
1046 * dependent format. But we must still resize it and perform depth
1047 * conversions if necessary.
1048 * - I'm a bit unsure about the how the 'device dependent format' thing works.
1049 * I did some tests on windows and found that if you provide a 16bpp bitmap
1050 * in lpXORbits, then its format but be 565 RGB if the screen's bit depth
1051 * is 16bpp but it must be 555 RGB if the screen's bit depth is anything
1052 * else. I don't know if this is part of the GDI specs or if this is a
1053 * quirk of the graphics card driver.
1054 * - You may think that we check whether the bit depths match or not
1055 * as an optimization. But the truth is that the conversion using
1056 * CreateDIBitmap does not work for some bit depth (e.g. 8bpp) and I have
1058 * - I'm pretty sure that all the things we do in CreateIcon should
1059 * also be done in CreateIconIndirect...
1061 HICON WINAPI CreateIcon(
1062 HINSTANCE hInstance, /* [in] the application's hInstance */
1063 INT nWidth, /* [in] the width of the provided bitmaps */
1064 INT nHeight, /* [in] the height of the provided bitmaps */
1065 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1066 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1067 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1068 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1073 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1074 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1080 if (GetDeviceCaps(hdc,BITSPIXEL)==bBitsPixel) {
1081 CURSORICONINFO info;
1083 info.ptHotSpot.x = ICON_HOTSPOT;
1084 info.ptHotSpot.y = ICON_HOTSPOT;
1085 info.nWidth = nWidth;
1086 info.nHeight = nHeight;
1087 info.nWidthBytes = 0;
1088 info.bPlanes = bPlanes;
1089 info.bBitsPerPixel = bBitsPixel;
1091 hIcon=HICON_32(CreateCursorIconIndirect16(MapHModuleLS(hInstance), &info,
1092 lpANDbits, lpXORbits));
1098 iinfo.xHotspot=ICON_HOTSPOT;
1099 iinfo.yHotspot=ICON_HOTSPOT;
1100 iinfo.hbmMask=CreateBitmap(nWidth,nHeight,1,1,lpANDbits);
1102 bmi.bmiHeader.biSize=sizeof(bmi.bmiHeader);
1103 bmi.bmiHeader.biWidth=nWidth;
1104 bmi.bmiHeader.biHeight=-nHeight;
1105 bmi.bmiHeader.biPlanes=bPlanes;
1106 bmi.bmiHeader.biBitCount=bBitsPixel;
1107 bmi.bmiHeader.biCompression=BI_RGB;
1108 bmi.bmiHeader.biSizeImage=0;
1109 bmi.bmiHeader.biXPelsPerMeter=0;
1110 bmi.bmiHeader.biYPelsPerMeter=0;
1111 bmi.bmiHeader.biClrUsed=0;
1112 bmi.bmiHeader.biClrImportant=0;
1114 iinfo.hbmColor = CreateDIBitmap( hdc, &bmi.bmiHeader,
1115 CBM_INIT, lpXORbits,
1116 &bmi, DIB_RGB_COLORS );
1118 hIcon=CreateIconIndirect(&iinfo);
1119 DeleteObject(iinfo.hbmMask);
1120 DeleteObject(iinfo.hbmColor);
1127 /***********************************************************************
1128 * CreateCursorIconIndirect (USER.408)
1130 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1131 CURSORICONINFO *info,
1137 int sizeAnd, sizeXor;
1139 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1140 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1141 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1142 sizeXor = info->nHeight * info->nWidthBytes;
1143 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1144 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1145 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1147 FarSetOwner16( handle, hInstance );
1148 ptr = (char *)GlobalLock16( handle );
1149 memcpy( ptr, info, sizeof(*info) );
1150 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1151 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1152 GlobalUnlock16( handle );
1157 /***********************************************************************
1158 * CopyIcon (USER.368)
1160 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1162 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1163 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1167 /***********************************************************************
1168 * CopyIcon (USER32.@)
1170 HICON WINAPI CopyIcon( HICON hIcon )
1172 TRACE_(icon)("%04x\n", hIcon );
1173 return CURSORICON_Copy( 0, hIcon );
1177 /***********************************************************************
1178 * CopyCursor (USER.369)
1180 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1182 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1183 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1186 /**********************************************************************
1187 * DestroyIcon32 (USER.610)
1189 * This routine is actually exported from Win95 USER under the name
1190 * DestroyIcon32 ... The behaviour implemented here should mimic
1191 * the Win95 one exactly, especially the return values, which
1192 * depend on the setting of various flags.
1194 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1198 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1200 /* Check whether destroying active cursor */
1202 if ( QUEUE_Current()->cursor == HICON_32(handle) )
1204 WARN_(cursor)("Destroying active cursor!\n" );
1208 /* Try shared cursor/icon first */
1210 if ( !(flags & CID_NONSHARED) )
1212 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1215 return (flags & CID_WIN32)? TRUE : (count == 0);
1217 /* FIXME: OEM cursors/icons should be recognized */
1220 /* Now assume non-shared cursor/icon */
1222 retv = GlobalFree16( handle );
1223 return (flags & CID_RESOURCE)? retv : TRUE;
1226 /***********************************************************************
1227 * DestroyIcon (USER32.@)
1229 BOOL WINAPI DestroyIcon( HICON hIcon )
1231 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1235 /***********************************************************************
1236 * DestroyCursor (USER32.@)
1238 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1240 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1244 /***********************************************************************
1245 * DrawIcon (USER32.@)
1247 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1249 CURSORICONINFO *ptr;
1251 HBITMAP hXorBits, hAndBits;
1252 COLORREF oldFg, oldBg;
1254 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
1255 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1256 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
1258 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1259 ptr->bBitsPerPixel, (char *)(ptr + 1)
1260 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
1261 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1262 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1264 if (hXorBits && hAndBits)
1266 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1267 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
1268 SelectObject( hMemDC, hXorBits );
1269 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
1270 SelectObject( hMemDC, hBitTemp );
1273 if (hXorBits) DeleteObject( hXorBits );
1274 if (hAndBits) DeleteObject( hAndBits );
1275 GlobalUnlock16(HICON_16(hIcon));
1276 SetTextColor( hdc, oldFg );
1277 SetBkColor( hdc, oldBg );
1281 /***********************************************************************
1282 * DumpIcon (USER.459)
1284 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1285 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1287 CURSORICONINFO *info = MapSL( pInfo );
1288 int sizeAnd, sizeXor;
1290 if (!info) return 0;
1291 sizeXor = info->nHeight * info->nWidthBytes;
1292 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1293 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1294 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1295 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1296 return MAKELONG( sizeXor, sizeXor );
1300 /***********************************************************************
1301 * SetCursor (USER32.@)
1303 * A handle to the previous cursor shape.
1305 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1307 MESSAGEQUEUE *queue = QUEUE_Current();
1310 if (hCursor == queue->cursor) return hCursor; /* No change */
1311 TRACE_(cursor)("%04x\n", hCursor );
1312 hOldCursor = queue->cursor;
1313 queue->cursor = hCursor;
1314 /* Change the cursor shape only if it is visible */
1315 if (queue->cursor_count >= 0)
1317 USER_Driver.pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
1318 GlobalUnlock16(HCURSOR_16(hCursor));
1323 /***********************************************************************
1324 * ShowCursor (USER32.@)
1326 INT WINAPI ShowCursor( BOOL bShow )
1328 MESSAGEQUEUE *queue = QUEUE_Current();
1330 TRACE_(cursor)("%d, count=%d\n", bShow, queue->cursor_count );
1334 if (++queue->cursor_count == 0) /* Show it */
1336 USER_Driver.pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(queue->cursor)));
1337 GlobalUnlock16(HCURSOR_16(queue->cursor));
1342 if (--queue->cursor_count == -1) /* Hide it */
1343 USER_Driver.pSetCursor( NULL );
1345 return queue->cursor_count;
1348 /***********************************************************************
1349 * GetCursor (USER32.@)
1351 HCURSOR WINAPI GetCursor(void)
1353 return QUEUE_Current()->cursor;
1357 /***********************************************************************
1358 * ClipCursor (USER.16)
1360 BOOL16 WINAPI ClipCursor16( const RECT16 *rect )
1362 if (!rect) SetRectEmpty( &CURSOR_ClipRect );
1363 else CONV_RECT16TO32( rect, &CURSOR_ClipRect );
1368 /***********************************************************************
1369 * ClipCursor (USER32.@)
1371 BOOL WINAPI ClipCursor( const RECT *rect )
1373 if (!rect) SetRectEmpty( &CURSOR_ClipRect );
1374 else CopyRect( &CURSOR_ClipRect, rect );
1379 /***********************************************************************
1380 * GetClipCursor (USER.309)
1382 void WINAPI GetClipCursor16( RECT16 *rect )
1384 if (rect) CONV_RECT32TO16( &CURSOR_ClipRect, rect );
1388 /***********************************************************************
1389 * GetClipCursor (USER32.@)
1391 BOOL WINAPI GetClipCursor( RECT *rect )
1395 CopyRect( rect, &CURSOR_ClipRect );
1401 /**********************************************************************
1402 * LookupIconIdFromDirectoryEx (USER.364)
1404 * FIXME: exact parameter sizes
1406 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE xdir, BOOL16 bIcon,
1407 INT16 width, INT16 height, UINT16 cFlag )
1409 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1411 if( dir && !dir->idReserved && (dir->idType & 3) )
1413 CURSORICONDIRENTRY* entry;
1418 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1421 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1426 entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1428 entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1430 if( entry ) retVal = entry->wResId;
1432 else WARN_(cursor)("invalid resource directory\n");
1436 /**********************************************************************
1437 * LookupIconIdFromDirectoryEx (USER32.@)
1439 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE dir, BOOL bIcon,
1440 INT width, INT height, UINT cFlag )
1442 return LookupIconIdFromDirectoryEx16( dir, bIcon, width, height, cFlag );
1445 /**********************************************************************
1446 * LookupIconIdFromDirectory (USER.?)
1448 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
1450 return LookupIconIdFromDirectoryEx16( dir, bIcon,
1451 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1452 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1455 /**********************************************************************
1456 * LookupIconIdFromDirectory (USER32.@)
1458 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1460 return LookupIconIdFromDirectoryEx( dir, bIcon,
1461 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1462 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1465 /**********************************************************************
1466 * GetIconID (USER.455)
1468 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1470 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
1472 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1473 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1478 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1479 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1481 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1482 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1484 WARN_(cursor)("invalid res type %ld\n", resType );
1489 /**********************************************************************
1490 * LoadCursorIconHandler (USER.336)
1492 * Supposed to load resources of Windows 2.x applications.
1494 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1496 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1497 hResource, hModule, hRsrc);
1498 return (HGLOBAL16)0;
1501 /**********************************************************************
1502 * LoadDIBIconHandler (USER.357)
1504 * RT_ICON resource loader, installed by USER_SignalProc when module
1507 HGLOBAL16 WINAPI LoadDIBIconHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1509 /* If hResource is zero we must allocate a new memory block, if it's
1510 * non-zero but GlobalLock() returns NULL then it was discarded and
1511 * we have to recommit some memory, otherwise we just need to check
1512 * the block size. See LoadProc() in 16-bit SDK for more.
1515 hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
1518 LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1519 hMemObj = HICON_16(CURSORICON_CreateFromResource(
1520 hModule, hMemObj, bits,
1521 SizeofResource16(hModule, hRsrc), TRUE, 0x00030000,
1522 GetSystemMetrics(SM_CXICON),
1523 GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR));
1528 /**********************************************************************
1529 * LoadDIBCursorHandler (USER.356)
1531 * RT_CURSOR resource loader. Same as above.
1533 HGLOBAL16 WINAPI LoadDIBCursorHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1535 hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
1538 LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1539 hMemObj = HICON_16(CURSORICON_CreateFromResource(
1540 hModule, hMemObj, bits,
1541 SizeofResource16(hModule, hRsrc), FALSE, 0x00030000,
1542 GetSystemMetrics(SM_CXCURSOR),
1543 GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME));
1548 /**********************************************************************
1549 * LoadIconHandler (USER.456)
1551 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
1553 LPBYTE bits = (LPBYTE)LockResource16( hResource );
1555 TRACE_(cursor)("hRes=%04x\n",hResource);
1557 return HICON_16(CURSORICON_CreateFromResource(0, 0, bits, 0, TRUE,
1558 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
1561 /***********************************************************************
1562 * LoadCursorW (USER32.@)
1564 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1566 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1567 LR_SHARED | LR_DEFAULTSIZE );
1570 /***********************************************************************
1571 * LoadCursorA (USER32.@)
1573 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1575 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1576 LR_SHARED | LR_DEFAULTSIZE );
1579 /***********************************************************************
1580 * LoadCursorFromFileW (USER32.@)
1582 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1584 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1585 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1588 /***********************************************************************
1589 * LoadCursorFromFileA (USER32.@)
1591 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1593 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1594 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1597 /***********************************************************************
1598 * LoadIconW (USER32.@)
1600 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1602 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1603 LR_SHARED | LR_DEFAULTSIZE );
1606 /***********************************************************************
1607 * LoadIconA (USER32.@)
1609 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1611 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1612 LR_SHARED | LR_DEFAULTSIZE );
1615 /**********************************************************************
1616 * GetIconInfo (USER32.@)
1618 BOOL WINAPI GetIconInfo(HICON hIcon,PICONINFO iconinfo) {
1619 CURSORICONINFO *ciconinfo;
1621 ciconinfo = GlobalLock16(HICON_16(hIcon));
1625 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1626 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1628 iconinfo->fIcon = TRUE;
1629 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1630 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1634 iconinfo->fIcon = FALSE;
1635 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1636 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1639 iconinfo->hbmColor = CreateBitmap ( ciconinfo->nWidth, ciconinfo->nHeight,
1640 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1641 (char *)(ciconinfo + 1)
1642 + ciconinfo->nHeight *
1643 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1644 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, ciconinfo->nHeight,
1645 1, 1, (char *)(ciconinfo + 1));
1647 GlobalUnlock16(HICON_16(hIcon));
1652 /**********************************************************************
1653 * CreateIconIndirect (USER32.@)
1655 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1657 BITMAP bmpXor,bmpAnd;
1659 int sizeXor,sizeAnd;
1661 GetObjectA( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1662 GetObjectA( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1664 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1665 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1667 hObj = GlobalAlloc16( GMEM_MOVEABLE,
1668 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1671 CURSORICONINFO *info;
1673 info = (CURSORICONINFO *)GlobalLock16( hObj );
1675 /* If we are creating an icon, the hotspot is unused */
1676 if (iconinfo->fIcon)
1678 info->ptHotSpot.x = ICON_HOTSPOT;
1679 info->ptHotSpot.y = ICON_HOTSPOT;
1683 info->ptHotSpot.x = iconinfo->xHotspot;
1684 info->ptHotSpot.y = iconinfo->yHotspot;
1687 info->nWidth = bmpXor.bmWidth;
1688 info->nHeight = bmpXor.bmHeight;
1689 info->nWidthBytes = bmpXor.bmWidthBytes;
1690 info->bPlanes = bmpXor.bmPlanes;
1691 info->bBitsPerPixel = bmpXor.bmBitsPixel;
1693 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1695 GetBitmapBits( iconinfo->hbmMask ,sizeAnd,(char*)(info + 1) );
1696 GetBitmapBits( iconinfo->hbmColor,sizeXor,(char*)(info + 1) +sizeAnd);
1697 GlobalUnlock16( hObj );
1699 return HICON_32(hObj);
1702 /******************************************************************************
1703 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1706 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1709 * hdc [I] Handle to device context
1710 * x0 [I] X coordinate of upper left corner
1711 * y0 [I] Y coordinate of upper left corner
1712 * hIcon [I] Handle to icon to draw
1713 * cxWidth [I] Width of icon
1714 * cyWidth [I] Height of icon
1715 * istep [I] Index of frame in animated cursor
1716 * hbr [I] Handle to background brush
1717 * flags [I] Icon-drawing flags
1723 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1724 INT cxWidth, INT cyWidth, UINT istep,
1725 HBRUSH hbr, UINT flags )
1727 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
1728 HDC hDC_off = 0, hMemDC = CreateCompatibleDC (hdc);
1729 BOOL result = FALSE, DoOffscreen;
1730 HBITMAP hB_off = 0, hOld = 0;
1732 if (!ptr) return FALSE;
1733 TRACE_(icon)("(hdc=%x,pos=%d.%d,hicon=%x,extend=%d.%d,istep=%d,br=%x,flags=0x%08x)\n",
1734 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags
1738 FIXME_(icon)("Ignoring istep=%d\n", istep);
1739 if (flags & DI_COMPAT)
1740 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
1743 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
1747 /* Calculate the size of the destination image. */
1750 if (flags & DI_DEFAULTSIZE)
1751 cxWidth = GetSystemMetrics (SM_CXICON);
1753 cxWidth = ptr->nWidth;
1757 if (flags & DI_DEFAULTSIZE)
1758 cyWidth = GetSystemMetrics (SM_CYICON);
1760 cyWidth = ptr->nHeight;
1763 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1773 hDC_off = CreateCompatibleDC(hdc);
1774 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
1775 if (hDC_off && hB_off) {
1776 hOld = SelectObject(hDC_off, hB_off);
1777 FillRect(hDC_off, &r, hbr);
1781 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
1783 HBITMAP hXorBits, hAndBits;
1784 COLORREF oldFg, oldBg;
1787 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
1789 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1790 ptr->bPlanes, ptr->bBitsPerPixel,
1793 get_bitmap_width_bytes(ptr->nWidth,1) );
1794 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1795 1, 1, (char *)(ptr+1) );
1796 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1797 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1799 if (hXorBits && hAndBits)
1801 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1802 if (flags & DI_MASK)
1805 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1806 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1808 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1809 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1811 SelectObject( hMemDC, hXorBits );
1812 if (flags & DI_IMAGE)
1815 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1816 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1818 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1819 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1821 SelectObject( hMemDC, hBitTemp );
1825 SetTextColor( hdc, oldFg );
1826 SetBkColor( hdc, oldBg );
1827 if (hXorBits) DeleteObject( hXorBits );
1828 if (hAndBits) DeleteObject( hAndBits );
1829 SetStretchBltMode (hdc, nStretchMode);
1831 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
1832 SelectObject(hDC_off, hOld);
1835 if (hMemDC) DeleteDC( hMemDC );
1836 if (hDC_off) DeleteDC(hDC_off);
1837 if (hB_off) DeleteObject(hB_off);
1838 GlobalUnlock16(HICON_16(hIcon));
1842 /***********************************************************************
1843 * DIB_FixColorsToLoadflags
1845 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
1848 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
1851 COLORREF c_W, c_S, c_F, c_L, c_C;
1855 if (bmi->bmiHeader.biBitCount > 8) return;
1856 if (bmi->bmiHeader.biSize == sizeof(BITMAPINFOHEADER)) incr = 4;
1857 else if (bmi->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) incr = 3;
1859 WARN_(resource)("Wrong bitmap header size!\n");
1862 colors = bmi->bmiHeader.biClrUsed;
1863 if (!colors && (bmi->bmiHeader.biBitCount <= 8))
1864 colors = 1 << bmi->bmiHeader.biBitCount;
1865 c_W = GetSysColor(COLOR_WINDOW);
1866 c_S = GetSysColor(COLOR_3DSHADOW);
1867 c_F = GetSysColor(COLOR_3DFACE);
1868 c_L = GetSysColor(COLOR_3DLIGHT);
1869 if (loadflags & LR_LOADTRANSPARENT) {
1870 switch (bmi->bmiHeader.biBitCount) {
1871 case 1: pix = pix >> 7; break;
1872 case 4: pix = pix >> 4; break;
1875 WARN_(resource)("(%d): Unsupported depth\n", bmi->bmiHeader.biBitCount);
1878 if (pix >= colors) {
1879 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
1882 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
1883 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
1884 ptr->rgbBlue = GetBValue(c_W);
1885 ptr->rgbGreen = GetGValue(c_W);
1886 ptr->rgbRed = GetRValue(c_W);
1888 if (loadflags & LR_LOADMAP3DCOLORS)
1889 for (i=0; i<colors; i++) {
1890 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
1891 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
1892 if (c_C == RGB(128, 128, 128)) {
1893 ptr->rgbRed = GetRValue(c_S);
1894 ptr->rgbGreen = GetGValue(c_S);
1895 ptr->rgbBlue = GetBValue(c_S);
1896 } else if (c_C == RGB(192, 192, 192)) {
1897 ptr->rgbRed = GetRValue(c_F);
1898 ptr->rgbGreen = GetGValue(c_F);
1899 ptr->rgbBlue = GetBValue(c_F);
1900 } else if (c_C == RGB(223, 223, 223)) {
1901 ptr->rgbRed = GetRValue(c_L);
1902 ptr->rgbGreen = GetGValue(c_L);
1903 ptr->rgbBlue = GetBValue(c_L);
1909 /**********************************************************************
1912 static HBITMAP BITMAP_Load( HINSTANCE instance,LPCWSTR name, UINT loadflags )
1914 HBITMAP hbitmap = 0;
1918 BITMAPINFO *info, *fix_info=NULL;
1922 if (!(loadflags & LR_LOADFROMFILE))
1926 /* OEM bitmap: try to load the resource from user32.dll */
1927 if (HIWORD(name)) return 0;
1928 if (!(instance = GetModuleHandleA("user32.dll"))) return 0;
1930 if (!(hRsrc = FindResourceW( instance, name, RT_BITMAPW ))) return 0;
1931 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
1933 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
1937 if (!(ptr = map_fileW( name ))) return 0;
1938 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
1940 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
1941 if ((hFix = GlobalAlloc(0, size))) fix_info=GlobalLock(hFix);
1945 memcpy(fix_info, info, size);
1946 pix = *((LPBYTE)info+DIB_BitmapInfoSize(info, DIB_RGB_COLORS));
1947 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
1948 if (!screen_dc) screen_dc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
1951 char *bits = (char *)info + size;
1952 if (loadflags & LR_CREATEDIBSECTION) {
1954 hbitmap = CreateDIBSection(screen_dc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
1955 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
1956 SetDIBits(screen_dc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
1960 hbitmap = CreateDIBitmap( screen_dc, &fix_info->bmiHeader, CBM_INIT,
1961 bits, fix_info, DIB_RGB_COLORS );
1967 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
1971 /**********************************************************************
1972 * LoadImageA (USER32.@)
1974 * FIXME: implementation lacks some features, see LR_ defines in winuser.h
1977 /* filter for page-fault exceptions */
1978 static WINE_EXCEPTION_FILTER(page_fault)
1980 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
1981 return EXCEPTION_EXECUTE_HANDLER;
1982 return EXCEPTION_CONTINUE_SEARCH;
1985 /*********************************************************************/
1987 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
1988 INT desiredx, INT desiredy, UINT loadflags)
1994 return LoadImageW(hinst, (LPWSTR)name, type, desiredx, desiredy, loadflags);
1997 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1998 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1999 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2001 __EXCEPT(page_fault) {
2002 SetLastError( ERROR_INVALID_PARAMETER );
2006 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2007 HeapFree(GetProcessHeap(), 0, u_name);
2012 /******************************************************************************
2013 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2016 * hinst [I] Handle of instance that contains image
2017 * name [I] Name of image
2018 * type [I] Type of image
2019 * desiredx [I] Desired width
2020 * desiredy [I] Desired height
2021 * loadflags [I] Load flags
2024 * Success: Handle to newly loaded image
2027 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2029 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2030 INT desiredx, INT desiredy, UINT loadflags )
2033 TRACE_(resource)("(0x%04x,%p,%d,%d,%d,0x%08x)\n",
2034 hinst,name,type,desiredx,desiredy,loadflags);
2036 TRACE_(resource)("(0x%04x,%p,%d,%d,%d,0x%08x)\n",
2037 hinst,name,type,desiredx,desiredy,loadflags);
2039 if (loadflags & LR_DEFAULTSIZE) {
2040 if (type == IMAGE_ICON) {
2041 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2042 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2043 } else if (type == IMAGE_CURSOR) {
2044 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2045 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2048 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2051 return BITMAP_Load( hinst, name, loadflags );
2054 if (!screen_dc) screen_dc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2057 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2058 if (palEnts == 0) palEnts = 256;
2059 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2060 palEnts, FALSE, loadflags);
2065 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2066 1, TRUE, loadflags);
2071 /******************************************************************************
2072 * CopyImage (USER32.@) Creates new image and copies attributes to it
2075 * hnd [I] Handle to image to copy
2076 * type [I] Type of image to copy
2077 * desiredx [I] Desired width of new image
2078 * desiredy [I] Desired height of new image
2079 * flags [I] Copy flags
2082 * Success: Handle to newly created image
2085 * FIXME: implementation still lacks nearly all features, see LR_*
2086 * defines in winuser.h
2088 HICON WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2089 INT desiredy, UINT flags )
2098 if (!GetObjectW( hnd, sizeof(bm), &bm )) return 0;
2100 if ((res = CreateBitmapIndirect(&bm)))
2102 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight );
2103 GetBitmapBits( hnd, bm.bmWidthBytes * bm.bmHeight, buf );
2104 SetBitmapBits( res, bm.bmWidthBytes * bm.bmHeight, buf );
2105 HeapFree( GetProcessHeap(), 0, buf );
2110 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2112 /* Should call CURSORICON_ExtCopy but more testing
2113 * needs to be done before we change this
2115 return CopyCursor(hnd);
2121 /******************************************************************************
2122 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2125 * Success: Handle to specified bitmap
2128 HBITMAP WINAPI LoadBitmapW(
2129 HINSTANCE instance, /* [in] Handle to application instance */
2130 LPCWSTR name) /* [in] Address of bitmap resource name */
2132 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2135 /**********************************************************************
2136 * LoadBitmapA (USER32.@)
2138 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2140 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );