user32: Fixed CURSORICON_CreateIconFromBMI to preserve the alpha channel.
[wine.git] / dlls / user32 / cursoricon.c
bloba74b79d33d580c8a31f86e0f3e24197383f4303f
1 /*
2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
6 * 1997 Alex Korobka
7 * 1998 Turchanov Sergey
8 * 2007 Henri Verbeet
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Theory:
28 * Cursors and icons are stored in a global heap block, with the
29 * following layout:
31 * CURSORICONINFO info;
32 * BYTE[] ANDbits;
33 * BYTE[] XORbits;
35 * The bits structures are in the format of a device-dependent bitmap.
37 * This layout is very sub-optimal, as the bitmap bits are stored in
38 * the X client instead of in the server like other bitmaps; however,
39 * some programs (notably Paint Brush) expect to be able to manipulate
40 * the bits directly :-(
43 #include "config.h"
44 #include "wine/port.h"
46 #include <stdarg.h>
47 #include <string.h>
48 #include <stdlib.h>
50 #include "windef.h"
51 #include "winbase.h"
52 #include "wingdi.h"
53 #include "winerror.h"
54 #include "wine/winbase16.h"
55 #include "wine/winuser16.h"
56 #include "wine/exception.h"
57 #include "wine/debug.h"
58 #include "user_private.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
61 WINE_DECLARE_DEBUG_CHANNEL(icon);
62 WINE_DECLARE_DEBUG_CHANNEL(resource);
64 #include "pshpack1.h"
66 typedef struct {
67 BYTE bWidth;
68 BYTE bHeight;
69 BYTE bColorCount;
70 BYTE bReserved;
71 WORD xHotspot;
72 WORD yHotspot;
73 DWORD dwDIBSize;
74 DWORD dwDIBOffset;
75 } CURSORICONFILEDIRENTRY;
77 typedef struct
79 WORD idReserved;
80 WORD idType;
81 WORD idCount;
82 CURSORICONFILEDIRENTRY idEntries[1];
83 } CURSORICONFILEDIR;
85 #include "poppack.h"
87 #define CID_RESOURCE 0x0001
88 #define CID_WIN32 0x0004
89 #define CID_NONSHARED 0x0008
91 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
93 static HDC screen_dc;
95 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
97 /**********************************************************************
98 * ICONCACHE for cursors/icons loaded with LR_SHARED.
100 * FIXME: This should not be allocated on the system heap, but on a
101 * subsystem-global heap (i.e. one for all Win16 processes,
102 * and one for each Win32 process).
104 typedef struct tagICONCACHE
106 struct tagICONCACHE *next;
108 HMODULE hModule;
109 HRSRC hRsrc;
110 HRSRC hGroupRsrc;
111 HICON hIcon;
113 INT count;
115 } ICONCACHE;
117 static ICONCACHE *IconAnchor = NULL;
119 static CRITICAL_SECTION IconCrst;
120 static CRITICAL_SECTION_DEBUG critsect_debug =
122 0, 0, &IconCrst,
123 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
124 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
126 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
128 static const WORD ICON_HOTSPOT = 0x4242;
131 /***********************************************************************
132 * map_fileW
134 * Helper function to map a file to memory:
135 * name - file name
136 * [RETURN] ptr - pointer to mapped file
137 * [RETURN] filesize - pointer size of file to be stored if not NULL
139 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
141 HANDLE hFile, hMapping;
142 LPVOID ptr = NULL;
144 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
145 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
146 if (hFile != INVALID_HANDLE_VALUE)
148 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
149 if (hMapping)
151 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
152 CloseHandle( hMapping );
153 if (filesize)
154 *filesize = GetFileSize( hFile, NULL );
156 CloseHandle( hFile );
158 return ptr;
162 /***********************************************************************
163 * get_bitmap_width_bytes
165 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
166 * data.
168 static int get_bitmap_width_bytes( int width, int bpp )
170 switch(bpp)
172 case 1:
173 return 2 * ((width+15) / 16);
174 case 4:
175 return 2 * ((width+3) / 4);
176 case 24:
177 width *= 3;
178 /* fall through */
179 case 8:
180 return width + (width & 1);
181 case 16:
182 case 15:
183 return width * 2;
184 case 32:
185 return width * 4;
186 default:
187 WARN("Unknown depth %d, please report.\n", bpp );
189 return -1;
193 /***********************************************************************
194 * get_dib_width_bytes
196 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
198 static int get_dib_width_bytes( int width, int depth )
200 int words;
202 switch(depth)
204 case 1: words = (width + 31) / 32; break;
205 case 4: words = (width + 7) / 8; break;
206 case 8: words = (width + 3) / 4; break;
207 case 15:
208 case 16: words = (width + 1) / 2; break;
209 case 24: words = (width * 3 + 3)/4; break;
210 default:
211 WARN("(%d): Unsupported depth\n", depth );
212 /* fall through */
213 case 32:
214 words = width;
216 return 4 * words;
220 /***********************************************************************
221 * bitmap_info_size
223 * Return the size of the bitmap info structure including color table.
225 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
227 int colors, masks = 0;
229 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
231 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
232 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
233 return sizeof(BITMAPCOREHEADER) + colors *
234 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
236 else /* assume BITMAPINFOHEADER */
238 colors = info->bmiHeader.biClrUsed;
239 if (colors > 256) /* buffer overflow otherwise */
240 colors = 256;
241 if (!colors && (info->bmiHeader.biBitCount <= 8))
242 colors = 1 << info->bmiHeader.biBitCount;
243 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
244 return sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) + colors *
245 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
250 /***********************************************************************
251 * is_dib_monochrome
253 * Returns whether a DIB can be converted to a monochrome DDB.
255 * A DIB can be converted if its color table contains only black and
256 * white. Black must be the first color in the color table.
258 * Note : If the first color in the color table is white followed by
259 * black, we can't convert it to a monochrome DDB with
260 * SetDIBits, because black and white would be inverted.
262 static BOOL is_dib_monochrome( const BITMAPINFO* info )
264 if (info->bmiHeader.biBitCount != 1) return FALSE;
266 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
268 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
270 /* Check if the first color is black */
271 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
273 rgb++;
275 /* Check if the second color is white */
276 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
277 && (rgb->rgbtBlue == 0xff));
279 else return FALSE;
281 else /* assume BITMAPINFOHEADER */
283 const RGBQUAD *rgb = info->bmiColors;
285 /* Check if the first color is black */
286 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
287 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
289 rgb++;
291 /* Check if the second color is white */
292 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
293 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
295 else return FALSE;
299 /***********************************************************************
300 * DIB_GetBitmapInfo
302 * Get the info from a bitmap header.
303 * Return 1 for INFOHEADER, 0 for COREHEADER,
304 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
306 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
307 LONG *height, WORD *bpp, DWORD *compr )
309 if (header->biSize == sizeof(BITMAPINFOHEADER))
311 *width = header->biWidth;
312 *height = header->biHeight;
313 *bpp = header->biBitCount;
314 *compr = header->biCompression;
315 return 1;
317 if (header->biSize == sizeof(BITMAPCOREHEADER))
319 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
320 *width = core->bcWidth;
321 *height = core->bcHeight;
322 *bpp = core->bcBitCount;
323 *compr = 0;
324 return 0;
326 if (header->biSize == sizeof(BITMAPV4HEADER))
328 const BITMAPV4HEADER *v4hdr = (const BITMAPV4HEADER *)header;
329 *width = v4hdr->bV4Width;
330 *height = v4hdr->bV4Height;
331 *bpp = v4hdr->bV4BitCount;
332 *compr = v4hdr->bV4V4Compression;
333 return 4;
335 if (header->biSize == sizeof(BITMAPV5HEADER))
337 const BITMAPV5HEADER *v5hdr = (const BITMAPV5HEADER *)header;
338 *width = v5hdr->bV5Width;
339 *height = v5hdr->bV5Height;
340 *bpp = v5hdr->bV5BitCount;
341 *compr = v5hdr->bV5Compression;
342 return 5;
344 ERR("(%d): unknown/wrong size for header\n", header->biSize );
345 return -1;
348 /**********************************************************************
349 * CURSORICON_FindSharedIcon
351 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
353 HICON hIcon = 0;
354 ICONCACHE *ptr;
356 EnterCriticalSection( &IconCrst );
358 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
359 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
361 ptr->count++;
362 hIcon = ptr->hIcon;
363 break;
366 LeaveCriticalSection( &IconCrst );
368 return hIcon;
371 /*************************************************************************
372 * CURSORICON_FindCache
374 * Given a handle, find the corresponding cache element
376 * PARAMS
377 * Handle [I] handle to an Image
379 * RETURNS
380 * Success: The cache entry
381 * Failure: NULL
384 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
386 ICONCACHE *ptr;
387 ICONCACHE *pRet=NULL;
388 BOOL IsFound = FALSE;
390 EnterCriticalSection( &IconCrst );
392 for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
394 if ( hIcon == ptr->hIcon )
396 IsFound = TRUE;
397 pRet = ptr;
401 LeaveCriticalSection( &IconCrst );
403 return pRet;
406 /**********************************************************************
407 * CURSORICON_AddSharedIcon
409 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
411 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
412 if ( !ptr ) return;
414 ptr->hModule = hModule;
415 ptr->hRsrc = hRsrc;
416 ptr->hIcon = hIcon;
417 ptr->hGroupRsrc = hGroupRsrc;
418 ptr->count = 1;
420 EnterCriticalSection( &IconCrst );
421 ptr->next = IconAnchor;
422 IconAnchor = ptr;
423 LeaveCriticalSection( &IconCrst );
426 /**********************************************************************
427 * CURSORICON_DelSharedIcon
429 static INT CURSORICON_DelSharedIcon( HICON hIcon )
431 INT count = -1;
432 ICONCACHE *ptr;
434 EnterCriticalSection( &IconCrst );
436 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
437 if ( ptr->hIcon == hIcon )
439 if ( ptr->count > 0 ) ptr->count--;
440 count = ptr->count;
441 break;
444 LeaveCriticalSection( &IconCrst );
446 return count;
449 /**********************************************************************
450 * CURSORICON_FreeModuleIcons
452 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
454 ICONCACHE **ptr = &IconAnchor;
455 HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
457 EnterCriticalSection( &IconCrst );
459 while ( *ptr )
461 if ( (*ptr)->hModule == hModule )
463 ICONCACHE *freePtr = *ptr;
464 *ptr = freePtr->next;
466 GlobalFree16(HICON_16(freePtr->hIcon));
467 HeapFree( GetProcessHeap(), 0, freePtr );
468 continue;
470 ptr = &(*ptr)->next;
473 LeaveCriticalSection( &IconCrst );
477 * The following macro functions account for the irregularities of
478 * accessing cursor and icon resources in files and resource entries.
480 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
481 int *width, int *height, int *bits );
483 /**********************************************************************
484 * CURSORICON_FindBestIcon
486 * Find the icon closest to the requested size and number of colors.
488 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
489 int width, int height, int colors )
491 int i, cx, cy, bits, bestEntry = -1;
492 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
493 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
495 /* Find Best Fit */
496 iTotalDiff = 0xFFFFFFFF;
497 iColorDiff = 0xFFFFFFFF;
498 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
500 iTempXDiff = abs(width - cx);
501 iTempYDiff = abs(height - cy);
503 if(iTotalDiff > (iTempXDiff + iTempYDiff))
505 iXDiff = iTempXDiff;
506 iYDiff = iTempYDiff;
507 iTotalDiff = iXDiff + iYDiff;
511 /* Find Best Colors for Best Fit */
512 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
514 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
516 iTempColorDiff = abs(colors - (1<<bits));
517 if(iColorDiff > iTempColorDiff)
519 bestEntry = i;
520 iColorDiff = iTempColorDiff;
525 return bestEntry;
528 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
529 int *width, int *height, int *bits )
531 CURSORICONDIR *resdir = dir;
532 ICONRESDIR *icon;
534 if ( resdir->idCount <= n )
535 return FALSE;
536 icon = &resdir->idEntries[n].ResInfo.icon;
537 *width = icon->bWidth;
538 *height = icon->bHeight;
539 *bits = resdir->idEntries[n].wBitCount;
540 return TRUE;
543 /**********************************************************************
544 * CURSORICON_FindBestCursor
546 * Find the cursor closest to the requested size.
548 * FIXME: parameter 'color' ignored.
550 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
551 int width, int height, int color )
553 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
555 /* Double height to account for AND and XOR masks */
557 height *= 2;
559 /* First find the largest one smaller than or equal to the requested size*/
561 maxwidth = maxheight = 0;
562 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
564 if ((cx <= width) && (cy <= height) &&
565 (cx > maxwidth) && (cy > maxheight))
567 bestEntry = i;
568 maxwidth = cx;
569 maxheight = cy;
572 if (bestEntry != -1) return bestEntry;
574 /* Now find the smallest one larger than the requested size */
576 maxwidth = maxheight = 255;
577 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
579 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
581 bestEntry = i;
582 maxwidth = cx;
583 maxheight = cy;
587 return bestEntry;
590 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
591 int *width, int *height, int *bits )
593 CURSORICONDIR *resdir = dir;
594 CURSORDIR *cursor;
596 if ( resdir->idCount <= n )
597 return FALSE;
598 cursor = &resdir->idEntries[n].ResInfo.cursor;
599 *width = cursor->wWidth;
600 *height = cursor->wHeight;
601 *bits = resdir->idEntries[n].wBitCount;
602 return TRUE;
605 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
606 int width, int height, int colors )
608 int n;
610 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
611 width, height, colors );
612 if ( n < 0 )
613 return NULL;
614 return &dir->idEntries[n];
617 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
618 int width, int height, int color )
620 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
621 width, height, color );
622 if ( n < 0 )
623 return NULL;
624 return &dir->idEntries[n];
627 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
628 int *width, int *height, int *bits )
630 CURSORICONFILEDIR *filedir = dir;
631 CURSORICONFILEDIRENTRY *entry;
633 if ( filedir->idCount <= n )
634 return FALSE;
635 entry = &filedir->idEntries[n];
636 *width = entry->bWidth;
637 *height = entry->bHeight;
638 *bits = entry->bColorCount;
639 return TRUE;
642 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
643 int width, int height, int color )
645 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
646 width, height, color );
647 if ( n < 0 )
648 return NULL;
649 return &dir->idEntries[n];
652 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
653 int width, int height, int color )
655 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
656 width, height, color );
657 if ( n < 0 )
658 return NULL;
659 return &dir->idEntries[n];
662 /***********************************************************************
663 * stretch_blt_icon
665 * A helper function that stretches a bitmap buffer into an HBITMAP.
667 * PARAMS
668 * hDest [I] The handle of the destination bitmap.
669 * pDestInfo [I] The BITMAPINFO of the destination bitmap.
670 * pSrcInfo [I] The BITMAPINFO of the source bitmap.
671 * pSrcBits [I] A pointer to the source bitmap buffer.
673 static BOOL stretch_blt_icon(HBITMAP hDest, BITMAPINFO *pDestInfo, BITMAPINFO *pSrcInfo, char *pSrcBits)
675 HBITMAP hOld;
676 BOOL res = FALSE;
677 static HDC hdcMem = NULL;
679 if (!hdcMem)
680 hdcMem = CreateCompatibleDC(screen_dc);
682 if (hdcMem)
684 hOld = SelectObject(hdcMem, hDest);
685 res = StretchDIBits(hdcMem,
686 0, 0, pDestInfo->bmiHeader.biWidth, pDestInfo->bmiHeader.biHeight,
687 0, 0, pSrcInfo->bmiHeader.biWidth, pSrcInfo->bmiHeader.biHeight,
688 pSrcBits, pSrcInfo, DIB_RGB_COLORS, SRCCOPY);
689 SelectObject(hdcMem, hOld);
692 return res;
695 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi,
696 POINT16 hotspot, BOOL bIcon,
697 DWORD dwVersion,
698 INT width, INT height,
699 UINT cFlag )
701 HGLOBAL16 hObj;
702 int sizeAnd, sizeXor;
703 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
704 BITMAP bmpXor, bmpAnd;
705 INT size;
706 BITMAPINFO *pSrcInfo, *pDestInfo;
708 if (dwVersion == 0x00020000)
710 FIXME_(cursor)("\t2.xx resources are not supported\n");
711 return 0;
714 /* Check bitmap header */
716 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
717 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
718 bmi->bmiHeader.biCompression != BI_RGB) )
720 WARN_(cursor)("\tinvalid resource bitmap header.\n");
721 return 0;
724 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
726 if (!width) width = bmi->bmiHeader.biWidth;
727 if (!height) height = bmi->bmiHeader.biHeight/2;
729 /* Scale the hotspot */
730 if (((bmi->bmiHeader.biHeight/2 != height) || (bmi->bmiHeader.biWidth != width)) &&
731 hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
733 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
734 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
737 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
738 if (screen_dc)
740 /* Make sure we have room for the monochrome bitmap later on.
741 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
742 * up to and including the biBitCount. In-memory icon resource
743 * format is as follows:
745 * BITMAPINFOHEADER icHeader // DIB header
746 * RGBQUAD icColors[] // Color table
747 * BYTE icXOR[] // DIB bits for XOR mask
748 * BYTE icAND[] // DIB bits for AND mask
751 pSrcInfo = HeapAlloc( GetProcessHeap(), 0,
752 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
753 pDestInfo = HeapAlloc( GetProcessHeap(), 0,
754 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
755 if (pSrcInfo && pDestInfo)
757 memcpy( pSrcInfo, bmi, size );
758 pSrcInfo->bmiHeader.biHeight /= 2;
760 memcpy( pDestInfo, bmi, size );
761 pDestInfo->bmiHeader.biWidth = width;
762 pDestInfo->bmiHeader.biHeight = height;
763 pDestInfo->bmiHeader.biSizeImage = 0;
765 /* Create the XOR bitmap */
766 if(pSrcInfo->bmiHeader.biBitCount == 32)
768 void *pDIBBuffer = NULL;
769 hXorBits = CreateDIBSection(screen_dc, pDestInfo, DIB_RGB_COLORS, &pDIBBuffer, NULL, 0);
771 if(hXorBits)
773 if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
775 DeleteObject(hXorBits);
776 hXorBits = 0;
780 else
782 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
784 if(hXorBits)
786 if(!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
788 DeleteObject(hXorBits);
789 hXorBits = 0;
794 if( hXorBits )
796 char* xbits = (char *)bmi + size +
797 get_dib_width_bytes( bmi->bmiHeader.biWidth,
798 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
800 pSrcInfo->bmiHeader.biBitCount = 1;
801 if (pSrcInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
803 RGBQUAD *rgb = pSrcInfo->bmiColors;
805 pSrcInfo->bmiHeader.biClrUsed = pSrcInfo->bmiHeader.biClrImportant = 2;
806 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
807 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
808 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
810 else
812 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pSrcInfo) + 1);
814 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
815 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
818 /* Create the AND bitmap */
819 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
821 if(!stretch_blt_icon(hAndBits, pDestInfo, pSrcInfo, xbits))
823 DeleteObject(hAndBits);
824 hAndBits = 0;
827 if( !hAndBits )
829 DeleteObject( hXorBits );
830 hXorBits = 0;
834 HeapFree( GetProcessHeap(), 0, pSrcInfo );
835 HeapFree( GetProcessHeap(), 0, pDestInfo );
839 if( !hXorBits || !hAndBits )
841 WARN_(cursor)("\tunable to create an icon bitmap.\n");
842 return 0;
845 /* Now create the CURSORICONINFO structure */
846 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
847 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
848 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
849 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
851 hObj = GlobalAlloc16( GMEM_MOVEABLE,
852 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
853 if (hObj)
855 CURSORICONINFO *info;
857 info = GlobalLock16( hObj );
858 info->ptHotSpot.x = hotspot.x;
859 info->ptHotSpot.y = hotspot.y;
860 info->nWidth = bmpXor.bmWidth;
861 info->nHeight = bmpXor.bmHeight;
862 info->nWidthBytes = bmpXor.bmWidthBytes;
863 info->bPlanes = bmpXor.bmPlanes;
864 info->bBitsPerPixel = bmpXor.bmBitsPixel;
866 /* Transfer the bitmap bits to the CURSORICONINFO structure */
868 GetBitmapBits( hAndBits, sizeAnd, info + 1 );
869 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
870 GlobalUnlock16( hObj );
873 DeleteObject( hAndBits );
874 DeleteObject( hXorBits );
875 return HICON_32(hObj);
879 /**********************************************************************
880 * .ANI cursor support
882 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
883 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
884 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
886 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
887 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
888 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
889 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
890 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
891 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
893 #define ANI_FLAG_ICON 0x1
894 #define ANI_FLAG_SEQUENCE 0x2
896 typedef struct {
897 DWORD header_size;
898 DWORD num_frames;
899 DWORD num_steps;
900 DWORD width;
901 DWORD height;
902 DWORD bpp;
903 DWORD num_planes;
904 DWORD display_rate;
905 DWORD flags;
906 } ani_header;
908 typedef struct {
909 DWORD data_size;
910 const unsigned char *data;
911 } riff_chunk_t;
913 static void dump_ani_header( const ani_header *header )
915 TRACE(" header size: %d\n", header->header_size);
916 TRACE(" frames: %d\n", header->num_frames);
917 TRACE(" steps: %d\n", header->num_steps);
918 TRACE(" width: %d\n", header->width);
919 TRACE(" height: %d\n", header->height);
920 TRACE(" bpp: %d\n", header->bpp);
921 TRACE(" planes: %d\n", header->num_planes);
922 TRACE(" display rate: %d\n", header->display_rate);
923 TRACE(" flags: 0x%08x\n", header->flags);
928 * RIFF:
929 * DWORD "RIFF"
930 * DWORD size
931 * DWORD riff_id
932 * BYTE[] data
934 * LIST:
935 * DWORD "LIST"
936 * DWORD size
937 * DWORD list_id
938 * BYTE[] data
940 * CHUNK:
941 * DWORD chunk_id
942 * DWORD size
943 * BYTE[] data
945 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
947 const unsigned char *ptr = parent_chunk->data;
948 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
950 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
952 while (ptr < end)
954 if ((!chunk_type && *(DWORD *)ptr == chunk_id )
955 || (chunk_type && *(DWORD *)ptr == chunk_type && *((DWORD *)ptr + 2) == chunk_id ))
957 ptr += sizeof(DWORD);
958 chunk->data_size = *(DWORD *)ptr;
959 ptr += sizeof(DWORD);
960 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
961 chunk->data = ptr;
963 return;
966 ptr += sizeof(DWORD);
967 ptr += *(DWORD *)ptr;
968 ptr += sizeof(DWORD);
974 * .ANI layout:
976 * RIFF:'ACON' RIFF chunk
977 * |- CHUNK:'anih' Header
978 * |- CHUNK:'seq ' Sequence information (optional)
979 * \- LIST:'fram' Frame list
980 * |- CHUNK:icon Cursor frames
981 * |- CHUNK:icon
982 * |- ...
983 * \- CHUNK:icon
985 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
986 INT width, INT height, INT colors )
988 HCURSOR cursor;
989 ani_header header = {0};
990 LPBYTE frame_bits = 0;
991 POINT16 hotspot;
992 CURSORICONFILEDIRENTRY *entry;
994 riff_chunk_t root_chunk = { bits_size, bits };
995 riff_chunk_t ACON_chunk = {0};
996 riff_chunk_t anih_chunk = {0};
997 riff_chunk_t fram_chunk = {0};
998 const unsigned char *icon_data;
1000 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1002 if (!bits) return 0;
1004 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1005 if (!ACON_chunk.data)
1007 ERR("Failed to get root chunk.\n");
1008 return 0;
1011 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1012 if (!anih_chunk.data)
1014 ERR("Failed to get 'anih' chunk.\n");
1015 return 0;
1017 memcpy( &header, anih_chunk.data, sizeof(header) );
1018 dump_ani_header( &header );
1020 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1021 if (!fram_chunk.data)
1023 ERR("Failed to get icon list.\n");
1024 return 0;
1027 /* FIXME: For now, just load the first frame. Before we can load all the
1028 * frames, we need to write the needed code in wineserver, etc. to handle
1029 * cursors. Once this code is written, we can extend it to support .ani
1030 * cursors and then update user32 and winex11.drv to load all frames.
1032 * Hopefully this will at least make some games (C&C3, etc.) more playable
1033 * in the meantime.
1035 FIXME("Loading all frames for .ani cursors not implemented.\n");
1036 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1038 entry = CURSORICON_FindBestIconFile( (CURSORICONFILEDIR *) icon_data,
1039 width, height, colors );
1041 frame_bits = HeapAlloc( GetProcessHeap(), 0, entry->dwDIBSize );
1042 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1044 if (!header.width || !header.height)
1046 header.width = entry->bWidth;
1047 header.height = entry->bHeight;
1050 hotspot.x = entry->xHotspot;
1051 hotspot.y = entry->yHotspot;
1053 cursor = CURSORICON_CreateIconFromBMI( (BITMAPINFO *) frame_bits, hotspot,
1054 FALSE, 0x00030000, header.width, header.height, 0 );
1056 HeapFree( GetProcessHeap(), 0, frame_bits );
1058 return cursor;
1062 /**********************************************************************
1063 * CreateIconFromResourceEx (USER32.@)
1065 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1066 * with cbSize parameter as well.
1068 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1069 BOOL bIcon, DWORD dwVersion,
1070 INT width, INT height,
1071 UINT cFlag )
1073 POINT16 hotspot;
1074 BITMAPINFO *bmi;
1076 hotspot.x = ICON_HOTSPOT;
1077 hotspot.y = ICON_HOTSPOT;
1079 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1080 bits, cbSize, dwVersion, width, height,
1081 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1083 if (bIcon)
1084 bmi = (BITMAPINFO *)bits;
1085 else /* get the hotspot */
1087 POINT16 *pt = (POINT16 *)bits;
1088 hotspot = *pt;
1089 bmi = (BITMAPINFO *)(pt + 1);
1092 return CURSORICON_CreateIconFromBMI( bmi, hotspot, bIcon, dwVersion,
1093 width, height, cFlag );
1097 /**********************************************************************
1098 * CreateIconFromResource (USER32.@)
1100 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1101 BOOL bIcon, DWORD dwVersion)
1103 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1107 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1108 INT width, INT height, INT colors,
1109 BOOL fCursor, UINT loadflags)
1111 CURSORICONFILEDIRENTRY *entry;
1112 CURSORICONFILEDIR *dir;
1113 DWORD filesize = 0;
1114 HICON hIcon = 0;
1115 LPBYTE bits;
1116 POINT16 hotspot;
1118 TRACE("loading %s\n", debugstr_w( filename ));
1120 bits = map_fileW( filename, &filesize );
1121 if (!bits)
1122 return hIcon;
1124 /* Check for .ani. */
1125 if (memcmp( bits, "RIFF", 4 ) == 0)
1127 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height,
1128 colors );
1129 goto end;
1132 dir = (CURSORICONFILEDIR*) bits;
1133 if ( filesize < sizeof(*dir) )
1134 goto end;
1136 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1137 goto end;
1139 if ( fCursor )
1140 entry = CURSORICON_FindBestCursorFile( dir, width, height, colors );
1141 else
1142 entry = CURSORICON_FindBestIconFile( dir, width, height, colors );
1144 if ( !entry )
1145 goto end;
1147 /* check that we don't run off the end of the file */
1148 if ( entry->dwDIBOffset > filesize )
1149 goto end;
1150 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1151 goto end;
1153 /* Set the actual hotspot for cursors and ICON_HOTSPOT for icons. */
1154 if ( fCursor )
1156 hotspot.x = entry->xHotspot;
1157 hotspot.y = entry->yHotspot;
1159 else
1161 hotspot.x = ICON_HOTSPOT;
1162 hotspot.y = ICON_HOTSPOT;
1164 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset],
1165 hotspot, !fCursor, 0x00030000,
1166 width, height, loadflags );
1167 end:
1168 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1169 UnmapViewOfFile( bits );
1170 return hIcon;
1173 /**********************************************************************
1174 * CURSORICON_Load
1176 * Load a cursor or icon from resource or file.
1178 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1179 INT width, INT height, INT colors,
1180 BOOL fCursor, UINT loadflags)
1182 HANDLE handle = 0;
1183 HICON hIcon = 0;
1184 HRSRC hRsrc, hGroupRsrc;
1185 CURSORICONDIR *dir;
1186 CURSORICONDIRENTRY *dirEntry;
1187 LPBYTE bits;
1188 WORD wResId;
1189 DWORD dwBytesInRes;
1191 TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
1192 hInstance, debugstr_w(name), width, height, colors, fCursor, loadflags);
1194 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1195 return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
1197 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1199 /* Normalize hInstance (must be uniquely represented for icon cache) */
1201 if (!HIWORD( hInstance ))
1202 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
1204 /* Get directory resource ID */
1206 if (!(hRsrc = FindResourceW( hInstance, name,
1207 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1208 return 0;
1209 hGroupRsrc = hRsrc;
1211 /* Find the best entry in the directory */
1213 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1214 if (!(dir = LockResource( handle ))) return 0;
1215 if (fCursor)
1216 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, colors );
1217 else
1218 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1219 if (!dirEntry) return 0;
1220 wResId = dirEntry->wResId;
1221 dwBytesInRes = dirEntry->dwBytesInRes;
1222 FreeResource( handle );
1224 /* Load the resource */
1226 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1227 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1229 /* If shared icon, check whether it was already loaded */
1230 if ( (loadflags & LR_SHARED)
1231 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1232 return hIcon;
1234 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1235 bits = LockResource( handle );
1236 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1237 !fCursor, 0x00030000, width, height, loadflags);
1238 FreeResource( handle );
1240 /* If shared icon, add to icon cache */
1242 if ( hIcon && (loadflags & LR_SHARED) )
1243 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1245 return hIcon;
1248 /***********************************************************************
1249 * CURSORICON_Copy
1251 * Make a copy of a cursor or icon.
1253 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
1255 char *ptrOld, *ptrNew;
1256 int size;
1257 HICON16 hOld = HICON_16(hIcon);
1258 HICON16 hNew;
1260 if (!(ptrOld = GlobalLock16( hOld ))) return 0;
1261 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
1262 size = GlobalSize16( hOld );
1263 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
1264 FarSetOwner16( hNew, hInst16 );
1265 ptrNew = GlobalLock16( hNew );
1266 memcpy( ptrNew, ptrOld, size );
1267 GlobalUnlock16( hOld );
1268 GlobalUnlock16( hNew );
1269 return HICON_32(hNew);
1272 /*************************************************************************
1273 * CURSORICON_ExtCopy
1275 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1277 * PARAMS
1278 * Handle [I] handle to an Image
1279 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1280 * iDesiredCX [I] The Desired width of the Image
1281 * iDesiredCY [I] The desired height of the Image
1282 * nFlags [I] The flags from CopyImage
1284 * RETURNS
1285 * Success: The new handle of the Image
1287 * NOTES
1288 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1289 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1290 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1295 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1296 INT iDesiredCX, INT iDesiredCY,
1297 UINT nFlags)
1299 HICON hNew=0;
1301 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1302 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1304 if(hIcon == 0)
1306 return 0;
1309 /* Best Fit or Monochrome */
1310 if( (nFlags & LR_COPYFROMRESOURCE
1311 && (iDesiredCX > 0 || iDesiredCY > 0))
1312 || nFlags & LR_MONOCHROME)
1314 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1316 /* Not Found in Cache, then do a straight copy
1318 if(pIconCache == NULL)
1320 hNew = CURSORICON_Copy(0, hIcon);
1321 if(nFlags & LR_COPYFROMRESOURCE)
1323 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1326 else
1328 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1329 LPBYTE pBits;
1330 HANDLE hMem;
1331 HRSRC hRsrc;
1332 DWORD dwBytesInRes;
1333 WORD wResId;
1334 CURSORICONDIR *pDir;
1335 CURSORICONDIRENTRY *pDirEntry;
1336 BOOL bIsIcon = (nType == IMAGE_ICON);
1338 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1340 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1341 || (iDesiredCX == 0 && iDesiredCY == 0))
1343 iDesiredCY = GetSystemMetrics(bIsIcon ?
1344 SM_CYICON : SM_CYCURSOR);
1345 iDesiredCX = GetSystemMetrics(bIsIcon ?
1346 SM_CXICON : SM_CXCURSOR);
1349 /* Retrieve the CURSORICONDIRENTRY
1351 if (!(hMem = LoadResource( pIconCache->hModule ,
1352 pIconCache->hGroupRsrc)))
1354 return 0;
1356 if (!(pDir = LockResource( hMem )))
1358 return 0;
1361 /* Find Best Fit
1363 if(bIsIcon)
1365 pDirEntry = CURSORICON_FindBestIconRes(
1366 pDir, iDesiredCX, iDesiredCY, 256 );
1368 else
1370 pDirEntry = CURSORICON_FindBestCursorRes(
1371 pDir, iDesiredCX, iDesiredCY, 1);
1374 wResId = pDirEntry->wResId;
1375 dwBytesInRes = pDirEntry->dwBytesInRes;
1376 FreeResource(hMem);
1378 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1379 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1380 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1382 /* Get the Best Fit
1384 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1385 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1387 return 0;
1389 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1391 return 0;
1394 pBits = LockResource( hMem );
1396 if(nFlags & LR_DEFAULTSIZE)
1398 iTargetCY = GetSystemMetrics(SM_CYICON);
1399 iTargetCX = GetSystemMetrics(SM_CXICON);
1402 /* Create a New Icon with the proper dimension
1404 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1405 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1406 FreeResource(hMem);
1409 else hNew = CURSORICON_Copy(0, hIcon);
1410 return hNew;
1414 /***********************************************************************
1415 * CreateCursor (USER32.@)
1417 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1418 INT xHotSpot, INT yHotSpot,
1419 INT nWidth, INT nHeight,
1420 LPCVOID lpANDbits, LPCVOID lpXORbits )
1422 CURSORICONINFO info;
1424 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1425 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1427 info.ptHotSpot.x = xHotSpot;
1428 info.ptHotSpot.y = yHotSpot;
1429 info.nWidth = nWidth;
1430 info.nHeight = nHeight;
1431 info.nWidthBytes = 0;
1432 info.bPlanes = 1;
1433 info.bBitsPerPixel = 1;
1435 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1439 /***********************************************************************
1440 * CreateIcon (USER.407)
1442 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1443 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1444 LPCVOID lpANDbits, LPCVOID lpXORbits )
1446 CURSORICONINFO info;
1448 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1449 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1451 info.ptHotSpot.x = ICON_HOTSPOT;
1452 info.ptHotSpot.y = ICON_HOTSPOT;
1453 info.nWidth = nWidth;
1454 info.nHeight = nHeight;
1455 info.nWidthBytes = 0;
1456 info.bPlanes = bPlanes;
1457 info.bBitsPerPixel = bBitsPixel;
1459 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1463 /***********************************************************************
1464 * CreateIcon (USER32.@)
1466 * Creates an icon based on the specified bitmaps. The bitmaps must be
1467 * provided in a device dependent format and will be resized to
1468 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1469 * depth. The provided bitmaps must be top-down bitmaps.
1470 * Although Windows does not support 15bpp(*) this API must support it
1471 * for Winelib applications.
1473 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1474 * format!
1476 * RETURNS
1477 * Success: handle to an icon
1478 * Failure: NULL
1480 * FIXME: Do we need to resize the bitmaps?
1482 HICON WINAPI CreateIcon(
1483 HINSTANCE hInstance, /* [in] the application's hInstance */
1484 INT nWidth, /* [in] the width of the provided bitmaps */
1485 INT nHeight, /* [in] the height of the provided bitmaps */
1486 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1487 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1488 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1489 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1491 ICONINFO iinfo;
1492 HICON hIcon;
1494 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1495 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1497 iinfo.fIcon = TRUE;
1498 iinfo.xHotspot = ICON_HOTSPOT;
1499 iinfo.yHotspot = ICON_HOTSPOT;
1500 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1501 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1503 hIcon = CreateIconIndirect( &iinfo );
1505 DeleteObject( iinfo.hbmMask );
1506 DeleteObject( iinfo.hbmColor );
1508 return hIcon;
1512 /***********************************************************************
1513 * CreateCursorIconIndirect (USER.408)
1515 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1516 CURSORICONINFO *info,
1517 LPCVOID lpANDbits,
1518 LPCVOID lpXORbits )
1520 HGLOBAL16 handle;
1521 char *ptr;
1522 int sizeAnd, sizeXor;
1524 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1525 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1526 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1527 sizeXor = info->nHeight * info->nWidthBytes;
1528 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1529 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1530 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1531 return 0;
1532 FarSetOwner16( handle, hInstance );
1533 ptr = GlobalLock16( handle );
1534 memcpy( ptr, info, sizeof(*info) );
1535 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1536 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1537 GlobalUnlock16( handle );
1538 return handle;
1542 /***********************************************************************
1543 * CopyIcon (USER.368)
1545 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1547 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1548 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1552 /***********************************************************************
1553 * CopyIcon (USER32.@)
1555 HICON WINAPI CopyIcon( HICON hIcon )
1557 TRACE_(icon)("%p\n", hIcon );
1558 return CURSORICON_Copy( 0, hIcon );
1562 /***********************************************************************
1563 * CopyCursor (USER.369)
1565 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1567 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1568 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1571 /**********************************************************************
1572 * DestroyIcon32 (USER.610)
1574 * This routine is actually exported from Win95 USER under the name
1575 * DestroyIcon32 ... The behaviour implemented here should mimic
1576 * the Win95 one exactly, especially the return values, which
1577 * depend on the setting of various flags.
1579 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1581 WORD retv;
1583 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1585 /* Check whether destroying active cursor */
1587 if ( get_user_thread_info()->cursor == HICON_32(handle) )
1589 WARN_(cursor)("Destroying active cursor!\n" );
1590 return FALSE;
1593 /* Try shared cursor/icon first */
1595 if ( !(flags & CID_NONSHARED) )
1597 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1599 if ( count != -1 )
1600 return (flags & CID_WIN32)? TRUE : (count == 0);
1602 /* FIXME: OEM cursors/icons should be recognized */
1605 /* Now assume non-shared cursor/icon */
1607 retv = GlobalFree16( handle );
1608 return (flags & CID_RESOURCE)? retv : TRUE;
1611 /***********************************************************************
1612 * DestroyIcon (USER32.@)
1614 BOOL WINAPI DestroyIcon( HICON hIcon )
1616 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1620 /***********************************************************************
1621 * DestroyCursor (USER32.@)
1623 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1625 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1628 /***********************************************************************
1629 * bitmap_has_alpha_channel
1631 * Analyses bits bitmap to determine if alpha data is present.
1633 * PARAMS
1634 * bpp [I] The bits-per-pixel of the bitmap
1635 * bitmapBits [I] A pointer to the bitmap data
1636 * bitmapLength [I] The length of the bitmap in bytes
1638 * RETURNS
1639 * TRUE if an alpha channel is discovered, FALSE
1641 * NOTE
1642 * Windows' behaviour is that if the icon bitmap is 32-bit and at
1643 * least one pixel has a non-zero alpha, then the bitmap is a
1644 * treated as having an alpha channel transparentcy. Otherwise,
1645 * it's treated as being completely opaque.
1648 static BOOL bitmap_has_alpha_channel( int bpp, unsigned char *bitmapBits,
1649 unsigned int bitmapLength )
1651 /* Detect an alpha channel by looking for non-zero alpha pixels */
1652 if(bpp == 32)
1654 unsigned int offset;
1655 for(offset = 3; offset < bitmapLength; offset += 4)
1657 if(bitmapBits[offset] != 0)
1659 return TRUE;
1663 return FALSE;
1666 /***********************************************************************
1667 * premultiply_alpha_channel
1669 * Premultiplies the color channels of a 32-bit bitmap by the alpha
1670 * channel. This is a necessary step that must be carried out on
1671 * the image before it is passed to GdiAlphaBlend
1673 * PARAMS
1674 * destBitmap [I] The destination bitmap buffer
1675 * srcBitmap [I] The source bitmap buffer
1676 * bitmapLength [I] The length of the bitmap in bytes
1679 static void premultiply_alpha_channel( unsigned char *destBitmap,
1680 unsigned char *srcBitmap,
1681 unsigned int bitmapLength )
1683 unsigned char *destPixel = destBitmap;
1684 unsigned char *srcPixel = srcBitmap;
1686 while(destPixel < destBitmap + bitmapLength)
1688 unsigned char alpha = srcPixel[3];
1689 *(destPixel++) = *(srcPixel++) * alpha / 255;
1690 *(destPixel++) = *(srcPixel++) * alpha / 255;
1691 *(destPixel++) = *(srcPixel++) * alpha / 255;
1692 *(destPixel++) = *(srcPixel++);
1696 /***********************************************************************
1697 * DrawIcon (USER32.@)
1699 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1701 CURSORICONINFO *ptr;
1702 HDC hMemDC;
1703 HBITMAP hXorBits = NULL, hAndBits = NULL, hBitTemp = NULL;
1704 COLORREF oldFg, oldBg;
1705 unsigned char *xorBitmapBits;
1706 unsigned int dibLength;
1708 TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);
1710 if (!(ptr = GlobalLock16(HICON_16(hIcon)))) return FALSE;
1711 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1713 dibLength = ptr->nHeight * get_bitmap_width_bytes(
1714 ptr->nWidth, ptr->bBitsPerPixel);
1716 xorBitmapBits = (unsigned char *)(ptr + 1) + ptr->nHeight *
1717 get_bitmap_width_bytes(ptr->nWidth, 1);
1719 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1720 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1722 if(bitmap_has_alpha_channel(ptr->bBitsPerPixel, xorBitmapBits, dibLength))
1724 BITMAPINFOHEADER bmih;
1725 unsigned char *dibBits;
1727 memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
1728 bmih.biSize = sizeof(BITMAPINFOHEADER);
1729 bmih.biWidth = ptr->nWidth;
1730 bmih.biHeight = -ptr->nHeight;
1731 bmih.biPlanes = ptr->bPlanes;
1732 bmih.biBitCount = 32;
1733 bmih.biCompression = BI_RGB;
1735 hXorBits = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
1736 (void*)&dibBits, NULL, 0);
1738 if (hXorBits && dibBits)
1740 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
1742 /* Do the alpha blending render */
1743 premultiply_alpha_channel(dibBits, xorBitmapBits, dibLength);
1744 hBitTemp = SelectObject( hMemDC, hXorBits );
1745 /* Destination width/height has to be "System Large" size */
1746 GdiAlphaBlend(hdc, x, y, GetSystemMetrics(SM_CXICON),
1747 GetSystemMetrics(SM_CYICON), hMemDC,
1748 0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
1749 SelectObject( hMemDC, hBitTemp );
1752 else
1754 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1, ptr + 1 );
1755 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1756 ptr->bBitsPerPixel, xorBitmapBits);
1758 if (hXorBits && hAndBits)
1760 hBitTemp = SelectObject( hMemDC, hAndBits );
1761 StretchBlt( hdc, x, y, GetSystemMetrics(SM_CXICON),
1762 GetSystemMetrics(SM_CYICON), hMemDC, 0, 0,
1763 ptr->nWidth, ptr->nHeight, SRCAND );
1764 SelectObject( hMemDC, hXorBits );
1765 StretchBlt( hdc, x, y, GetSystemMetrics(SM_CXICON),
1766 GetSystemMetrics(SM_CYICON), hMemDC, 0, 0,
1767 ptr->nWidth, ptr->nHeight, SRCINVERT );
1768 SelectObject( hMemDC, hBitTemp );
1772 DeleteDC( hMemDC );
1773 if (hXorBits) DeleteObject( hXorBits );
1774 if (hAndBits) DeleteObject( hAndBits );
1775 GlobalUnlock16(HICON_16(hIcon));
1776 SetTextColor( hdc, oldFg );
1777 SetBkColor( hdc, oldBg );
1778 return TRUE;
1781 /***********************************************************************
1782 * DumpIcon (USER.459)
1784 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1785 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1787 CURSORICONINFO *info = MapSL( pInfo );
1788 int sizeAnd, sizeXor;
1790 if (!info) return 0;
1791 sizeXor = info->nHeight * info->nWidthBytes;
1792 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1793 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1794 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1795 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1796 return MAKELONG( sizeXor, sizeXor );
1800 /***********************************************************************
1801 * SetCursor (USER32.@)
1803 * Set the cursor shape.
1805 * RETURNS
1806 * A handle to the previous cursor shape.
1808 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1810 struct user_thread_info *thread_info = get_user_thread_info();
1811 HCURSOR hOldCursor;
1813 if (hCursor == thread_info->cursor) return hCursor; /* No change */
1814 TRACE("%p\n", hCursor);
1815 hOldCursor = thread_info->cursor;
1816 thread_info->cursor = hCursor;
1817 /* Change the cursor shape only if it is visible */
1818 if (thread_info->cursor_count >= 0)
1820 USER_Driver->pSetCursor(GlobalLock16(HCURSOR_16(hCursor)));
1821 GlobalUnlock16(HCURSOR_16(hCursor));
1823 return hOldCursor;
1826 /***********************************************************************
1827 * ShowCursor (USER32.@)
1829 INT WINAPI ShowCursor( BOOL bShow )
1831 struct user_thread_info *thread_info = get_user_thread_info();
1833 TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
1835 if (bShow)
1837 if (++thread_info->cursor_count == 0) /* Show it */
1839 USER_Driver->pSetCursor(GlobalLock16(HCURSOR_16(thread_info->cursor)));
1840 GlobalUnlock16(HCURSOR_16(thread_info->cursor));
1843 else
1845 if (--thread_info->cursor_count == -1) /* Hide it */
1846 USER_Driver->pSetCursor( NULL );
1848 return thread_info->cursor_count;
1851 /***********************************************************************
1852 * GetCursor (USER32.@)
1854 HCURSOR WINAPI GetCursor(void)
1856 return get_user_thread_info()->cursor;
1860 /***********************************************************************
1861 * ClipCursor (USER32.@)
1863 BOOL WINAPI ClipCursor( const RECT *rect )
1865 RECT virt;
1867 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1868 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1869 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1870 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1872 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1873 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1875 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1876 CURSOR_ClipRect = virt;
1878 USER_Driver->pClipCursor( rect );
1879 return TRUE;
1883 /***********************************************************************
1884 * GetClipCursor (USER32.@)
1886 BOOL WINAPI GetClipCursor( RECT *rect )
1888 /* If this is first time - initialize the rect */
1889 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1891 return CopyRect( rect, &CURSOR_ClipRect );
1895 /***********************************************************************
1896 * SetSystemCursor (USER32.@)
1898 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1900 FIXME("(%p,%08x),stub!\n", hcur, id);
1901 return TRUE;
1905 /**********************************************************************
1906 * LookupIconIdFromDirectoryEx (USER.364)
1908 * FIXME: exact parameter sizes
1910 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1911 INT16 width, INT16 height, UINT16 cFlag )
1913 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1916 /**********************************************************************
1917 * LookupIconIdFromDirectoryEx (USER32.@)
1919 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1920 INT width, INT height, UINT cFlag )
1922 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1923 UINT retVal = 0;
1924 if( dir && !dir->idReserved && (dir->idType & 3) )
1926 CURSORICONDIRENTRY* entry;
1927 HDC hdc;
1928 UINT palEnts;
1929 int colors;
1930 hdc = GetDC(0);
1931 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1932 if (palEnts == 0)
1933 palEnts = 256;
1934 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1936 ReleaseDC(0, hdc);
1938 if( bIcon )
1939 entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1940 else
1941 entry = CURSORICON_FindBestCursorRes( dir, width, height, colors );
1943 if( entry ) retVal = entry->wResId;
1945 else WARN_(cursor)("invalid resource directory\n");
1946 return retVal;
1949 /**********************************************************************
1950 * LookupIconIdFromDirectory (USER32.@)
1952 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1954 return LookupIconIdFromDirectoryEx( dir, bIcon,
1955 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1956 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1959 /**********************************************************************
1960 * GetIconID (USER.455)
1962 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1964 LPBYTE lpDir = GlobalLock16(hResource);
1966 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1967 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1969 switch(resType)
1971 case RT_CURSOR:
1972 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1973 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1974 case RT_ICON:
1975 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1976 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1977 default:
1978 WARN_(cursor)("invalid res type %d\n", resType );
1980 return 0;
1983 /**********************************************************************
1984 * LoadCursorIconHandler (USER.336)
1986 * Supposed to load resources of Windows 2.x applications.
1988 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1990 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1991 hResource, hModule, hRsrc);
1992 return 0;
1995 /**********************************************************************
1996 * LoadIconHandler (USER.456)
1998 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
2000 LPBYTE bits = LockResource16( hResource );
2002 TRACE_(cursor)("hRes=%04x\n",hResource);
2004 return HICON_16(CreateIconFromResourceEx( bits, 0, TRUE,
2005 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
2008 /***********************************************************************
2009 * LoadCursorW (USER32.@)
2011 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
2013 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2015 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
2016 LR_SHARED | LR_DEFAULTSIZE );
2019 /***********************************************************************
2020 * LoadCursorA (USER32.@)
2022 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
2024 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2026 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
2027 LR_SHARED | LR_DEFAULTSIZE );
2030 /***********************************************************************
2031 * LoadCursorFromFileW (USER32.@)
2033 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
2035 TRACE("%s\n", debugstr_w(name));
2037 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
2038 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2041 /***********************************************************************
2042 * LoadCursorFromFileA (USER32.@)
2044 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
2046 TRACE("%s\n", debugstr_a(name));
2048 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
2049 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2052 /***********************************************************************
2053 * LoadIconW (USER32.@)
2055 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
2057 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2059 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
2060 LR_SHARED | LR_DEFAULTSIZE );
2063 /***********************************************************************
2064 * LoadIconA (USER32.@)
2066 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
2068 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2070 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
2071 LR_SHARED | LR_DEFAULTSIZE );
2074 /**********************************************************************
2075 * GetIconInfo (USER32.@)
2077 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
2079 CURSORICONINFO *ciconinfo;
2080 INT height;
2082 ciconinfo = GlobalLock16(HICON_16(hIcon));
2083 if (!ciconinfo)
2084 return FALSE;
2086 TRACE("%p => %dx%d, %d bpp\n", hIcon,
2087 ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
2089 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
2090 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
2092 iconinfo->fIcon = TRUE;
2093 iconinfo->xHotspot = ciconinfo->nWidth / 2;
2094 iconinfo->yHotspot = ciconinfo->nHeight / 2;
2096 else
2098 iconinfo->fIcon = FALSE;
2099 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
2100 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
2103 height = ciconinfo->nHeight;
2105 if (ciconinfo->bBitsPerPixel > 1)
2107 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
2108 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
2109 (char *)(ciconinfo + 1)
2110 + ciconinfo->nHeight *
2111 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
2113 else
2115 iconinfo->hbmColor = 0;
2116 height *= 2;
2119 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
2120 1, 1, ciconinfo + 1);
2122 GlobalUnlock16(HICON_16(hIcon));
2124 return TRUE;
2127 /**********************************************************************
2128 * CreateIconIndirect (USER32.@)
2130 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2132 DIBSECTION bmpXor;
2133 BITMAP bmpAnd;
2134 HICON16 hObj;
2135 int xor_objsize = 0, sizeXor = 0, sizeAnd, planes, bpp;
2137 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2138 iconinfo->hbmColor, iconinfo->hbmMask,
2139 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2141 if (!iconinfo->hbmMask) return 0;
2143 planes = GetDeviceCaps( screen_dc, PLANES );
2144 bpp = GetDeviceCaps( screen_dc, BITSPIXEL );
2146 if (iconinfo->hbmColor)
2148 xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2149 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2150 bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
2151 bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
2152 /* we can use either depth 1 or screen depth for xor bitmap */
2153 if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
2154 sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
2156 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2157 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2158 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2159 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2161 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2163 hObj = GlobalAlloc16( GMEM_MOVEABLE,
2164 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
2165 if (hObj)
2167 CURSORICONINFO *info;
2169 info = GlobalLock16( hObj );
2171 /* If we are creating an icon, the hotspot is unused */
2172 if (iconinfo->fIcon)
2174 info->ptHotSpot.x = ICON_HOTSPOT;
2175 info->ptHotSpot.y = ICON_HOTSPOT;
2177 else
2179 info->ptHotSpot.x = iconinfo->xHotspot;
2180 info->ptHotSpot.y = iconinfo->yHotspot;
2183 if (iconinfo->hbmColor)
2185 info->nWidth = bmpXor.dsBm.bmWidth;
2186 info->nHeight = bmpXor.dsBm.bmHeight;
2187 info->nWidthBytes = bmpXor.dsBm.bmWidthBytes;
2188 info->bPlanes = planes;
2189 info->bBitsPerPixel = bpp;
2191 else
2193 info->nWidth = bmpAnd.bmWidth;
2194 info->nHeight = bmpAnd.bmHeight / 2;
2195 info->nWidthBytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2196 info->bPlanes = 1;
2197 info->bBitsPerPixel = 1;
2200 /* Transfer the bitmap bits to the CURSORICONINFO structure */
2202 /* Some apps pass a color bitmap as a mask, convert it to b/w */
2203 if (bmpAnd.bmBitsPixel == 1)
2205 GetBitmapBits( iconinfo->hbmMask, sizeAnd, info + 1 );
2207 else
2209 HDC hdc, hdc_mem;
2210 HBITMAP hbmp_old, hbmp_mem_old, hbmp_mono;
2212 hdc = GetDC( 0 );
2213 hdc_mem = CreateCompatibleDC( hdc );
2215 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
2217 hbmp_old = SelectObject( hdc, iconinfo->hbmMask );
2218 hbmp_mem_old = SelectObject( hdc_mem, hbmp_mono );
2220 BitBlt( hdc_mem, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc, 0, 0, SRCCOPY );
2222 SelectObject( hdc, hbmp_old );
2223 SelectObject( hdc_mem, hbmp_mem_old );
2225 DeleteDC( hdc_mem );
2226 ReleaseDC( 0, hdc );
2228 GetBitmapBits( hbmp_mono, sizeAnd, info + 1 );
2229 DeleteObject( hbmp_mono );
2232 if (iconinfo->hbmColor)
2234 char *dst_bits = (char*)(info + 1) + sizeAnd;
2236 if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
2237 GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
2238 else
2240 BITMAPINFO bminfo;
2241 int dib_width = get_dib_width_bytes( info->nWidth, info->bBitsPerPixel );
2242 int bitmap_width = get_bitmap_width_bytes( info->nWidth, info->bBitsPerPixel );
2244 bminfo.bmiHeader.biSize = sizeof(bminfo);
2245 bminfo.bmiHeader.biWidth = info->nWidth;
2246 bminfo.bmiHeader.biHeight = info->nHeight;
2247 bminfo.bmiHeader.biPlanes = info->bPlanes;
2248 bminfo.bmiHeader.biBitCount = info->bBitsPerPixel;
2249 bminfo.bmiHeader.biCompression = BI_RGB;
2250 bminfo.bmiHeader.biSizeImage = info->nHeight * dib_width;
2251 bminfo.bmiHeader.biXPelsPerMeter = 0;
2252 bminfo.bmiHeader.biYPelsPerMeter = 0;
2253 bminfo.bmiHeader.biClrUsed = 0;
2254 bminfo.bmiHeader.biClrImportant = 0;
2256 /* swap lines for dib sections */
2257 if (xor_objsize == sizeof(DIBSECTION))
2258 bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;
2260 if (dib_width != bitmap_width) /* need to fixup alignment */
2262 char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );
2264 if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2265 src_bits, &bminfo, DIB_RGB_COLORS ))
2267 int y;
2268 for (y = 0; y < info->nHeight; y++)
2269 memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
2271 HeapFree( GetProcessHeap(), 0, src_bits );
2273 else
2274 GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
2275 dst_bits, &bminfo, DIB_RGB_COLORS );
2278 GlobalUnlock16( hObj );
2280 return HICON_32(hObj);
2283 /******************************************************************************
2284 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2286 * NOTES
2287 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2289 * PARAMS
2290 * hdc [I] Handle to device context
2291 * x0 [I] X coordinate of upper left corner
2292 * y0 [I] Y coordinate of upper left corner
2293 * hIcon [I] Handle to icon to draw
2294 * cxWidth [I] Width of icon
2295 * cyWidth [I] Height of icon
2296 * istep [I] Index of frame in animated cursor
2297 * hbr [I] Handle to background brush
2298 * flags [I] Icon-drawing flags
2300 * RETURNS
2301 * Success: TRUE
2302 * Failure: FALSE
2304 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2305 INT cxWidth, INT cyWidth, UINT istep,
2306 HBRUSH hbr, UINT flags )
2308 CURSORICONINFO *ptr;
2309 HDC hDC_off = 0, hMemDC;
2310 BOOL result = FALSE, DoOffscreen;
2311 HBITMAP hB_off = 0, hOld = 0;
2312 unsigned char *xorBitmapBits;
2313 unsigned int xorLength;
2314 BOOL has_alpha = FALSE;
2316 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2317 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2319 if (!(ptr = GlobalLock16(HICON_16(hIcon)))) return FALSE;
2320 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
2322 if (istep)
2323 FIXME_(icon)("Ignoring istep=%d\n", istep);
2324 if (flags & DI_NOMIRROR)
2325 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2327 xorLength = ptr->nHeight * get_bitmap_width_bytes(
2328 ptr->nWidth, ptr->bBitsPerPixel);
2329 xorBitmapBits = (unsigned char *)(ptr + 1) + ptr->nHeight *
2330 get_bitmap_width_bytes(ptr->nWidth, 1);
2332 if (flags & DI_IMAGE)
2333 has_alpha = bitmap_has_alpha_channel(
2334 ptr->bBitsPerPixel, xorBitmapBits, xorLength);
2336 /* Calculate the size of the destination image. */
2337 if (cxWidth == 0)
2339 if (flags & DI_DEFAULTSIZE)
2340 cxWidth = GetSystemMetrics (SM_CXICON);
2341 else
2342 cxWidth = ptr->nWidth;
2344 if (cyWidth == 0)
2346 if (flags & DI_DEFAULTSIZE)
2347 cyWidth = GetSystemMetrics (SM_CYICON);
2348 else
2349 cyWidth = ptr->nHeight;
2352 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2354 if (DoOffscreen) {
2355 RECT r;
2357 r.left = 0;
2358 r.top = 0;
2359 r.right = cxWidth;
2360 r.bottom = cxWidth;
2362 hDC_off = CreateCompatibleDC(hdc);
2363 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2364 if (hDC_off && hB_off) {
2365 hOld = SelectObject(hDC_off, hB_off);
2366 FillRect(hDC_off, &r, hbr);
2370 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2372 HBITMAP hBitTemp;
2373 HBITMAP hXorBits = NULL, hAndBits = NULL;
2374 COLORREF oldFg, oldBg;
2375 INT nStretchMode;
2377 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2379 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2380 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2382 if (((flags & DI_MASK) && !(flags & DI_IMAGE)) ||
2383 ((flags & DI_MASK) && !has_alpha))
2385 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight, 1, 1, ptr + 1 );
2386 if (hAndBits)
2388 hBitTemp = SelectObject( hMemDC, hAndBits );
2389 if (DoOffscreen)
2390 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2391 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2392 else
2393 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2394 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2395 SelectObject( hMemDC, hBitTemp );
2399 if (flags & DI_IMAGE)
2401 BITMAPINFOHEADER bmih;
2402 unsigned char *dibBits;
2404 memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
2405 bmih.biSize = sizeof(BITMAPINFOHEADER);
2406 bmih.biWidth = ptr->nWidth;
2407 bmih.biHeight = -ptr->nHeight;
2408 bmih.biPlanes = ptr->bPlanes;
2409 bmih.biBitCount = ptr->bBitsPerPixel;
2410 bmih.biCompression = BI_RGB;
2412 hXorBits = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
2413 (void*)&dibBits, NULL, 0);
2415 if (hXorBits && dibBits)
2417 if(has_alpha)
2419 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2421 /* Do the alpha blending render */
2422 premultiply_alpha_channel(dibBits, xorBitmapBits, xorLength);
2423 hBitTemp = SelectObject( hMemDC, hXorBits );
2425 if (DoOffscreen)
2426 GdiAlphaBlend(hDC_off, 0, 0, cxWidth, cyWidth, hMemDC,
2427 0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
2428 else
2429 GdiAlphaBlend(hdc, x0, y0, cxWidth, cyWidth, hMemDC,
2430 0, 0, ptr->nWidth, ptr->nHeight, pixelblend);
2432 SelectObject( hMemDC, hBitTemp );
2434 else
2436 memcpy(dibBits, xorBitmapBits, xorLength);
2437 hBitTemp = SelectObject( hMemDC, hXorBits );
2438 if (DoOffscreen)
2439 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2440 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2441 else
2442 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2443 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2444 SelectObject( hMemDC, hBitTemp );
2447 DeleteObject( hXorBits );
2451 result = TRUE;
2453 SetTextColor( hdc, oldFg );
2454 SetBkColor( hdc, oldBg );
2456 if (hAndBits) DeleteObject( hAndBits );
2457 SetStretchBltMode (hdc, nStretchMode);
2458 if (DoOffscreen) {
2459 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2460 SelectObject(hDC_off, hOld);
2463 if (hMemDC) DeleteDC( hMemDC );
2464 if (hDC_off) DeleteDC(hDC_off);
2465 if (hB_off) DeleteObject(hB_off);
2466 GlobalUnlock16(HICON_16(hIcon));
2467 return result;
2470 /***********************************************************************
2471 * DIB_FixColorsToLoadflags
2473 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2474 * are in loadflags
2476 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2478 int colors;
2479 COLORREF c_W, c_S, c_F, c_L, c_C;
2480 int incr,i;
2481 RGBQUAD *ptr;
2482 int bitmap_type;
2483 LONG width;
2484 LONG height;
2485 WORD bpp;
2486 DWORD compr;
2488 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2490 WARN_(resource)("Invalid bitmap\n");
2491 return;
2494 if (bpp > 8) return;
2496 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2498 incr = 3;
2499 colors = 1 << bpp;
2501 else
2503 incr = 4;
2504 colors = bmi->bmiHeader.biClrUsed;
2505 if (colors > 256) colors = 256;
2506 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2509 c_W = GetSysColor(COLOR_WINDOW);
2510 c_S = GetSysColor(COLOR_3DSHADOW);
2511 c_F = GetSysColor(COLOR_3DFACE);
2512 c_L = GetSysColor(COLOR_3DLIGHT);
2514 if (loadflags & LR_LOADTRANSPARENT) {
2515 switch (bpp) {
2516 case 1: pix = pix >> 7; break;
2517 case 4: pix = pix >> 4; break;
2518 case 8: break;
2519 default:
2520 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2521 return;
2523 if (pix >= colors) {
2524 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2525 return;
2527 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2528 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2529 ptr->rgbBlue = GetBValue(c_W);
2530 ptr->rgbGreen = GetGValue(c_W);
2531 ptr->rgbRed = GetRValue(c_W);
2533 if (loadflags & LR_LOADMAP3DCOLORS)
2534 for (i=0; i<colors; i++) {
2535 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2536 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2537 if (c_C == RGB(128, 128, 128)) {
2538 ptr->rgbRed = GetRValue(c_S);
2539 ptr->rgbGreen = GetGValue(c_S);
2540 ptr->rgbBlue = GetBValue(c_S);
2541 } else if (c_C == RGB(192, 192, 192)) {
2542 ptr->rgbRed = GetRValue(c_F);
2543 ptr->rgbGreen = GetGValue(c_F);
2544 ptr->rgbBlue = GetBValue(c_F);
2545 } else if (c_C == RGB(223, 223, 223)) {
2546 ptr->rgbRed = GetRValue(c_L);
2547 ptr->rgbGreen = GetGValue(c_L);
2548 ptr->rgbBlue = GetBValue(c_L);
2554 /**********************************************************************
2555 * BITMAP_Load
2557 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2558 INT desiredx, INT desiredy, UINT loadflags )
2560 HBITMAP hbitmap = 0, orig_bm;
2561 HRSRC hRsrc;
2562 HGLOBAL handle;
2563 char *ptr = NULL;
2564 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2565 int size;
2566 BYTE pix;
2567 char *bits;
2568 LONG width, height, new_width, new_height;
2569 WORD bpp_dummy;
2570 DWORD compr_dummy;
2571 INT bm_type;
2572 HDC screen_mem_dc = NULL;
2574 if (!(loadflags & LR_LOADFROMFILE))
2576 if (!instance)
2578 /* OEM bitmap: try to load the resource from user32.dll */
2579 instance = user32_module;
2582 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2583 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2585 if ((info = LockResource( handle )) == NULL) return 0;
2587 else
2589 BITMAPFILEHEADER * bmfh;
2591 if (!(ptr = map_fileW( name, NULL ))) return 0;
2592 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2593 bmfh = (BITMAPFILEHEADER *)ptr;
2594 if (!( bmfh->bfType == 0x4d42 /* 'BM' */ &&
2595 bmfh->bfReserved1 == 0 &&
2596 bmfh->bfReserved2 == 0))
2598 WARN("Invalid/unsupported bitmap format!\n");
2599 UnmapViewOfFile( ptr );
2600 return 0;
2604 size = bitmap_info_size(info, DIB_RGB_COLORS);
2605 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2606 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2608 if (!fix_info || !scaled_info) goto end;
2609 memcpy(fix_info, info, size);
2611 pix = *((LPBYTE)info + size);
2612 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2614 memcpy(scaled_info, fix_info, size);
2615 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2616 &bpp_dummy, &compr_dummy);
2617 if(desiredx != 0)
2618 new_width = desiredx;
2619 else
2620 new_width = width;
2622 if(desiredy != 0)
2623 new_height = height > 0 ? desiredy : -desiredy;
2624 else
2625 new_height = height;
2627 if(bm_type == 0)
2629 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2630 core->bcWidth = new_width;
2631 core->bcHeight = new_height;
2633 else
2635 scaled_info->bmiHeader.biWidth = new_width;
2636 scaled_info->bmiHeader.biHeight = new_height;
2639 if (new_height < 0) new_height = -new_height;
2641 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2642 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2644 bits = (char *)info + size;
2646 if (loadflags & LR_CREATEDIBSECTION)
2648 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2649 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2651 else
2653 if (is_dib_monochrome(fix_info))
2654 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2655 else
2656 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2659 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2660 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2661 SelectObject(screen_mem_dc, orig_bm);
2663 end:
2664 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2665 HeapFree(GetProcessHeap(), 0, scaled_info);
2666 HeapFree(GetProcessHeap(), 0, fix_info);
2667 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2669 return hbitmap;
2672 /**********************************************************************
2673 * LoadImageA (USER32.@)
2675 * See LoadImageW.
2677 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2678 INT desiredx, INT desiredy, UINT loadflags)
2680 HANDLE res;
2681 LPWSTR u_name;
2683 if (!HIWORD(name))
2684 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2686 __TRY {
2687 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2688 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2689 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2691 __EXCEPT_PAGE_FAULT {
2692 SetLastError( ERROR_INVALID_PARAMETER );
2693 return 0;
2695 __ENDTRY
2696 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2697 HeapFree(GetProcessHeap(), 0, u_name);
2698 return res;
2702 /******************************************************************************
2703 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2705 * PARAMS
2706 * hinst [I] Handle of instance that contains image
2707 * name [I] Name of image
2708 * type [I] Type of image
2709 * desiredx [I] Desired width
2710 * desiredy [I] Desired height
2711 * loadflags [I] Load flags
2713 * RETURNS
2714 * Success: Handle to newly loaded image
2715 * Failure: NULL
2717 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2719 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2720 INT desiredx, INT desiredy, UINT loadflags )
2722 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2723 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2725 if (loadflags & LR_DEFAULTSIZE) {
2726 if (type == IMAGE_ICON) {
2727 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2728 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2729 } else if (type == IMAGE_CURSOR) {
2730 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2731 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2734 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2735 switch (type) {
2736 case IMAGE_BITMAP:
2737 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2739 case IMAGE_ICON:
2740 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2741 if (screen_dc)
2743 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2744 if (palEnts == 0) palEnts = 256;
2745 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2746 palEnts, FALSE, loadflags);
2748 break;
2750 case IMAGE_CURSOR:
2751 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2752 1, TRUE, loadflags);
2754 return 0;
2757 /******************************************************************************
2758 * CopyImage (USER32.@) Creates new image and copies attributes to it
2760 * PARAMS
2761 * hnd [I] Handle to image to copy
2762 * type [I] Type of image to copy
2763 * desiredx [I] Desired width of new image
2764 * desiredy [I] Desired height of new image
2765 * flags [I] Copy flags
2767 * RETURNS
2768 * Success: Handle to newly created image
2769 * Failure: NULL
2771 * BUGS
2772 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2773 * all other versions (95/2000/XP have been tested) ignore it.
2775 * NOTES
2776 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2777 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2778 * the copy will have the same depth as the screen.
2779 * The content of the image will only be copied if the bit depth of the
2780 * original image is compatible with the bit depth of the screen, or
2781 * if the source is a DIB section.
2782 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2784 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2785 INT desiredy, UINT flags )
2787 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2788 hnd, type, desiredx, desiredy, flags);
2790 switch (type)
2792 case IMAGE_BITMAP:
2794 HBITMAP res = NULL;
2795 DIBSECTION ds;
2796 int objSize;
2797 BITMAPINFO * bi;
2799 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2800 if (!objSize) return 0;
2801 if ((desiredx < 0) || (desiredy < 0)) return 0;
2803 if (flags & LR_COPYFROMRESOURCE)
2805 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2808 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2809 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2811 /* Allocate memory for a BITMAPINFOHEADER structure and a
2812 color table. The maximum number of colors in a color table
2813 is 256 which corresponds to a bitmap with depth 8.
2814 Bitmaps with higher depths don't have color tables. */
2815 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2816 if (!bi) return 0;
2818 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2819 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2820 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2821 bi->bmiHeader.biCompression = BI_RGB;
2823 if (flags & LR_CREATEDIBSECTION)
2825 /* Create a DIB section. LR_MONOCHROME is ignored */
2826 void * bits;
2827 HDC dc = CreateCompatibleDC(NULL);
2829 if (objSize == sizeof(DIBSECTION))
2831 /* The source bitmap is a DIB.
2832 Get its attributes to create an exact copy */
2833 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2836 /* Get the color table or the color masks */
2837 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2839 bi->bmiHeader.biWidth = desiredx;
2840 bi->bmiHeader.biHeight = desiredy;
2841 bi->bmiHeader.biSizeImage = 0;
2843 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2844 DeleteDC(dc);
2846 else
2848 /* Create a device-dependent bitmap */
2850 BOOL monochrome = (flags & LR_MONOCHROME);
2852 if (objSize == sizeof(DIBSECTION))
2854 /* The source bitmap is a DIB section.
2855 Get its attributes */
2856 HDC dc = CreateCompatibleDC(NULL);
2857 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2858 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2859 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2860 DeleteDC(dc);
2862 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2864 /* Look if the colors of the DIB are black and white */
2866 monochrome =
2867 (bi->bmiColors[0].rgbRed == 0xff
2868 && bi->bmiColors[0].rgbGreen == 0xff
2869 && bi->bmiColors[0].rgbBlue == 0xff
2870 && bi->bmiColors[0].rgbReserved == 0
2871 && bi->bmiColors[1].rgbRed == 0
2872 && bi->bmiColors[1].rgbGreen == 0
2873 && bi->bmiColors[1].rgbBlue == 0
2874 && bi->bmiColors[1].rgbReserved == 0)
2876 (bi->bmiColors[0].rgbRed == 0
2877 && bi->bmiColors[0].rgbGreen == 0
2878 && bi->bmiColors[0].rgbBlue == 0
2879 && bi->bmiColors[0].rgbReserved == 0
2880 && bi->bmiColors[1].rgbRed == 0xff
2881 && bi->bmiColors[1].rgbGreen == 0xff
2882 && bi->bmiColors[1].rgbBlue == 0xff
2883 && bi->bmiColors[1].rgbReserved == 0);
2886 else if (!monochrome)
2888 monochrome = ds.dsBm.bmBitsPixel == 1;
2891 if (monochrome)
2893 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2895 else
2897 HDC screenDC = GetDC(NULL);
2898 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2899 ReleaseDC(NULL, screenDC);
2903 if (res)
2905 /* Only copy the bitmap if it's a DIB section or if it's
2906 compatible to the screen */
2907 BOOL copyContents;
2909 if (objSize == sizeof(DIBSECTION))
2911 copyContents = TRUE;
2913 else
2915 HDC screenDC = GetDC(NULL);
2916 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2917 ReleaseDC(NULL, screenDC);
2919 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2922 if (copyContents)
2924 /* The source bitmap may already be selected in a device context,
2925 use GetDIBits/StretchDIBits and not StretchBlt */
2927 HDC dc;
2928 void * bits;
2930 dc = CreateCompatibleDC(NULL);
2932 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2933 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2934 bi->bmiHeader.biSizeImage = 0;
2935 bi->bmiHeader.biClrUsed = 0;
2936 bi->bmiHeader.biClrImportant = 0;
2938 /* Fill in biSizeImage */
2939 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2940 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2942 if (bits)
2944 HBITMAP oldBmp;
2946 /* Get the image bits of the source bitmap */
2947 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2949 /* Copy it to the destination bitmap */
2950 oldBmp = SelectObject(dc, res);
2951 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2952 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2953 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2954 SelectObject(dc, oldBmp);
2956 HeapFree(GetProcessHeap(), 0, bits);
2959 DeleteDC(dc);
2962 if (flags & LR_COPYDELETEORG)
2964 DeleteObject(hnd);
2967 HeapFree(GetProcessHeap(), 0, bi);
2968 return res;
2970 case IMAGE_ICON:
2971 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2972 case IMAGE_CURSOR:
2973 /* Should call CURSORICON_ExtCopy but more testing
2974 * needs to be done before we change this
2976 if (flags) FIXME("Flags are ignored\n");
2977 return CopyCursor(hnd);
2979 return 0;
2983 /******************************************************************************
2984 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2986 * RETURNS
2987 * Success: Handle to specified bitmap
2988 * Failure: NULL
2990 HBITMAP WINAPI LoadBitmapW(
2991 HINSTANCE instance, /* [in] Handle to application instance */
2992 LPCWSTR name) /* [in] Address of bitmap resource name */
2994 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2997 /**********************************************************************
2998 * LoadBitmapA (USER32.@)
3000 * See LoadBitmapW.
3002 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
3004 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );