Authors: Mike McCormack <mike@codeweavers.com>, Aric Stewart <aric@codeweavers.com...
[wine/multimedia.git] / windows / cursoricon.c
blob31001234d3bd2208c7577ca336e27ed98a1eae57
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_private.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 = CreateFileMappingW( 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 const BITMAPCOREHEADER *core = (const 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;
276 /***********************************************************************
277 * DIB_GetBitmapInfo
279 * Get the info from a bitmap header.
280 * Return 1 for INFOHEADER, 0 for COREHEADER,
281 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
283 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
284 LONG *height, WORD *bpp, DWORD *compr )
286 if (header->biSize == sizeof(BITMAPINFOHEADER))
288 *width = header->biWidth;
289 *height = header->biHeight;
290 *bpp = header->biBitCount;
291 *compr = header->biCompression;
292 return 1;
294 if (header->biSize == sizeof(BITMAPCOREHEADER))
296 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
297 *width = core->bcWidth;
298 *height = core->bcHeight;
299 *bpp = core->bcBitCount;
300 *compr = 0;
301 return 0;
303 if (header->biSize == sizeof(BITMAPV4HEADER))
305 BITMAPV4HEADER *v4hdr = (BITMAPV4HEADER *)header;
306 *width = v4hdr->bV4Width;
307 *height = v4hdr->bV4Height;
308 *bpp = v4hdr->bV4BitCount;
309 *compr = v4hdr->bV4V4Compression;
310 return 4;
312 if (header->biSize == sizeof(BITMAPV5HEADER))
314 BITMAPV5HEADER *v5hdr = (BITMAPV5HEADER *)header;
315 *width = v5hdr->bV5Width;
316 *height = v5hdr->bV5Height;
317 *bpp = v5hdr->bV5BitCount;
318 *compr = v5hdr->bV5Compression;
319 return 5;
321 ERR("(%ld): unknown/wrong size for header\n", header->biSize );
322 return -1;
325 /**********************************************************************
326 * CURSORICON_FindSharedIcon
328 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
330 HICON hIcon = 0;
331 ICONCACHE *ptr;
333 EnterCriticalSection( &IconCrst );
335 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
336 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
338 ptr->count++;
339 hIcon = ptr->hIcon;
340 break;
343 LeaveCriticalSection( &IconCrst );
345 return hIcon;
348 /*************************************************************************
349 * CURSORICON_FindCache
351 * Given a handle, find the corresponding cache element
353 * PARAMS
354 * Handle [I] handle to an Image
356 * RETURNS
357 * Success: The cache entry
358 * Failure: NULL
361 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
363 ICONCACHE *ptr;
364 ICONCACHE *pRet=NULL;
365 BOOL IsFound = FALSE;
366 int count;
368 EnterCriticalSection( &IconCrst );
370 for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
372 if ( hIcon == ptr->hIcon )
374 IsFound = TRUE;
375 pRet = ptr;
379 LeaveCriticalSection( &IconCrst );
381 return pRet;
384 /**********************************************************************
385 * CURSORICON_AddSharedIcon
387 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
389 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
390 if ( !ptr ) return;
392 ptr->hModule = hModule;
393 ptr->hRsrc = hRsrc;
394 ptr->hIcon = hIcon;
395 ptr->hGroupRsrc = hGroupRsrc;
396 ptr->count = 1;
398 EnterCriticalSection( &IconCrst );
399 ptr->next = IconAnchor;
400 IconAnchor = ptr;
401 LeaveCriticalSection( &IconCrst );
404 /**********************************************************************
405 * CURSORICON_DelSharedIcon
407 static INT CURSORICON_DelSharedIcon( HICON hIcon )
409 INT count = -1;
410 ICONCACHE *ptr;
412 EnterCriticalSection( &IconCrst );
414 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
415 if ( ptr->hIcon == hIcon )
417 if ( ptr->count > 0 ) ptr->count--;
418 count = ptr->count;
419 break;
422 LeaveCriticalSection( &IconCrst );
424 return count;
427 /**********************************************************************
428 * CURSORICON_FreeModuleIcons
430 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
432 ICONCACHE **ptr = &IconAnchor;
433 HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
435 EnterCriticalSection( &IconCrst );
437 while ( *ptr )
439 if ( (*ptr)->hModule == hModule )
441 ICONCACHE *freePtr = *ptr;
442 *ptr = freePtr->next;
444 GlobalFree16(HICON_16(freePtr->hIcon));
445 HeapFree( GetProcessHeap(), 0, freePtr );
446 continue;
448 ptr = &(*ptr)->next;
451 LeaveCriticalSection( &IconCrst );
454 /**********************************************************************
455 * CURSORICON_FindBestIcon
457 * Find the icon closest to the requested size and number of colors.
459 static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
460 int height, int colors )
462 int i;
463 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
464 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
465 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
467 if (dir->idCount < 1)
469 WARN_(icon)("Empty directory!\n" );
470 return NULL;
472 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
474 /* Find Best Fit */
475 iTotalDiff = 0xFFFFFFFF;
476 iColorDiff = 0xFFFFFFFF;
477 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
479 iTempXDiff = abs(width - entry->ResInfo.icon.bWidth);
480 iTempYDiff = abs(height - entry->ResInfo.icon.bHeight);
482 if(iTotalDiff > (iTempXDiff + iTempYDiff))
484 iXDiff = iTempXDiff;
485 iYDiff = iTempYDiff;
486 iTotalDiff = iXDiff + iYDiff;
490 /* Find Best Colors for Best Fit */
491 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
493 if(abs(width - entry->ResInfo.icon.bWidth) == iXDiff &&
494 abs(height - entry->ResInfo.icon.bHeight) == iYDiff)
496 iTempColorDiff = abs(colors - (1<<entry->wBitCount));
497 if(iColorDiff > iTempColorDiff)
499 bestEntry = entry;
500 iColorDiff = iTempColorDiff;
505 return bestEntry;
509 /**********************************************************************
510 * CURSORICON_FindBestCursor
512 * Find the cursor closest to the requested size.
513 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
514 * ignored too
516 static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
517 int width, int height, int color)
519 int i, maxwidth, maxheight;
520 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
522 if (dir->idCount < 1)
524 WARN_(cursor)("Empty directory!\n" );
525 return NULL;
527 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
529 /* Double height to account for AND and XOR masks */
531 height *= 2;
533 /* First find the largest one smaller than or equal to the requested size*/
535 maxwidth = maxheight = 0;
536 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
537 if ((entry->ResInfo.cursor.wWidth <= width) && (entry->ResInfo.cursor.wHeight <= height) &&
538 (entry->ResInfo.cursor.wWidth > maxwidth) && (entry->ResInfo.cursor.wHeight > maxheight) &&
539 (entry->wBitCount == 1))
541 bestEntry = entry;
542 maxwidth = entry->ResInfo.cursor.wWidth;
543 maxheight = entry->ResInfo.cursor.wHeight;
545 if (bestEntry) return bestEntry;
547 /* Now find the smallest one larger than the requested size */
549 maxwidth = maxheight = 255;
550 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
551 if ((entry->ResInfo.cursor.wWidth < maxwidth) && (entry->ResInfo.cursor.wHeight < maxheight) &&
552 (entry->wBitCount == 1))
554 bestEntry = entry;
555 maxwidth = entry->ResInfo.cursor.wWidth;
556 maxheight = entry->ResInfo.cursor.wHeight;
559 return bestEntry;
562 /*********************************************************************
563 * The main purpose of this function is to create fake resource directory
564 * and fake resource entries. There are several reasons for this:
565 * - CURSORICONDIR and CURSORICONFILEDIR differ in sizes and their
566 * fields
567 * There are some "bad" cursor files which do not have
568 * bColorCount initialized but instead one must read this info
569 * directly from corresponding DIB sections
570 * Note: wResId is index to array of pointer returned in ptrs (origin is 1)
572 static BOOL CURSORICON_SimulateLoadingFromResourceW( LPCWSTR filename, BOOL fCursor,
573 CURSORICONDIR **res, LPBYTE **ptr)
575 LPBYTE _free;
576 CURSORICONFILEDIR *bits;
577 int entries, size, i;
579 *res = NULL;
580 *ptr = NULL;
581 if (!(bits = map_fileW( filename ))) return FALSE;
583 /* FIXME: test for inimated icons
584 * hack to load the first icon from the *.ani file
586 if ( *(LPDWORD)bits==0x46464952 ) /* "RIFF" */
587 { LPBYTE pos = (LPBYTE) bits;
588 FIXME_(cursor)("Animated icons not correctly implemented! %p \n", bits);
590 for (;;)
591 { if (*(LPDWORD)pos==0x6e6f6369) /* "icon" */
592 { FIXME_(cursor)("icon entry found! %p\n", bits);
593 pos+=4;
594 if ( !*(LPWORD) pos==0x2fe) /* iconsize */
595 { goto fail;
597 bits=(CURSORICONFILEDIR*)(pos+4);
598 FIXME_(cursor)("icon size ok. offset=%p \n", bits);
599 break;
601 pos+=2;
602 if (pos>=(LPBYTE)bits+766) goto fail;
605 if (!(entries = bits->idCount)) goto fail;
606 size = sizeof(CURSORICONDIR) + sizeof(CURSORICONDIRENTRY) * (entries - 1);
607 _free = (LPBYTE) size;
609 for (i=0; i < entries; i++)
610 size += bits->idEntries[i].dwDIBSize + (fCursor ? sizeof(POINT16): 0);
612 if (!(*ptr = HeapAlloc( GetProcessHeap(), 0,
613 entries * sizeof (CURSORICONDIRENTRY*)))) goto fail;
614 if (!(*res = HeapAlloc( GetProcessHeap(), 0, size))) goto fail;
616 _free = (LPBYTE)(*res) + (int)_free;
617 memcpy((*res), bits, 6);
618 for (i=0; i<entries; i++)
620 ((LPBYTE*)(*ptr))[i] = _free;
621 if (fCursor) {
622 (*res)->idEntries[i].ResInfo.cursor.wWidth=bits->idEntries[i].bWidth;
623 (*res)->idEntries[i].ResInfo.cursor.wHeight=bits->idEntries[i].bHeight;
624 ((LPPOINT16)_free)->x=bits->idEntries[i].xHotspot;
625 ((LPPOINT16)_free)->y=bits->idEntries[i].yHotspot;
626 _free+=sizeof(POINT16);
627 } else {
628 (*res)->idEntries[i].ResInfo.icon.bWidth=bits->idEntries[i].bWidth;
629 (*res)->idEntries[i].ResInfo.icon.bHeight=bits->idEntries[i].bHeight;
630 (*res)->idEntries[i].ResInfo.icon.bColorCount = bits->idEntries[i].bColorCount;
632 (*res)->idEntries[i].wPlanes=1;
633 (*res)->idEntries[i].wBitCount = ((LPBITMAPINFOHEADER)((LPBYTE)bits +
634 bits->idEntries[i].dwDIBOffset))->biBitCount;
635 (*res)->idEntries[i].dwBytesInRes = bits->idEntries[i].dwDIBSize;
636 (*res)->idEntries[i].wResId=i+1;
638 memcpy(_free,(LPBYTE)bits +bits->idEntries[i].dwDIBOffset,
639 (*res)->idEntries[i].dwBytesInRes);
640 _free += (*res)->idEntries[i].dwBytesInRes;
642 UnmapViewOfFile( bits );
643 return TRUE;
644 fail:
645 HeapFree( GetProcessHeap(), 0, *res );
646 HeapFree( GetProcessHeap(), 0, *ptr );
647 UnmapViewOfFile( bits );
648 return FALSE;
652 /**********************************************************************
653 * CURSORICON_CreateFromResource
655 * Create a cursor or icon from in-memory resource template.
657 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
658 * with cbSize parameter as well.
660 static HICON CURSORICON_CreateFromResource( HMODULE16 hModule, HGLOBAL16 hObj, LPBYTE bits,
661 UINT cbSize, BOOL bIcon, DWORD dwVersion,
662 INT width, INT height, UINT loadflags )
664 static HDC hdcMem;
665 int sizeAnd, sizeXor;
666 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
667 BITMAP bmpXor, bmpAnd;
668 POINT16 hotspot;
669 BITMAPINFO *bmi;
670 BOOL DoStretch;
671 INT size;
673 hotspot.x = ICON_HOTSPOT;
674 hotspot.y = ICON_HOTSPOT;
676 TRACE_(cursor)("%08x (%u bytes), ver %08x, %ix%i %s %s\n",
677 (unsigned)bits, cbSize, (unsigned)dwVersion, width, height,
678 bIcon ? "icon" : "cursor", (loadflags & LR_MONOCHROME) ? "mono" : "" );
679 if (dwVersion == 0x00020000)
681 FIXME_(cursor)("\t2.xx resources are not supported\n");
682 return 0;
685 if (bIcon)
686 bmi = (BITMAPINFO *)bits;
687 else /* get the hotspot */
689 POINT16 *pt = (POINT16 *)bits;
690 hotspot = *pt;
691 bmi = (BITMAPINFO *)(pt + 1);
693 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
695 if (!width) width = bmi->bmiHeader.biWidth;
696 if (!height) height = bmi->bmiHeader.biHeight/2;
697 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
698 (bmi->bmiHeader.biWidth != width);
700 /* Check bitmap header */
702 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
703 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
704 bmi->bmiHeader.biCompression != BI_RGB) )
706 WARN_(cursor)("\tinvalid resource bitmap header.\n");
707 return 0;
710 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
711 if (screen_dc)
713 BITMAPINFO* pInfo;
715 /* Make sure we have room for the monochrome bitmap later on.
716 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
717 * up to and including the biBitCount. In-memory icon resource
718 * format is as follows:
720 * BITMAPINFOHEADER icHeader // DIB header
721 * RGBQUAD icColors[] // Color table
722 * BYTE icXOR[] // DIB bits for XOR mask
723 * BYTE icAND[] // DIB bits for AND mask
726 if ((pInfo = (BITMAPINFO *)HeapAlloc( GetProcessHeap(), 0,
727 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
729 memcpy( pInfo, bmi, size );
730 pInfo->bmiHeader.biHeight /= 2;
732 /* Create the XOR bitmap */
734 if (DoStretch) {
735 if(bIcon)
737 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
739 else
741 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
743 if(hXorBits)
745 HBITMAP hOld;
746 BOOL res = FALSE;
748 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
749 if (hdcMem) {
750 hOld = SelectObject(hdcMem, hXorBits);
751 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
752 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
753 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
754 SelectObject(hdcMem, hOld);
756 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
758 } else {
759 if (is_dib_monochrome(bmi)) {
760 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
761 SetDIBits(screen_dc, hXorBits, 0, height,
762 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
764 else
765 hXorBits = CreateDIBitmap(screen_dc, &pInfo->bmiHeader,
766 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS);
769 if( hXorBits )
771 char* xbits = (char *)bmi + size +
772 get_dib_width_bytes( bmi->bmiHeader.biWidth,
773 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
775 pInfo->bmiHeader.biBitCount = 1;
776 if (pInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
778 RGBQUAD *rgb = pInfo->bmiColors;
780 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
781 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
782 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
783 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
785 else
787 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
789 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
790 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
793 /* Create the AND bitmap */
795 if (DoStretch) {
796 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
797 HBITMAP hOld;
798 BOOL res = FALSE;
800 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
801 if (hdcMem) {
802 hOld = SelectObject(hdcMem, hAndBits);
803 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
804 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
805 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
806 SelectObject(hdcMem, hOld);
808 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
810 } else {
811 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
813 if (hAndBits) SetDIBits(screen_dc, hAndBits, 0, height,
814 xbits, pInfo, DIB_RGB_COLORS);
817 if( !hAndBits ) DeleteObject( hXorBits );
819 HeapFree( GetProcessHeap(), 0, pInfo );
823 if( !hXorBits || !hAndBits )
825 WARN_(cursor)("\tunable to create an icon bitmap.\n");
826 return 0;
829 /* Now create the CURSORICONINFO structure */
830 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
831 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
832 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
833 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
835 if (hObj) hObj = GlobalReAlloc16( hObj,
836 sizeof(CURSORICONINFO) + sizeXor + sizeAnd, GMEM_MOVEABLE );
837 if (!hObj) hObj = GlobalAlloc16( GMEM_MOVEABLE,
838 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
839 if (hObj)
841 CURSORICONINFO *info;
843 /* Make it owned by the module */
844 if (hModule) hModule = GetExePtr(hModule);
845 FarSetOwner16( hObj, hModule );
847 info = (CURSORICONINFO *)GlobalLock16( hObj );
848 info->ptHotSpot.x = hotspot.x;
849 info->ptHotSpot.y = hotspot.y;
850 info->nWidth = bmpXor.bmWidth;
851 info->nHeight = bmpXor.bmHeight;
852 info->nWidthBytes = bmpXor.bmWidthBytes;
853 info->bPlanes = bmpXor.bmPlanes;
854 info->bBitsPerPixel = bmpXor.bmBitsPixel;
856 /* Transfer the bitmap bits to the CURSORICONINFO structure */
858 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
859 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
860 GlobalUnlock16( hObj );
863 DeleteObject( hAndBits );
864 DeleteObject( hXorBits );
865 return HICON_32((HICON16)hObj);
869 /**********************************************************************
870 * CreateIconFromResource (USER32.@)
872 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
873 BOOL bIcon, DWORD dwVersion)
875 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
879 /**********************************************************************
880 * CreateIconFromResourceEx (USER32.@)
882 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
883 BOOL bIcon, DWORD dwVersion,
884 INT width, INT height,
885 UINT cFlag )
887 return CURSORICON_CreateFromResource( 0, 0, bits, cbSize, bIcon, dwVersion,
888 width, height, cFlag );
891 /**********************************************************************
892 * CURSORICON_Load
894 * Load a cursor or icon from resource or file.
896 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
897 INT width, INT height, INT colors,
898 BOOL fCursor, UINT loadflags)
900 HANDLE handle = 0;
901 HICON hIcon = 0;
902 HRSRC hRsrc;
903 CURSORICONDIR *dir;
904 CURSORICONDIRENTRY *dirEntry;
905 LPBYTE bits;
907 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
909 LPBYTE *ptr;
910 if (!CURSORICON_SimulateLoadingFromResourceW(name, fCursor, &dir, &ptr))
911 return 0;
912 if (fCursor)
913 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(dir, width, height, 1);
914 else
915 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(dir, width, height, colors);
916 bits = ptr[dirEntry->wResId-1];
917 hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dirEntry->dwBytesInRes,
918 !fCursor, 0x00030000, width, height, loadflags);
919 HeapFree( GetProcessHeap(), 0, dir );
920 HeapFree( GetProcessHeap(), 0, ptr );
922 else /* Load from resource */
924 HRSRC hGroupRsrc;
925 WORD wResId;
926 DWORD dwBytesInRes;
928 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
930 /* Normalize hInstance (must be uniquely represented for icon cache) */
932 if (!HIWORD( hInstance ))
933 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
935 /* Get directory resource ID */
937 if (!(hRsrc = FindResourceW( hInstance, name,
938 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
939 return 0;
940 hGroupRsrc = hRsrc;
942 /* Find the best entry in the directory */
944 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
945 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
946 if (fCursor)
947 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
948 width, height, 1);
949 else
950 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon( dir,
951 width, height, colors );
952 if (!dirEntry) return 0;
953 wResId = dirEntry->wResId;
954 dwBytesInRes = dirEntry->dwBytesInRes;
955 FreeResource( handle );
957 /* Load the resource */
959 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
960 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
962 /* If shared icon, check whether it was already loaded */
963 if ( (loadflags & LR_SHARED)
964 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
965 return hIcon;
967 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
968 bits = (LPBYTE)LockResource( handle );
969 hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dwBytesInRes,
970 !fCursor, 0x00030000, width, height, loadflags);
971 FreeResource( handle );
973 /* If shared icon, add to icon cache */
975 if ( hIcon && (loadflags & LR_SHARED) )
976 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
979 return hIcon;
982 /***********************************************************************
983 * CURSORICON_Copy
985 * Make a copy of a cursor or icon.
987 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
989 char *ptrOld, *ptrNew;
990 int size;
991 HICON16 hOld = HICON_16(hIcon);
992 HICON16 hNew;
994 if (!(ptrOld = (char *)GlobalLock16( hOld ))) return 0;
995 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
996 size = GlobalSize16( hOld );
997 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
998 FarSetOwner16( hNew, hInst16 );
999 ptrNew = (char *)GlobalLock16( hNew );
1000 memcpy( ptrNew, ptrOld, size );
1001 GlobalUnlock16( hOld );
1002 GlobalUnlock16( hNew );
1003 return HICON_32(hNew);
1006 /*************************************************************************
1007 * CURSORICON_ExtCopy
1009 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1011 * PARAMS
1012 * Handle [I] handle to an Image
1013 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1014 * iDesiredCX [I] The Desired width of the Image
1015 * iDesiredCY [I] The desired height of the Image
1016 * nFlags [I] The flags from CopyImage
1018 * RETURNS
1019 * Success: The new handle of the Image
1021 * NOTES
1022 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1023 * LR_MONOCHROME should be implemented by CURSORICON_CreateFromResource.
1024 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1029 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1030 INT iDesiredCX, INT iDesiredCY,
1031 UINT nFlags)
1033 HICON hNew=0;
1035 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1036 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1038 if(hIcon == 0)
1040 return 0;
1043 /* Best Fit or Monochrome */
1044 if( (nFlags & LR_COPYFROMRESOURCE
1045 && (iDesiredCX > 0 || iDesiredCY > 0))
1046 || nFlags & LR_MONOCHROME)
1048 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1050 /* Not Found in Cache, then do a straight copy
1052 if(pIconCache == NULL)
1054 hNew = CURSORICON_Copy(0, hIcon);
1055 if(nFlags & LR_COPYFROMRESOURCE)
1057 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1060 else
1062 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1063 LPBYTE pBits;
1064 HANDLE hMem;
1065 HRSRC hRsrc;
1066 DWORD dwBytesInRes;
1067 WORD wResId;
1068 CURSORICONDIR *pDir;
1069 CURSORICONDIRENTRY *pDirEntry;
1070 BOOL bIsIcon = (nType == IMAGE_ICON);
1072 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1074 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1075 || (iDesiredCX == 0 && iDesiredCY == 0))
1077 iDesiredCY = GetSystemMetrics(bIsIcon ?
1078 SM_CYICON : SM_CYCURSOR);
1079 iDesiredCX = GetSystemMetrics(bIsIcon ?
1080 SM_CXICON : SM_CXCURSOR);
1083 /* Retrieve the CURSORICONDIRENTRY
1085 if (!(hMem = LoadResource( pIconCache->hModule ,
1086 pIconCache->hGroupRsrc)))
1088 return 0;
1090 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1092 return 0;
1095 /* Find Best Fit
1097 if(bIsIcon)
1099 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
1100 pDir, iDesiredCX, iDesiredCY, 256);
1102 else
1104 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
1105 pDir, iDesiredCX, iDesiredCY, 1);
1108 wResId = pDirEntry->wResId;
1109 dwBytesInRes = pDirEntry->dwBytesInRes;
1110 FreeResource(hMem);
1112 TRACE_(icon)("ResID %u, BytesInRes %lu, Width %d, Height %d DX %d, DY %d\n",
1113 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1114 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1116 /* Get the Best Fit
1118 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1119 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1121 return 0;
1123 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1125 return 0;
1128 pBits = (LPBYTE)LockResource( hMem );
1130 if(nFlags & LR_DEFAULTSIZE)
1132 iTargetCY = GetSystemMetrics(SM_CYICON);
1133 iTargetCX = GetSystemMetrics(SM_CXICON);
1136 /* Create a New Icon with the proper dimension
1138 hNew = CURSORICON_CreateFromResource( 0, 0, pBits, dwBytesInRes,
1139 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1140 FreeResource(hMem);
1143 else hNew = CURSORICON_Copy(0, hIcon);
1144 return hNew;
1148 /***********************************************************************
1149 * CreateCursor (USER32.@)
1151 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1152 INT xHotSpot, INT yHotSpot,
1153 INT nWidth, INT nHeight,
1154 LPCVOID lpANDbits, LPCVOID lpXORbits )
1156 CURSORICONINFO info;
1158 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1159 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1161 info.ptHotSpot.x = xHotSpot;
1162 info.ptHotSpot.y = yHotSpot;
1163 info.nWidth = nWidth;
1164 info.nHeight = nHeight;
1165 info.nWidthBytes = 0;
1166 info.bPlanes = 1;
1167 info.bBitsPerPixel = 1;
1169 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1173 /***********************************************************************
1174 * CreateIcon (USER.407)
1176 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1177 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1178 LPCVOID lpANDbits, LPCVOID lpXORbits )
1180 CURSORICONINFO info;
1182 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1183 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1185 info.ptHotSpot.x = ICON_HOTSPOT;
1186 info.ptHotSpot.y = ICON_HOTSPOT;
1187 info.nWidth = nWidth;
1188 info.nHeight = nHeight;
1189 info.nWidthBytes = 0;
1190 info.bPlanes = bPlanes;
1191 info.bBitsPerPixel = bBitsPixel;
1193 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1197 /***********************************************************************
1198 * CreateIcon (USER32.@)
1200 * Creates an icon based on the specified bitmaps. The bitmaps must be
1201 * provided in a device dependent format and will be resized to
1202 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1203 * depth. The provided bitmaps must be top-down bitmaps.
1204 * Although Windows does not support 15bpp(*) this API must support it
1205 * for Winelib applications.
1207 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1208 * format!
1210 * BUGS
1212 * - The provided bitmaps are not resized!
1213 * - The documentation says the lpXORbits bitmap must be in a device
1214 * dependent format. But we must still resize it and perform depth
1215 * conversions if necessary.
1216 * - I'm a bit unsure about the how the 'device dependent format' thing works.
1217 * I did some tests on windows and found that if you provide a 16bpp bitmap
1218 * in lpXORbits, then its format but be 565 RGB if the screen's bit depth
1219 * is 16bpp but it must be 555 RGB if the screen's bit depth is anything
1220 * else. I don't know if this is part of the GDI specs or if this is a
1221 * quirk of the graphics card driver.
1222 * - You may think that we check whether the bit depths match or not
1223 * as an optimization. But the truth is that the conversion using
1224 * CreateDIBitmap does not work for some bit depth (e.g. 8bpp) and I have
1225 * no idea why.
1226 * - I'm pretty sure that all the things we do in CreateIcon should
1227 * also be done in CreateIconIndirect...
1229 HICON WINAPI CreateIcon(
1230 HINSTANCE hInstance, /* [in] the application's hInstance */
1231 INT nWidth, /* [in] the width of the provided bitmaps */
1232 INT nHeight, /* [in] the height of the provided bitmaps */
1233 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1234 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1235 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1236 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1238 HICON hIcon;
1239 HDC hdc;
1241 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1242 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1244 hdc=GetDC(0);
1245 if (!hdc)
1246 return 0;
1248 if (GetDeviceCaps(hdc,BITSPIXEL)==bBitsPixel) {
1249 CURSORICONINFO info;
1251 info.ptHotSpot.x = ICON_HOTSPOT;
1252 info.ptHotSpot.y = ICON_HOTSPOT;
1253 info.nWidth = nWidth;
1254 info.nHeight = nHeight;
1255 info.nWidthBytes = 0;
1256 info.bPlanes = bPlanes;
1257 info.bBitsPerPixel = bBitsPixel;
1259 hIcon=HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1260 } else {
1261 ICONINFO iinfo;
1262 BITMAPINFO bmi;
1264 iinfo.fIcon=TRUE;
1265 iinfo.xHotspot=ICON_HOTSPOT;
1266 iinfo.yHotspot=ICON_HOTSPOT;
1267 iinfo.hbmMask=CreateBitmap(nWidth,nHeight,1,1,lpANDbits);
1269 bmi.bmiHeader.biSize=sizeof(bmi.bmiHeader);
1270 bmi.bmiHeader.biWidth=nWidth;
1271 bmi.bmiHeader.biHeight=-nHeight;
1272 bmi.bmiHeader.biPlanes=bPlanes;
1273 bmi.bmiHeader.biBitCount=bBitsPixel;
1274 bmi.bmiHeader.biCompression=BI_RGB;
1275 bmi.bmiHeader.biSizeImage=0;
1276 bmi.bmiHeader.biXPelsPerMeter=0;
1277 bmi.bmiHeader.biYPelsPerMeter=0;
1278 bmi.bmiHeader.biClrUsed=0;
1279 bmi.bmiHeader.biClrImportant=0;
1281 iinfo.hbmColor = CreateDIBitmap( hdc, &bmi.bmiHeader,
1282 CBM_INIT, lpXORbits,
1283 &bmi, DIB_RGB_COLORS );
1285 hIcon=CreateIconIndirect(&iinfo);
1286 DeleteObject(iinfo.hbmMask);
1287 DeleteObject(iinfo.hbmColor);
1289 ReleaseDC(0,hdc);
1290 return hIcon;
1294 /***********************************************************************
1295 * CreateCursorIconIndirect (USER.408)
1297 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1298 CURSORICONINFO *info,
1299 LPCVOID lpANDbits,
1300 LPCVOID lpXORbits )
1302 HGLOBAL16 handle;
1303 char *ptr;
1304 int sizeAnd, sizeXor;
1306 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1307 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1308 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1309 sizeXor = info->nHeight * info->nWidthBytes;
1310 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1311 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1312 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1313 return 0;
1314 FarSetOwner16( handle, hInstance );
1315 ptr = (char *)GlobalLock16( handle );
1316 memcpy( ptr, info, sizeof(*info) );
1317 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1318 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1319 GlobalUnlock16( handle );
1320 return handle;
1324 /***********************************************************************
1325 * CopyIcon (USER.368)
1327 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1329 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1330 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1334 /***********************************************************************
1335 * CopyIcon (USER32.@)
1337 HICON WINAPI CopyIcon( HICON hIcon )
1339 TRACE_(icon)("%p\n", hIcon );
1340 return CURSORICON_Copy( 0, hIcon );
1344 /***********************************************************************
1345 * CopyCursor (USER.369)
1347 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1349 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1350 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1353 /**********************************************************************
1354 * DestroyIcon32 (USER.610)
1356 * This routine is actually exported from Win95 USER under the name
1357 * DestroyIcon32 ... The behaviour implemented here should mimic
1358 * the Win95 one exactly, especially the return values, which
1359 * depend on the setting of various flags.
1361 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1363 WORD retv;
1365 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1367 /* Check whether destroying active cursor */
1369 if ( QUEUE_Current()->cursor == HICON_32(handle) )
1371 WARN_(cursor)("Destroying active cursor!\n" );
1372 SetCursor( 0 );
1375 /* Try shared cursor/icon first */
1377 if ( !(flags & CID_NONSHARED) )
1379 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1381 if ( count != -1 )
1382 return (flags & CID_WIN32)? TRUE : (count == 0);
1384 /* FIXME: OEM cursors/icons should be recognized */
1387 /* Now assume non-shared cursor/icon */
1389 retv = GlobalFree16( handle );
1390 return (flags & CID_RESOURCE)? retv : TRUE;
1393 /***********************************************************************
1394 * DestroyIcon (USER32.@)
1396 BOOL WINAPI DestroyIcon( HICON hIcon )
1398 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1402 /***********************************************************************
1403 * DestroyCursor (USER32.@)
1405 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1407 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1411 /***********************************************************************
1412 * DrawIcon (USER32.@)
1414 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1416 CURSORICONINFO *ptr;
1417 HDC hMemDC;
1418 HBITMAP hXorBits, hAndBits;
1419 COLORREF oldFg, oldBg;
1421 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
1422 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1423 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
1424 (char *)(ptr+1) );
1425 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1426 ptr->bBitsPerPixel, (char *)(ptr + 1)
1427 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
1428 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1429 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1431 if (hXorBits && hAndBits)
1433 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1434 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
1435 SelectObject( hMemDC, hXorBits );
1436 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
1437 SelectObject( hMemDC, hBitTemp );
1439 DeleteDC( hMemDC );
1440 if (hXorBits) DeleteObject( hXorBits );
1441 if (hAndBits) DeleteObject( hAndBits );
1442 GlobalUnlock16(HICON_16(hIcon));
1443 SetTextColor( hdc, oldFg );
1444 SetBkColor( hdc, oldBg );
1445 return TRUE;
1448 /***********************************************************************
1449 * DumpIcon (USER.459)
1451 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1452 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1454 CURSORICONINFO *info = MapSL( pInfo );
1455 int sizeAnd, sizeXor;
1457 if (!info) return 0;
1458 sizeXor = info->nHeight * info->nWidthBytes;
1459 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1460 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1461 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1462 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1463 return MAKELONG( sizeXor, sizeXor );
1467 /***********************************************************************
1468 * SetCursor (USER32.@)
1469 * RETURNS:
1470 * A handle to the previous cursor shape.
1472 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1474 MESSAGEQUEUE *queue = QUEUE_Current();
1475 HCURSOR hOldCursor;
1477 if (hCursor == queue->cursor) return hCursor; /* No change */
1478 TRACE_(cursor)("%p\n", hCursor );
1479 hOldCursor = queue->cursor;
1480 queue->cursor = hCursor;
1481 /* Change the cursor shape only if it is visible */
1482 if (queue->cursor_count >= 0 && USER_Driver.pSetCursor)
1484 USER_Driver.pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
1485 GlobalUnlock16(HCURSOR_16(hCursor));
1487 return hOldCursor;
1490 /***********************************************************************
1491 * ShowCursor (USER32.@)
1493 INT WINAPI ShowCursor( BOOL bShow )
1495 MESSAGEQUEUE *queue = QUEUE_Current();
1497 TRACE_(cursor)("%d, count=%d\n", bShow, queue->cursor_count );
1499 if (bShow)
1501 if (++queue->cursor_count == 0 && USER_Driver.pSetCursor) /* Show it */
1503 USER_Driver.pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(queue->cursor)));
1504 GlobalUnlock16(HCURSOR_16(queue->cursor));
1507 else
1509 if (--queue->cursor_count == -1 && USER_Driver.pSetCursor) /* Hide it */
1510 USER_Driver.pSetCursor( NULL );
1512 return queue->cursor_count;
1515 /***********************************************************************
1516 * GetCursor (USER32.@)
1518 HCURSOR WINAPI GetCursor(void)
1520 return QUEUE_Current()->cursor;
1524 /***********************************************************************
1525 * ClipCursor (USER32.@)
1527 BOOL WINAPI ClipCursor( const RECT *rect )
1529 if (!rect) SetRectEmpty( &CURSOR_ClipRect );
1530 else CopyRect( &CURSOR_ClipRect, rect );
1531 return TRUE;
1535 /***********************************************************************
1536 * GetClipCursor (USER32.@)
1538 BOOL WINAPI GetClipCursor( RECT *rect )
1540 if (rect)
1542 CopyRect( rect, &CURSOR_ClipRect );
1543 return TRUE;
1545 return FALSE;
1548 /**********************************************************************
1549 * LookupIconIdFromDirectoryEx (USER.364)
1551 * FIXME: exact parameter sizes
1553 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1554 INT16 width, INT16 height, UINT16 cFlag )
1556 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1559 /**********************************************************************
1560 * LookupIconIdFromDirectoryEx (USER32.@)
1562 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1563 INT width, INT height, UINT cFlag )
1565 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1566 UINT retVal = 0;
1567 if( dir && !dir->idReserved && (dir->idType & 3) )
1569 CURSORICONDIRENTRY* entry;
1570 HDC hdc;
1571 UINT palEnts;
1572 int colors;
1573 hdc = GetDC(0);
1574 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1575 if (palEnts == 0)
1576 palEnts = 256;
1577 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1579 ReleaseDC(0, hdc);
1581 if( bIcon )
1582 entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1583 else
1584 entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1586 if( entry ) retVal = entry->wResId;
1588 else WARN_(cursor)("invalid resource directory\n");
1589 return retVal;
1592 /**********************************************************************
1593 * LookupIconIdFromDirectory (USER.?)
1595 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
1597 return LookupIconIdFromDirectoryEx16( dir, bIcon,
1598 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1599 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1602 /**********************************************************************
1603 * LookupIconIdFromDirectory (USER32.@)
1605 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1607 return LookupIconIdFromDirectoryEx( dir, bIcon,
1608 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1609 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1612 /**********************************************************************
1613 * GetIconID (USER.455)
1615 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1617 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
1619 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1620 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1622 switch(resType)
1624 case RT_CURSOR:
1625 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1626 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1627 case RT_ICON:
1628 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1629 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1630 default:
1631 WARN_(cursor)("invalid res type %ld\n", resType );
1633 return 0;
1636 /**********************************************************************
1637 * LoadCursorIconHandler (USER.336)
1639 * Supposed to load resources of Windows 2.x applications.
1641 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1643 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1644 hResource, hModule, hRsrc);
1645 return (HGLOBAL16)0;
1648 /**********************************************************************
1649 * LoadDIBIconHandler (USER.357)
1651 * RT_ICON resource loader, installed by USER_SignalProc when module
1652 * is initialized.
1654 HGLOBAL16 WINAPI LoadDIBIconHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1656 /* If hResource is zero we must allocate a new memory block, if it's
1657 * non-zero but GlobalLock() returns NULL then it was discarded and
1658 * we have to recommit some memory, otherwise we just need to check
1659 * the block size. See LoadProc() in 16-bit SDK for more.
1662 hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
1663 if( hMemObj )
1665 LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1666 hMemObj = HICON_16(CURSORICON_CreateFromResource(
1667 hModule, hMemObj, bits,
1668 SizeofResource16(hModule, hRsrc), TRUE, 0x00030000,
1669 GetSystemMetrics(SM_CXICON),
1670 GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR));
1672 return hMemObj;
1675 /**********************************************************************
1676 * LoadDIBCursorHandler (USER.356)
1678 * RT_CURSOR resource loader. Same as above.
1680 HGLOBAL16 WINAPI LoadDIBCursorHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1682 hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
1683 if( hMemObj )
1685 LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1686 hMemObj = HICON_16(CURSORICON_CreateFromResource(
1687 hModule, hMemObj, bits,
1688 SizeofResource16(hModule, hRsrc), FALSE, 0x00030000,
1689 GetSystemMetrics(SM_CXCURSOR),
1690 GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME));
1692 return hMemObj;
1695 /**********************************************************************
1696 * LoadIconHandler (USER.456)
1698 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
1700 LPBYTE bits = (LPBYTE)LockResource16( hResource );
1702 TRACE_(cursor)("hRes=%04x\n",hResource);
1704 return HICON_16(CURSORICON_CreateFromResource(0, 0, bits, 0, TRUE,
1705 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
1708 /***********************************************************************
1709 * LoadCursorW (USER32.@)
1711 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1713 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1714 LR_SHARED | LR_DEFAULTSIZE );
1717 /***********************************************************************
1718 * LoadCursorA (USER32.@)
1720 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1722 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1723 LR_SHARED | LR_DEFAULTSIZE );
1726 /***********************************************************************
1727 * LoadCursorFromFileW (USER32.@)
1729 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1731 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1732 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1735 /***********************************************************************
1736 * LoadCursorFromFileA (USER32.@)
1738 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1740 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1741 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1744 /***********************************************************************
1745 * LoadIconW (USER32.@)
1747 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1749 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1750 LR_SHARED | LR_DEFAULTSIZE );
1753 /***********************************************************************
1754 * LoadIconA (USER32.@)
1756 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1758 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1759 LR_SHARED | LR_DEFAULTSIZE );
1762 /**********************************************************************
1763 * GetIconInfo (USER32.@)
1765 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1767 CURSORICONINFO *ciconinfo;
1768 INT height;
1770 ciconinfo = GlobalLock16(HICON_16(hIcon));
1771 if (!ciconinfo)
1772 return FALSE;
1774 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1775 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1777 iconinfo->fIcon = TRUE;
1778 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1779 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1781 else
1783 iconinfo->fIcon = FALSE;
1784 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1785 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1788 if (ciconinfo->bBitsPerPixel > 1)
1790 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
1791 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1792 (char *)(ciconinfo + 1)
1793 + ciconinfo->nHeight *
1794 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1795 height = ciconinfo->nHeight;
1797 else
1799 iconinfo->hbmColor = 0;
1800 height = ciconinfo->nHeight * 2;
1803 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
1804 1, 1, (char *)(ciconinfo + 1));
1806 GlobalUnlock16(HICON_16(hIcon));
1808 return TRUE;
1811 /**********************************************************************
1812 * CreateIconIndirect (USER32.@)
1814 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1816 BITMAP bmpXor,bmpAnd;
1817 HICON16 hObj;
1818 int sizeXor,sizeAnd;
1820 GetObjectA( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1821 GetObjectA( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1823 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1824 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1826 hObj = GlobalAlloc16( GMEM_MOVEABLE,
1827 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1828 if (hObj)
1830 CURSORICONINFO *info;
1832 info = (CURSORICONINFO *)GlobalLock16( hObj );
1834 /* If we are creating an icon, the hotspot is unused */
1835 if (iconinfo->fIcon)
1837 info->ptHotSpot.x = ICON_HOTSPOT;
1838 info->ptHotSpot.y = ICON_HOTSPOT;
1840 else
1842 info->ptHotSpot.x = iconinfo->xHotspot;
1843 info->ptHotSpot.y = iconinfo->yHotspot;
1846 info->nWidth = bmpXor.bmWidth;
1847 info->nHeight = bmpXor.bmHeight;
1848 info->nWidthBytes = bmpXor.bmWidthBytes;
1849 info->bPlanes = bmpXor.bmPlanes;
1850 info->bBitsPerPixel = bmpXor.bmBitsPixel;
1852 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1854 GetBitmapBits( iconinfo->hbmMask ,sizeAnd,(char*)(info + 1) );
1855 GetBitmapBits( iconinfo->hbmColor,sizeXor,(char*)(info + 1) +sizeAnd);
1856 GlobalUnlock16( hObj );
1858 return HICON_32(hObj);
1861 /******************************************************************************
1862 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1864 * NOTES
1865 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1867 * PARAMS
1868 * hdc [I] Handle to device context
1869 * x0 [I] X coordinate of upper left corner
1870 * y0 [I] Y coordinate of upper left corner
1871 * hIcon [I] Handle to icon to draw
1872 * cxWidth [I] Width of icon
1873 * cyWidth [I] Height of icon
1874 * istep [I] Index of frame in animated cursor
1875 * hbr [I] Handle to background brush
1876 * flags [I] Icon-drawing flags
1878 * RETURNS
1879 * Success: TRUE
1880 * Failure: FALSE
1882 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1883 INT cxWidth, INT cyWidth, UINT istep,
1884 HBRUSH hbr, UINT flags )
1886 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
1887 HDC hDC_off = 0, hMemDC;
1888 BOOL result = FALSE, DoOffscreen;
1889 HBITMAP hB_off = 0, hOld = 0;
1891 if (!ptr) return FALSE;
1892 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1893 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1895 hMemDC = CreateCompatibleDC (hdc);
1896 if (istep)
1897 FIXME_(icon)("Ignoring istep=%d\n", istep);
1898 if (flags & DI_COMPAT)
1899 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
1901 if (!flags) {
1902 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
1903 flags = DI_NORMAL;
1906 /* Calculate the size of the destination image. */
1907 if (cxWidth == 0)
1909 if (flags & DI_DEFAULTSIZE)
1910 cxWidth = GetSystemMetrics (SM_CXICON);
1911 else
1912 cxWidth = ptr->nWidth;
1914 if (cyWidth == 0)
1916 if (flags & DI_DEFAULTSIZE)
1917 cyWidth = GetSystemMetrics (SM_CYICON);
1918 else
1919 cyWidth = ptr->nHeight;
1922 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1924 if (DoOffscreen) {
1925 RECT r;
1927 r.left = 0;
1928 r.top = 0;
1929 r.right = cxWidth;
1930 r.bottom = cxWidth;
1932 hDC_off = CreateCompatibleDC(hdc);
1933 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
1934 if (hDC_off && hB_off) {
1935 hOld = SelectObject(hDC_off, hB_off);
1936 FillRect(hDC_off, &r, hbr);
1940 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
1942 HBITMAP hXorBits, hAndBits;
1943 COLORREF oldFg, oldBg;
1944 INT nStretchMode;
1946 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
1948 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1949 ptr->bPlanes, ptr->bBitsPerPixel,
1950 (char *)(ptr + 1)
1951 + ptr->nHeight *
1952 get_bitmap_width_bytes(ptr->nWidth,1) );
1953 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1954 1, 1, (char *)(ptr+1) );
1955 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1956 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1958 if (hXorBits && hAndBits)
1960 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1961 if (flags & DI_MASK)
1963 if (DoOffscreen)
1964 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1965 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1966 else
1967 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1968 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1970 SelectObject( hMemDC, hXorBits );
1971 if (flags & DI_IMAGE)
1973 if (DoOffscreen)
1974 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1975 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1976 else
1977 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1978 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1980 SelectObject( hMemDC, hBitTemp );
1981 result = TRUE;
1984 SetTextColor( hdc, oldFg );
1985 SetBkColor( hdc, oldBg );
1986 if (hXorBits) DeleteObject( hXorBits );
1987 if (hAndBits) DeleteObject( hAndBits );
1988 SetStretchBltMode (hdc, nStretchMode);
1989 if (DoOffscreen) {
1990 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
1991 SelectObject(hDC_off, hOld);
1994 if (hMemDC) DeleteDC( hMemDC );
1995 if (hDC_off) DeleteDC(hDC_off);
1996 if (hB_off) DeleteObject(hB_off);
1997 GlobalUnlock16(HICON_16(hIcon));
1998 return result;
2001 /***********************************************************************
2002 * DIB_FixColorsToLoadflags
2004 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2005 * are in loadflags
2007 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2009 int colors;
2010 COLORREF c_W, c_S, c_F, c_L, c_C;
2011 int incr,i;
2012 RGBQUAD *ptr;
2013 int bitmap_type;
2014 LONG width;
2015 LONG height;
2016 WORD bpp;
2017 DWORD compr;
2019 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2021 WARN_(resource)("Invalid bitmap\n");
2022 return;
2025 if (bpp > 8) return;
2027 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2029 incr = 3;
2030 colors = 1 << bpp;
2032 else
2034 incr = 4;
2035 colors = bmi->bmiHeader.biClrUsed;
2036 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2039 c_W = GetSysColor(COLOR_WINDOW);
2040 c_S = GetSysColor(COLOR_3DSHADOW);
2041 c_F = GetSysColor(COLOR_3DFACE);
2042 c_L = GetSysColor(COLOR_3DLIGHT);
2044 if (loadflags & LR_LOADTRANSPARENT) {
2045 switch (bpp) {
2046 case 1: pix = pix >> 7; break;
2047 case 4: pix = pix >> 4; break;
2048 case 8: break;
2049 default:
2050 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2051 return;
2053 if (pix >= colors) {
2054 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2055 return;
2057 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2058 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2059 ptr->rgbBlue = GetBValue(c_W);
2060 ptr->rgbGreen = GetGValue(c_W);
2061 ptr->rgbRed = GetRValue(c_W);
2063 if (loadflags & LR_LOADMAP3DCOLORS)
2064 for (i=0; i<colors; i++) {
2065 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2066 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2067 if (c_C == RGB(128, 128, 128)) {
2068 ptr->rgbRed = GetRValue(c_S);
2069 ptr->rgbGreen = GetGValue(c_S);
2070 ptr->rgbBlue = GetBValue(c_S);
2071 } else if (c_C == RGB(192, 192, 192)) {
2072 ptr->rgbRed = GetRValue(c_F);
2073 ptr->rgbGreen = GetGValue(c_F);
2074 ptr->rgbBlue = GetBValue(c_F);
2075 } else if (c_C == RGB(223, 223, 223)) {
2076 ptr->rgbRed = GetRValue(c_L);
2077 ptr->rgbGreen = GetGValue(c_L);
2078 ptr->rgbBlue = GetBValue(c_L);
2084 /**********************************************************************
2085 * BITMAP_Load
2087 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name, UINT loadflags )
2089 HBITMAP hbitmap = 0;
2090 HRSRC hRsrc;
2091 HGLOBAL handle;
2092 char *ptr = NULL;
2093 BITMAPINFO *info, *fix_info=NULL;
2094 HGLOBAL hFix;
2095 int size;
2097 if (!(loadflags & LR_LOADFROMFILE))
2099 if (!instance)
2101 /* OEM bitmap: try to load the resource from user32.dll */
2102 if (HIWORD(name)) return 0;
2103 instance = user32_module;
2106 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2107 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2109 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
2111 else
2113 if (!(ptr = map_fileW( name ))) return 0;
2114 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2117 size = bitmap_info_size(info, DIB_RGB_COLORS);
2118 if ((hFix = GlobalAlloc(0, size))) fix_info=GlobalLock(hFix);
2120 if (fix_info) {
2121 BYTE pix;
2123 memcpy(fix_info, info, size);
2124 pix = *((LPBYTE)info + size);
2125 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2126 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2128 if (screen_dc)
2130 char *bits = (char *)info + size;
2132 if (loadflags & LR_CREATEDIBSECTION) {
2133 DIBSECTION dib;
2134 fix_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2135 hbitmap = CreateDIBSection(screen_dc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
2136 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
2137 SetDIBits(screen_dc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
2138 DIB_RGB_COLORS);
2140 else {
2141 /* If it's possible, create a monochrome bitmap */
2143 LONG width;
2144 LONG height;
2145 WORD bpp;
2146 DWORD compr;
2148 if (DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height, &bpp, &compr ) != -1)
2150 if (width < 0)
2151 TRACE("Bitmap has a negative width\n");
2152 else
2154 /* Top-down DIBs have a negative height */
2155 if (height < 0) height = -height;
2157 TRACE("width=%ld, height=%ld, bpp=%u, compr=%lu\n", width, height, bpp, compr);
2159 if (is_dib_monochrome(fix_info))
2160 hbitmap = CreateBitmap(width, height, 1, 1, NULL);
2161 else
2162 hbitmap = CreateCompatibleBitmap(screen_dc, width, height);
2164 SetDIBits(screen_dc, hbitmap, 0, height, bits, info, DIB_RGB_COLORS);
2170 GlobalUnlock(hFix);
2171 GlobalFree(hFix);
2174 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2176 return hbitmap;
2179 /**********************************************************************
2180 * LoadImageA (USER32.@)
2182 * FIXME: implementation lacks some features, see LR_ defines in winuser.h
2185 /* filter for page-fault exceptions */
2186 static WINE_EXCEPTION_FILTER(page_fault)
2188 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
2189 return EXCEPTION_EXECUTE_HANDLER;
2190 return EXCEPTION_CONTINUE_SEARCH;
2193 /*********************************************************************/
2195 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2196 INT desiredx, INT desiredy, UINT loadflags)
2198 HANDLE res;
2199 LPWSTR u_name;
2201 if (!HIWORD(name))
2202 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2204 __TRY {
2205 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2206 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2207 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2209 __EXCEPT(page_fault) {
2210 SetLastError( ERROR_INVALID_PARAMETER );
2211 return 0;
2213 __ENDTRY
2214 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2215 HeapFree(GetProcessHeap(), 0, u_name);
2216 return res;
2220 /******************************************************************************
2221 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2223 * PARAMS
2224 * hinst [I] Handle of instance that contains image
2225 * name [I] Name of image
2226 * type [I] Type of image
2227 * desiredx [I] Desired width
2228 * desiredy [I] Desired height
2229 * loadflags [I] Load flags
2231 * RETURNS
2232 * Success: Handle to newly loaded image
2233 * Failure: NULL
2235 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2237 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2238 INT desiredx, INT desiredy, UINT loadflags )
2240 if (HIWORD(name)) {
2241 TRACE_(resource)("(%p,%p,%d,%d,%d,0x%08x)\n",
2242 hinst,name,type,desiredx,desiredy,loadflags);
2243 } else {
2244 TRACE_(resource)("(%p,%p,%d,%d,%d,0x%08x)\n",
2245 hinst,name,type,desiredx,desiredy,loadflags);
2247 if (loadflags & LR_DEFAULTSIZE) {
2248 if (type == IMAGE_ICON) {
2249 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2250 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2251 } else if (type == IMAGE_CURSOR) {
2252 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2253 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2256 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2257 switch (type) {
2258 case IMAGE_BITMAP:
2259 return BITMAP_Load( hinst, name, loadflags );
2261 case IMAGE_ICON:
2262 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2263 if (screen_dc)
2265 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2266 if (palEnts == 0) palEnts = 256;
2267 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2268 palEnts, FALSE, loadflags);
2270 break;
2272 case IMAGE_CURSOR:
2273 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2274 1, TRUE, loadflags);
2276 return 0;
2279 /******************************************************************************
2280 * CopyImage (USER32.@) Creates new image and copies attributes to it
2282 * PARAMS
2283 * hnd [I] Handle to image to copy
2284 * type [I] Type of image to copy
2285 * desiredx [I] Desired width of new image
2286 * desiredy [I] Desired height of new image
2287 * flags [I] Copy flags
2289 * RETURNS
2290 * Success: Handle to newly created image
2291 * Failure: NULL
2293 * FIXME: implementation still lacks nearly all features, see LR_*
2294 * defines in winuser.h
2296 HICON WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2297 INT desiredy, UINT flags )
2299 switch (type)
2301 case IMAGE_BITMAP:
2303 HBITMAP res;
2304 BITMAP bm;
2306 if (!GetObjectW( hnd, sizeof(bm), &bm )) return 0;
2307 bm.bmBits = NULL;
2308 if ((res = CreateBitmapIndirect(&bm)))
2310 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight );
2311 GetBitmapBits( hnd, bm.bmWidthBytes * bm.bmHeight, buf );
2312 SetBitmapBits( res, bm.bmWidthBytes * bm.bmHeight, buf );
2313 HeapFree( GetProcessHeap(), 0, buf );
2315 return (HICON)res;
2317 case IMAGE_ICON:
2318 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2319 case IMAGE_CURSOR:
2320 /* Should call CURSORICON_ExtCopy but more testing
2321 * needs to be done before we change this
2323 return CopyCursor(hnd);
2325 return 0;
2329 /******************************************************************************
2330 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2332 * RETURNS
2333 * Success: Handle to specified bitmap
2334 * Failure: NULL
2336 HBITMAP WINAPI LoadBitmapW(
2337 HINSTANCE instance, /* [in] Handle to application instance */
2338 LPCWSTR name) /* [in] Address of bitmap resource name */
2340 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2343 /**********************************************************************
2344 * LoadBitmapA (USER32.@)
2346 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2348 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );