Release 20040914.
[wine/multimedia.git] / windows / cursoricon.c
blobd1b45e43b858344f9bc6feda0afac91c1e76bd6b
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://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_icons.asp
29 * Cursors and icons are stored in a global heap block, with the
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 "config.h"
47 #include "wine/port.h"
49 #include <stdarg.h>
50 #include <string.h>
51 #include <stdlib.h>
53 #include "windef.h"
54 #include "winbase.h"
55 #include "wingdi.h"
56 #include "wownt32.h"
57 #include "winerror.h"
58 #include "ntstatus.h"
59 #include "excpt.h"
60 #include "wine/winbase16.h"
61 #include "wine/winuser16.h"
62 #include "wine/exception.h"
63 #include "cursoricon.h"
64 #include "module.h"
65 #include "wine/debug.h"
66 #include "user.h"
67 #include "message.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
70 WINE_DECLARE_DEBUG_CHANNEL(icon);
71 WINE_DECLARE_DEBUG_CHANNEL(resource);
74 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
76 static HDC screen_dc;
78 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
80 /**********************************************************************
81 * ICONCACHE for cursors/icons loaded with LR_SHARED.
83 * FIXME: This should not be allocated on the system heap, but on a
84 * subsystem-global heap (i.e. one for all Win16 processes,
85 * and one for each Win32 process).
87 typedef struct tagICONCACHE
89 struct tagICONCACHE *next;
91 HMODULE hModule;
92 HRSRC hRsrc;
93 HRSRC hGroupRsrc;
94 HICON hIcon;
96 INT count;
98 } ICONCACHE;
100 static ICONCACHE *IconAnchor = NULL;
102 static CRITICAL_SECTION IconCrst;
103 static CRITICAL_SECTION_DEBUG critsect_debug =
105 0, 0, &IconCrst,
106 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
107 0, 0, { 0, (DWORD)(__FILE__ ": IconCrst") }
109 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
111 static WORD ICON_HOTSPOT = 0x4242;
114 /***********************************************************************
115 * map_fileW
117 * Helper function to map a file to memory:
118 * name - file name
119 * [RETURN] ptr - pointer to mapped file
121 static void *map_fileW( LPCWSTR name )
123 HANDLE hFile, hMapping;
124 LPVOID ptr = NULL;
126 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
127 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
128 if (hFile != INVALID_HANDLE_VALUE)
130 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
131 CloseHandle( hFile );
132 if (hMapping)
134 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
135 CloseHandle( hMapping );
138 return ptr;
142 /***********************************************************************
143 * get_bitmap_width_bytes
145 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
146 * data.
148 static int get_bitmap_width_bytes( int width, int bpp )
150 switch(bpp)
152 case 1:
153 return 2 * ((width+15) / 16);
154 case 4:
155 return 2 * ((width+3) / 4);
156 case 24:
157 width *= 3;
158 /* fall through */
159 case 8:
160 return width + (width & 1);
161 case 16:
162 case 15:
163 return width * 2;
164 case 32:
165 return width * 4;
166 default:
167 WARN("Unknown depth %d, please report.\n", bpp );
169 return -1;
173 /***********************************************************************
174 * get_dib_width_bytes
176 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
178 static int get_dib_width_bytes( int width, int depth )
180 int words;
182 switch(depth)
184 case 1: words = (width + 31) / 32; break;
185 case 4: words = (width + 7) / 8; break;
186 case 8: words = (width + 3) / 4; break;
187 case 15:
188 case 16: words = (width + 1) / 2; break;
189 case 24: words = (width * 3 + 3)/4; break;
190 default:
191 WARN("(%d): Unsupported depth\n", depth );
192 /* fall through */
193 case 32:
194 words = width;
196 return 4 * words;
200 /***********************************************************************
201 * bitmap_info_size
203 * Return the size of the bitmap info structure including color table.
205 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
207 int colors;
209 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
211 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
212 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
213 return sizeof(BITMAPCOREHEADER) + colors *
214 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
216 else /* assume BITMAPINFOHEADER */
218 colors = info->bmiHeader.biClrUsed;
219 if (!colors && (info->bmiHeader.biBitCount <= 8))
220 colors = 1 << info->bmiHeader.biBitCount;
221 return sizeof(BITMAPINFOHEADER) + colors *
222 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
227 /***********************************************************************
228 * is_dib_monochrome
230 * Returns whether a DIB can be converted to a monochrome DDB.
232 * A DIB can be converted if its color table contains only black and
233 * white. Black must be the first color in the color table.
235 * Note : If the first color in the color table is white followed by
236 * black, we can't convert it to a monochrome DDB with
237 * SetDIBits, because black and white would be inverted.
239 static BOOL is_dib_monochrome( const BITMAPINFO* info )
241 if (info->bmiHeader.biBitCount != 1) return FALSE;
243 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
245 RGBTRIPLE *rgb = ((BITMAPCOREINFO *) info)->bmciColors;
247 /* Check if the first color is black */
248 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
250 rgb++;
252 /* Check if the second color is white */
253 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
254 && (rgb->rgbtBlue == 0xff));
256 else return FALSE;
258 else /* assume BITMAPINFOHEADER */
260 RGBQUAD *rgb = info->bmiColors;
262 /* Check if the first color is black */
263 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
264 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
266 rgb++;
268 /* Check if the second color is white */
269 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
270 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
272 else return FALSE;
277 /**********************************************************************
278 * CURSORICON_FindSharedIcon
280 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
282 HICON hIcon = 0;
283 ICONCACHE *ptr;
285 EnterCriticalSection( &IconCrst );
287 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
288 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
290 ptr->count++;
291 hIcon = ptr->hIcon;
292 break;
295 LeaveCriticalSection( &IconCrst );
297 return hIcon;
300 /*************************************************************************
301 * CURSORICON_FindCache
303 * Given a handle, find the corresponding cache element
305 * PARAMS
306 * Handle [I] handle to an Image
308 * RETURNS
309 * Success: The cache entry
310 * Failure: NULL
313 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
315 ICONCACHE *ptr;
316 ICONCACHE *pRet=NULL;
317 BOOL IsFound = FALSE;
318 int count;
320 EnterCriticalSection( &IconCrst );
322 for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
324 if ( hIcon == ptr->hIcon )
326 IsFound = TRUE;
327 pRet = ptr;
331 LeaveCriticalSection( &IconCrst );
333 return pRet;
336 /**********************************************************************
337 * CURSORICON_AddSharedIcon
339 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
341 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
342 if ( !ptr ) return;
344 ptr->hModule = hModule;
345 ptr->hRsrc = hRsrc;
346 ptr->hIcon = hIcon;
347 ptr->hGroupRsrc = hGroupRsrc;
348 ptr->count = 1;
350 EnterCriticalSection( &IconCrst );
351 ptr->next = IconAnchor;
352 IconAnchor = ptr;
353 LeaveCriticalSection( &IconCrst );
356 /**********************************************************************
357 * CURSORICON_DelSharedIcon
359 static INT CURSORICON_DelSharedIcon( HICON hIcon )
361 INT count = -1;
362 ICONCACHE *ptr;
364 EnterCriticalSection( &IconCrst );
366 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
367 if ( ptr->hIcon == hIcon )
369 if ( ptr->count > 0 ) ptr->count--;
370 count = ptr->count;
371 break;
374 LeaveCriticalSection( &IconCrst );
376 return count;
379 /**********************************************************************
380 * CURSORICON_FreeModuleIcons
382 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
384 ICONCACHE **ptr = &IconAnchor;
385 HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
387 EnterCriticalSection( &IconCrst );
389 while ( *ptr )
391 if ( (*ptr)->hModule == hModule )
393 ICONCACHE *freePtr = *ptr;
394 *ptr = freePtr->next;
396 GlobalFree16(HICON_16(freePtr->hIcon));
397 HeapFree( GetProcessHeap(), 0, freePtr );
398 continue;
400 ptr = &(*ptr)->next;
403 LeaveCriticalSection( &IconCrst );
406 /**********************************************************************
407 * CURSORICON_FindBestIcon
409 * Find the icon closest to the requested size and number of colors.
411 static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
412 int height, int colors )
414 int i;
415 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
416 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
417 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
419 if (dir->idCount < 1)
421 WARN_(icon)("Empty directory!\n" );
422 return NULL;
424 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
426 /* Find Best Fit */
427 iTotalDiff = 0xFFFFFFFF;
428 iColorDiff = 0xFFFFFFFF;
429 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
431 iTempXDiff = abs(width - entry->ResInfo.icon.bWidth);
432 iTempYDiff = abs(height - entry->ResInfo.icon.bHeight);
434 if(iTotalDiff > (iTempXDiff + iTempYDiff))
436 iXDiff = iTempXDiff;
437 iYDiff = iTempYDiff;
438 iTotalDiff = iXDiff + iYDiff;
442 /* Find Best Colors for Best Fit */
443 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
445 if(abs(width - entry->ResInfo.icon.bWidth) == iXDiff &&
446 abs(height - entry->ResInfo.icon.bHeight) == iYDiff)
448 iTempColorDiff = abs(colors - (1<<entry->wBitCount));
449 if(iColorDiff > iTempColorDiff)
451 bestEntry = entry;
452 iColorDiff = iTempColorDiff;
457 return bestEntry;
461 /**********************************************************************
462 * CURSORICON_FindBestCursor
464 * Find the cursor closest to the requested size.
465 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
466 * ignored too
468 static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
469 int width, int height, int color)
471 int i, maxwidth, maxheight;
472 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
474 if (dir->idCount < 1)
476 WARN_(cursor)("Empty directory!\n" );
477 return NULL;
479 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
481 /* Double height to account for AND and XOR masks */
483 height *= 2;
485 /* First find the largest one smaller than or equal to the requested size*/
487 maxwidth = maxheight = 0;
488 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
489 if ((entry->ResInfo.cursor.wWidth <= width) && (entry->ResInfo.cursor.wHeight <= height) &&
490 (entry->ResInfo.cursor.wWidth > maxwidth) && (entry->ResInfo.cursor.wHeight > maxheight) &&
491 (entry->wBitCount == 1))
493 bestEntry = entry;
494 maxwidth = entry->ResInfo.cursor.wWidth;
495 maxheight = entry->ResInfo.cursor.wHeight;
497 if (bestEntry) return bestEntry;
499 /* Now find the smallest one larger than the requested size */
501 maxwidth = maxheight = 255;
502 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
503 if ((entry->ResInfo.cursor.wWidth < maxwidth) && (entry->ResInfo.cursor.wHeight < maxheight) &&
504 (entry->wBitCount == 1))
506 bestEntry = entry;
507 maxwidth = entry->ResInfo.cursor.wWidth;
508 maxheight = entry->ResInfo.cursor.wHeight;
511 return bestEntry;
514 /*********************************************************************
515 * The main purpose of this function is to create fake resource directory
516 * and fake resource entries. There are several reasons for this:
517 * - CURSORICONDIR and CURSORICONFILEDIR differ in sizes and their
518 * fields
519 * There are some "bad" cursor files which do not have
520 * bColorCount initialized but instead one must read this info
521 * directly from corresponding DIB sections
522 * Note: wResId is index to array of pointer returned in ptrs (origin is 1)
524 static BOOL CURSORICON_SimulateLoadingFromResourceW( LPWSTR filename, BOOL fCursor,
525 CURSORICONDIR **res, LPBYTE **ptr)
527 LPBYTE _free;
528 CURSORICONFILEDIR *bits;
529 int entries, size, i;
531 *res = NULL;
532 *ptr = NULL;
533 if (!(bits = map_fileW( filename ))) return FALSE;
535 /* FIXME: test for inimated icons
536 * hack to load the first icon from the *.ani file
538 if ( *(LPDWORD)bits==0x46464952 ) /* "RIFF" */
539 { LPBYTE pos = (LPBYTE) bits;
540 FIXME_(cursor)("Animated icons not correctly implemented! %p \n", bits);
542 for (;;)
543 { if (*(LPDWORD)pos==0x6e6f6369) /* "icon" */
544 { FIXME_(cursor)("icon entry found! %p\n", bits);
545 pos+=4;
546 if ( !*(LPWORD) pos==0x2fe) /* iconsize */
547 { goto fail;
549 bits=(CURSORICONFILEDIR*)(pos+4);
550 FIXME_(cursor)("icon size ok. offset=%p \n", bits);
551 break;
553 pos+=2;
554 if (pos>=(LPBYTE)bits+766) goto fail;
557 if (!(entries = bits->idCount)) goto fail;
558 size = sizeof(CURSORICONDIR) + sizeof(CURSORICONDIRENTRY) * (entries - 1);
559 _free = (LPBYTE) size;
561 for (i=0; i < entries; i++)
562 size += bits->idEntries[i].dwDIBSize + (fCursor ? sizeof(POINT16): 0);
564 if (!(*ptr = HeapAlloc( GetProcessHeap(), 0,
565 entries * sizeof (CURSORICONDIRENTRY*)))) goto fail;
566 if (!(*res = HeapAlloc( GetProcessHeap(), 0, size))) goto fail;
568 _free = (LPBYTE)(*res) + (int)_free;
569 memcpy((*res), bits, 6);
570 for (i=0; i<entries; i++)
572 ((LPBYTE*)(*ptr))[i] = _free;
573 if (fCursor) {
574 (*res)->idEntries[i].ResInfo.cursor.wWidth=bits->idEntries[i].bWidth;
575 (*res)->idEntries[i].ResInfo.cursor.wHeight=bits->idEntries[i].bHeight;
576 ((LPPOINT16)_free)->x=bits->idEntries[i].xHotspot;
577 ((LPPOINT16)_free)->y=bits->idEntries[i].yHotspot;
578 _free+=sizeof(POINT16);
579 } else {
580 (*res)->idEntries[i].ResInfo.icon.bWidth=bits->idEntries[i].bWidth;
581 (*res)->idEntries[i].ResInfo.icon.bHeight=bits->idEntries[i].bHeight;
582 (*res)->idEntries[i].ResInfo.icon.bColorCount = bits->idEntries[i].bColorCount;
584 (*res)->idEntries[i].wPlanes=1;
585 (*res)->idEntries[i].wBitCount = ((LPBITMAPINFOHEADER)((LPBYTE)bits +
586 bits->idEntries[i].dwDIBOffset))->biBitCount;
587 (*res)->idEntries[i].dwBytesInRes = bits->idEntries[i].dwDIBSize;
588 (*res)->idEntries[i].wResId=i+1;
590 memcpy(_free,(LPBYTE)bits +bits->idEntries[i].dwDIBOffset,
591 (*res)->idEntries[i].dwBytesInRes);
592 _free += (*res)->idEntries[i].dwBytesInRes;
594 UnmapViewOfFile( bits );
595 return TRUE;
596 fail:
597 if (*res) HeapFree( GetProcessHeap(), 0, *res );
598 if (*ptr) HeapFree( GetProcessHeap(), 0, *ptr );
599 UnmapViewOfFile( bits );
600 return FALSE;
604 /**********************************************************************
605 * CURSORICON_CreateFromResource
607 * Create a cursor or icon from in-memory resource template.
609 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
610 * with cbSize parameter as well.
612 static HICON CURSORICON_CreateFromResource( HMODULE16 hModule, HGLOBAL16 hObj, LPBYTE bits,
613 UINT cbSize, BOOL bIcon, DWORD dwVersion,
614 INT width, INT height, UINT loadflags )
616 static HDC hdcMem;
617 int sizeAnd, sizeXor;
618 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
619 BITMAP bmpXor, bmpAnd;
620 POINT16 hotspot;
621 BITMAPINFO *bmi;
622 BOOL DoStretch;
623 INT size;
625 hotspot.x = ICON_HOTSPOT;
626 hotspot.y = ICON_HOTSPOT;
628 TRACE_(cursor)("%08x (%u bytes), ver %08x, %ix%i %s %s\n",
629 (unsigned)bits, cbSize, (unsigned)dwVersion, width, height,
630 bIcon ? "icon" : "cursor", (loadflags & LR_MONOCHROME) ? "mono" : "" );
631 if (dwVersion == 0x00020000)
633 FIXME_(cursor)("\t2.xx resources are not supported\n");
634 return 0;
637 if (bIcon)
638 bmi = (BITMAPINFO *)bits;
639 else /* get the hotspot */
641 POINT16 *pt = (POINT16 *)bits;
642 hotspot = *pt;
643 bmi = (BITMAPINFO *)(pt + 1);
645 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
647 if (!width) width = bmi->bmiHeader.biWidth;
648 if (!height) height = bmi->bmiHeader.biHeight/2;
649 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
650 (bmi->bmiHeader.biWidth != width);
652 /* Check bitmap header */
654 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
655 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
656 bmi->bmiHeader.biCompression != BI_RGB) )
658 WARN_(cursor)("\tinvalid resource bitmap header.\n");
659 return 0;
662 if (!screen_dc) screen_dc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
663 if (screen_dc)
665 BITMAPINFO* pInfo;
667 /* Make sure we have room for the monochrome bitmap later on.
668 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
669 * up to and including the biBitCount. In-memory icon resource
670 * format is as follows:
672 * BITMAPINFOHEADER icHeader // DIB header
673 * RGBQUAD icColors[] // Color table
674 * BYTE icXOR[] // DIB bits for XOR mask
675 * BYTE icAND[] // DIB bits for AND mask
678 if ((pInfo = (BITMAPINFO *)HeapAlloc( GetProcessHeap(), 0,
679 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
681 memcpy( pInfo, bmi, size );
682 pInfo->bmiHeader.biHeight /= 2;
684 /* Create the XOR bitmap */
686 if (DoStretch) {
687 if(bIcon)
689 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
691 else
693 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
695 if(hXorBits)
697 HBITMAP hOld;
698 BOOL res = FALSE;
700 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
701 if (hdcMem) {
702 hOld = SelectObject(hdcMem, hXorBits);
703 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
704 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
705 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
706 SelectObject(hdcMem, hOld);
708 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
710 } else {
711 if (is_dib_monochrome(bmi)) {
712 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
713 SetDIBits(screen_dc, hXorBits, 0, height,
714 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
716 else
717 hXorBits = CreateDIBitmap(screen_dc, &pInfo->bmiHeader,
718 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS);
721 if( hXorBits )
723 char* xbits = (char *)bmi + size +
724 get_dib_width_bytes( bmi->bmiHeader.biWidth,
725 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
727 pInfo->bmiHeader.biBitCount = 1;
728 if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
730 RGBQUAD *rgb = pInfo->bmiColors;
732 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
733 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
734 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
735 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
737 else
739 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
741 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
742 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
745 /* Create the AND bitmap */
747 if (DoStretch) {
748 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
749 HBITMAP hOld;
750 BOOL res = FALSE;
752 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
753 if (hdcMem) {
754 hOld = SelectObject(hdcMem, hAndBits);
755 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
756 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
757 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
758 SelectObject(hdcMem, hOld);
760 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
762 } else {
763 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
765 if (hAndBits) SetDIBits(screen_dc, hAndBits, 0, height,
766 xbits, pInfo, DIB_RGB_COLORS);
769 if( !hAndBits ) DeleteObject( hXorBits );
771 HeapFree( GetProcessHeap(), 0, pInfo );
775 if( !hXorBits || !hAndBits )
777 WARN_(cursor)("\tunable to create an icon bitmap.\n");
778 return 0;
781 /* Now create the CURSORICONINFO structure */
782 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
783 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
784 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
785 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
787 if (hObj) hObj = GlobalReAlloc16( hObj,
788 sizeof(CURSORICONINFO) + sizeXor + sizeAnd, GMEM_MOVEABLE );
789 if (!hObj) hObj = GlobalAlloc16( GMEM_MOVEABLE,
790 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
791 if (hObj)
793 CURSORICONINFO *info;
795 /* Make it owned by the module */
796 if (hModule) hModule = GetExePtr(hModule);
797 FarSetOwner16( hObj, hModule );
799 info = (CURSORICONINFO *)GlobalLock16( hObj );
800 info->ptHotSpot.x = hotspot.x;
801 info->ptHotSpot.y = hotspot.y;
802 info->nWidth = bmpXor.bmWidth;
803 info->nHeight = bmpXor.bmHeight;
804 info->nWidthBytes = bmpXor.bmWidthBytes;
805 info->bPlanes = bmpXor.bmPlanes;
806 info->bBitsPerPixel = bmpXor.bmBitsPixel;
808 /* Transfer the bitmap bits to the CURSORICONINFO structure */
810 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
811 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
812 GlobalUnlock16( hObj );
815 DeleteObject( hAndBits );
816 DeleteObject( hXorBits );
817 return HICON_32((HICON16)hObj);
821 /**********************************************************************
822 * CreateIconFromResource (USER32.@)
824 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
825 BOOL bIcon, DWORD dwVersion)
827 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
831 /**********************************************************************
832 * CreateIconFromResourceEx (USER32.@)
834 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
835 BOOL bIcon, DWORD dwVersion,
836 INT width, INT height,
837 UINT cFlag )
839 return CURSORICON_CreateFromResource( 0, 0, bits, cbSize, bIcon, dwVersion,
840 width, height, cFlag );
843 /**********************************************************************
844 * CURSORICON_Load
846 * Load a cursor or icon from resource or file.
848 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
849 INT width, INT height, INT colors,
850 BOOL fCursor, UINT loadflags)
852 HANDLE handle = 0;
853 HICON hIcon = 0;
854 HRSRC hRsrc;
855 CURSORICONDIR *dir;
856 CURSORICONDIRENTRY *dirEntry;
857 LPBYTE bits;
859 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
861 LPBYTE *ptr;
862 if (!CURSORICON_SimulateLoadingFromResourceW((LPWSTR)name, fCursor, &dir, &ptr))
863 return 0;
864 if (fCursor)
865 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(dir, width, height, 1);
866 else
867 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(dir, width, height, colors);
868 bits = ptr[dirEntry->wResId-1];
869 hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dirEntry->dwBytesInRes,
870 !fCursor, 0x00030000, width, height, loadflags);
871 HeapFree( GetProcessHeap(), 0, dir );
872 HeapFree( GetProcessHeap(), 0, ptr );
874 else /* Load from resource */
876 HRSRC hGroupRsrc;
877 WORD wResId;
878 DWORD dwBytesInRes;
880 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
882 /* Normalize hInstance (must be uniquely represented for icon cache) */
884 if (!HIWORD( hInstance ))
885 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
887 /* Get directory resource ID */
889 if (!(hRsrc = FindResourceW( hInstance, name,
890 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
891 return 0;
892 hGroupRsrc = hRsrc;
894 /* Find the best entry in the directory */
896 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
897 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
898 if (fCursor)
899 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
900 width, height, 1);
901 else
902 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon( dir,
903 width, height, colors );
904 if (!dirEntry) return 0;
905 wResId = dirEntry->wResId;
906 dwBytesInRes = dirEntry->dwBytesInRes;
907 FreeResource( handle );
909 /* Load the resource */
911 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
912 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
914 /* If shared icon, check whether it was already loaded */
915 if ( (loadflags & LR_SHARED)
916 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
917 return hIcon;
919 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
920 bits = (LPBYTE)LockResource( handle );
921 hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dwBytesInRes,
922 !fCursor, 0x00030000, width, height, loadflags);
923 FreeResource( handle );
925 /* If shared icon, add to icon cache */
927 if ( hIcon && (loadflags & LR_SHARED) )
928 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
931 return hIcon;
934 /***********************************************************************
935 * CURSORICON_Copy
937 * Make a copy of a cursor or icon.
939 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
941 char *ptrOld, *ptrNew;
942 int size;
943 HICON16 hOld = HICON_16(hIcon);
944 HICON16 hNew;
946 if (!(ptrOld = (char *)GlobalLock16( hOld ))) return 0;
947 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
948 size = GlobalSize16( hOld );
949 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
950 FarSetOwner16( hNew, hInst16 );
951 ptrNew = (char *)GlobalLock16( hNew );
952 memcpy( ptrNew, ptrOld, size );
953 GlobalUnlock16( hOld );
954 GlobalUnlock16( hNew );
955 return HICON_32(hNew);
958 /*************************************************************************
959 * CURSORICON_ExtCopy
961 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
963 * PARAMS
964 * Handle [I] handle to an Image
965 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
966 * iDesiredCX [I] The Desired width of the Image
967 * iDesiredCY [I] The desired height of the Image
968 * nFlags [I] The flags from CopyImage
970 * RETURNS
971 * Success: The new handle of the Image
973 * NOTES
974 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
975 * LR_MONOCHROME should be implemented by CURSORICON_CreateFromResource.
976 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
981 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
982 INT iDesiredCX, INT iDesiredCY,
983 UINT nFlags)
985 HICON hNew=0;
987 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
988 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
990 if(hIcon == 0)
992 return 0;
995 /* Best Fit or Monochrome */
996 if( (nFlags & LR_COPYFROMRESOURCE
997 && (iDesiredCX > 0 || iDesiredCY > 0))
998 || nFlags & LR_MONOCHROME)
1000 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1002 /* Not Found in Cache, then do a straight copy
1004 if(pIconCache == NULL)
1006 hNew = CURSORICON_Copy(0, hIcon);
1007 if(nFlags & LR_COPYFROMRESOURCE)
1009 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1012 else
1014 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1015 LPBYTE pBits;
1016 HANDLE hMem;
1017 HRSRC hRsrc;
1018 DWORD dwBytesInRes;
1019 WORD wResId;
1020 CURSORICONDIR *pDir;
1021 CURSORICONDIRENTRY *pDirEntry;
1022 BOOL bIsIcon = (nType == IMAGE_ICON);
1024 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1026 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1027 || (iDesiredCX == 0 && iDesiredCY == 0))
1029 iDesiredCY = GetSystemMetrics(bIsIcon ?
1030 SM_CYICON : SM_CYCURSOR);
1031 iDesiredCX = GetSystemMetrics(bIsIcon ?
1032 SM_CXICON : SM_CXCURSOR);
1035 /* Retrieve the CURSORICONDIRENTRY
1037 if (!(hMem = LoadResource( pIconCache->hModule ,
1038 pIconCache->hGroupRsrc)))
1040 return 0;
1042 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1044 return 0;
1047 /* Find Best Fit
1049 if(bIsIcon)
1051 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
1052 pDir, iDesiredCX, iDesiredCY, 256);
1054 else
1056 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
1057 pDir, iDesiredCX, iDesiredCY, 1);
1060 wResId = pDirEntry->wResId;
1061 dwBytesInRes = pDirEntry->dwBytesInRes;
1062 FreeResource(hMem);
1064 TRACE_(icon)("ResID %u, BytesInRes %lu, Width %d, Height %d DX %d, DY %d\n",
1065 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1066 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1068 /* Get the Best Fit
1070 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1071 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1073 return 0;
1075 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1077 return 0;
1080 pBits = (LPBYTE)LockResource( hMem );
1082 if(nFlags & LR_DEFAULTSIZE)
1084 iTargetCY = GetSystemMetrics(SM_CYICON);
1085 iTargetCX = GetSystemMetrics(SM_CXICON);
1088 /* Create a New Icon with the proper dimension
1090 hNew = CURSORICON_CreateFromResource( 0, 0, pBits, dwBytesInRes,
1091 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1092 FreeResource(hMem);
1095 else hNew = CURSORICON_Copy(0, hIcon);
1096 return hNew;
1100 /***********************************************************************
1101 * CreateCursor (USER32.@)
1103 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1104 INT xHotSpot, INT yHotSpot,
1105 INT nWidth, INT nHeight,
1106 LPCVOID lpANDbits, LPCVOID lpXORbits )
1108 CURSORICONINFO info;
1110 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1111 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1113 info.ptHotSpot.x = xHotSpot;
1114 info.ptHotSpot.y = yHotSpot;
1115 info.nWidth = nWidth;
1116 info.nHeight = nHeight;
1117 info.nWidthBytes = 0;
1118 info.bPlanes = 1;
1119 info.bBitsPerPixel = 1;
1121 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1125 /***********************************************************************
1126 * CreateIcon (USER.407)
1128 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1129 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1130 LPCVOID lpANDbits, LPCVOID lpXORbits )
1132 CURSORICONINFO info;
1134 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1135 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1137 info.ptHotSpot.x = ICON_HOTSPOT;
1138 info.ptHotSpot.y = ICON_HOTSPOT;
1139 info.nWidth = nWidth;
1140 info.nHeight = nHeight;
1141 info.nWidthBytes = 0;
1142 info.bPlanes = bPlanes;
1143 info.bBitsPerPixel = bBitsPixel;
1145 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1149 /***********************************************************************
1150 * CreateIcon (USER32.@)
1152 * Creates an icon based on the specified bitmaps. The bitmaps must be
1153 * provided in a device dependent format and will be resized to
1154 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1155 * depth. The provided bitmaps must be top-down bitmaps.
1156 * Although Windows does not support 15bpp(*) this API must support it
1157 * for Winelib applications.
1159 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1160 * format!
1162 * BUGS
1164 * - The provided bitmaps are not resized!
1165 * - The documentation says the lpXORbits bitmap must be in a device
1166 * dependent format. But we must still resize it and perform depth
1167 * conversions if necessary.
1168 * - I'm a bit unsure about the how the 'device dependent format' thing works.
1169 * I did some tests on windows and found that if you provide a 16bpp bitmap
1170 * in lpXORbits, then its format but be 565 RGB if the screen's bit depth
1171 * is 16bpp but it must be 555 RGB if the screen's bit depth is anything
1172 * else. I don't know if this is part of the GDI specs or if this is a
1173 * quirk of the graphics card driver.
1174 * - You may think that we check whether the bit depths match or not
1175 * as an optimization. But the truth is that the conversion using
1176 * CreateDIBitmap does not work for some bit depth (e.g. 8bpp) and I have
1177 * no idea why.
1178 * - I'm pretty sure that all the things we do in CreateIcon should
1179 * also be done in CreateIconIndirect...
1181 HICON WINAPI CreateIcon(
1182 HINSTANCE hInstance, /* [in] the application's hInstance */
1183 INT nWidth, /* [in] the width of the provided bitmaps */
1184 INT nHeight, /* [in] the height of the provided bitmaps */
1185 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1186 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1187 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1188 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1190 HICON hIcon;
1191 HDC hdc;
1193 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1194 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1196 hdc=GetDC(0);
1197 if (!hdc)
1198 return 0;
1200 if (GetDeviceCaps(hdc,BITSPIXEL)==bBitsPixel) {
1201 CURSORICONINFO info;
1203 info.ptHotSpot.x = ICON_HOTSPOT;
1204 info.ptHotSpot.y = ICON_HOTSPOT;
1205 info.nWidth = nWidth;
1206 info.nHeight = nHeight;
1207 info.nWidthBytes = 0;
1208 info.bPlanes = bPlanes;
1209 info.bBitsPerPixel = bBitsPixel;
1211 hIcon=HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1212 } else {
1213 ICONINFO iinfo;
1214 BITMAPINFO bmi;
1216 iinfo.fIcon=TRUE;
1217 iinfo.xHotspot=ICON_HOTSPOT;
1218 iinfo.yHotspot=ICON_HOTSPOT;
1219 iinfo.hbmMask=CreateBitmap(nWidth,nHeight,1,1,lpANDbits);
1221 bmi.bmiHeader.biSize=sizeof(bmi.bmiHeader);
1222 bmi.bmiHeader.biWidth=nWidth;
1223 bmi.bmiHeader.biHeight=-nHeight;
1224 bmi.bmiHeader.biPlanes=bPlanes;
1225 bmi.bmiHeader.biBitCount=bBitsPixel;
1226 bmi.bmiHeader.biCompression=BI_RGB;
1227 bmi.bmiHeader.biSizeImage=0;
1228 bmi.bmiHeader.biXPelsPerMeter=0;
1229 bmi.bmiHeader.biYPelsPerMeter=0;
1230 bmi.bmiHeader.biClrUsed=0;
1231 bmi.bmiHeader.biClrImportant=0;
1233 iinfo.hbmColor = CreateDIBitmap( hdc, &bmi.bmiHeader,
1234 CBM_INIT, lpXORbits,
1235 &bmi, DIB_RGB_COLORS );
1237 hIcon=CreateIconIndirect(&iinfo);
1238 DeleteObject(iinfo.hbmMask);
1239 DeleteObject(iinfo.hbmColor);
1241 ReleaseDC(0,hdc);
1242 return hIcon;
1246 /***********************************************************************
1247 * CreateCursorIconIndirect (USER.408)
1249 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1250 CURSORICONINFO *info,
1251 LPCVOID lpANDbits,
1252 LPCVOID lpXORbits )
1254 HGLOBAL16 handle;
1255 char *ptr;
1256 int sizeAnd, sizeXor;
1258 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1259 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1260 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1261 sizeXor = info->nHeight * info->nWidthBytes;
1262 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1263 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1264 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1265 return 0;
1266 FarSetOwner16( handle, hInstance );
1267 ptr = (char *)GlobalLock16( handle );
1268 memcpy( ptr, info, sizeof(*info) );
1269 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1270 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1271 GlobalUnlock16( handle );
1272 return handle;
1276 /***********************************************************************
1277 * CopyIcon (USER.368)
1279 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1281 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1282 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1286 /***********************************************************************
1287 * CopyIcon (USER32.@)
1289 HICON WINAPI CopyIcon( HICON hIcon )
1291 TRACE_(icon)("%p\n", hIcon );
1292 return CURSORICON_Copy( 0, hIcon );
1296 /***********************************************************************
1297 * CopyCursor (USER.369)
1299 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1301 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1302 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1305 /**********************************************************************
1306 * DestroyIcon32 (USER.610)
1308 * This routine is actually exported from Win95 USER under the name
1309 * DestroyIcon32 ... The behaviour implemented here should mimic
1310 * the Win95 one exactly, especially the return values, which
1311 * depend on the setting of various flags.
1313 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1315 WORD retv;
1317 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1319 /* Check whether destroying active cursor */
1321 if ( QUEUE_Current()->cursor == HICON_32(handle) )
1323 WARN_(cursor)("Destroying active cursor!\n" );
1324 SetCursor( 0 );
1327 /* Try shared cursor/icon first */
1329 if ( !(flags & CID_NONSHARED) )
1331 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1333 if ( count != -1 )
1334 return (flags & CID_WIN32)? TRUE : (count == 0);
1336 /* FIXME: OEM cursors/icons should be recognized */
1339 /* Now assume non-shared cursor/icon */
1341 retv = GlobalFree16( handle );
1342 return (flags & CID_RESOURCE)? retv : TRUE;
1345 /***********************************************************************
1346 * DestroyIcon (USER32.@)
1348 BOOL WINAPI DestroyIcon( HICON hIcon )
1350 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1354 /***********************************************************************
1355 * DestroyCursor (USER32.@)
1357 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1359 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1363 /***********************************************************************
1364 * DrawIcon (USER32.@)
1366 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1368 CURSORICONINFO *ptr;
1369 HDC hMemDC;
1370 HBITMAP hXorBits, hAndBits;
1371 COLORREF oldFg, oldBg;
1373 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
1374 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1375 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
1376 (char *)(ptr+1) );
1377 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1378 ptr->bBitsPerPixel, (char *)(ptr + 1)
1379 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
1380 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1381 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1383 if (hXorBits && hAndBits)
1385 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1386 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
1387 SelectObject( hMemDC, hXorBits );
1388 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
1389 SelectObject( hMemDC, hBitTemp );
1391 DeleteDC( hMemDC );
1392 if (hXorBits) DeleteObject( hXorBits );
1393 if (hAndBits) DeleteObject( hAndBits );
1394 GlobalUnlock16(HICON_16(hIcon));
1395 SetTextColor( hdc, oldFg );
1396 SetBkColor( hdc, oldBg );
1397 return TRUE;
1400 /***********************************************************************
1401 * DumpIcon (USER.459)
1403 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1404 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1406 CURSORICONINFO *info = MapSL( pInfo );
1407 int sizeAnd, sizeXor;
1409 if (!info) return 0;
1410 sizeXor = info->nHeight * info->nWidthBytes;
1411 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1412 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1413 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1414 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1415 return MAKELONG( sizeXor, sizeXor );
1419 /***********************************************************************
1420 * SetCursor (USER32.@)
1421 * RETURNS:
1422 * A handle to the previous cursor shape.
1424 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1426 MESSAGEQUEUE *queue = QUEUE_Current();
1427 HCURSOR hOldCursor;
1429 if (hCursor == queue->cursor) return hCursor; /* No change */
1430 TRACE_(cursor)("%p\n", hCursor );
1431 hOldCursor = queue->cursor;
1432 queue->cursor = hCursor;
1433 /* Change the cursor shape only if it is visible */
1434 if (queue->cursor_count >= 0)
1436 USER_Driver.pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
1437 GlobalUnlock16(HCURSOR_16(hCursor));
1439 return hOldCursor;
1442 /***********************************************************************
1443 * ShowCursor (USER32.@)
1445 INT WINAPI ShowCursor( BOOL bShow )
1447 MESSAGEQUEUE *queue = QUEUE_Current();
1449 TRACE_(cursor)("%d, count=%d\n", bShow, queue->cursor_count );
1451 if (bShow)
1453 if (++queue->cursor_count == 0) /* Show it */
1455 USER_Driver.pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(queue->cursor)));
1456 GlobalUnlock16(HCURSOR_16(queue->cursor));
1459 else
1461 if (--queue->cursor_count == -1) /* Hide it */
1462 USER_Driver.pSetCursor( NULL );
1464 return queue->cursor_count;
1467 /***********************************************************************
1468 * GetCursor (USER32.@)
1470 HCURSOR WINAPI GetCursor(void)
1472 return QUEUE_Current()->cursor;
1476 /***********************************************************************
1477 * ClipCursor (USER32.@)
1479 BOOL WINAPI ClipCursor( const RECT *rect )
1481 if (!rect) SetRectEmpty( &CURSOR_ClipRect );
1482 else CopyRect( &CURSOR_ClipRect, rect );
1483 return TRUE;
1487 /***********************************************************************
1488 * GetClipCursor (USER32.@)
1490 BOOL WINAPI GetClipCursor( RECT *rect )
1492 if (rect)
1494 CopyRect( rect, &CURSOR_ClipRect );
1495 return TRUE;
1497 return FALSE;
1500 /**********************************************************************
1501 * LookupIconIdFromDirectoryEx (USER.364)
1503 * FIXME: exact parameter sizes
1505 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1506 INT16 width, INT16 height, UINT16 cFlag )
1508 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1511 /**********************************************************************
1512 * LookupIconIdFromDirectoryEx (USER32.@)
1514 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1515 INT width, INT height, UINT cFlag )
1517 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1518 UINT retVal = 0;
1519 if( dir && !dir->idReserved && (dir->idType & 3) )
1521 CURSORICONDIRENTRY* entry;
1522 HDC hdc;
1523 UINT palEnts;
1524 int colors;
1525 hdc = GetDC(0);
1526 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1527 if (palEnts == 0)
1528 palEnts = 256;
1529 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1531 ReleaseDC(0, hdc);
1533 if( bIcon )
1534 entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1535 else
1536 entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1538 if( entry ) retVal = entry->wResId;
1540 else WARN_(cursor)("invalid resource directory\n");
1541 return retVal;
1544 /**********************************************************************
1545 * LookupIconIdFromDirectory (USER.?)
1547 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
1549 return LookupIconIdFromDirectoryEx16( dir, bIcon,
1550 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1551 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1554 /**********************************************************************
1555 * LookupIconIdFromDirectory (USER32.@)
1557 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1559 return LookupIconIdFromDirectoryEx( dir, bIcon,
1560 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1561 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1564 /**********************************************************************
1565 * GetIconID (USER.455)
1567 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1569 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
1571 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1572 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1574 switch(resType)
1576 case RT_CURSOR:
1577 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1578 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1579 case RT_ICON:
1580 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1581 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1582 default:
1583 WARN_(cursor)("invalid res type %ld\n", resType );
1585 return 0;
1588 /**********************************************************************
1589 * LoadCursorIconHandler (USER.336)
1591 * Supposed to load resources of Windows 2.x applications.
1593 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1595 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1596 hResource, hModule, hRsrc);
1597 return (HGLOBAL16)0;
1600 /**********************************************************************
1601 * LoadDIBIconHandler (USER.357)
1603 * RT_ICON resource loader, installed by USER_SignalProc when module
1604 * is initialized.
1606 HGLOBAL16 WINAPI LoadDIBIconHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1608 /* If hResource is zero we must allocate a new memory block, if it's
1609 * non-zero but GlobalLock() returns NULL then it was discarded and
1610 * we have to recommit some memory, otherwise we just need to check
1611 * the block size. See LoadProc() in 16-bit SDK for more.
1614 hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
1615 if( hMemObj )
1617 LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1618 hMemObj = HICON_16(CURSORICON_CreateFromResource(
1619 hModule, hMemObj, bits,
1620 SizeofResource16(hModule, hRsrc), TRUE, 0x00030000,
1621 GetSystemMetrics(SM_CXICON),
1622 GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR));
1624 return hMemObj;
1627 /**********************************************************************
1628 * LoadDIBCursorHandler (USER.356)
1630 * RT_CURSOR resource loader. Same as above.
1632 HGLOBAL16 WINAPI LoadDIBCursorHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1634 hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
1635 if( hMemObj )
1637 LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1638 hMemObj = HICON_16(CURSORICON_CreateFromResource(
1639 hModule, hMemObj, bits,
1640 SizeofResource16(hModule, hRsrc), FALSE, 0x00030000,
1641 GetSystemMetrics(SM_CXCURSOR),
1642 GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME));
1644 return hMemObj;
1647 /**********************************************************************
1648 * LoadIconHandler (USER.456)
1650 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
1652 LPBYTE bits = (LPBYTE)LockResource16( hResource );
1654 TRACE_(cursor)("hRes=%04x\n",hResource);
1656 return HICON_16(CURSORICON_CreateFromResource(0, 0, bits, 0, TRUE,
1657 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
1660 /***********************************************************************
1661 * LoadCursorW (USER32.@)
1663 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1665 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1666 LR_SHARED | LR_DEFAULTSIZE );
1669 /***********************************************************************
1670 * LoadCursorA (USER32.@)
1672 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1674 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1675 LR_SHARED | LR_DEFAULTSIZE );
1678 /***********************************************************************
1679 * LoadCursorFromFileW (USER32.@)
1681 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1683 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1684 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1687 /***********************************************************************
1688 * LoadCursorFromFileA (USER32.@)
1690 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1692 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1693 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1696 /***********************************************************************
1697 * LoadIconW (USER32.@)
1699 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1701 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1702 LR_SHARED | LR_DEFAULTSIZE );
1705 /***********************************************************************
1706 * LoadIconA (USER32.@)
1708 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1710 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1711 LR_SHARED | LR_DEFAULTSIZE );
1714 /**********************************************************************
1715 * GetIconInfo (USER32.@)
1717 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1719 CURSORICONINFO *ciconinfo;
1720 INT height;
1722 ciconinfo = GlobalLock16(HICON_16(hIcon));
1723 if (!ciconinfo)
1724 return FALSE;
1726 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1727 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1729 iconinfo->fIcon = TRUE;
1730 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1731 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1733 else
1735 iconinfo->fIcon = FALSE;
1736 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1737 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1740 if (ciconinfo->bBitsPerPixel > 1)
1742 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
1743 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1744 (char *)(ciconinfo + 1)
1745 + ciconinfo->nHeight *
1746 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1747 height = ciconinfo->nHeight;
1749 else
1751 iconinfo->hbmColor = 0;
1752 height = ciconinfo->nHeight * 2;
1755 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
1756 1, 1, (char *)(ciconinfo + 1));
1758 GlobalUnlock16(HICON_16(hIcon));
1760 return TRUE;
1763 /**********************************************************************
1764 * CreateIconIndirect (USER32.@)
1766 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1768 BITMAP bmpXor,bmpAnd;
1769 HICON16 hObj;
1770 int sizeXor,sizeAnd;
1772 GetObjectA( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1773 GetObjectA( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1775 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1776 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1778 hObj = GlobalAlloc16( GMEM_MOVEABLE,
1779 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1780 if (hObj)
1782 CURSORICONINFO *info;
1784 info = (CURSORICONINFO *)GlobalLock16( hObj );
1786 /* If we are creating an icon, the hotspot is unused */
1787 if (iconinfo->fIcon)
1789 info->ptHotSpot.x = ICON_HOTSPOT;
1790 info->ptHotSpot.y = ICON_HOTSPOT;
1792 else
1794 info->ptHotSpot.x = iconinfo->xHotspot;
1795 info->ptHotSpot.y = iconinfo->yHotspot;
1798 info->nWidth = bmpXor.bmWidth;
1799 info->nHeight = bmpXor.bmHeight;
1800 info->nWidthBytes = bmpXor.bmWidthBytes;
1801 info->bPlanes = bmpXor.bmPlanes;
1802 info->bBitsPerPixel = bmpXor.bmBitsPixel;
1804 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1806 GetBitmapBits( iconinfo->hbmMask ,sizeAnd,(char*)(info + 1) );
1807 GetBitmapBits( iconinfo->hbmColor,sizeXor,(char*)(info + 1) +sizeAnd);
1808 GlobalUnlock16( hObj );
1810 return HICON_32(hObj);
1813 /******************************************************************************
1814 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1816 * NOTES
1817 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1819 * PARAMS
1820 * hdc [I] Handle to device context
1821 * x0 [I] X coordinate of upper left corner
1822 * y0 [I] Y coordinate of upper left corner
1823 * hIcon [I] Handle to icon to draw
1824 * cxWidth [I] Width of icon
1825 * cyWidth [I] Height of icon
1826 * istep [I] Index of frame in animated cursor
1827 * hbr [I] Handle to background brush
1828 * flags [I] Icon-drawing flags
1830 * RETURNS
1831 * Success: TRUE
1832 * Failure: FALSE
1834 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1835 INT cxWidth, INT cyWidth, UINT istep,
1836 HBRUSH hbr, UINT flags )
1838 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
1839 HDC hDC_off = 0, hMemDC;
1840 BOOL result = FALSE, DoOffscreen;
1841 HBITMAP hB_off = 0, hOld = 0;
1843 if (!ptr) return FALSE;
1844 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1845 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1847 hMemDC = CreateCompatibleDC (hdc);
1848 if (istep)
1849 FIXME_(icon)("Ignoring istep=%d\n", istep);
1850 if (flags & DI_COMPAT)
1851 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
1853 if (!flags) {
1854 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
1855 flags = DI_NORMAL;
1858 /* Calculate the size of the destination image. */
1859 if (cxWidth == 0)
1861 if (flags & DI_DEFAULTSIZE)
1862 cxWidth = GetSystemMetrics (SM_CXICON);
1863 else
1864 cxWidth = ptr->nWidth;
1866 if (cyWidth == 0)
1868 if (flags & DI_DEFAULTSIZE)
1869 cyWidth = GetSystemMetrics (SM_CYICON);
1870 else
1871 cyWidth = ptr->nHeight;
1874 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1876 if (DoOffscreen) {
1877 RECT r;
1879 r.left = 0;
1880 r.top = 0;
1881 r.right = cxWidth;
1882 r.bottom = cxWidth;
1884 hDC_off = CreateCompatibleDC(hdc);
1885 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
1886 if (hDC_off && hB_off) {
1887 hOld = SelectObject(hDC_off, hB_off);
1888 FillRect(hDC_off, &r, hbr);
1892 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
1894 HBITMAP hXorBits, hAndBits;
1895 COLORREF oldFg, oldBg;
1896 INT nStretchMode;
1898 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
1900 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1901 ptr->bPlanes, ptr->bBitsPerPixel,
1902 (char *)(ptr + 1)
1903 + ptr->nHeight *
1904 get_bitmap_width_bytes(ptr->nWidth,1) );
1905 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1906 1, 1, (char *)(ptr+1) );
1907 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1908 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1910 if (hXorBits && hAndBits)
1912 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1913 if (flags & DI_MASK)
1915 if (DoOffscreen)
1916 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1917 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1918 else
1919 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1920 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1922 SelectObject( hMemDC, hXorBits );
1923 if (flags & DI_IMAGE)
1925 if (DoOffscreen)
1926 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1927 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1928 else
1929 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1930 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1932 SelectObject( hMemDC, hBitTemp );
1933 result = TRUE;
1936 SetTextColor( hdc, oldFg );
1937 SetBkColor( hdc, oldBg );
1938 if (hXorBits) DeleteObject( hXorBits );
1939 if (hAndBits) DeleteObject( hAndBits );
1940 SetStretchBltMode (hdc, nStretchMode);
1941 if (DoOffscreen) {
1942 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
1943 SelectObject(hDC_off, hOld);
1946 if (hMemDC) DeleteDC( hMemDC );
1947 if (hDC_off) DeleteDC(hDC_off);
1948 if (hB_off) DeleteObject(hB_off);
1949 GlobalUnlock16(HICON_16(hIcon));
1950 return result;
1953 /***********************************************************************
1954 * DIB_FixColorsToLoadflags
1956 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
1957 * are in loadflags
1959 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
1961 int colors;
1962 COLORREF c_W, c_S, c_F, c_L, c_C;
1963 int incr,i;
1964 RGBQUAD *ptr;
1966 if (bmi->bmiHeader.biBitCount > 8) return;
1967 if (bmi->bmiHeader.biSize == sizeof(BITMAPINFOHEADER)) incr = 4;
1968 else if (bmi->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) incr = 3;
1969 else {
1970 WARN_(resource)("Wrong bitmap header size!\n");
1971 return;
1973 colors = bmi->bmiHeader.biClrUsed;
1974 if (!colors && (bmi->bmiHeader.biBitCount <= 8))
1975 colors = 1 << bmi->bmiHeader.biBitCount;
1976 c_W = GetSysColor(COLOR_WINDOW);
1977 c_S = GetSysColor(COLOR_3DSHADOW);
1978 c_F = GetSysColor(COLOR_3DFACE);
1979 c_L = GetSysColor(COLOR_3DLIGHT);
1980 if (loadflags & LR_LOADTRANSPARENT) {
1981 switch (bmi->bmiHeader.biBitCount) {
1982 case 1: pix = pix >> 7; break;
1983 case 4: pix = pix >> 4; break;
1984 case 8: break;
1985 default:
1986 WARN_(resource)("(%d): Unsupported depth\n", bmi->bmiHeader.biBitCount);
1987 return;
1989 if (pix >= colors) {
1990 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
1991 return;
1993 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
1994 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
1995 ptr->rgbBlue = GetBValue(c_W);
1996 ptr->rgbGreen = GetGValue(c_W);
1997 ptr->rgbRed = GetRValue(c_W);
1999 if (loadflags & LR_LOADMAP3DCOLORS)
2000 for (i=0; i<colors; i++) {
2001 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2002 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2003 if (c_C == RGB(128, 128, 128)) {
2004 ptr->rgbRed = GetRValue(c_S);
2005 ptr->rgbGreen = GetGValue(c_S);
2006 ptr->rgbBlue = GetBValue(c_S);
2007 } else if (c_C == RGB(192, 192, 192)) {
2008 ptr->rgbRed = GetRValue(c_F);
2009 ptr->rgbGreen = GetGValue(c_F);
2010 ptr->rgbBlue = GetBValue(c_F);
2011 } else if (c_C == RGB(223, 223, 223)) {
2012 ptr->rgbRed = GetRValue(c_L);
2013 ptr->rgbGreen = GetGValue(c_L);
2014 ptr->rgbBlue = GetBValue(c_L);
2020 /**********************************************************************
2021 * BITMAP_Load
2023 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name, UINT loadflags )
2025 HBITMAP hbitmap = 0;
2026 HRSRC hRsrc;
2027 HGLOBAL handle;
2028 char *ptr = NULL;
2029 BITMAPINFO *info, *fix_info=NULL;
2030 HGLOBAL hFix;
2031 int size;
2033 if (!(loadflags & LR_LOADFROMFILE))
2035 if (!instance)
2037 /* OEM bitmap: try to load the resource from user32.dll */
2038 if (HIWORD(name)) return 0;
2039 instance = user32_module;
2042 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2043 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2045 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
2047 else
2049 if (!(ptr = map_fileW( name ))) return 0;
2050 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2053 size = bitmap_info_size(info, DIB_RGB_COLORS);
2054 if ((hFix = GlobalAlloc(0, size))) fix_info=GlobalLock(hFix);
2056 if (fix_info) {
2057 BYTE pix;
2059 memcpy(fix_info, info, size);
2060 pix = *((LPBYTE)info + size);
2061 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2062 if (!screen_dc) screen_dc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2064 if (screen_dc)
2066 char *bits = (char *)info + size;
2068 if (loadflags & LR_CREATEDIBSECTION) {
2069 DIBSECTION dib;
2070 hbitmap = CreateDIBSection(screen_dc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
2071 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
2072 SetDIBits(screen_dc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
2073 DIB_RGB_COLORS);
2075 else {
2076 /* If it's possible, create a monochrome bitmap */
2078 LONG height = fix_info->bmiHeader.biHeight;
2079 if (height < 0) height = -height;
2081 if (is_dib_monochrome(fix_info))
2082 hbitmap = CreateBitmap(fix_info->bmiHeader.biWidth, height, 1, 1, NULL);
2083 else
2084 hbitmap = CreateBitmap(fix_info->bmiHeader.biWidth, height,
2085 GetDeviceCaps(screen_dc, PLANES),
2086 GetDeviceCaps(screen_dc, BITSPIXEL), NULL);
2088 SetDIBits(screen_dc, hbitmap, 0, height, bits, info, DIB_RGB_COLORS);
2092 GlobalUnlock(hFix);
2093 GlobalFree(hFix);
2096 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2098 return hbitmap;
2101 /**********************************************************************
2102 * LoadImageA (USER32.@)
2104 * FIXME: implementation lacks some features, see LR_ defines in winuser.h
2107 /* filter for page-fault exceptions */
2108 static WINE_EXCEPTION_FILTER(page_fault)
2110 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
2111 return EXCEPTION_EXECUTE_HANDLER;
2112 return EXCEPTION_CONTINUE_SEARCH;
2115 /*********************************************************************/
2117 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2118 INT desiredx, INT desiredy, UINT loadflags)
2120 HANDLE res;
2121 LPWSTR u_name;
2123 if (!HIWORD(name))
2124 return LoadImageW(hinst, (LPWSTR)name, type, desiredx, desiredy, loadflags);
2126 __TRY {
2127 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2128 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2129 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2131 __EXCEPT(page_fault) {
2132 SetLastError( ERROR_INVALID_PARAMETER );
2133 return 0;
2135 __ENDTRY
2136 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2137 HeapFree(GetProcessHeap(), 0, u_name);
2138 return res;
2142 /******************************************************************************
2143 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2145 * PARAMS
2146 * hinst [I] Handle of instance that contains image
2147 * name [I] Name of image
2148 * type [I] Type of image
2149 * desiredx [I] Desired width
2150 * desiredy [I] Desired height
2151 * loadflags [I] Load flags
2153 * RETURNS
2154 * Success: Handle to newly loaded image
2155 * Failure: NULL
2157 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2159 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2160 INT desiredx, INT desiredy, UINT loadflags )
2162 if (HIWORD(name)) {
2163 TRACE_(resource)("(%p,%p,%d,%d,%d,0x%08x)\n",
2164 hinst,name,type,desiredx,desiredy,loadflags);
2165 } else {
2166 TRACE_(resource)("(%p,%p,%d,%d,%d,0x%08x)\n",
2167 hinst,name,type,desiredx,desiredy,loadflags);
2169 if (loadflags & LR_DEFAULTSIZE) {
2170 if (type == IMAGE_ICON) {
2171 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2172 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2173 } else if (type == IMAGE_CURSOR) {
2174 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2175 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2178 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2179 switch (type) {
2180 case IMAGE_BITMAP:
2181 return BITMAP_Load( hinst, name, loadflags );
2183 case IMAGE_ICON:
2184 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2185 if (screen_dc)
2187 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2188 if (palEnts == 0) palEnts = 256;
2189 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2190 palEnts, FALSE, loadflags);
2192 break;
2194 case IMAGE_CURSOR:
2195 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2196 1, TRUE, loadflags);
2198 return 0;
2201 /******************************************************************************
2202 * CopyImage (USER32.@) Creates new image and copies attributes to it
2204 * PARAMS
2205 * hnd [I] Handle to image to copy
2206 * type [I] Type of image to copy
2207 * desiredx [I] Desired width of new image
2208 * desiredy [I] Desired height of new image
2209 * flags [I] Copy flags
2211 * RETURNS
2212 * Success: Handle to newly created image
2213 * Failure: NULL
2215 * FIXME: implementation still lacks nearly all features, see LR_*
2216 * defines in winuser.h
2218 HICON WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2219 INT desiredy, UINT flags )
2221 switch (type)
2223 case IMAGE_BITMAP:
2225 HBITMAP res;
2226 BITMAP bm;
2228 if (!GetObjectW( hnd, sizeof(bm), &bm )) return 0;
2229 bm.bmBits = NULL;
2230 if ((res = CreateBitmapIndirect(&bm)))
2232 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight );
2233 GetBitmapBits( hnd, bm.bmWidthBytes * bm.bmHeight, buf );
2234 SetBitmapBits( res, bm.bmWidthBytes * bm.bmHeight, buf );
2235 HeapFree( GetProcessHeap(), 0, buf );
2237 return (HICON)res;
2239 case IMAGE_ICON:
2240 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2241 case IMAGE_CURSOR:
2242 /* Should call CURSORICON_ExtCopy but more testing
2243 * needs to be done before we change this
2245 return CopyCursor(hnd);
2247 return 0;
2251 /******************************************************************************
2252 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2254 * RETURNS
2255 * Success: Handle to specified bitmap
2256 * Failure: NULL
2258 HBITMAP WINAPI LoadBitmapW(
2259 HINSTANCE instance, /* [in] Handle to application instance */
2260 LPCWSTR name) /* [in] Address of bitmap resource name */
2262 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2265 /**********************************************************************
2266 * LoadBitmapA (USER32.@)
2268 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2270 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );