X11DRV_DrawArc: Don't overwrite the ENDCAP style.
[wine/multimedia.git] / windows / cursoricon.c
blobe6e94c67e286f18f87c38f579abfdcadaf901f99
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 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
77 /**********************************************************************
78 * ICONCACHE for cursors/icons loaded with LR_SHARED.
80 * FIXME: This should not be allocated on the system heap, but on a
81 * subsystem-global heap (i.e. one for all Win16 processes,
82 * and one for each Win32 process).
84 typedef struct tagICONCACHE
86 struct tagICONCACHE *next;
88 HMODULE hModule;
89 HRSRC hRsrc;
90 HRSRC hGroupRsrc;
91 HICON hIcon;
93 INT count;
95 } ICONCACHE;
97 static ICONCACHE *IconAnchor = NULL;
98 static CRITICAL_SECTION IconCrst = CRITICAL_SECTION_INIT("IconCrst");
99 static WORD ICON_HOTSPOT = 0x4242;
102 /***********************************************************************
103 * map_fileW
105 * Helper function to map a file to memory:
106 * name - file name
107 * [RETURN] ptr - pointer to mapped file
109 static void *map_fileW( LPCWSTR name )
111 HANDLE hFile, hMapping;
112 LPVOID ptr = NULL;
114 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
115 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
116 if (hFile != INVALID_HANDLE_VALUE)
118 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
119 CloseHandle( hFile );
120 if (hMapping)
122 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
123 CloseHandle( hMapping );
126 return ptr;
130 /***********************************************************************
131 * get_bitmap_width_bytes
133 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
134 * data.
136 static int get_bitmap_width_bytes( int width, int bpp )
138 switch(bpp)
140 case 1:
141 return 2 * ((width+15) / 16);
142 case 4:
143 return 2 * ((width+3) / 4);
144 case 24:
145 width *= 3;
146 /* fall through */
147 case 8:
148 return width + (width & 1);
149 case 16:
150 case 15:
151 return width * 2;
152 case 32:
153 return width * 4;
154 default:
155 WARN("Unknown depth %d, please report.\n", bpp );
157 return -1;
161 /**********************************************************************
162 * CURSORICON_FindSharedIcon
164 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
166 HICON hIcon = 0;
167 ICONCACHE *ptr;
169 EnterCriticalSection( &IconCrst );
171 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
172 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
174 ptr->count++;
175 hIcon = ptr->hIcon;
176 break;
179 LeaveCriticalSection( &IconCrst );
181 return hIcon;
184 /*************************************************************************
185 * CURSORICON_FindCache
187 * Given a handle, find the corresponding cache element
189 * PARAMS
190 * Handle [I] handle to an Image
192 * RETURNS
193 * Success: The cache entry
194 * Failure: NULL
197 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
199 ICONCACHE *ptr;
200 ICONCACHE *pRet=NULL;
201 BOOL IsFound = FALSE;
202 int count;
204 EnterCriticalSection( &IconCrst );
206 for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
208 if ( hIcon == ptr->hIcon )
210 IsFound = TRUE;
211 pRet = ptr;
215 LeaveCriticalSection( &IconCrst );
217 return pRet;
220 /**********************************************************************
221 * CURSORICON_AddSharedIcon
223 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
225 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
226 if ( !ptr ) return;
228 ptr->hModule = hModule;
229 ptr->hRsrc = hRsrc;
230 ptr->hIcon = hIcon;
231 ptr->hGroupRsrc = hGroupRsrc;
232 ptr->count = 1;
234 EnterCriticalSection( &IconCrst );
235 ptr->next = IconAnchor;
236 IconAnchor = ptr;
237 LeaveCriticalSection( &IconCrst );
240 /**********************************************************************
241 * CURSORICON_DelSharedIcon
243 static INT CURSORICON_DelSharedIcon( HICON hIcon )
245 INT count = -1;
246 ICONCACHE *ptr;
248 EnterCriticalSection( &IconCrst );
250 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
251 if ( ptr->hIcon == hIcon )
253 if ( ptr->count > 0 ) ptr->count--;
254 count = ptr->count;
255 break;
258 LeaveCriticalSection( &IconCrst );
260 return count;
263 /**********************************************************************
264 * CURSORICON_FreeModuleIcons
266 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
268 ICONCACHE **ptr = &IconAnchor;
269 HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
271 EnterCriticalSection( &IconCrst );
273 while ( *ptr )
275 if ( (*ptr)->hModule == hModule )
277 ICONCACHE *freePtr = *ptr;
278 *ptr = freePtr->next;
280 GlobalFree16(HICON_16(freePtr->hIcon));
281 HeapFree( GetProcessHeap(), 0, freePtr );
282 continue;
284 ptr = &(*ptr)->next;
287 LeaveCriticalSection( &IconCrst );
290 /**********************************************************************
291 * CURSORICON_FindBestIcon
293 * Find the icon closest to the requested size and number of colors.
295 static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
296 int height, int colors )
298 int i;
299 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
300 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
301 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
303 if (dir->idCount < 1)
305 WARN_(icon)("Empty directory!\n" );
306 return NULL;
308 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
310 /* Find Best Fit */
311 iTotalDiff = 0xFFFFFFFF;
312 iColorDiff = 0xFFFFFFFF;
313 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
315 iTempXDiff = abs(width - entry->ResInfo.icon.bWidth);
316 iTempYDiff = abs(height - entry->ResInfo.icon.bHeight);
318 if(iTotalDiff > (iTempXDiff + iTempYDiff))
320 iXDiff = iTempXDiff;
321 iYDiff = iTempYDiff;
322 iTotalDiff = iXDiff + iYDiff;
326 /* Find Best Colors for Best Fit */
327 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
329 if(abs(width - entry->ResInfo.icon.bWidth) == iXDiff &&
330 abs(height - entry->ResInfo.icon.bHeight) == iYDiff)
332 iTempColorDiff = abs(colors - entry->ResInfo.icon.bColorCount);
333 if(iColorDiff > iTempColorDiff)
335 bestEntry = entry;
336 iColorDiff = iTempColorDiff;
341 return bestEntry;
345 /**********************************************************************
346 * CURSORICON_FindBestCursor
348 * Find the cursor closest to the requested size.
349 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
350 * ignored too
352 static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
353 int width, int height, int color)
355 int i, maxwidth, maxheight;
356 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
358 if (dir->idCount < 1)
360 WARN_(cursor)("Empty directory!\n" );
361 return NULL;
363 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
365 /* Double height to account for AND and XOR masks */
367 height *= 2;
369 /* First find the largest one smaller than or equal to the requested size*/
371 maxwidth = maxheight = 0;
372 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
373 if ((entry->ResInfo.cursor.wWidth <= width) && (entry->ResInfo.cursor.wHeight <= height) &&
374 (entry->ResInfo.cursor.wWidth > maxwidth) && (entry->ResInfo.cursor.wHeight > maxheight) &&
375 (entry->wBitCount == 1))
377 bestEntry = entry;
378 maxwidth = entry->ResInfo.cursor.wWidth;
379 maxheight = entry->ResInfo.cursor.wHeight;
381 if (bestEntry) return bestEntry;
383 /* Now find the smallest one larger than the requested size */
385 maxwidth = maxheight = 255;
386 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
387 if ((entry->ResInfo.cursor.wWidth < maxwidth) && (entry->ResInfo.cursor.wHeight < maxheight) &&
388 (entry->wBitCount == 1))
390 bestEntry = entry;
391 maxwidth = entry->ResInfo.cursor.wWidth;
392 maxheight = entry->ResInfo.cursor.wHeight;
395 return bestEntry;
398 /*********************************************************************
399 * The main purpose of this function is to create fake resource directory
400 * and fake resource entries. There are several reasons for this:
401 * - CURSORICONDIR and CURSORICONFILEDIR differ in sizes and their
402 * fields
403 * There are some "bad" cursor files which do not have
404 * bColorCount initialized but instead one must read this info
405 * directly from corresponding DIB sections
406 * Note: wResId is index to array of pointer returned in ptrs (origin is 1)
408 static BOOL CURSORICON_SimulateLoadingFromResourceW( LPWSTR filename, BOOL fCursor,
409 CURSORICONDIR **res, LPBYTE **ptr)
411 LPBYTE _free;
412 CURSORICONFILEDIR *bits;
413 int entries, size, i;
415 *res = NULL;
416 *ptr = NULL;
417 if (!(bits = map_fileW( filename ))) return FALSE;
419 /* FIXME: test for inimated icons
420 * hack to load the first icon from the *.ani file
422 if ( *(LPDWORD)bits==0x46464952 ) /* "RIFF" */
423 { LPBYTE pos = (LPBYTE) bits;
424 FIXME_(cursor)("Animated icons not correctly implemented! %p \n", bits);
426 for (;;)
427 { if (*(LPDWORD)pos==0x6e6f6369) /* "icon" */
428 { FIXME_(cursor)("icon entry found! %p\n", bits);
429 pos+=4;
430 if ( !*(LPWORD) pos==0x2fe) /* iconsize */
431 { goto fail;
433 bits=(CURSORICONFILEDIR*)(pos+4);
434 FIXME_(cursor)("icon size ok. offset=%p \n", bits);
435 break;
437 pos+=2;
438 if (pos>=(LPBYTE)bits+766) goto fail;
441 if (!(entries = bits->idCount)) goto fail;
442 size = sizeof(CURSORICONDIR) + sizeof(CURSORICONDIRENTRY) * (entries - 1);
443 _free = (LPBYTE) size;
445 for (i=0; i < entries; i++)
446 size += bits->idEntries[i].dwDIBSize + (fCursor ? sizeof(POINT16): 0);
448 if (!(*ptr = HeapAlloc( GetProcessHeap(), 0,
449 entries * sizeof (CURSORICONDIRENTRY*)))) goto fail;
450 if (!(*res = HeapAlloc( GetProcessHeap(), 0, size))) goto fail;
452 _free = (LPBYTE)(*res) + (int)_free;
453 memcpy((*res), bits, 6);
454 for (i=0; i<entries; i++)
456 ((LPBYTE*)(*ptr))[i] = _free;
457 if (fCursor) {
458 (*res)->idEntries[i].ResInfo.cursor.wWidth=bits->idEntries[i].bWidth;
459 (*res)->idEntries[i].ResInfo.cursor.wHeight=bits->idEntries[i].bHeight;
460 ((LPPOINT16)_free)->x=bits->idEntries[i].xHotspot;
461 ((LPPOINT16)_free)->y=bits->idEntries[i].yHotspot;
462 _free+=sizeof(POINT16);
463 } else {
464 (*res)->idEntries[i].ResInfo.icon.bWidth=bits->idEntries[i].bWidth;
465 (*res)->idEntries[i].ResInfo.icon.bHeight=bits->idEntries[i].bHeight;
466 (*res)->idEntries[i].ResInfo.icon.bColorCount = bits->idEntries[i].bColorCount;
468 (*res)->idEntries[i].wPlanes=1;
469 (*res)->idEntries[i].wBitCount = ((LPBITMAPINFOHEADER)((LPBYTE)bits +
470 bits->idEntries[i].dwDIBOffset))->biBitCount;
471 (*res)->idEntries[i].dwBytesInRes = bits->idEntries[i].dwDIBSize;
472 (*res)->idEntries[i].wResId=i+1;
474 memcpy(_free,(LPBYTE)bits +bits->idEntries[i].dwDIBOffset,
475 (*res)->idEntries[i].dwBytesInRes);
476 _free += (*res)->idEntries[i].dwBytesInRes;
478 UnmapViewOfFile( bits );
479 return TRUE;
480 fail:
481 if (*res) HeapFree( GetProcessHeap(), 0, *res );
482 if (*ptr) HeapFree( GetProcessHeap(), 0, *ptr );
483 UnmapViewOfFile( bits );
484 return FALSE;
488 /**********************************************************************
489 * CURSORICON_CreateFromResource
491 * Create a cursor or icon from in-memory resource template.
493 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
494 * with cbSize parameter as well.
496 static HICON CURSORICON_CreateFromResource( HMODULE16 hModule, HGLOBAL16 hObj, LPBYTE bits,
497 UINT cbSize, BOOL bIcon, DWORD dwVersion,
498 INT width, INT height, UINT loadflags )
500 static HDC hdcMem;
501 int sizeAnd, sizeXor;
502 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
503 BITMAP bmpXor, bmpAnd;
504 POINT16 hotspot;
505 BITMAPINFO *bmi;
506 BOOL DoStretch;
507 INT size;
509 hotspot.x = ICON_HOTSPOT;
510 hotspot.y = ICON_HOTSPOT;
512 TRACE_(cursor)("%08x (%u bytes), ver %08x, %ix%i %s %s\n",
513 (unsigned)bits, cbSize, (unsigned)dwVersion, width, height,
514 bIcon ? "icon" : "cursor", (loadflags & LR_MONOCHROME) ? "mono" : "" );
515 if (dwVersion == 0x00020000)
517 FIXME_(cursor)("\t2.xx resources are not supported\n");
518 return 0;
521 if (bIcon)
522 bmi = (BITMAPINFO *)bits;
523 else /* get the hotspot */
525 POINT16 *pt = (POINT16 *)bits;
526 hotspot = *pt;
527 bmi = (BITMAPINFO *)(pt + 1);
529 size = DIB_BitmapInfoSize( bmi, DIB_RGB_COLORS );
531 if (!width) width = bmi->bmiHeader.biWidth;
532 if (!height) height = bmi->bmiHeader.biHeight/2;
533 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
534 (bmi->bmiHeader.biWidth != width);
536 /* Check bitmap header */
538 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
539 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
540 bmi->bmiHeader.biCompression != BI_RGB) )
542 WARN_(cursor)("\tinvalid resource bitmap header.\n");
543 return 0;
546 if (!screen_dc) screen_dc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
547 if (screen_dc)
549 BITMAPINFO* pInfo;
551 /* Make sure we have room for the monochrome bitmap later on.
552 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
553 * up to and including the biBitCount. In-memory icon resource
554 * format is as follows:
556 * BITMAPINFOHEADER icHeader // DIB header
557 * RGBQUAD icColors[] // Color table
558 * BYTE icXOR[] // DIB bits for XOR mask
559 * BYTE icAND[] // DIB bits for AND mask
562 if ((pInfo = (BITMAPINFO *)HeapAlloc( GetProcessHeap(), 0,
563 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
565 memcpy( pInfo, bmi, size );
566 pInfo->bmiHeader.biHeight /= 2;
568 /* Create the XOR bitmap */
570 if (DoStretch) {
571 if(bIcon)
573 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
575 else
577 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
579 if(hXorBits)
581 HBITMAP hOld;
582 BOOL res = FALSE;
584 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
585 if (hdcMem) {
586 hOld = SelectObject(hdcMem, hXorBits);
587 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
588 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
589 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
590 SelectObject(hdcMem, hOld);
592 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
594 } else hXorBits = CreateDIBitmap( screen_dc, &pInfo->bmiHeader,
595 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS );
596 if( hXorBits )
598 char* xbits = (char *)bmi + size +
599 DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth,
600 bmi->bmiHeader.biHeight,
601 bmi->bmiHeader.biBitCount) / 2;
603 pInfo->bmiHeader.biBitCount = 1;
604 if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
606 RGBQUAD *rgb = pInfo->bmiColors;
608 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
609 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
610 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
611 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
613 else
615 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
617 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
618 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
621 /* Create the AND bitmap */
623 if (DoStretch) {
624 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
625 HBITMAP hOld;
626 BOOL res = FALSE;
628 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
629 if (hdcMem) {
630 hOld = SelectObject(hdcMem, hAndBits);
631 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
632 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
633 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
634 SelectObject(hdcMem, hOld);
636 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
638 } else hAndBits = CreateDIBitmap( screen_dc, &pInfo->bmiHeader,
639 CBM_INIT, xbits, pInfo, DIB_RGB_COLORS );
641 if( !hAndBits ) DeleteObject( hXorBits );
643 HeapFree( GetProcessHeap(), 0, pInfo );
647 if( !hXorBits || !hAndBits )
649 WARN_(cursor)("\tunable to create an icon bitmap.\n");
650 return 0;
653 /* Now create the CURSORICONINFO structure */
654 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
655 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
656 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
657 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
659 if (hObj) hObj = GlobalReAlloc16( hObj,
660 sizeof(CURSORICONINFO) + sizeXor + sizeAnd, GMEM_MOVEABLE );
661 if (!hObj) hObj = GlobalAlloc16( GMEM_MOVEABLE,
662 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
663 if (hObj)
665 CURSORICONINFO *info;
667 /* Make it owned by the module */
668 if (hModule) hModule = GetExePtr(hModule);
669 FarSetOwner16( hObj, hModule );
671 info = (CURSORICONINFO *)GlobalLock16( hObj );
672 info->ptHotSpot.x = hotspot.x;
673 info->ptHotSpot.y = hotspot.y;
674 info->nWidth = bmpXor.bmWidth;
675 info->nHeight = bmpXor.bmHeight;
676 info->nWidthBytes = bmpXor.bmWidthBytes;
677 info->bPlanes = bmpXor.bmPlanes;
678 info->bBitsPerPixel = bmpXor.bmBitsPixel;
680 /* Transfer the bitmap bits to the CURSORICONINFO structure */
682 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
683 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
684 GlobalUnlock16( hObj );
687 DeleteObject( hAndBits );
688 DeleteObject( hXorBits );
689 return HICON_32((HICON16)hObj);
693 /**********************************************************************
694 * CreateIconFromResource (USER32.@)
696 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
697 BOOL bIcon, DWORD dwVersion)
699 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
703 /**********************************************************************
704 * CreateIconFromResourceEx (USER32.@)
706 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
707 BOOL bIcon, DWORD dwVersion,
708 INT width, INT height,
709 UINT cFlag )
711 return CURSORICON_CreateFromResource( 0, 0, bits, cbSize, bIcon, dwVersion,
712 width, height, cFlag );
715 /**********************************************************************
716 * CURSORICON_Load
718 * Load a cursor or icon from resource or file.
720 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
721 INT width, INT height, INT colors,
722 BOOL fCursor, UINT loadflags)
724 HANDLE handle = 0;
725 HICON hIcon = 0;
726 HRSRC hRsrc;
727 CURSORICONDIR *dir;
728 CURSORICONDIRENTRY *dirEntry;
729 LPBYTE bits;
731 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
733 LPBYTE *ptr;
734 if (!CURSORICON_SimulateLoadingFromResourceW((LPWSTR)name, fCursor, &dir, &ptr))
735 return 0;
736 if (fCursor)
737 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(dir, width, height, 1);
738 else
739 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(dir, width, height, colors);
740 bits = ptr[dirEntry->wResId-1];
741 hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dirEntry->dwBytesInRes,
742 !fCursor, 0x00030000, width, height, loadflags);
743 HeapFree( GetProcessHeap(), 0, dir );
744 HeapFree( GetProcessHeap(), 0, ptr );
746 else /* Load from resource */
748 HRSRC hGroupRsrc;
749 WORD wResId;
750 DWORD dwBytesInRes;
752 if (!hInstance) /* Load OEM cursor/icon */
754 if (!(hInstance = GetModuleHandleA( "user32.dll" ))) return 0;
757 /* Normalize hInstance (must be uniquely represented for icon cache) */
759 if ( HIWORD( hInstance ) )
760 hInstance = HINSTANCE_32(MapHModuleLS( hInstance ));
761 else
762 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
764 /* Get directory resource ID */
766 if (!(hRsrc = FindResourceW( hInstance, name,
767 fCursor ? RT_GROUP_CURSORW : RT_GROUP_ICONW )))
768 return 0;
769 hGroupRsrc = hRsrc;
771 /* Find the best entry in the directory */
773 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
774 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
775 if (fCursor)
776 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
777 width, height, 1);
778 else
779 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon( dir,
780 width, height, colors );
781 if (!dirEntry) return 0;
782 wResId = dirEntry->wResId;
783 dwBytesInRes = dirEntry->dwBytesInRes;
784 FreeResource( handle );
786 /* Load the resource */
788 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
789 fCursor ? RT_CURSORW : RT_ICONW ))) return 0;
791 /* If shared icon, check whether it was already loaded */
792 if ( (loadflags & LR_SHARED)
793 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
794 return hIcon;
796 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
797 bits = (LPBYTE)LockResource( handle );
798 hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dwBytesInRes,
799 !fCursor, 0x00030000, width, height, loadflags);
800 FreeResource( handle );
802 /* If shared icon, add to icon cache */
804 if ( hIcon && (loadflags & LR_SHARED) )
805 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
808 return hIcon;
811 /***********************************************************************
812 * CURSORICON_Copy
814 * Make a copy of a cursor or icon.
816 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
818 char *ptrOld, *ptrNew;
819 int size;
820 HICON16 hOld = HICON_16(hIcon);
821 HICON16 hNew;
823 if (!(ptrOld = (char *)GlobalLock16( hOld ))) return 0;
824 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
825 size = GlobalSize16( hOld );
826 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
827 FarSetOwner16( hNew, hInst16 );
828 ptrNew = (char *)GlobalLock16( hNew );
829 memcpy( ptrNew, ptrOld, size );
830 GlobalUnlock16( hOld );
831 GlobalUnlock16( hNew );
832 return HICON_32(hNew);
835 /*************************************************************************
836 * CURSORICON_ExtCopy
838 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
840 * PARAMS
841 * Handle [I] handle to an Image
842 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
843 * iDesiredCX [I] The Desired width of the Image
844 * iDesiredCY [I] The desired height of the Image
845 * nFlags [I] The flags from CopyImage
847 * RETURNS
848 * Success: The new handle of the Image
850 * NOTES
851 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
852 * LR_MONOCHROME should be implemented by CURSORICON_CreateFromResource.
853 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
858 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
859 INT iDesiredCX, INT iDesiredCY,
860 UINT nFlags)
862 HICON hNew=0;
864 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
865 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
867 if(hIcon == 0)
869 return 0;
872 /* Best Fit or Monochrome */
873 if( (nFlags & LR_COPYFROMRESOURCE
874 && (iDesiredCX > 0 || iDesiredCY > 0))
875 || nFlags & LR_MONOCHROME)
877 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
879 /* Not Found in Cache, then do a straight copy
881 if(pIconCache == NULL)
883 hNew = CURSORICON_Copy(0, hIcon);
884 if(nFlags & LR_COPYFROMRESOURCE)
886 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
889 else
891 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
892 LPBYTE pBits;
893 HANDLE hMem;
894 HRSRC hRsrc;
895 DWORD dwBytesInRes;
896 WORD wResId;
897 CURSORICONDIR *pDir;
898 CURSORICONDIRENTRY *pDirEntry;
899 BOOL bIsIcon = (nType == IMAGE_ICON);
901 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
903 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
904 || (iDesiredCX == 0 && iDesiredCY == 0))
906 iDesiredCY = GetSystemMetrics(bIsIcon ?
907 SM_CYICON : SM_CYCURSOR);
908 iDesiredCX = GetSystemMetrics(bIsIcon ?
909 SM_CXICON : SM_CXCURSOR);
912 /* Retrieve the CURSORICONDIRENTRY
914 if (!(hMem = LoadResource( pIconCache->hModule ,
915 pIconCache->hGroupRsrc)))
917 return 0;
919 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
921 return 0;
924 /* Find Best Fit
926 if(bIsIcon)
928 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
929 pDir, iDesiredCX, iDesiredCY, 256);
931 else
933 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
934 pDir, iDesiredCX, iDesiredCY, 1);
937 wResId = pDirEntry->wResId;
938 dwBytesInRes = pDirEntry->dwBytesInRes;
939 FreeResource(hMem);
941 TRACE_(icon)("ResID %u, BytesInRes %lu, Width %d, Height %d DX %d, DY %d\n",
942 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
943 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
945 /* Get the Best Fit
947 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
948 MAKEINTRESOURCEW(wResId), bIsIcon ? RT_ICONW : RT_CURSORW)))
950 return 0;
952 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
954 return 0;
957 pBits = (LPBYTE)LockResource( hMem );
959 if(nFlags & LR_DEFAULTSIZE)
961 iTargetCY = GetSystemMetrics(SM_CYICON);
962 iTargetCX = GetSystemMetrics(SM_CXICON);
965 /* Create a New Icon with the proper dimension
967 hNew = CURSORICON_CreateFromResource( 0, 0, pBits, dwBytesInRes,
968 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
969 FreeResource(hMem);
972 else hNew = CURSORICON_Copy(0, hIcon);
973 return hNew;
977 /***********************************************************************
978 * CreateCursor (USER32.@)
980 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
981 INT xHotSpot, INT yHotSpot,
982 INT nWidth, INT nHeight,
983 LPCVOID lpANDbits, LPCVOID lpXORbits )
985 CURSORICONINFO info;
987 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
988 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
990 info.ptHotSpot.x = xHotSpot;
991 info.ptHotSpot.y = yHotSpot;
992 info.nWidth = nWidth;
993 info.nHeight = nHeight;
994 info.nWidthBytes = 0;
995 info.bPlanes = 1;
996 info.bBitsPerPixel = 1;
998 return HICON_32(CreateCursorIconIndirect16(MapHModuleLS(hInstance), &info,
999 lpANDbits, lpXORbits));
1003 /***********************************************************************
1004 * CreateIcon (USER.407)
1006 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1007 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1008 LPCVOID lpANDbits, LPCVOID lpXORbits )
1010 CURSORICONINFO info;
1012 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1013 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1015 info.ptHotSpot.x = ICON_HOTSPOT;
1016 info.ptHotSpot.y = ICON_HOTSPOT;
1017 info.nWidth = nWidth;
1018 info.nHeight = nHeight;
1019 info.nWidthBytes = 0;
1020 info.bPlanes = bPlanes;
1021 info.bBitsPerPixel = bBitsPixel;
1023 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1027 /***********************************************************************
1028 * CreateIcon (USER32.@)
1030 * Creates an icon based on the specified bitmaps. The bitmaps must be
1031 * provided in a device dependent format and will be resized to
1032 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1033 * depth. The provided bitmaps must be top-down bitmaps.
1034 * Although Windows does not support 15bpp(*) this API must support it
1035 * for Winelib applications.
1037 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1038 * format!
1040 * BUGS
1042 * - The provided bitmaps are not resized!
1043 * - The documentation says the lpXORbits bitmap must be in a device
1044 * dependent format. But we must still resize it and perform depth
1045 * conversions if necessary.
1046 * - I'm a bit unsure about the how the 'device dependent format' thing works.
1047 * I did some tests on windows and found that if you provide a 16bpp bitmap
1048 * in lpXORbits, then its format but be 565 RGB if the screen's bit depth
1049 * is 16bpp but it must be 555 RGB if the screen's bit depth is anything
1050 * else. I don't know if this is part of the GDI specs or if this is a
1051 * quirk of the graphics card driver.
1052 * - You may think that we check whether the bit depths match or not
1053 * as an optimization. But the truth is that the conversion using
1054 * CreateDIBitmap does not work for some bit depth (e.g. 8bpp) and I have
1055 * no idea why.
1056 * - I'm pretty sure that all the things we do in CreateIcon should
1057 * also be done in CreateIconIndirect...
1059 HICON WINAPI CreateIcon(
1060 HINSTANCE hInstance, /* [in] the application's hInstance */
1061 INT nWidth, /* [in] the width of the provided bitmaps */
1062 INT nHeight, /* [in] the height of the provided bitmaps */
1063 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1064 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1065 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1066 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1068 HICON hIcon;
1069 HDC hdc;
1071 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1072 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1074 hdc=GetDC(0);
1075 if (!hdc)
1076 return 0;
1078 if (GetDeviceCaps(hdc,BITSPIXEL)==bBitsPixel) {
1079 CURSORICONINFO info;
1081 info.ptHotSpot.x = ICON_HOTSPOT;
1082 info.ptHotSpot.y = ICON_HOTSPOT;
1083 info.nWidth = nWidth;
1084 info.nHeight = nHeight;
1085 info.nWidthBytes = 0;
1086 info.bPlanes = bPlanes;
1087 info.bBitsPerPixel = bBitsPixel;
1089 hIcon=HICON_32(CreateCursorIconIndirect16(MapHModuleLS(hInstance), &info,
1090 lpANDbits, lpXORbits));
1091 } else {
1092 ICONINFO iinfo;
1093 BITMAPINFO bmi;
1095 iinfo.fIcon=TRUE;
1096 iinfo.xHotspot=ICON_HOTSPOT;
1097 iinfo.yHotspot=ICON_HOTSPOT;
1098 iinfo.hbmMask=CreateBitmap(nWidth,nHeight,1,1,lpANDbits);
1100 bmi.bmiHeader.biSize=sizeof(bmi.bmiHeader);
1101 bmi.bmiHeader.biWidth=nWidth;
1102 bmi.bmiHeader.biHeight=-nHeight;
1103 bmi.bmiHeader.biPlanes=bPlanes;
1104 bmi.bmiHeader.biBitCount=bBitsPixel;
1105 bmi.bmiHeader.biCompression=BI_RGB;
1106 bmi.bmiHeader.biSizeImage=0;
1107 bmi.bmiHeader.biXPelsPerMeter=0;
1108 bmi.bmiHeader.biYPelsPerMeter=0;
1109 bmi.bmiHeader.biClrUsed=0;
1110 bmi.bmiHeader.biClrImportant=0;
1112 iinfo.hbmColor = CreateDIBitmap( hdc, &bmi.bmiHeader,
1113 CBM_INIT, lpXORbits,
1114 &bmi, DIB_RGB_COLORS );
1116 hIcon=CreateIconIndirect(&iinfo);
1117 DeleteObject(iinfo.hbmMask);
1118 DeleteObject(iinfo.hbmColor);
1120 ReleaseDC(0,hdc);
1121 return hIcon;
1125 /***********************************************************************
1126 * CreateCursorIconIndirect (USER.408)
1128 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1129 CURSORICONINFO *info,
1130 LPCVOID lpANDbits,
1131 LPCVOID lpXORbits )
1133 HGLOBAL16 handle;
1134 char *ptr;
1135 int sizeAnd, sizeXor;
1137 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1138 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1139 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1140 sizeXor = info->nHeight * info->nWidthBytes;
1141 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1142 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1143 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1144 return 0;
1145 FarSetOwner16( handle, hInstance );
1146 ptr = (char *)GlobalLock16( handle );
1147 memcpy( ptr, info, sizeof(*info) );
1148 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1149 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1150 GlobalUnlock16( handle );
1151 return handle;
1155 /***********************************************************************
1156 * CopyIcon (USER.368)
1158 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1160 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1161 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1165 /***********************************************************************
1166 * CopyIcon (USER32.@)
1168 HICON WINAPI CopyIcon( HICON hIcon )
1170 TRACE_(icon)("%p\n", hIcon );
1171 return CURSORICON_Copy( 0, hIcon );
1175 /***********************************************************************
1176 * CopyCursor (USER.369)
1178 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1180 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1181 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1184 /**********************************************************************
1185 * DestroyIcon32 (USER.610)
1187 * This routine is actually exported from Win95 USER under the name
1188 * DestroyIcon32 ... The behaviour implemented here should mimic
1189 * the Win95 one exactly, especially the return values, which
1190 * depend on the setting of various flags.
1192 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1194 WORD retv;
1196 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1198 /* Check whether destroying active cursor */
1200 if ( QUEUE_Current()->cursor == HICON_32(handle) )
1202 WARN_(cursor)("Destroying active cursor!\n" );
1203 SetCursor( 0 );
1206 /* Try shared cursor/icon first */
1208 if ( !(flags & CID_NONSHARED) )
1210 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1212 if ( count != -1 )
1213 return (flags & CID_WIN32)? TRUE : (count == 0);
1215 /* FIXME: OEM cursors/icons should be recognized */
1218 /* Now assume non-shared cursor/icon */
1220 retv = GlobalFree16( handle );
1221 return (flags & CID_RESOURCE)? retv : TRUE;
1224 /***********************************************************************
1225 * DestroyIcon (USER32.@)
1227 BOOL WINAPI DestroyIcon( HICON hIcon )
1229 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1233 /***********************************************************************
1234 * DestroyCursor (USER32.@)
1236 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1238 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1242 /***********************************************************************
1243 * DrawIcon (USER32.@)
1245 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1247 CURSORICONINFO *ptr;
1248 HDC hMemDC;
1249 HBITMAP hXorBits, hAndBits;
1250 COLORREF oldFg, oldBg;
1252 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
1253 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1254 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
1255 (char *)(ptr+1) );
1256 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1257 ptr->bBitsPerPixel, (char *)(ptr + 1)
1258 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
1259 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1260 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1262 if (hXorBits && hAndBits)
1264 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1265 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
1266 SelectObject( hMemDC, hXorBits );
1267 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
1268 SelectObject( hMemDC, hBitTemp );
1270 DeleteDC( hMemDC );
1271 if (hXorBits) DeleteObject( hXorBits );
1272 if (hAndBits) DeleteObject( hAndBits );
1273 GlobalUnlock16(HICON_16(hIcon));
1274 SetTextColor( hdc, oldFg );
1275 SetBkColor( hdc, oldBg );
1276 return TRUE;
1279 /***********************************************************************
1280 * DumpIcon (USER.459)
1282 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1283 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1285 CURSORICONINFO *info = MapSL( pInfo );
1286 int sizeAnd, sizeXor;
1288 if (!info) return 0;
1289 sizeXor = info->nHeight * info->nWidthBytes;
1290 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1291 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1292 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1293 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1294 return MAKELONG( sizeXor, sizeXor );
1298 /***********************************************************************
1299 * SetCursor (USER32.@)
1300 * RETURNS:
1301 * A handle to the previous cursor shape.
1303 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1305 MESSAGEQUEUE *queue = QUEUE_Current();
1306 HCURSOR hOldCursor;
1308 if (hCursor == queue->cursor) return hCursor; /* No change */
1309 TRACE_(cursor)("%p\n", hCursor );
1310 hOldCursor = queue->cursor;
1311 queue->cursor = hCursor;
1312 /* Change the cursor shape only if it is visible */
1313 if (queue->cursor_count >= 0)
1315 USER_Driver.pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
1316 GlobalUnlock16(HCURSOR_16(hCursor));
1318 return hOldCursor;
1321 /***********************************************************************
1322 * ShowCursor (USER32.@)
1324 INT WINAPI ShowCursor( BOOL bShow )
1326 MESSAGEQUEUE *queue = QUEUE_Current();
1328 TRACE_(cursor)("%d, count=%d\n", bShow, queue->cursor_count );
1330 if (bShow)
1332 if (++queue->cursor_count == 0) /* Show it */
1334 USER_Driver.pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(queue->cursor)));
1335 GlobalUnlock16(HCURSOR_16(queue->cursor));
1338 else
1340 if (--queue->cursor_count == -1) /* Hide it */
1341 USER_Driver.pSetCursor( NULL );
1343 return queue->cursor_count;
1346 /***********************************************************************
1347 * GetCursor (USER32.@)
1349 HCURSOR WINAPI GetCursor(void)
1351 return QUEUE_Current()->cursor;
1355 /***********************************************************************
1356 * ClipCursor (USER.16)
1358 BOOL16 WINAPI ClipCursor16( const RECT16 *rect )
1360 if (!rect) SetRectEmpty( &CURSOR_ClipRect );
1361 else CONV_RECT16TO32( rect, &CURSOR_ClipRect );
1362 return TRUE;
1366 /***********************************************************************
1367 * ClipCursor (USER32.@)
1369 BOOL WINAPI ClipCursor( const RECT *rect )
1371 if (!rect) SetRectEmpty( &CURSOR_ClipRect );
1372 else CopyRect( &CURSOR_ClipRect, rect );
1373 return TRUE;
1377 /***********************************************************************
1378 * GetClipCursor (USER.309)
1380 void WINAPI GetClipCursor16( RECT16 *rect )
1382 if (rect) CONV_RECT32TO16( &CURSOR_ClipRect, rect );
1386 /***********************************************************************
1387 * GetClipCursor (USER32.@)
1389 BOOL WINAPI GetClipCursor( RECT *rect )
1391 if (rect)
1393 CopyRect( rect, &CURSOR_ClipRect );
1394 return TRUE;
1396 return FALSE;
1399 /**********************************************************************
1400 * LookupIconIdFromDirectoryEx (USER.364)
1402 * FIXME: exact parameter sizes
1404 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1405 INT16 width, INT16 height, UINT16 cFlag )
1407 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1410 /**********************************************************************
1411 * LookupIconIdFromDirectoryEx (USER32.@)
1413 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1414 INT width, INT height, UINT cFlag )
1416 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1417 UINT retVal = 0;
1418 if( dir && !dir->idReserved && (dir->idType & 3) )
1420 CURSORICONDIRENTRY* entry;
1421 HDC hdc;
1422 UINT palEnts;
1423 int colors;
1424 hdc = GetDC(0);
1425 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1426 if (palEnts == 0)
1427 palEnts = 256;
1428 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1430 ReleaseDC(0, hdc);
1432 if( bIcon )
1433 entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1434 else
1435 entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1437 if( entry ) retVal = entry->wResId;
1439 else WARN_(cursor)("invalid resource directory\n");
1440 return retVal;
1443 /**********************************************************************
1444 * LookupIconIdFromDirectory (USER.?)
1446 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
1448 return LookupIconIdFromDirectoryEx16( dir, bIcon,
1449 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1450 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1453 /**********************************************************************
1454 * LookupIconIdFromDirectory (USER32.@)
1456 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1458 return LookupIconIdFromDirectoryEx( dir, bIcon,
1459 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1460 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1463 /**********************************************************************
1464 * GetIconID (USER.455)
1466 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1468 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
1470 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1471 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1473 switch(resType)
1475 case RT_CURSOR16:
1476 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1477 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1478 case RT_ICON16:
1479 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1480 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1481 default:
1482 WARN_(cursor)("invalid res type %ld\n", resType );
1484 return 0;
1487 /**********************************************************************
1488 * LoadCursorIconHandler (USER.336)
1490 * Supposed to load resources of Windows 2.x applications.
1492 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1494 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1495 hResource, hModule, hRsrc);
1496 return (HGLOBAL16)0;
1499 /**********************************************************************
1500 * LoadDIBIconHandler (USER.357)
1502 * RT_ICON resource loader, installed by USER_SignalProc when module
1503 * is initialized.
1505 HGLOBAL16 WINAPI LoadDIBIconHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1507 /* If hResource is zero we must allocate a new memory block, if it's
1508 * non-zero but GlobalLock() returns NULL then it was discarded and
1509 * we have to recommit some memory, otherwise we just need to check
1510 * the block size. See LoadProc() in 16-bit SDK for more.
1513 hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
1514 if( hMemObj )
1516 LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1517 hMemObj = HICON_16(CURSORICON_CreateFromResource(
1518 hModule, hMemObj, bits,
1519 SizeofResource16(hModule, hRsrc), TRUE, 0x00030000,
1520 GetSystemMetrics(SM_CXICON),
1521 GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR));
1523 return hMemObj;
1526 /**********************************************************************
1527 * LoadDIBCursorHandler (USER.356)
1529 * RT_CURSOR resource loader. Same as above.
1531 HGLOBAL16 WINAPI LoadDIBCursorHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1533 hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
1534 if( hMemObj )
1536 LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1537 hMemObj = HICON_16(CURSORICON_CreateFromResource(
1538 hModule, hMemObj, bits,
1539 SizeofResource16(hModule, hRsrc), FALSE, 0x00030000,
1540 GetSystemMetrics(SM_CXCURSOR),
1541 GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME));
1543 return hMemObj;
1546 /**********************************************************************
1547 * LoadIconHandler (USER.456)
1549 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
1551 LPBYTE bits = (LPBYTE)LockResource16( hResource );
1553 TRACE_(cursor)("hRes=%04x\n",hResource);
1555 return HICON_16(CURSORICON_CreateFromResource(0, 0, bits, 0, TRUE,
1556 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
1559 /***********************************************************************
1560 * LoadCursorW (USER32.@)
1562 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1564 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1565 LR_SHARED | LR_DEFAULTSIZE );
1568 /***********************************************************************
1569 * LoadCursorA (USER32.@)
1571 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1573 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1574 LR_SHARED | LR_DEFAULTSIZE );
1577 /***********************************************************************
1578 * LoadCursorFromFileW (USER32.@)
1580 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1582 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1583 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1586 /***********************************************************************
1587 * LoadCursorFromFileA (USER32.@)
1589 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1591 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1592 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1595 /***********************************************************************
1596 * LoadIconW (USER32.@)
1598 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1600 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1601 LR_SHARED | LR_DEFAULTSIZE );
1604 /***********************************************************************
1605 * LoadIconA (USER32.@)
1607 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1609 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1610 LR_SHARED | LR_DEFAULTSIZE );
1613 /**********************************************************************
1614 * GetIconInfo (USER32.@)
1616 BOOL WINAPI GetIconInfo(HICON hIcon,PICONINFO iconinfo) {
1617 CURSORICONINFO *ciconinfo;
1619 ciconinfo = GlobalLock16(HICON_16(hIcon));
1620 if (!ciconinfo)
1621 return FALSE;
1623 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1624 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1626 iconinfo->fIcon = TRUE;
1627 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1628 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1630 else
1632 iconinfo->fIcon = FALSE;
1633 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1634 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1637 iconinfo->hbmColor = CreateBitmap ( ciconinfo->nWidth, ciconinfo->nHeight,
1638 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1639 (char *)(ciconinfo + 1)
1640 + ciconinfo->nHeight *
1641 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1642 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, ciconinfo->nHeight,
1643 1, 1, (char *)(ciconinfo + 1));
1645 GlobalUnlock16(HICON_16(hIcon));
1647 return TRUE;
1650 /**********************************************************************
1651 * CreateIconIndirect (USER32.@)
1653 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1655 BITMAP bmpXor,bmpAnd;
1656 HICON16 hObj;
1657 int sizeXor,sizeAnd;
1659 GetObjectA( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1660 GetObjectA( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1662 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1663 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1665 hObj = GlobalAlloc16( GMEM_MOVEABLE,
1666 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1667 if (hObj)
1669 CURSORICONINFO *info;
1671 info = (CURSORICONINFO *)GlobalLock16( hObj );
1673 /* If we are creating an icon, the hotspot is unused */
1674 if (iconinfo->fIcon)
1676 info->ptHotSpot.x = ICON_HOTSPOT;
1677 info->ptHotSpot.y = ICON_HOTSPOT;
1679 else
1681 info->ptHotSpot.x = iconinfo->xHotspot;
1682 info->ptHotSpot.y = iconinfo->yHotspot;
1685 info->nWidth = bmpXor.bmWidth;
1686 info->nHeight = bmpXor.bmHeight;
1687 info->nWidthBytes = bmpXor.bmWidthBytes;
1688 info->bPlanes = bmpXor.bmPlanes;
1689 info->bBitsPerPixel = bmpXor.bmBitsPixel;
1691 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1693 GetBitmapBits( iconinfo->hbmMask ,sizeAnd,(char*)(info + 1) );
1694 GetBitmapBits( iconinfo->hbmColor,sizeXor,(char*)(info + 1) +sizeAnd);
1695 GlobalUnlock16( hObj );
1697 return HICON_32(hObj);
1700 /******************************************************************************
1701 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1703 * NOTES
1704 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1706 * PARAMS
1707 * hdc [I] Handle to device context
1708 * x0 [I] X coordinate of upper left corner
1709 * y0 [I] Y coordinate of upper left corner
1710 * hIcon [I] Handle to icon to draw
1711 * cxWidth [I] Width of icon
1712 * cyWidth [I] Height of icon
1713 * istep [I] Index of frame in animated cursor
1714 * hbr [I] Handle to background brush
1715 * flags [I] Icon-drawing flags
1717 * RETURNS
1718 * Success: TRUE
1719 * Failure: FALSE
1721 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1722 INT cxWidth, INT cyWidth, UINT istep,
1723 HBRUSH hbr, UINT flags )
1725 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
1726 HDC hDC_off = 0, hMemDC;
1727 BOOL result = FALSE, DoOffscreen;
1728 HBITMAP hB_off = 0, hOld = 0;
1730 if (!ptr) return FALSE;
1731 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1732 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1734 hMemDC = CreateCompatibleDC (hdc);
1735 if (istep)
1736 FIXME_(icon)("Ignoring istep=%d\n", istep);
1737 if (flags & DI_COMPAT)
1738 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
1740 if (!flags) {
1741 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
1742 flags = DI_NORMAL;
1745 /* Calculate the size of the destination image. */
1746 if (cxWidth == 0)
1748 if (flags & DI_DEFAULTSIZE)
1749 cxWidth = GetSystemMetrics (SM_CXICON);
1750 else
1751 cxWidth = ptr->nWidth;
1753 if (cyWidth == 0)
1755 if (flags & DI_DEFAULTSIZE)
1756 cyWidth = GetSystemMetrics (SM_CYICON);
1757 else
1758 cyWidth = ptr->nHeight;
1761 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1763 if (DoOffscreen) {
1764 RECT r;
1766 r.left = 0;
1767 r.top = 0;
1768 r.right = cxWidth;
1769 r.bottom = cxWidth;
1771 hDC_off = CreateCompatibleDC(hdc);
1772 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
1773 if (hDC_off && hB_off) {
1774 hOld = SelectObject(hDC_off, hB_off);
1775 FillRect(hDC_off, &r, hbr);
1779 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
1781 HBITMAP hXorBits, hAndBits;
1782 COLORREF oldFg, oldBg;
1783 INT nStretchMode;
1785 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
1787 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1788 ptr->bPlanes, ptr->bBitsPerPixel,
1789 (char *)(ptr + 1)
1790 + ptr->nHeight *
1791 get_bitmap_width_bytes(ptr->nWidth,1) );
1792 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1793 1, 1, (char *)(ptr+1) );
1794 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1795 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1797 if (hXorBits && hAndBits)
1799 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1800 if (flags & DI_MASK)
1802 if (DoOffscreen)
1803 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1804 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1805 else
1806 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1807 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1809 SelectObject( hMemDC, hXorBits );
1810 if (flags & DI_IMAGE)
1812 if (DoOffscreen)
1813 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1814 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1815 else
1816 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1817 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1819 SelectObject( hMemDC, hBitTemp );
1820 result = TRUE;
1823 SetTextColor( hdc, oldFg );
1824 SetBkColor( hdc, oldBg );
1825 if (hXorBits) DeleteObject( hXorBits );
1826 if (hAndBits) DeleteObject( hAndBits );
1827 SetStretchBltMode (hdc, nStretchMode);
1828 if (DoOffscreen) {
1829 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
1830 SelectObject(hDC_off, hOld);
1833 if (hMemDC) DeleteDC( hMemDC );
1834 if (hDC_off) DeleteDC(hDC_off);
1835 if (hB_off) DeleteObject(hB_off);
1836 GlobalUnlock16(HICON_16(hIcon));
1837 return result;
1840 /***********************************************************************
1841 * DIB_FixColorsToLoadflags
1843 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
1844 * are in loadflags
1846 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
1848 int colors;
1849 COLORREF c_W, c_S, c_F, c_L, c_C;
1850 int incr,i;
1851 RGBQUAD *ptr;
1853 if (bmi->bmiHeader.biBitCount > 8) return;
1854 if (bmi->bmiHeader.biSize == sizeof(BITMAPINFOHEADER)) incr = 4;
1855 else if (bmi->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) incr = 3;
1856 else {
1857 WARN_(resource)("Wrong bitmap header size!\n");
1858 return;
1860 colors = bmi->bmiHeader.biClrUsed;
1861 if (!colors && (bmi->bmiHeader.biBitCount <= 8))
1862 colors = 1 << bmi->bmiHeader.biBitCount;
1863 c_W = GetSysColor(COLOR_WINDOW);
1864 c_S = GetSysColor(COLOR_3DSHADOW);
1865 c_F = GetSysColor(COLOR_3DFACE);
1866 c_L = GetSysColor(COLOR_3DLIGHT);
1867 if (loadflags & LR_LOADTRANSPARENT) {
1868 switch (bmi->bmiHeader.biBitCount) {
1869 case 1: pix = pix >> 7; break;
1870 case 4: pix = pix >> 4; break;
1871 case 8: break;
1872 default:
1873 WARN_(resource)("(%d): Unsupported depth\n", bmi->bmiHeader.biBitCount);
1874 return;
1876 if (pix >= colors) {
1877 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
1878 return;
1880 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
1881 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
1882 ptr->rgbBlue = GetBValue(c_W);
1883 ptr->rgbGreen = GetGValue(c_W);
1884 ptr->rgbRed = GetRValue(c_W);
1886 if (loadflags & LR_LOADMAP3DCOLORS)
1887 for (i=0; i<colors; i++) {
1888 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
1889 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
1890 if (c_C == RGB(128, 128, 128)) {
1891 ptr->rgbRed = GetRValue(c_S);
1892 ptr->rgbGreen = GetGValue(c_S);
1893 ptr->rgbBlue = GetBValue(c_S);
1894 } else if (c_C == RGB(192, 192, 192)) {
1895 ptr->rgbRed = GetRValue(c_F);
1896 ptr->rgbGreen = GetGValue(c_F);
1897 ptr->rgbBlue = GetBValue(c_F);
1898 } else if (c_C == RGB(223, 223, 223)) {
1899 ptr->rgbRed = GetRValue(c_L);
1900 ptr->rgbGreen = GetGValue(c_L);
1901 ptr->rgbBlue = GetBValue(c_L);
1907 /**********************************************************************
1908 * BITMAP_Load
1910 static HBITMAP BITMAP_Load( HINSTANCE instance,LPCWSTR name, UINT loadflags )
1912 HBITMAP hbitmap = 0;
1913 HRSRC hRsrc;
1914 HGLOBAL handle;
1915 char *ptr = NULL;
1916 BITMAPINFO *info, *fix_info=NULL;
1917 HGLOBAL hFix;
1918 int size;
1920 if (!(loadflags & LR_LOADFROMFILE))
1922 if (!instance)
1924 /* OEM bitmap: try to load the resource from user32.dll */
1925 if (HIWORD(name)) return 0;
1926 if (!(instance = GetModuleHandleA("user32.dll"))) return 0;
1928 if (!(hRsrc = FindResourceW( instance, name, RT_BITMAPW ))) return 0;
1929 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
1931 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
1933 else
1935 if (!(ptr = map_fileW( name ))) return 0;
1936 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
1938 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
1939 if ((hFix = GlobalAlloc(0, size))) fix_info=GlobalLock(hFix);
1940 if (fix_info) {
1941 BYTE pix;
1943 memcpy(fix_info, info, size);
1944 pix = *((LPBYTE)info+DIB_BitmapInfoSize(info, DIB_RGB_COLORS));
1945 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
1946 if (!screen_dc) screen_dc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
1947 if (screen_dc)
1949 char *bits = (char *)info + size;
1950 if (loadflags & LR_CREATEDIBSECTION) {
1951 DIBSECTION dib;
1952 hbitmap = CreateDIBSection(screen_dc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
1953 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
1954 SetDIBits(screen_dc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
1955 DIB_RGB_COLORS);
1957 else {
1958 hbitmap = CreateDIBitmap( screen_dc, &fix_info->bmiHeader, CBM_INIT,
1959 bits, fix_info, DIB_RGB_COLORS );
1962 GlobalUnlock(hFix);
1963 GlobalFree(hFix);
1965 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
1966 return hbitmap;
1969 /**********************************************************************
1970 * LoadImageA (USER32.@)
1972 * FIXME: implementation lacks some features, see LR_ defines in winuser.h
1975 /* filter for page-fault exceptions */
1976 static WINE_EXCEPTION_FILTER(page_fault)
1978 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
1979 return EXCEPTION_EXECUTE_HANDLER;
1980 return EXCEPTION_CONTINUE_SEARCH;
1983 /*********************************************************************/
1985 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
1986 INT desiredx, INT desiredy, UINT loadflags)
1988 HANDLE res;
1989 LPWSTR u_name;
1991 if (!HIWORD(name))
1992 return LoadImageW(hinst, (LPWSTR)name, type, desiredx, desiredy, loadflags);
1994 __TRY {
1995 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1996 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1997 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
1999 __EXCEPT(page_fault) {
2000 SetLastError( ERROR_INVALID_PARAMETER );
2001 return 0;
2003 __ENDTRY
2004 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2005 HeapFree(GetProcessHeap(), 0, u_name);
2006 return res;
2010 /******************************************************************************
2011 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2013 * PARAMS
2014 * hinst [I] Handle of instance that contains image
2015 * name [I] Name of image
2016 * type [I] Type of image
2017 * desiredx [I] Desired width
2018 * desiredy [I] Desired height
2019 * loadflags [I] Load flags
2021 * RETURNS
2022 * Success: Handle to newly loaded image
2023 * Failure: NULL
2025 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2027 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2028 INT desiredx, INT desiredy, UINT loadflags )
2030 if (HIWORD(name)) {
2031 TRACE_(resource)("(%p,%p,%d,%d,%d,0x%08x)\n",
2032 hinst,name,type,desiredx,desiredy,loadflags);
2033 } else {
2034 TRACE_(resource)("(%p,%p,%d,%d,%d,0x%08x)\n",
2035 hinst,name,type,desiredx,desiredy,loadflags);
2037 if (loadflags & LR_DEFAULTSIZE) {
2038 if (type == IMAGE_ICON) {
2039 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2040 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2041 } else if (type == IMAGE_CURSOR) {
2042 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2043 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2046 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2047 switch (type) {
2048 case IMAGE_BITMAP:
2049 return BITMAP_Load( hinst, name, loadflags );
2051 case IMAGE_ICON:
2052 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2053 if (screen_dc)
2055 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2056 if (palEnts == 0) palEnts = 256;
2057 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2058 palEnts, FALSE, loadflags);
2060 break;
2062 case IMAGE_CURSOR:
2063 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2064 1, TRUE, loadflags);
2066 return 0;
2069 /******************************************************************************
2070 * CopyImage (USER32.@) Creates new image and copies attributes to it
2072 * PARAMS
2073 * hnd [I] Handle to image to copy
2074 * type [I] Type of image to copy
2075 * desiredx [I] Desired width of new image
2076 * desiredy [I] Desired height of new image
2077 * flags [I] Copy flags
2079 * RETURNS
2080 * Success: Handle to newly created image
2081 * Failure: NULL
2083 * FIXME: implementation still lacks nearly all features, see LR_*
2084 * defines in winuser.h
2086 HICON WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2087 INT desiredy, UINT flags )
2089 switch (type)
2091 case IMAGE_BITMAP:
2093 HBITMAP res;
2094 BITMAP bm;
2096 if (!GetObjectW( hnd, sizeof(bm), &bm )) return 0;
2097 bm.bmBits = NULL;
2098 if ((res = CreateBitmapIndirect(&bm)))
2100 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight );
2101 GetBitmapBits( hnd, bm.bmWidthBytes * bm.bmHeight, buf );
2102 SetBitmapBits( res, bm.bmWidthBytes * bm.bmHeight, buf );
2103 HeapFree( GetProcessHeap(), 0, buf );
2105 return (HICON)res;
2107 case IMAGE_ICON:
2108 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2109 case IMAGE_CURSOR:
2110 /* Should call CURSORICON_ExtCopy but more testing
2111 * needs to be done before we change this
2113 return CopyCursor(hnd);
2115 return 0;
2119 /******************************************************************************
2120 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2122 * RETURNS
2123 * Success: Handle to specified bitmap
2124 * Failure: NULL
2126 HBITMAP WINAPI LoadBitmapW(
2127 HINSTANCE instance, /* [in] Handle to application instance */
2128 LPCWSTR name) /* [in] Address of bitmap resource name */
2130 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2133 /**********************************************************************
2134 * LoadBitmapA (USER32.@)
2136 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2138 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );