Moved a couple of locale functions from ole2nls.c into locale.c.
[wine/multimedia.git] / windows / cursoricon.c
blobca8abd27e04b8b7b10c11a397efdf78a6acdeabb
1 /*
2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
6 * 1997 Alex Korobka
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
25 * Theory:
27 * http://www.microsoft.com/win32dev/ui/icons.htm
29 * Cursors and icons are stored in a global heap block, with the
30 * following layout:
32 * CURSORICONINFO info;
33 * BYTE[] ANDbits;
34 * BYTE[] XORbits;
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 ?!
46 #include <string.h>
47 #include <stdlib.h>
49 #include "windef.h"
50 #include "wingdi.h"
51 #include "wownt32.h"
52 #include "wine/winbase16.h"
53 #include "wine/winuser16.h"
54 #include "wine/exception.h"
55 #include "bitmap.h"
56 #include "cursoricon.h"
57 #include "module.h"
58 #include "wine/debug.h"
59 #include "user.h"
60 #include "message.h"
61 #include "winerror.h"
62 #include "excpt.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
65 WINE_DECLARE_DEBUG_CHANNEL(icon);
66 WINE_DECLARE_DEBUG_CHANNEL(resource);
69 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
71 static HDC screen_dc;
73 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
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;
86 HMODULE hModule;
87 HRSRC hRsrc;
88 HRSRC hGroupRsrc;
89 HICON hIcon;
91 INT count;
93 } ICONCACHE;
95 static ICONCACHE *IconAnchor = NULL;
96 static CRITICAL_SECTION IconCrst = CRITICAL_SECTION_INIT("IconCrst");
97 static WORD ICON_HOTSPOT = 0x4242;
100 /***********************************************************************
101 * map_fileW
103 * Helper function to map a file to memory:
104 * name - file name
105 * [RETURN] ptr - pointer to mapped file
107 static void *map_fileW( LPCWSTR name )
109 HANDLE hFile, hMapping;
110 LPVOID ptr = NULL;
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 );
118 if (hMapping)
120 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
121 CloseHandle( hMapping );
124 return ptr;
128 /***********************************************************************
129 * get_bitmap_width_bytes
131 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
132 * data.
134 static int get_bitmap_width_bytes( int width, int bpp )
136 switch(bpp)
138 case 1:
139 return 2 * ((width+15) / 16);
140 case 4:
141 return 2 * ((width+3) / 4);
142 case 24:
143 width *= 3;
144 /* fall through */
145 case 8:
146 return width + (width & 1);
147 case 16:
148 case 15:
149 return width * 2;
150 case 32:
151 return width * 4;
152 default:
153 WARN("Unknown depth %d, please report.\n", bpp );
155 return -1;
159 /**********************************************************************
160 * CURSORICON_FindSharedIcon
162 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
164 HICON hIcon = 0;
165 ICONCACHE *ptr;
167 EnterCriticalSection( &IconCrst );
169 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
170 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
172 ptr->count++;
173 hIcon = ptr->hIcon;
174 break;
177 LeaveCriticalSection( &IconCrst );
179 return hIcon;
182 /*************************************************************************
183 * CURSORICON_FindCache
185 * Given a handle, find the corresponding cache element
187 * PARAMS
188 * Handle [I] handle to an Image
190 * RETURNS
191 * Success: The cache entry
192 * Failure: NULL
195 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
197 ICONCACHE *ptr;
198 ICONCACHE *pRet=NULL;
199 BOOL IsFound = FALSE;
200 int count;
202 EnterCriticalSection( &IconCrst );
204 for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
206 if ( hIcon == ptr->hIcon )
208 IsFound = TRUE;
209 pRet = ptr;
213 LeaveCriticalSection( &IconCrst );
215 return pRet;
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) );
224 if ( !ptr ) return;
226 ptr->hModule = hModule;
227 ptr->hRsrc = hRsrc;
228 ptr->hIcon = hIcon;
229 ptr->hGroupRsrc = hGroupRsrc;
230 ptr->count = 1;
232 EnterCriticalSection( &IconCrst );
233 ptr->next = IconAnchor;
234 IconAnchor = ptr;
235 LeaveCriticalSection( &IconCrst );
238 /**********************************************************************
239 * CURSORICON_DelSharedIcon
241 static INT CURSORICON_DelSharedIcon( HICON hIcon )
243 INT count = -1;
244 ICONCACHE *ptr;
246 EnterCriticalSection( &IconCrst );
248 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
249 if ( ptr->hIcon == hIcon )
251 if ( ptr->count > 0 ) ptr->count--;
252 count = ptr->count;
253 break;
256 LeaveCriticalSection( &IconCrst );
258 return count;
261 /**********************************************************************
262 * CURSORICON_FreeModuleIcons
264 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
266 ICONCACHE **ptr = &IconAnchor;
267 HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
269 EnterCriticalSection( &IconCrst );
271 while ( *ptr )
273 if ( (*ptr)->hModule == hModule )
275 ICONCACHE *freePtr = *ptr;
276 *ptr = freePtr->next;
278 GlobalFree16(HICON_16(freePtr->hIcon));
279 HeapFree( GetProcessHeap(), 0, freePtr );
280 continue;
282 ptr = &(*ptr)->next;
285 LeaveCriticalSection( &IconCrst );
288 /**********************************************************************
289 * CURSORICON_FindBestIcon
291 * Find the icon closest to the requested size and number of colors.
293 static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
294 int height, int colors )
296 int i;
297 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
298 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
299 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
301 if (dir->idCount < 1)
303 WARN_(icon)("Empty directory!\n" );
304 return NULL;
306 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
308 /* Find Best Fit */
309 iTotalDiff = 0xFFFFFFFF;
310 iColorDiff = 0xFFFFFFFF;
311 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
313 iTempXDiff = abs(width - entry->ResInfo.icon.bWidth);
314 iTempYDiff = abs(height - entry->ResInfo.icon.bHeight);
316 if(iTotalDiff > (iTempXDiff + iTempYDiff))
318 iXDiff = iTempXDiff;
319 iYDiff = iTempYDiff;
320 iTotalDiff = iXDiff + iYDiff;
324 /* Find Best Colors for Best Fit */
325 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
327 if(abs(width - entry->ResInfo.icon.bWidth) == iXDiff &&
328 abs(height - entry->ResInfo.icon.bHeight) == iYDiff)
330 iTempColorDiff = abs(colors - entry->ResInfo.icon.bColorCount);
331 if(iColorDiff > iTempColorDiff)
333 bestEntry = entry;
334 iColorDiff = iTempColorDiff;
339 return bestEntry;
343 /**********************************************************************
344 * CURSORICON_FindBestCursor
346 * Find the cursor closest to the requested size.
347 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
348 * ignored too
350 static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
351 int width, int height, int color)
353 int i, maxwidth, maxheight;
354 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
356 if (dir->idCount < 1)
358 WARN_(cursor)("Empty directory!\n" );
359 return NULL;
361 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
363 /* Double height to account for AND and XOR masks */
365 height *= 2;
367 /* First find the largest one smaller than or equal to the requested size*/
369 maxwidth = maxheight = 0;
370 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
371 if ((entry->ResInfo.cursor.wWidth <= width) && (entry->ResInfo.cursor.wHeight <= height) &&
372 (entry->ResInfo.cursor.wWidth > maxwidth) && (entry->ResInfo.cursor.wHeight > maxheight) &&
373 (entry->wBitCount == 1))
375 bestEntry = entry;
376 maxwidth = entry->ResInfo.cursor.wWidth;
377 maxheight = entry->ResInfo.cursor.wHeight;
379 if (bestEntry) return bestEntry;
381 /* Now find the smallest one larger than the requested size */
383 maxwidth = maxheight = 255;
384 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
385 if ((entry->ResInfo.cursor.wWidth < maxwidth) && (entry->ResInfo.cursor.wHeight < maxheight) &&
386 (entry->wBitCount == 1))
388 bestEntry = entry;
389 maxwidth = entry->ResInfo.cursor.wWidth;
390 maxheight = entry->ResInfo.cursor.wHeight;
393 return bestEntry;
396 /*********************************************************************
397 * The main purpose of this function is to create fake resource directory
398 * and fake resource entries. There are several reasons for this:
399 * - CURSORICONDIR and CURSORICONFILEDIR differ in sizes and their
400 * fields
401 * There are some "bad" cursor files which do not have
402 * bColorCount initialized but instead one must read this info
403 * directly from corresponding DIB sections
404 * Note: wResId is index to array of pointer returned in ptrs (origin is 1)
406 static BOOL CURSORICON_SimulateLoadingFromResourceW( LPWSTR filename, BOOL fCursor,
407 CURSORICONDIR **res, LPBYTE **ptr)
409 LPBYTE _free;
410 CURSORICONFILEDIR *bits;
411 int entries, size, i;
413 *res = NULL;
414 *ptr = NULL;
415 if (!(bits = map_fileW( filename ))) return FALSE;
417 /* FIXME: test for inimated icons
418 * hack to load the first icon from the *.ani file
420 if ( *(LPDWORD)bits==0x46464952 ) /* "RIFF" */
421 { LPBYTE pos = (LPBYTE) bits;
422 FIXME_(cursor)("Animated icons not correctly implemented! %p \n", bits);
424 for (;;)
425 { if (*(LPDWORD)pos==0x6e6f6369) /* "icon" */
426 { FIXME_(cursor)("icon entry found! %p\n", bits);
427 pos+=4;
428 if ( !*(LPWORD) pos==0x2fe) /* iconsize */
429 { goto fail;
431 bits=(CURSORICONFILEDIR*)(pos+4);
432 FIXME_(cursor)("icon size ok. offset=%p \n", bits);
433 break;
435 pos+=2;
436 if (pos>=(LPBYTE)bits+766) goto fail;
439 if (!(entries = bits->idCount)) goto fail;
440 size = sizeof(CURSORICONDIR) + sizeof(CURSORICONDIRENTRY) * (entries - 1);
441 _free = (LPBYTE) size;
443 for (i=0; i < entries; i++)
444 size += bits->idEntries[i].dwDIBSize + (fCursor ? sizeof(POINT16): 0);
446 if (!(*ptr = HeapAlloc( GetProcessHeap(), 0,
447 entries * sizeof (CURSORICONDIRENTRY*)))) goto fail;
448 if (!(*res = HeapAlloc( GetProcessHeap(), 0, size))) goto fail;
450 _free = (LPBYTE)(*res) + (int)_free;
451 memcpy((*res), bits, 6);
452 for (i=0; i<entries; i++)
454 ((LPBYTE*)(*ptr))[i] = _free;
455 if (fCursor) {
456 (*res)->idEntries[i].ResInfo.cursor.wWidth=bits->idEntries[i].bWidth;
457 (*res)->idEntries[i].ResInfo.cursor.wHeight=bits->idEntries[i].bHeight;
458 ((LPPOINT16)_free)->x=bits->idEntries[i].xHotspot;
459 ((LPPOINT16)_free)->y=bits->idEntries[i].yHotspot;
460 _free+=sizeof(POINT16);
461 } else {
462 (*res)->idEntries[i].ResInfo.icon.bWidth=bits->idEntries[i].bWidth;
463 (*res)->idEntries[i].ResInfo.icon.bHeight=bits->idEntries[i].bHeight;
464 (*res)->idEntries[i].ResInfo.icon.bColorCount = bits->idEntries[i].bColorCount;
466 (*res)->idEntries[i].wPlanes=1;
467 (*res)->idEntries[i].wBitCount = ((LPBITMAPINFOHEADER)((LPBYTE)bits +
468 bits->idEntries[i].dwDIBOffset))->biBitCount;
469 (*res)->idEntries[i].dwBytesInRes = bits->idEntries[i].dwDIBSize;
470 (*res)->idEntries[i].wResId=i+1;
472 memcpy(_free,(LPBYTE)bits +bits->idEntries[i].dwDIBOffset,
473 (*res)->idEntries[i].dwBytesInRes);
474 _free += (*res)->idEntries[i].dwBytesInRes;
476 UnmapViewOfFile( bits );
477 return TRUE;
478 fail:
479 if (*res) HeapFree( GetProcessHeap(), 0, *res );
480 if (*ptr) HeapFree( GetProcessHeap(), 0, *ptr );
481 UnmapViewOfFile( bits );
482 return FALSE;
486 /**********************************************************************
487 * CURSORICON_CreateFromResource
489 * Create a cursor or icon from in-memory resource template.
491 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
492 * with cbSize parameter as well.
494 static HICON CURSORICON_CreateFromResource( HMODULE16 hModule, HGLOBAL16 hObj, LPBYTE bits,
495 UINT cbSize, BOOL bIcon, DWORD dwVersion,
496 INT width, INT height, UINT loadflags )
498 static HDC hdcMem;
499 int sizeAnd, sizeXor;
500 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
501 BITMAP bmpXor, bmpAnd;
502 POINT16 hotspot;
503 BITMAPINFO *bmi;
504 BOOL DoStretch;
505 INT size;
507 hotspot.x = ICON_HOTSPOT;
508 hotspot.y = ICON_HOTSPOT;
510 TRACE_(cursor)("%08x (%u bytes), ver %08x, %ix%i %s %s\n",
511 (unsigned)bits, cbSize, (unsigned)dwVersion, width, height,
512 bIcon ? "icon" : "cursor", (loadflags & LR_MONOCHROME) ? "mono" : "" );
513 if (dwVersion == 0x00020000)
515 FIXME_(cursor)("\t2.xx resources are not supported\n");
516 return 0;
519 if (bIcon)
520 bmi = (BITMAPINFO *)bits;
521 else /* get the hotspot */
523 POINT16 *pt = (POINT16 *)bits;
524 hotspot = *pt;
525 bmi = (BITMAPINFO *)(pt + 1);
527 size = DIB_BitmapInfoSize( bmi, DIB_RGB_COLORS );
529 if (!width) width = bmi->bmiHeader.biWidth;
530 if (!height) height = bmi->bmiHeader.biHeight/2;
531 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
532 (bmi->bmiHeader.biWidth != width);
534 /* Check bitmap header */
536 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
537 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
538 bmi->bmiHeader.biCompression != BI_RGB) )
540 WARN_(cursor)("\tinvalid resource bitmap header.\n");
541 return 0;
544 if (!screen_dc) screen_dc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
545 if (screen_dc)
547 BITMAPINFO* pInfo;
549 /* Make sure we have room for the monochrome bitmap later on.
550 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
551 * up to and including the biBitCount. In-memory icon resource
552 * format is as follows:
554 * BITMAPINFOHEADER icHeader // DIB header
555 * RGBQUAD icColors[] // Color table
556 * BYTE icXOR[] // DIB bits for XOR mask
557 * BYTE icAND[] // DIB bits for AND mask
560 if ((pInfo = (BITMAPINFO *)HeapAlloc( GetProcessHeap(), 0,
561 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
563 memcpy( pInfo, bmi, size );
564 pInfo->bmiHeader.biHeight /= 2;
566 /* Create the XOR bitmap */
568 if (DoStretch) {
569 if(bIcon)
571 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
573 else
575 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
577 if(hXorBits)
579 HBITMAP hOld;
580 BOOL res = FALSE;
582 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
583 if (hdcMem) {
584 hOld = SelectObject(hdcMem, hXorBits);
585 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
586 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
587 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
588 SelectObject(hdcMem, hOld);
590 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
592 } else hXorBits = CreateDIBitmap( screen_dc, &pInfo->bmiHeader,
593 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS );
594 if( hXorBits )
596 char* xbits = (char *)bmi + size +
597 DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth,
598 bmi->bmiHeader.biHeight,
599 bmi->bmiHeader.biBitCount) / 2;
601 pInfo->bmiHeader.biBitCount = 1;
602 if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
604 RGBQUAD *rgb = pInfo->bmiColors;
606 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
607 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
608 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
609 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
611 else
613 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
615 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
616 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
619 /* Create the AND bitmap */
621 if (DoStretch) {
622 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
623 HBITMAP hOld;
624 BOOL res = FALSE;
626 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
627 if (hdcMem) {
628 hOld = SelectObject(hdcMem, hAndBits);
629 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
630 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
631 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
632 SelectObject(hdcMem, hOld);
634 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
636 } else hAndBits = CreateDIBitmap( screen_dc, &pInfo->bmiHeader,
637 CBM_INIT, xbits, pInfo, DIB_RGB_COLORS );
639 if( !hAndBits ) DeleteObject( hXorBits );
641 HeapFree( GetProcessHeap(), 0, pInfo );
645 if( !hXorBits || !hAndBits )
647 WARN_(cursor)("\tunable to create an icon bitmap.\n");
648 return 0;
651 /* Now create the CURSORICONINFO structure */
652 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
653 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
654 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
655 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
657 if (hObj) hObj = GlobalReAlloc16( hObj,
658 sizeof(CURSORICONINFO) + sizeXor + sizeAnd, GMEM_MOVEABLE );
659 if (!hObj) hObj = GlobalAlloc16( GMEM_MOVEABLE,
660 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
661 if (hObj)
663 CURSORICONINFO *info;
665 /* Make it owned by the module */
666 if (hModule) hModule = GetExePtr(hModule);
667 FarSetOwner16( hObj, hModule );
669 info = (CURSORICONINFO *)GlobalLock16( hObj );
670 info->ptHotSpot.x = hotspot.x;
671 info->ptHotSpot.y = hotspot.y;
672 info->nWidth = bmpXor.bmWidth;
673 info->nHeight = bmpXor.bmHeight;
674 info->nWidthBytes = bmpXor.bmWidthBytes;
675 info->bPlanes = bmpXor.bmPlanes;
676 info->bBitsPerPixel = bmpXor.bmBitsPixel;
678 /* Transfer the bitmap bits to the CURSORICONINFO structure */
680 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
681 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
682 GlobalUnlock16( hObj );
685 DeleteObject( hAndBits );
686 DeleteObject( hXorBits );
687 return HICON_32((HICON16)hObj);
691 /**********************************************************************
692 * CreateIconFromResource (USER32.@)
694 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
695 BOOL bIcon, DWORD dwVersion)
697 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
701 /**********************************************************************
702 * CreateIconFromResourceEx (USER32.@)
704 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
705 BOOL bIcon, DWORD dwVersion,
706 INT width, INT height,
707 UINT cFlag )
709 return CURSORICON_CreateFromResource( 0, 0, bits, cbSize, bIcon, dwVersion,
710 width, height, cFlag );
713 /**********************************************************************
714 * CURSORICON_Load
716 * Load a cursor or icon from resource or file.
718 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
719 INT width, INT height, INT colors,
720 BOOL fCursor, UINT loadflags)
722 HANDLE handle = 0;
723 HICON hIcon = 0;
724 HRSRC hRsrc;
725 CURSORICONDIR *dir;
726 CURSORICONDIRENTRY *dirEntry;
727 LPBYTE bits;
729 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
731 LPBYTE *ptr;
732 if (!CURSORICON_SimulateLoadingFromResourceW((LPWSTR)name, fCursor, &dir, &ptr))
733 return 0;
734 if (fCursor)
735 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(dir, width, height, 1);
736 else
737 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(dir, width, height, colors);
738 bits = ptr[dirEntry->wResId-1];
739 hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dirEntry->dwBytesInRes,
740 !fCursor, 0x00030000, width, height, loadflags);
741 HeapFree( GetProcessHeap(), 0, dir );
742 HeapFree( GetProcessHeap(), 0, ptr );
744 else /* Load from resource */
746 HRSRC hGroupRsrc;
747 WORD wResId;
748 DWORD dwBytesInRes;
750 if (!hInstance) /* Load OEM cursor/icon */
752 if (!(hInstance = GetModuleHandleA( "user32.dll" ))) return 0;
755 /* Normalize hInstance (must be uniquely represented for icon cache) */
757 if ( HIWORD( hInstance ) )
758 hInstance = HINSTANCE_32(MapHModuleLS( hInstance ));
759 else
760 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
762 /* Get directory resource ID */
764 if (!(hRsrc = FindResourceW( hInstance, name,
765 fCursor ? RT_GROUP_CURSORW : RT_GROUP_ICONW )))
766 return 0;
767 hGroupRsrc = hRsrc;
769 /* Find the best entry in the directory */
771 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
772 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
773 if (fCursor)
774 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
775 width, height, 1);
776 else
777 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon( dir,
778 width, height, colors );
779 if (!dirEntry) return 0;
780 wResId = dirEntry->wResId;
781 dwBytesInRes = dirEntry->dwBytesInRes;
782 FreeResource( handle );
784 /* Load the resource */
786 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
787 fCursor ? RT_CURSORW : RT_ICONW ))) return 0;
789 /* If shared icon, check whether it was already loaded */
790 if ( (loadflags & LR_SHARED)
791 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
792 return hIcon;
794 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
795 bits = (LPBYTE)LockResource( handle );
796 hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dwBytesInRes,
797 !fCursor, 0x00030000, width, height, loadflags);
798 FreeResource( handle );
800 /* If shared icon, add to icon cache */
802 if ( hIcon && (loadflags & LR_SHARED) )
803 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
806 return hIcon;
809 /***********************************************************************
810 * CURSORICON_Copy
812 * Make a copy of a cursor or icon.
814 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
816 char *ptrOld, *ptrNew;
817 int size;
818 HICON16 hOld = HICON_16(hIcon);
819 HICON16 hNew;
821 if (!(ptrOld = (char *)GlobalLock16( hOld ))) return 0;
822 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
823 size = GlobalSize16( hOld );
824 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
825 FarSetOwner16( hNew, hInst16 );
826 ptrNew = (char *)GlobalLock16( hNew );
827 memcpy( ptrNew, ptrOld, size );
828 GlobalUnlock16( hOld );
829 GlobalUnlock16( hNew );
830 return HICON_32(hNew);
833 /*************************************************************************
834 * CURSORICON_ExtCopy
836 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
838 * PARAMS
839 * Handle [I] handle to an Image
840 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
841 * iDesiredCX [I] The Desired width of the Image
842 * iDesiredCY [I] The desired height of the Image
843 * nFlags [I] The flags from CopyImage
845 * RETURNS
846 * Success: The new handle of the Image
848 * NOTES
849 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
850 * LR_MONOCHROME should be implemented by CURSORICON_CreateFromResource.
851 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
856 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
857 INT iDesiredCX, INT iDesiredCY,
858 UINT nFlags)
860 HICON hNew=0;
862 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
863 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
865 if(hIcon == 0)
867 return 0;
870 /* Best Fit or Monochrome */
871 if( (nFlags & LR_COPYFROMRESOURCE
872 && (iDesiredCX > 0 || iDesiredCY > 0))
873 || nFlags & LR_MONOCHROME)
875 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
877 /* Not Found in Cache, then do a straight copy
879 if(pIconCache == NULL)
881 hNew = CURSORICON_Copy(0, hIcon);
882 if(nFlags & LR_COPYFROMRESOURCE)
884 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
887 else
889 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
890 LPBYTE pBits;
891 HANDLE hMem;
892 HRSRC hRsrc;
893 DWORD dwBytesInRes;
894 WORD wResId;
895 CURSORICONDIR *pDir;
896 CURSORICONDIRENTRY *pDirEntry;
897 BOOL bIsIcon = (nType == IMAGE_ICON);
899 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
901 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
902 || (iDesiredCX == 0 && iDesiredCY == 0))
904 iDesiredCY = GetSystemMetrics(bIsIcon ?
905 SM_CYICON : SM_CYCURSOR);
906 iDesiredCX = GetSystemMetrics(bIsIcon ?
907 SM_CXICON : SM_CXCURSOR);
910 /* Retrieve the CURSORICONDIRENTRY
912 if (!(hMem = LoadResource( pIconCache->hModule ,
913 pIconCache->hGroupRsrc)))
915 return 0;
917 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
919 return 0;
922 /* Find Best Fit
924 if(bIsIcon)
926 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
927 pDir, iDesiredCX, iDesiredCY, 256);
929 else
931 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
932 pDir, iDesiredCX, iDesiredCY, 1);
935 wResId = pDirEntry->wResId;
936 dwBytesInRes = pDirEntry->dwBytesInRes;
937 FreeResource(hMem);
939 TRACE_(icon)("ResID %u, BytesInRes %lu, Width %d, Height %d DX %d, DY %d\n",
940 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
941 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
943 /* Get the Best Fit
945 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
946 MAKEINTRESOURCEW(wResId), bIsIcon ? RT_ICONW : RT_CURSORW)))
948 return 0;
950 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
952 return 0;
955 pBits = (LPBYTE)LockResource( hMem );
957 if(nFlags & LR_DEFAULTSIZE)
959 iTargetCY = GetSystemMetrics(SM_CYICON);
960 iTargetCX = GetSystemMetrics(SM_CXICON);
963 /* Create a New Icon with the proper dimension
965 hNew = CURSORICON_CreateFromResource( 0, 0, pBits, dwBytesInRes,
966 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
967 FreeResource(hMem);
970 else hNew = CURSORICON_Copy(0, hIcon);
971 return hNew;
975 /***********************************************************************
976 * CreateCursor (USER32.@)
978 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
979 INT xHotSpot, INT yHotSpot,
980 INT nWidth, INT nHeight,
981 LPCVOID lpANDbits, LPCVOID lpXORbits )
983 CURSORICONINFO info;
985 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
986 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
988 info.ptHotSpot.x = xHotSpot;
989 info.ptHotSpot.y = yHotSpot;
990 info.nWidth = nWidth;
991 info.nHeight = nHeight;
992 info.nWidthBytes = 0;
993 info.bPlanes = 1;
994 info.bBitsPerPixel = 1;
996 return HICON_32(CreateCursorIconIndirect16(MapHModuleLS(hInstance), &info,
997 lpANDbits, lpXORbits));
1001 /***********************************************************************
1002 * CreateIcon (USER.407)
1004 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1005 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1006 LPCVOID lpANDbits, LPCVOID lpXORbits )
1008 CURSORICONINFO info;
1010 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1011 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1013 info.ptHotSpot.x = ICON_HOTSPOT;
1014 info.ptHotSpot.y = ICON_HOTSPOT;
1015 info.nWidth = nWidth;
1016 info.nHeight = nHeight;
1017 info.nWidthBytes = 0;
1018 info.bPlanes = bPlanes;
1019 info.bBitsPerPixel = bBitsPixel;
1021 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1025 /***********************************************************************
1026 * CreateIcon (USER32.@)
1028 * Creates an icon based on the specified bitmaps. The bitmaps must be
1029 * provided in a device dependent format and will be resized to
1030 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1031 * depth. The provided bitmaps must be top-down bitmaps.
1032 * Although Windows does not support 15bpp(*) this API must support it
1033 * for Winelib applications.
1035 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1036 * format!
1038 * BUGS
1040 * - The provided bitmaps are not resized!
1041 * - The documentation says the lpXORbits bitmap must be in a device
1042 * dependent format. But we must still resize it and perform depth
1043 * conversions if necessary.
1044 * - I'm a bit unsure about the how the 'device dependent format' thing works.
1045 * I did some tests on windows and found that if you provide a 16bpp bitmap
1046 * in lpXORbits, then its format but be 565 RGB if the screen's bit depth
1047 * is 16bpp but it must be 555 RGB if the screen's bit depth is anything
1048 * else. I don't know if this is part of the GDI specs or if this is a
1049 * quirk of the graphics card driver.
1050 * - You may think that we check whether the bit depths match or not
1051 * as an optimization. But the truth is that the conversion using
1052 * CreateDIBitmap does not work for some bit depth (e.g. 8bpp) and I have
1053 * no idea why.
1054 * - I'm pretty sure that all the things we do in CreateIcon should
1055 * also be done in CreateIconIndirect...
1057 HICON WINAPI CreateIcon(
1058 HINSTANCE hInstance, /* [in] the application's hInstance */
1059 INT nWidth, /* [in] the width of the provided bitmaps */
1060 INT nHeight, /* [in] the height of the provided bitmaps */
1061 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1062 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1063 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1064 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1066 HICON hIcon;
1067 HDC hdc;
1069 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1070 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1072 hdc=GetDC(0);
1073 if (!hdc)
1074 return 0;
1076 if (GetDeviceCaps(hdc,BITSPIXEL)==bBitsPixel) {
1077 CURSORICONINFO info;
1079 info.ptHotSpot.x = ICON_HOTSPOT;
1080 info.ptHotSpot.y = ICON_HOTSPOT;
1081 info.nWidth = nWidth;
1082 info.nHeight = nHeight;
1083 info.nWidthBytes = 0;
1084 info.bPlanes = bPlanes;
1085 info.bBitsPerPixel = bBitsPixel;
1087 hIcon=HICON_32(CreateCursorIconIndirect16(MapHModuleLS(hInstance), &info,
1088 lpANDbits, lpXORbits));
1089 } else {
1090 ICONINFO iinfo;
1091 BITMAPINFO bmi;
1093 iinfo.fIcon=TRUE;
1094 iinfo.xHotspot=ICON_HOTSPOT;
1095 iinfo.yHotspot=ICON_HOTSPOT;
1096 iinfo.hbmMask=CreateBitmap(nWidth,nHeight,1,1,lpANDbits);
1098 bmi.bmiHeader.biSize=sizeof(bmi.bmiHeader);
1099 bmi.bmiHeader.biWidth=nWidth;
1100 bmi.bmiHeader.biHeight=-nHeight;
1101 bmi.bmiHeader.biPlanes=bPlanes;
1102 bmi.bmiHeader.biBitCount=bBitsPixel;
1103 bmi.bmiHeader.biCompression=BI_RGB;
1104 bmi.bmiHeader.biSizeImage=0;
1105 bmi.bmiHeader.biXPelsPerMeter=0;
1106 bmi.bmiHeader.biYPelsPerMeter=0;
1107 bmi.bmiHeader.biClrUsed=0;
1108 bmi.bmiHeader.biClrImportant=0;
1110 iinfo.hbmColor = CreateDIBitmap( hdc, &bmi.bmiHeader,
1111 CBM_INIT, lpXORbits,
1112 &bmi, DIB_RGB_COLORS );
1114 hIcon=CreateIconIndirect(&iinfo);
1115 DeleteObject(iinfo.hbmMask);
1116 DeleteObject(iinfo.hbmColor);
1118 ReleaseDC(0,hdc);
1119 return hIcon;
1123 /***********************************************************************
1124 * CreateCursorIconIndirect (USER.408)
1126 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1127 CURSORICONINFO *info,
1128 LPCVOID lpANDbits,
1129 LPCVOID lpXORbits )
1131 HGLOBAL16 handle;
1132 char *ptr;
1133 int sizeAnd, sizeXor;
1135 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1136 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1137 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1138 sizeXor = info->nHeight * info->nWidthBytes;
1139 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1140 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1141 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1142 return 0;
1143 FarSetOwner16( handle, hInstance );
1144 ptr = (char *)GlobalLock16( handle );
1145 memcpy( ptr, info, sizeof(*info) );
1146 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1147 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1148 GlobalUnlock16( handle );
1149 return handle;
1153 /***********************************************************************
1154 * CopyIcon (USER.368)
1156 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1158 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1159 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1163 /***********************************************************************
1164 * CopyIcon (USER32.@)
1166 HICON WINAPI CopyIcon( HICON hIcon )
1168 TRACE_(icon)("%p\n", hIcon );
1169 return CURSORICON_Copy( 0, hIcon );
1173 /***********************************************************************
1174 * CopyCursor (USER.369)
1176 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1178 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1179 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1182 /**********************************************************************
1183 * DestroyIcon32 (USER.610)
1185 * This routine is actually exported from Win95 USER under the name
1186 * DestroyIcon32 ... The behaviour implemented here should mimic
1187 * the Win95 one exactly, especially the return values, which
1188 * depend on the setting of various flags.
1190 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1192 WORD retv;
1194 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1196 /* Check whether destroying active cursor */
1198 if ( QUEUE_Current()->cursor == HICON_32(handle) )
1200 WARN_(cursor)("Destroying active cursor!\n" );
1201 SetCursor( 0 );
1204 /* Try shared cursor/icon first */
1206 if ( !(flags & CID_NONSHARED) )
1208 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1210 if ( count != -1 )
1211 return (flags & CID_WIN32)? TRUE : (count == 0);
1213 /* FIXME: OEM cursors/icons should be recognized */
1216 /* Now assume non-shared cursor/icon */
1218 retv = GlobalFree16( handle );
1219 return (flags & CID_RESOURCE)? retv : TRUE;
1222 /***********************************************************************
1223 * DestroyIcon (USER32.@)
1225 BOOL WINAPI DestroyIcon( HICON hIcon )
1227 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1231 /***********************************************************************
1232 * DestroyCursor (USER32.@)
1234 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1236 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1240 /***********************************************************************
1241 * DrawIcon (USER32.@)
1243 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1245 CURSORICONINFO *ptr;
1246 HDC hMemDC;
1247 HBITMAP hXorBits, hAndBits;
1248 COLORREF oldFg, oldBg;
1250 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
1251 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1252 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
1253 (char *)(ptr+1) );
1254 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1255 ptr->bBitsPerPixel, (char *)(ptr + 1)
1256 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
1257 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1258 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1260 if (hXorBits && hAndBits)
1262 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1263 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
1264 SelectObject( hMemDC, hXorBits );
1265 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
1266 SelectObject( hMemDC, hBitTemp );
1268 DeleteDC( hMemDC );
1269 if (hXorBits) DeleteObject( hXorBits );
1270 if (hAndBits) DeleteObject( hAndBits );
1271 GlobalUnlock16(HICON_16(hIcon));
1272 SetTextColor( hdc, oldFg );
1273 SetBkColor( hdc, oldBg );
1274 return TRUE;
1277 /***********************************************************************
1278 * DumpIcon (USER.459)
1280 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1281 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1283 CURSORICONINFO *info = MapSL( pInfo );
1284 int sizeAnd, sizeXor;
1286 if (!info) return 0;
1287 sizeXor = info->nHeight * info->nWidthBytes;
1288 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1289 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1290 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1291 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1292 return MAKELONG( sizeXor, sizeXor );
1296 /***********************************************************************
1297 * SetCursor (USER32.@)
1298 * RETURNS:
1299 * A handle to the previous cursor shape.
1301 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1303 MESSAGEQUEUE *queue = QUEUE_Current();
1304 HCURSOR hOldCursor;
1306 if (hCursor == queue->cursor) return hCursor; /* No change */
1307 TRACE_(cursor)("%p\n", hCursor );
1308 hOldCursor = queue->cursor;
1309 queue->cursor = hCursor;
1310 /* Change the cursor shape only if it is visible */
1311 if (queue->cursor_count >= 0)
1313 USER_Driver.pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
1314 GlobalUnlock16(HCURSOR_16(hCursor));
1316 return hOldCursor;
1319 /***********************************************************************
1320 * ShowCursor (USER32.@)
1322 INT WINAPI ShowCursor( BOOL bShow )
1324 MESSAGEQUEUE *queue = QUEUE_Current();
1326 TRACE_(cursor)("%d, count=%d\n", bShow, queue->cursor_count );
1328 if (bShow)
1330 if (++queue->cursor_count == 0) /* Show it */
1332 USER_Driver.pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(queue->cursor)));
1333 GlobalUnlock16(HCURSOR_16(queue->cursor));
1336 else
1338 if (--queue->cursor_count == -1) /* Hide it */
1339 USER_Driver.pSetCursor( NULL );
1341 return queue->cursor_count;
1344 /***********************************************************************
1345 * GetCursor (USER32.@)
1347 HCURSOR WINAPI GetCursor(void)
1349 return QUEUE_Current()->cursor;
1353 /***********************************************************************
1354 * ClipCursor (USER.16)
1356 BOOL16 WINAPI ClipCursor16( const RECT16 *rect )
1358 if (!rect) SetRectEmpty( &CURSOR_ClipRect );
1359 else CONV_RECT16TO32( rect, &CURSOR_ClipRect );
1360 return TRUE;
1364 /***********************************************************************
1365 * ClipCursor (USER32.@)
1367 BOOL WINAPI ClipCursor( const RECT *rect )
1369 if (!rect) SetRectEmpty( &CURSOR_ClipRect );
1370 else CopyRect( &CURSOR_ClipRect, rect );
1371 return TRUE;
1375 /***********************************************************************
1376 * GetClipCursor (USER.309)
1378 void WINAPI GetClipCursor16( RECT16 *rect )
1380 if (rect) CONV_RECT32TO16( &CURSOR_ClipRect, rect );
1384 /***********************************************************************
1385 * GetClipCursor (USER32.@)
1387 BOOL WINAPI GetClipCursor( RECT *rect )
1389 if (rect)
1391 CopyRect( rect, &CURSOR_ClipRect );
1392 return TRUE;
1394 return FALSE;
1397 /**********************************************************************
1398 * LookupIconIdFromDirectoryEx (USER.364)
1400 * FIXME: exact parameter sizes
1402 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1403 INT16 width, INT16 height, UINT16 cFlag )
1405 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1408 /**********************************************************************
1409 * LookupIconIdFromDirectoryEx (USER32.@)
1411 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1412 INT width, INT height, UINT cFlag )
1414 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1415 UINT retVal = 0;
1416 if( dir && !dir->idReserved && (dir->idType & 3) )
1418 CURSORICONDIRENTRY* entry;
1419 HDC hdc;
1420 UINT palEnts;
1421 int colors;
1422 hdc = GetDC(0);
1423 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1424 if (palEnts == 0)
1425 palEnts = 256;
1426 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1428 ReleaseDC(0, hdc);
1430 if( bIcon )
1431 entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1432 else
1433 entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1435 if( entry ) retVal = entry->wResId;
1437 else WARN_(cursor)("invalid resource directory\n");
1438 return retVal;
1441 /**********************************************************************
1442 * LookupIconIdFromDirectory (USER.?)
1444 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
1446 return LookupIconIdFromDirectoryEx16( dir, bIcon,
1447 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1448 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1451 /**********************************************************************
1452 * LookupIconIdFromDirectory (USER32.@)
1454 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1456 return LookupIconIdFromDirectoryEx( dir, bIcon,
1457 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1458 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1461 /**********************************************************************
1462 * GetIconID (USER.455)
1464 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1466 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
1468 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1469 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1471 switch(resType)
1473 case RT_CURSOR16:
1474 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1475 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1476 case RT_ICON16:
1477 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1478 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1479 default:
1480 WARN_(cursor)("invalid res type %ld\n", resType );
1482 return 0;
1485 /**********************************************************************
1486 * LoadCursorIconHandler (USER.336)
1488 * Supposed to load resources of Windows 2.x applications.
1490 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1492 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1493 hResource, hModule, hRsrc);
1494 return (HGLOBAL16)0;
1497 /**********************************************************************
1498 * LoadDIBIconHandler (USER.357)
1500 * RT_ICON resource loader, installed by USER_SignalProc when module
1501 * is initialized.
1503 HGLOBAL16 WINAPI LoadDIBIconHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1505 /* If hResource is zero we must allocate a new memory block, if it's
1506 * non-zero but GlobalLock() returns NULL then it was discarded and
1507 * we have to recommit some memory, otherwise we just need to check
1508 * the block size. See LoadProc() in 16-bit SDK for more.
1511 hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
1512 if( hMemObj )
1514 LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1515 hMemObj = HICON_16(CURSORICON_CreateFromResource(
1516 hModule, hMemObj, bits,
1517 SizeofResource16(hModule, hRsrc), TRUE, 0x00030000,
1518 GetSystemMetrics(SM_CXICON),
1519 GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR));
1521 return hMemObj;
1524 /**********************************************************************
1525 * LoadDIBCursorHandler (USER.356)
1527 * RT_CURSOR resource loader. Same as above.
1529 HGLOBAL16 WINAPI LoadDIBCursorHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1531 hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
1532 if( hMemObj )
1534 LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1535 hMemObj = HICON_16(CURSORICON_CreateFromResource(
1536 hModule, hMemObj, bits,
1537 SizeofResource16(hModule, hRsrc), FALSE, 0x00030000,
1538 GetSystemMetrics(SM_CXCURSOR),
1539 GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME));
1541 return hMemObj;
1544 /**********************************************************************
1545 * LoadIconHandler (USER.456)
1547 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
1549 LPBYTE bits = (LPBYTE)LockResource16( hResource );
1551 TRACE_(cursor)("hRes=%04x\n",hResource);
1553 return HICON_16(CURSORICON_CreateFromResource(0, 0, bits, 0, TRUE,
1554 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
1557 /***********************************************************************
1558 * LoadCursorW (USER32.@)
1560 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1562 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1563 LR_SHARED | LR_DEFAULTSIZE );
1566 /***********************************************************************
1567 * LoadCursorA (USER32.@)
1569 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1571 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1572 LR_SHARED | LR_DEFAULTSIZE );
1575 /***********************************************************************
1576 * LoadCursorFromFileW (USER32.@)
1578 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1580 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1581 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1584 /***********************************************************************
1585 * LoadCursorFromFileA (USER32.@)
1587 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1589 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1590 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1593 /***********************************************************************
1594 * LoadIconW (USER32.@)
1596 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1598 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1599 LR_SHARED | LR_DEFAULTSIZE );
1602 /***********************************************************************
1603 * LoadIconA (USER32.@)
1605 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1607 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1608 LR_SHARED | LR_DEFAULTSIZE );
1611 /**********************************************************************
1612 * GetIconInfo (USER32.@)
1614 BOOL WINAPI GetIconInfo(HICON hIcon,PICONINFO iconinfo) {
1615 CURSORICONINFO *ciconinfo;
1617 ciconinfo = GlobalLock16(HICON_16(hIcon));
1618 if (!ciconinfo)
1619 return FALSE;
1621 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1622 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1624 iconinfo->fIcon = TRUE;
1625 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1626 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1628 else
1630 iconinfo->fIcon = FALSE;
1631 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1632 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1635 iconinfo->hbmColor = CreateBitmap ( ciconinfo->nWidth, ciconinfo->nHeight,
1636 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1637 (char *)(ciconinfo + 1)
1638 + ciconinfo->nHeight *
1639 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1640 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, ciconinfo->nHeight,
1641 1, 1, (char *)(ciconinfo + 1));
1643 GlobalUnlock16(HICON_16(hIcon));
1645 return TRUE;
1648 /**********************************************************************
1649 * CreateIconIndirect (USER32.@)
1651 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1653 BITMAP bmpXor,bmpAnd;
1654 HICON16 hObj;
1655 int sizeXor,sizeAnd;
1657 GetObjectA( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1658 GetObjectA( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1660 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1661 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1663 hObj = GlobalAlloc16( GMEM_MOVEABLE,
1664 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1665 if (hObj)
1667 CURSORICONINFO *info;
1669 info = (CURSORICONINFO *)GlobalLock16( hObj );
1671 /* If we are creating an icon, the hotspot is unused */
1672 if (iconinfo->fIcon)
1674 info->ptHotSpot.x = ICON_HOTSPOT;
1675 info->ptHotSpot.y = ICON_HOTSPOT;
1677 else
1679 info->ptHotSpot.x = iconinfo->xHotspot;
1680 info->ptHotSpot.y = iconinfo->yHotspot;
1683 info->nWidth = bmpXor.bmWidth;
1684 info->nHeight = bmpXor.bmHeight;
1685 info->nWidthBytes = bmpXor.bmWidthBytes;
1686 info->bPlanes = bmpXor.bmPlanes;
1687 info->bBitsPerPixel = bmpXor.bmBitsPixel;
1689 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1691 GetBitmapBits( iconinfo->hbmMask ,sizeAnd,(char*)(info + 1) );
1692 GetBitmapBits( iconinfo->hbmColor,sizeXor,(char*)(info + 1) +sizeAnd);
1693 GlobalUnlock16( hObj );
1695 return HICON_32(hObj);
1698 /******************************************************************************
1699 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1701 * NOTES
1702 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1704 * PARAMS
1705 * hdc [I] Handle to device context
1706 * x0 [I] X coordinate of upper left corner
1707 * y0 [I] Y coordinate of upper left corner
1708 * hIcon [I] Handle to icon to draw
1709 * cxWidth [I] Width of icon
1710 * cyWidth [I] Height of icon
1711 * istep [I] Index of frame in animated cursor
1712 * hbr [I] Handle to background brush
1713 * flags [I] Icon-drawing flags
1715 * RETURNS
1716 * Success: TRUE
1717 * Failure: FALSE
1719 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1720 INT cxWidth, INT cyWidth, UINT istep,
1721 HBRUSH hbr, UINT flags )
1723 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
1724 HDC hDC_off = 0, hMemDC;
1725 BOOL result = FALSE, DoOffscreen;
1726 HBITMAP hB_off = 0, hOld = 0;
1728 if (!ptr) return FALSE;
1729 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1730 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1732 hMemDC = CreateCompatibleDC (hdc);
1733 if (istep)
1734 FIXME_(icon)("Ignoring istep=%d\n", istep);
1735 if (flags & DI_COMPAT)
1736 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
1738 if (!flags) {
1739 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
1740 flags = DI_NORMAL;
1743 /* Calculate the size of the destination image. */
1744 if (cxWidth == 0)
1746 if (flags & DI_DEFAULTSIZE)
1747 cxWidth = GetSystemMetrics (SM_CXICON);
1748 else
1749 cxWidth = ptr->nWidth;
1751 if (cyWidth == 0)
1753 if (flags & DI_DEFAULTSIZE)
1754 cyWidth = GetSystemMetrics (SM_CYICON);
1755 else
1756 cyWidth = ptr->nHeight;
1759 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1761 if (DoOffscreen) {
1762 RECT r;
1764 r.left = 0;
1765 r.top = 0;
1766 r.right = cxWidth;
1767 r.bottom = cxWidth;
1769 hDC_off = CreateCompatibleDC(hdc);
1770 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
1771 if (hDC_off && hB_off) {
1772 hOld = SelectObject(hDC_off, hB_off);
1773 FillRect(hDC_off, &r, hbr);
1777 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
1779 HBITMAP hXorBits, hAndBits;
1780 COLORREF oldFg, oldBg;
1781 INT nStretchMode;
1783 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
1785 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1786 ptr->bPlanes, ptr->bBitsPerPixel,
1787 (char *)(ptr + 1)
1788 + ptr->nHeight *
1789 get_bitmap_width_bytes(ptr->nWidth,1) );
1790 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1791 1, 1, (char *)(ptr+1) );
1792 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1793 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1795 if (hXorBits && hAndBits)
1797 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1798 if (flags & DI_MASK)
1800 if (DoOffscreen)
1801 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1802 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1803 else
1804 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1805 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1807 SelectObject( hMemDC, hXorBits );
1808 if (flags & DI_IMAGE)
1810 if (DoOffscreen)
1811 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1812 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1813 else
1814 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1815 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1817 SelectObject( hMemDC, hBitTemp );
1818 result = TRUE;
1821 SetTextColor( hdc, oldFg );
1822 SetBkColor( hdc, oldBg );
1823 if (hXorBits) DeleteObject( hXorBits );
1824 if (hAndBits) DeleteObject( hAndBits );
1825 SetStretchBltMode (hdc, nStretchMode);
1826 if (DoOffscreen) {
1827 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
1828 SelectObject(hDC_off, hOld);
1831 if (hMemDC) DeleteDC( hMemDC );
1832 if (hDC_off) DeleteDC(hDC_off);
1833 if (hB_off) DeleteObject(hB_off);
1834 GlobalUnlock16(HICON_16(hIcon));
1835 return result;
1838 /***********************************************************************
1839 * DIB_FixColorsToLoadflags
1841 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
1842 * are in loadflags
1844 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
1846 int colors;
1847 COLORREF c_W, c_S, c_F, c_L, c_C;
1848 int incr,i;
1849 RGBQUAD *ptr;
1851 if (bmi->bmiHeader.biBitCount > 8) return;
1852 if (bmi->bmiHeader.biSize == sizeof(BITMAPINFOHEADER)) incr = 4;
1853 else if (bmi->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) incr = 3;
1854 else {
1855 WARN_(resource)("Wrong bitmap header size!\n");
1856 return;
1858 colors = bmi->bmiHeader.biClrUsed;
1859 if (!colors && (bmi->bmiHeader.biBitCount <= 8))
1860 colors = 1 << bmi->bmiHeader.biBitCount;
1861 c_W = GetSysColor(COLOR_WINDOW);
1862 c_S = GetSysColor(COLOR_3DSHADOW);
1863 c_F = GetSysColor(COLOR_3DFACE);
1864 c_L = GetSysColor(COLOR_3DLIGHT);
1865 if (loadflags & LR_LOADTRANSPARENT) {
1866 switch (bmi->bmiHeader.biBitCount) {
1867 case 1: pix = pix >> 7; break;
1868 case 4: pix = pix >> 4; break;
1869 case 8: break;
1870 default:
1871 WARN_(resource)("(%d): Unsupported depth\n", bmi->bmiHeader.biBitCount);
1872 return;
1874 if (pix >= colors) {
1875 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
1876 return;
1878 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
1879 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
1880 ptr->rgbBlue = GetBValue(c_W);
1881 ptr->rgbGreen = GetGValue(c_W);
1882 ptr->rgbRed = GetRValue(c_W);
1884 if (loadflags & LR_LOADMAP3DCOLORS)
1885 for (i=0; i<colors; i++) {
1886 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
1887 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
1888 if (c_C == RGB(128, 128, 128)) {
1889 ptr->rgbRed = GetRValue(c_S);
1890 ptr->rgbGreen = GetGValue(c_S);
1891 ptr->rgbBlue = GetBValue(c_S);
1892 } else if (c_C == RGB(192, 192, 192)) {
1893 ptr->rgbRed = GetRValue(c_F);
1894 ptr->rgbGreen = GetGValue(c_F);
1895 ptr->rgbBlue = GetBValue(c_F);
1896 } else if (c_C == RGB(223, 223, 223)) {
1897 ptr->rgbRed = GetRValue(c_L);
1898 ptr->rgbGreen = GetGValue(c_L);
1899 ptr->rgbBlue = GetBValue(c_L);
1905 /**********************************************************************
1906 * BITMAP_Load
1908 static HBITMAP BITMAP_Load( HINSTANCE instance,LPCWSTR name, UINT loadflags )
1910 HBITMAP hbitmap = 0;
1911 HRSRC hRsrc;
1912 HGLOBAL handle;
1913 char *ptr = NULL;
1914 BITMAPINFO *info, *fix_info=NULL;
1915 HGLOBAL hFix;
1916 int size;
1918 if (!(loadflags & LR_LOADFROMFILE))
1920 if (!instance)
1922 /* OEM bitmap: try to load the resource from user32.dll */
1923 if (HIWORD(name)) return 0;
1924 if (!(instance = GetModuleHandleA("user32.dll"))) return 0;
1926 if (!(hRsrc = FindResourceW( instance, name, RT_BITMAPW ))) return 0;
1927 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
1929 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
1931 else
1933 if (!(ptr = map_fileW( name ))) return 0;
1934 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
1936 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
1937 if ((hFix = GlobalAlloc(0, size))) fix_info=GlobalLock(hFix);
1938 if (fix_info) {
1939 BYTE pix;
1941 memcpy(fix_info, info, size);
1942 pix = *((LPBYTE)info+DIB_BitmapInfoSize(info, DIB_RGB_COLORS));
1943 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
1944 if (!screen_dc) screen_dc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
1945 if (screen_dc)
1947 char *bits = (char *)info + size;
1948 if (loadflags & LR_CREATEDIBSECTION) {
1949 DIBSECTION dib;
1950 hbitmap = CreateDIBSection(screen_dc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
1951 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
1952 SetDIBits(screen_dc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
1953 DIB_RGB_COLORS);
1955 else {
1956 hbitmap = CreateDIBitmap( screen_dc, &fix_info->bmiHeader, CBM_INIT,
1957 bits, fix_info, DIB_RGB_COLORS );
1960 GlobalUnlock(hFix);
1961 GlobalFree(hFix);
1963 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
1964 return hbitmap;
1967 /**********************************************************************
1968 * LoadImageA (USER32.@)
1970 * FIXME: implementation lacks some features, see LR_ defines in winuser.h
1973 /* filter for page-fault exceptions */
1974 static WINE_EXCEPTION_FILTER(page_fault)
1976 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
1977 return EXCEPTION_EXECUTE_HANDLER;
1978 return EXCEPTION_CONTINUE_SEARCH;
1981 /*********************************************************************/
1983 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
1984 INT desiredx, INT desiredy, UINT loadflags)
1986 HANDLE res;
1987 LPWSTR u_name;
1989 if (!HIWORD(name))
1990 return LoadImageW(hinst, (LPWSTR)name, type, desiredx, desiredy, loadflags);
1992 __TRY {
1993 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1994 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1995 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
1997 __EXCEPT(page_fault) {
1998 SetLastError( ERROR_INVALID_PARAMETER );
1999 return 0;
2001 __ENDTRY
2002 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2003 HeapFree(GetProcessHeap(), 0, u_name);
2004 return res;
2008 /******************************************************************************
2009 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2011 * PARAMS
2012 * hinst [I] Handle of instance that contains image
2013 * name [I] Name of image
2014 * type [I] Type of image
2015 * desiredx [I] Desired width
2016 * desiredy [I] Desired height
2017 * loadflags [I] Load flags
2019 * RETURNS
2020 * Success: Handle to newly loaded image
2021 * Failure: NULL
2023 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2025 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2026 INT desiredx, INT desiredy, UINT loadflags )
2028 if (HIWORD(name)) {
2029 TRACE_(resource)("(%p,%p,%d,%d,%d,0x%08x)\n",
2030 hinst,name,type,desiredx,desiredy,loadflags);
2031 } else {
2032 TRACE_(resource)("(%p,%p,%d,%d,%d,0x%08x)\n",
2033 hinst,name,type,desiredx,desiredy,loadflags);
2035 if (loadflags & LR_DEFAULTSIZE) {
2036 if (type == IMAGE_ICON) {
2037 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2038 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2039 } else if (type == IMAGE_CURSOR) {
2040 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2041 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2044 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2045 switch (type) {
2046 case IMAGE_BITMAP:
2047 return BITMAP_Load( hinst, name, loadflags );
2049 case IMAGE_ICON:
2050 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2051 if (screen_dc)
2053 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2054 if (palEnts == 0) palEnts = 256;
2055 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2056 palEnts, FALSE, loadflags);
2058 break;
2060 case IMAGE_CURSOR:
2061 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2062 1, TRUE, loadflags);
2064 return 0;
2067 /******************************************************************************
2068 * CopyImage (USER32.@) Creates new image and copies attributes to it
2070 * PARAMS
2071 * hnd [I] Handle to image to copy
2072 * type [I] Type of image to copy
2073 * desiredx [I] Desired width of new image
2074 * desiredy [I] Desired height of new image
2075 * flags [I] Copy flags
2077 * RETURNS
2078 * Success: Handle to newly created image
2079 * Failure: NULL
2081 * FIXME: implementation still lacks nearly all features, see LR_*
2082 * defines in winuser.h
2084 HICON WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2085 INT desiredy, UINT flags )
2087 switch (type)
2089 case IMAGE_BITMAP:
2091 HBITMAP res;
2092 BITMAP bm;
2094 if (!GetObjectW( hnd, sizeof(bm), &bm )) return 0;
2095 bm.bmBits = NULL;
2096 if ((res = CreateBitmapIndirect(&bm)))
2098 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight );
2099 GetBitmapBits( hnd, bm.bmWidthBytes * bm.bmHeight, buf );
2100 SetBitmapBits( res, bm.bmWidthBytes * bm.bmHeight, buf );
2101 HeapFree( GetProcessHeap(), 0, buf );
2103 return (HICON)res;
2105 case IMAGE_ICON:
2106 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2107 case IMAGE_CURSOR:
2108 /* Should call CURSORICON_ExtCopy but more testing
2109 * needs to be done before we change this
2111 return CopyCursor(hnd);
2113 return 0;
2117 /******************************************************************************
2118 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2120 * RETURNS
2121 * Success: Handle to specified bitmap
2122 * Failure: NULL
2124 HBITMAP WINAPI LoadBitmapW(
2125 HINSTANCE instance, /* [in] Handle to application instance */
2126 LPCWSTR name) /* [in] Address of bitmap resource name */
2128 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2131 /**********************************************************************
2132 * LoadBitmapA (USER32.@)
2134 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2136 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );