Release 20021031.
[wine/multimedia.git] / windows / cursoricon.c
blob67d887c86d57149ac70baef596e4efb08a9a56fb
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 "queue.h"
61 #include "input.h"
62 #include "message.h"
63 #include "winerror.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 */
73 static HDC screen_dc;
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( HMODULE hModule )
266 ICONCACHE **ptr = &IconAnchor;
268 if ( HIWORD( hModule ) )
269 hModule = MapHModuleLS( hModule );
270 else
271 hModule = GetExePtr( hModule );
273 EnterCriticalSection( &IconCrst );
275 while ( *ptr )
277 if ( (*ptr)->hModule == hModule )
279 ICONCACHE *freePtr = *ptr;
280 *ptr = freePtr->next;
282 GlobalFree16(HICON_16(freePtr->hIcon));
283 HeapFree( GetProcessHeap(), 0, freePtr );
284 continue;
286 ptr = &(*ptr)->next;
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 )
300 int i;
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" );
308 return NULL;
310 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
312 /* Find Best Fit */
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))
322 iXDiff = iTempXDiff;
323 iYDiff = 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)
337 bestEntry = entry;
338 iColorDiff = iTempColorDiff;
343 return bestEntry;
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
352 * ignored too
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" );
363 return NULL;
365 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
367 /* Double height to account for AND and XOR masks */
369 height *= 2;
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))
379 bestEntry = entry;
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))
392 bestEntry = entry;
393 maxwidth = entry->ResInfo.cursor.wWidth;
394 maxheight = entry->ResInfo.cursor.wHeight;
397 return bestEntry;
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
404 * fields
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)
413 LPBYTE _free;
414 CURSORICONFILEDIR *bits;
415 int entries, size, i;
417 *res = NULL;
418 *ptr = NULL;
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);
428 for (;;)
429 { if (*(LPDWORD)pos==0x6e6f6369) /* "icon" */
430 { FIXME_(cursor)("icon entry found! %p\n", bits);
431 pos+=4;
432 if ( !*(LPWORD) pos==0x2fe) /* iconsize */
433 { goto fail;
435 bits=(CURSORICONFILEDIR*)(pos+4);
436 FIXME_(cursor)("icon size ok. offset=%p \n", bits);
437 break;
439 pos+=2;
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;
459 if (fCursor) {
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);
465 } else {
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 );
481 return TRUE;
482 fail:
483 if (*res) HeapFree( GetProcessHeap(), 0, *res );
484 if (*ptr) HeapFree( GetProcessHeap(), 0, *ptr );
485 UnmapViewOfFile( bits );
486 return FALSE;
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 )
502 static HDC hdcMem;
503 int sizeAnd, sizeXor;
504 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
505 BITMAP bmpXor, bmpAnd;
506 POINT16 hotspot;
507 BITMAPINFO *bmi;
508 BOOL DoStretch;
509 INT size;
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");
520 return 0;
523 if (bIcon)
524 bmi = (BITMAPINFO *)bits;
525 else /* get the hotspot */
527 POINT16 *pt = (POINT16 *)bits;
528 hotspot = *pt;
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");
545 return 0;
548 if (!screen_dc) screen_dc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
549 if (screen_dc)
551 BITMAPINFO* pInfo;
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 */
572 if (DoStretch) {
573 if(bIcon)
575 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
577 else
579 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
581 if(hXorBits)
583 HBITMAP hOld;
584 BOOL res = FALSE;
586 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
587 if (hdcMem) {
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 );
598 if( hXorBits )
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;
615 else
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 */
625 if (DoStretch) {
626 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
627 HBITMAP hOld;
628 BOOL res = FALSE;
630 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
631 if (hdcMem) {
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");
652 return 0;
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 );
665 if (hObj)
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,
711 UINT cFlag )
713 return CURSORICON_CreateFromResource( 0, 0, bits, cbSize, bIcon, dwVersion,
714 width, height, cFlag );
717 /**********************************************************************
718 * CURSORICON_Load
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)
726 HANDLE handle = 0;
727 HICON hIcon = 0;
728 HRSRC hRsrc;
729 CURSORICONDIR *dir;
730 CURSORICONDIRENTRY *dirEntry;
731 LPBYTE bits;
733 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
735 LPBYTE *ptr;
736 if (!CURSORICON_SimulateLoadingFromResourceW((LPWSTR)name, fCursor, &dir, &ptr))
737 return 0;
738 if (fCursor)
739 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(dir, width, height, 1);
740 else
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 */
750 HRSRC hGroupRsrc;
751 WORD wResId;
752 DWORD dwBytesInRes;
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 );
763 else
764 hInstance = GetExePtr( hInstance );
766 /* Get directory resource ID */
768 if (!(hRsrc = FindResourceW( hInstance, name,
769 fCursor ? RT_GROUP_CURSORW : RT_GROUP_ICONW )))
770 return 0;
771 hGroupRsrc = hRsrc;
773 /* Find the best entry in the directory */
775 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
776 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
777 if (fCursor)
778 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
779 width, height, 1);
780 else
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 )
796 return hIcon;
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 );
810 return hIcon;
813 /***********************************************************************
814 * CURSORICON_Copy
816 * Make a copy of a cursor or icon.
818 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
820 char *ptrOld, *ptrNew;
821 int size;
822 HICON16 hOld = HICON_16(hIcon);
823 HICON16 hNew;
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 /*************************************************************************
838 * CURSORICON_ExtCopy
840 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
842 * PARAMS
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
849 * RETURNS
850 * Success: The new handle of the Image
852 * NOTES
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,
862 UINT nFlags)
864 HICON hNew=0;
866 TRACE_(icon)("hIcon %u, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
867 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
869 if(hIcon == 0)
871 return 0;
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");
891 else
893 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
894 LPBYTE pBits;
895 HANDLE hMem;
896 HRSRC hRsrc;
897 DWORD dwBytesInRes;
898 WORD wResId;
899 CURSORICONDIR *pDir;
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)))
919 return 0;
921 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
923 return 0;
926 /* Find Best Fit
928 if(bIsIcon)
930 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
931 pDir, iDesiredCX, iDesiredCY, 256);
933 else
935 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
936 pDir, iDesiredCX, iDesiredCY, 1);
939 wResId = pDirEntry->wResId;
940 dwBytesInRes = pDirEntry->dwBytesInRes;
941 FreeResource(hMem);
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);
947 /* Get the Best Fit
949 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
950 MAKEINTRESOURCEW(wResId), bIsIcon ? RT_ICONW : RT_CURSORW)))
952 return 0;
954 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
956 return 0;
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);
971 FreeResource(hMem);
974 else hNew = CURSORICON_Copy(0, hIcon);
975 return hNew;
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 )
987 CURSORICONINFO info;
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;
997 info.bPlanes = 1;
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
1040 * format!
1042 * BUGS
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
1057 * no idea why.
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 */
1070 HICON hIcon;
1071 HDC hdc;
1073 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1074 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1076 hdc=GetDC(0);
1077 if (!hdc)
1078 return 0;
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));
1093 } else {
1094 ICONINFO iinfo;
1095 BITMAPINFO bmi;
1097 iinfo.fIcon=TRUE;
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);
1122 ReleaseDC(0,hdc);
1123 return hIcon;
1127 /***********************************************************************
1128 * CreateCursorIconIndirect (USER.408)
1130 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1131 CURSORICONINFO *info,
1132 LPCVOID lpANDbits,
1133 LPCVOID lpXORbits )
1135 HGLOBAL16 handle;
1136 char *ptr;
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)))
1146 return 0;
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 );
1153 return 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 )
1196 WORD retv;
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" );
1205 SetCursor( 0 );
1208 /* Try shared cursor/icon first */
1210 if ( !(flags & CID_NONSHARED) )
1212 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1214 if ( count != -1 )
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;
1250 HDC hMemDC;
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,
1257 (char *)(ptr+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 );
1272 DeleteDC( hMemDC );
1273 if (hXorBits) DeleteObject( hXorBits );
1274 if (hAndBits) DeleteObject( hAndBits );
1275 GlobalUnlock16(HICON_16(hIcon));
1276 SetTextColor( hdc, oldFg );
1277 SetBkColor( hdc, oldBg );
1278 return TRUE;
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.@)
1302 * RETURNS:
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();
1308 HCURSOR hOldCursor;
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));
1320 return hOldCursor;
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 );
1332 if (bShow)
1334 if (++queue->cursor_count == 0) /* Show it */
1336 USER_Driver.pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(queue->cursor)));
1337 GlobalUnlock16(HCURSOR_16(queue->cursor));
1340 else
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 );
1364 return TRUE;
1368 /***********************************************************************
1369 * ClipCursor (USER32.@)
1371 BOOL WINAPI ClipCursor( const RECT *rect )
1373 if (!rect) SetRectEmpty( &CURSOR_ClipRect );
1374 else CopyRect( &CURSOR_ClipRect, rect );
1375 return TRUE;
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 )
1393 if (rect)
1395 CopyRect( rect, &CURSOR_ClipRect );
1396 return TRUE;
1398 return FALSE;
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;
1410 UINT16 retVal = 0;
1411 if( dir && !dir->idReserved && (dir->idType & 3) )
1413 CURSORICONDIRENTRY* entry;
1414 HDC hdc;
1415 UINT palEnts;
1416 int colors;
1417 hdc = GetDC(0);
1418 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1419 if (palEnts == 0)
1420 palEnts = 256;
1421 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1423 ReleaseDC(0, hdc);
1425 if( bIcon )
1426 entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1427 else
1428 entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1430 if( entry ) retVal = entry->wResId;
1432 else WARN_(cursor)("invalid resource directory\n");
1433 return retVal;
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);
1475 switch(resType)
1477 case RT_CURSOR16:
1478 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1479 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1480 case RT_ICON16:
1481 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1482 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1483 default:
1484 WARN_(cursor)("invalid res type %ld\n", resType );
1486 return 0;
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
1505 * is initialized.
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 );
1516 if( hMemObj )
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));
1525 return hMemObj;
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 );
1536 if( hMemObj )
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));
1545 return hMemObj;
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));
1622 if (!ciconinfo)
1623 return FALSE;
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;
1632 else
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));
1649 return TRUE;
1652 /**********************************************************************
1653 * CreateIconIndirect (USER32.@)
1655 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1657 BITMAP bmpXor,bmpAnd;
1658 HICON16 hObj;
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 );
1669 if (hObj)
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;
1681 else
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
1705 * NOTES
1706 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1708 * PARAMS
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
1719 * RETURNS
1720 * Success: TRUE
1721 * Failure: FALSE
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
1737 if (istep)
1738 FIXME_(icon)("Ignoring istep=%d\n", istep);
1739 if (flags & DI_COMPAT)
1740 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
1742 if (!flags) {
1743 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
1744 flags = DI_NORMAL;
1747 /* Calculate the size of the destination image. */
1748 if (cxWidth == 0)
1750 if (flags & DI_DEFAULTSIZE)
1751 cxWidth = GetSystemMetrics (SM_CXICON);
1752 else
1753 cxWidth = ptr->nWidth;
1755 if (cyWidth == 0)
1757 if (flags & DI_DEFAULTSIZE)
1758 cyWidth = GetSystemMetrics (SM_CYICON);
1759 else
1760 cyWidth = ptr->nHeight;
1763 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1765 if (DoOffscreen) {
1766 RECT r;
1768 r.left = 0;
1769 r.top = 0;
1770 r.right = cxWidth;
1771 r.bottom = cxWidth;
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;
1785 INT nStretchMode;
1787 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
1789 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1790 ptr->bPlanes, ptr->bBitsPerPixel,
1791 (char *)(ptr + 1)
1792 + ptr->nHeight *
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)
1804 if (DoOffscreen)
1805 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1806 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1807 else
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)
1814 if (DoOffscreen)
1815 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1816 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1817 else
1818 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1819 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1821 SelectObject( hMemDC, hBitTemp );
1822 result = TRUE;
1825 SetTextColor( hdc, oldFg );
1826 SetBkColor( hdc, oldBg );
1827 if (hXorBits) DeleteObject( hXorBits );
1828 if (hAndBits) DeleteObject( hAndBits );
1829 SetStretchBltMode (hdc, nStretchMode);
1830 if (DoOffscreen) {
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));
1839 return result;
1842 /***********************************************************************
1843 * DIB_FixColorsToLoadflags
1845 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
1846 * are in loadflags
1848 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
1850 int colors;
1851 COLORREF c_W, c_S, c_F, c_L, c_C;
1852 int incr,i;
1853 RGBQUAD *ptr;
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;
1858 else {
1859 WARN_(resource)("Wrong bitmap header size!\n");
1860 return;
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;
1873 case 8: break;
1874 default:
1875 WARN_(resource)("(%d): Unsupported depth\n", bmi->bmiHeader.biBitCount);
1876 return;
1878 if (pix >= colors) {
1879 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
1880 return;
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 /**********************************************************************
1910 * BITMAP_Load
1912 static HBITMAP BITMAP_Load( HINSTANCE instance,LPCWSTR name, UINT loadflags )
1914 HBITMAP hbitmap = 0;
1915 HRSRC hRsrc;
1916 HGLOBAL handle;
1917 char *ptr = NULL;
1918 BITMAPINFO *info, *fix_info=NULL;
1919 HGLOBAL hFix;
1920 int size;
1922 if (!(loadflags & LR_LOADFROMFILE))
1924 if (!instance)
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;
1935 else
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);
1942 if (fix_info) {
1943 BYTE pix;
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 );
1949 if (screen_dc)
1951 char *bits = (char *)info + size;
1952 if (loadflags & LR_CREATEDIBSECTION) {
1953 DIBSECTION dib;
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,
1957 DIB_RGB_COLORS);
1959 else {
1960 hbitmap = CreateDIBitmap( screen_dc, &fix_info->bmiHeader, CBM_INIT,
1961 bits, fix_info, DIB_RGB_COLORS );
1964 GlobalUnlock(hFix);
1965 GlobalFree(hFix);
1967 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
1968 return hbitmap;
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)
1990 HANDLE res;
1991 LPWSTR u_name;
1993 if (!HIWORD(name))
1994 return LoadImageW(hinst, (LPWSTR)name, type, desiredx, desiredy, loadflags);
1996 __TRY {
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 );
2003 return 0;
2005 __ENDTRY
2006 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2007 HeapFree(GetProcessHeap(), 0, u_name);
2008 return res;
2012 /******************************************************************************
2013 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2015 * PARAMS
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
2023 * RETURNS
2024 * Success: Handle to newly loaded image
2025 * Failure: NULL
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 )
2032 if (HIWORD(name)) {
2033 TRACE_(resource)("(0x%04x,%p,%d,%d,%d,0x%08x)\n",
2034 hinst,name,type,desiredx,desiredy,loadflags);
2035 } else {
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;
2049 switch (type) {
2050 case IMAGE_BITMAP:
2051 return BITMAP_Load( hinst, name, loadflags );
2053 case IMAGE_ICON:
2054 if (!screen_dc) screen_dc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2055 if (screen_dc)
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);
2062 break;
2064 case IMAGE_CURSOR:
2065 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2066 1, TRUE, loadflags);
2068 return 0;
2071 /******************************************************************************
2072 * CopyImage (USER32.@) Creates new image and copies attributes to it
2074 * PARAMS
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
2081 * RETURNS
2082 * Success: Handle to newly created image
2083 * Failure: NULL
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 )
2091 switch (type)
2093 case IMAGE_BITMAP:
2095 HBITMAP res;
2096 BITMAP bm;
2098 if (!GetObjectW( hnd, sizeof(bm), &bm )) return 0;
2099 bm.bmBits = NULL;
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 );
2107 return (HICON)res;
2109 case IMAGE_ICON:
2110 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2111 case IMAGE_CURSOR:
2112 /* Should call CURSORICON_ExtCopy but more testing
2113 * needs to be done before we change this
2115 return CopyCursor(hnd);
2117 return 0;
2121 /******************************************************************************
2122 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2124 * RETURNS
2125 * Success: Handle to specified bitmap
2126 * Failure: NULL
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 );