2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
7 * 1998 Turchanov Sergey
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
28 * Cursors and icons are stored in a global heap block, with the
31 * CURSORICONINFO info;
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 :-(
44 #include "wine/port.h"
54 #include "wine/winbase16.h"
55 #include "wine/winuser16.h"
56 #include "wine/exception.h"
57 #include "wine/server.h"
59 #include "user_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
63 WINE_DECLARE_DEBUG_CHANNEL(icon);
64 WINE_DECLARE_DEBUG_CHANNEL(resource);
77 } CURSORICONFILEDIRENTRY;
84 CURSORICONFILEDIRENTRY idEntries[1];
89 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
93 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
96 /**********************************************************************
97 * ICONCACHE for cursors/icons loaded with LR_SHARED.
99 * FIXME: This should not be allocated on the system heap, but on a
100 * subsystem-global heap (i.e. one for all Win16 processes,
101 * and one for each Win32 process).
103 typedef struct tagICONCACHE
105 struct tagICONCACHE *next;
116 static ICONCACHE *IconAnchor = NULL;
118 static CRITICAL_SECTION IconCrst;
119 static CRITICAL_SECTION_DEBUG critsect_debug =
122 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
123 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
125 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
127 static const WORD ICON_HOTSPOT = 0x4242;
130 /**********************************************************************
131 * User objects management
134 struct cursoricon_object
136 struct user_object obj; /* object header */
137 ULONG_PTR param; /* opaque param used by 16-bit code */
138 HBITMAP color; /* color bitmap */
139 HBITMAP alpha; /* pre-multiplied alpha bitmap for 32-bpp icons */
140 HBITMAP mask; /* mask bitmap (followed by color for 1-bpp icons) */
142 /* followed by cursor bits in CURSORICONINFO format */
145 static HICON alloc_icon_handle( unsigned int size )
147 struct cursoricon_object *obj = HeapAlloc( GetProcessHeap(), 0, sizeof(*obj) + size );
153 return alloc_user_handle( &obj->obj, USER_ICON );
156 static struct cursoricon_object *get_icon_ptr( HICON handle )
158 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
159 if (obj == OBJ_OTHER_PROCESS)
161 WARN( "icon handle %p from other process\n", handle );
167 static void release_icon_ptr( HICON handle, struct cursoricon_object *ptr )
169 release_user_handle_ptr( ptr );
172 static BOOL free_icon_handle( HICON handle )
174 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
176 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
179 ULONG_PTR param = obj->param;
180 if (obj->color) DeleteObject( obj->color );
181 if (obj->alpha) DeleteObject( obj->alpha );
182 DeleteObject( obj->mask );
183 HeapFree( GetProcessHeap(), 0, obj );
184 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
185 USER_Driver->pDestroyCursorIcon( handle );
191 ULONG_PTR get_icon_param( HICON handle )
194 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
196 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
200 release_user_handle_ptr( obj );
205 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
208 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
210 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
215 release_user_handle_ptr( obj );
221 /***********************************************************************
224 * Helper function to map a file to memory:
226 * [RETURN] ptr - pointer to mapped file
227 * [RETURN] filesize - pointer size of file to be stored if not NULL
229 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
231 HANDLE hFile, hMapping;
234 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
235 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
236 if (hFile != INVALID_HANDLE_VALUE)
238 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
241 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
242 CloseHandle( hMapping );
244 *filesize = GetFileSize( hFile, NULL );
246 CloseHandle( hFile );
252 /***********************************************************************
253 * get_bitmap_width_bytes
255 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
258 static int get_bitmap_width_bytes( int width, int bpp )
263 return 2 * ((width+15) / 16);
265 return 2 * ((width+3) / 4);
270 return width + (width & 1);
277 WARN("Unknown depth %d, please report.\n", bpp );
283 /***********************************************************************
284 * get_dib_width_bytes
286 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
288 static int get_dib_width_bytes( int width, int depth )
294 case 1: words = (width + 31) / 32; break;
295 case 4: words = (width + 7) / 8; break;
296 case 8: words = (width + 3) / 4; break;
298 case 16: words = (width + 1) / 2; break;
299 case 24: words = (width * 3 + 3)/4; break;
301 WARN("(%d): Unsupported depth\n", depth );
310 /***********************************************************************
313 * Return the size of the bitmap info structure including color table.
315 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
317 unsigned int colors, size, masks = 0;
319 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
321 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
322 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
323 return sizeof(BITMAPCOREHEADER) + colors *
324 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
326 else /* assume BITMAPINFOHEADER */
328 colors = info->bmiHeader.biClrUsed;
329 if (colors > 256) /* buffer overflow otherwise */
331 if (!colors && (info->bmiHeader.biBitCount <= 8))
332 colors = 1 << info->bmiHeader.biBitCount;
333 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
334 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
335 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
340 /***********************************************************************
343 * Helper function to duplicate a bitmap.
345 static HBITMAP copy_bitmap( HBITMAP bitmap )
351 if (!bitmap) return 0;
352 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
354 src = CreateCompatibleDC( 0 );
355 dst = CreateCompatibleDC( 0 );
356 SelectObject( src, bitmap );
357 new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight );
358 SelectObject( dst, new_bitmap );
359 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
366 /***********************************************************************
369 * Returns whether a DIB can be converted to a monochrome DDB.
371 * A DIB can be converted if its color table contains only black and
372 * white. Black must be the first color in the color table.
374 * Note : If the first color in the color table is white followed by
375 * black, we can't convert it to a monochrome DDB with
376 * SetDIBits, because black and white would be inverted.
378 static BOOL is_dib_monochrome( const BITMAPINFO* info )
380 if (info->bmiHeader.biBitCount != 1) return FALSE;
382 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
384 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
386 /* Check if the first color is black */
387 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
391 /* Check if the second color is white */
392 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
393 && (rgb->rgbtBlue == 0xff));
397 else /* assume BITMAPINFOHEADER */
399 const RGBQUAD *rgb = info->bmiColors;
401 /* Check if the first color is black */
402 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
403 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
407 /* Check if the second color is white */
408 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
409 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
415 /***********************************************************************
418 * Get the info from a bitmap header.
419 * Return 1 for INFOHEADER, 0 for COREHEADER,
421 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
422 LONG *height, WORD *bpp, DWORD *compr )
424 if (header->biSize == sizeof(BITMAPCOREHEADER))
426 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
427 *width = core->bcWidth;
428 *height = core->bcHeight;
429 *bpp = core->bcBitCount;
433 else if (header->biSize >= sizeof(BITMAPINFOHEADER))
435 *width = header->biWidth;
436 *height = header->biHeight;
437 *bpp = header->biBitCount;
438 *compr = header->biCompression;
441 ERR("(%d): unknown/wrong size for header\n", header->biSize );
445 /**********************************************************************
446 * CURSORICON_FindSharedIcon
448 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
453 EnterCriticalSection( &IconCrst );
455 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
456 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
463 LeaveCriticalSection( &IconCrst );
468 /*************************************************************************
469 * CURSORICON_FindCache
471 * Given a handle, find the corresponding cache element
474 * Handle [I] handle to an Image
477 * Success: The cache entry
481 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
484 ICONCACHE *pRet=NULL;
485 BOOL IsFound = FALSE;
487 EnterCriticalSection( &IconCrst );
489 for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
491 if ( hIcon == ptr->hIcon )
498 LeaveCriticalSection( &IconCrst );
503 /**********************************************************************
504 * CURSORICON_AddSharedIcon
506 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
508 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
511 ptr->hModule = hModule;
514 ptr->hGroupRsrc = hGroupRsrc;
517 EnterCriticalSection( &IconCrst );
518 ptr->next = IconAnchor;
520 LeaveCriticalSection( &IconCrst );
523 /**********************************************************************
524 * CURSORICON_DelSharedIcon
526 static INT CURSORICON_DelSharedIcon( HICON hIcon )
531 EnterCriticalSection( &IconCrst );
533 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
534 if ( ptr->hIcon == hIcon )
536 if ( ptr->count > 0 ) ptr->count--;
541 LeaveCriticalSection( &IconCrst );
546 /**********************************************************************
549 BOOL get_icon_size( HICON handle, SIZE *size )
551 struct cursoricon_object *info;
553 if (!(info = get_icon_ptr( handle ))) return FALSE;
554 size->cx = info->data.nWidth;
555 size->cy = info->data.nHeight;
556 release_icon_ptr( handle, info );
561 * The following macro functions account for the irregularities of
562 * accessing cursor and icon resources in files and resource entries.
564 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
565 int *width, int *height, int *bits );
567 /**********************************************************************
568 * CURSORICON_FindBestIcon
570 * Find the icon closest to the requested size and bit depth.
572 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
573 int width, int height, int depth )
575 int i, cx, cy, bits, bestEntry = -1;
576 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
577 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
580 iTotalDiff = 0xFFFFFFFF;
581 iColorDiff = 0xFFFFFFFF;
582 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
584 iTempXDiff = abs(width - cx);
585 iTempYDiff = abs(height - cy);
587 if(iTotalDiff > (iTempXDiff + iTempYDiff))
591 iTotalDiff = iXDiff + iYDiff;
595 /* Find Best Colors for Best Fit */
596 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
598 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
600 iTempColorDiff = abs(depth - bits);
601 if(iColorDiff > iTempColorDiff)
604 iColorDiff = iTempColorDiff;
612 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
613 int *width, int *height, int *bits )
615 CURSORICONDIR *resdir = dir;
618 if ( resdir->idCount <= n )
620 icon = &resdir->idEntries[n].ResInfo.icon;
621 *width = icon->bWidth;
622 *height = icon->bHeight;
623 *bits = resdir->idEntries[n].wBitCount;
627 /**********************************************************************
628 * CURSORICON_FindBestCursor
630 * Find the cursor closest to the requested size.
632 * FIXME: parameter 'color' ignored.
634 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
635 int width, int height, int depth )
637 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
639 /* Double height to account for AND and XOR masks */
643 /* First find the largest one smaller than or equal to the requested size*/
645 maxwidth = maxheight = 0;
646 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
648 if ((cx <= width) && (cy <= height) &&
649 (cx > maxwidth) && (cy > maxheight))
656 if (bestEntry != -1) return bestEntry;
658 /* Now find the smallest one larger than the requested size */
660 maxwidth = maxheight = 255;
661 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
663 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
674 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
675 int *width, int *height, int *bits )
677 CURSORICONDIR *resdir = dir;
680 if ( resdir->idCount <= n )
682 cursor = &resdir->idEntries[n].ResInfo.cursor;
683 *width = cursor->wWidth;
684 *height = cursor->wHeight;
685 *bits = resdir->idEntries[n].wBitCount;
689 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
690 int width, int height, int depth )
694 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
695 width, height, depth );
698 return &dir->idEntries[n];
701 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
702 int width, int height, int depth )
704 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
705 width, height, depth );
708 return &dir->idEntries[n];
711 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
712 int *width, int *height, int *bits )
714 CURSORICONFILEDIR *filedir = dir;
715 CURSORICONFILEDIRENTRY *entry;
716 BITMAPINFOHEADER *info;
718 if ( filedir->idCount <= n )
720 entry = &filedir->idEntries[n];
721 /* FIXME: check against file size */
722 info = (BITMAPINFOHEADER *)((char *)dir + entry->dwDIBOffset);
723 *width = entry->bWidth;
724 *height = entry->bHeight;
725 *bits = info->biBitCount;
729 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
730 int width, int height, int depth )
732 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
733 width, height, depth );
736 return &dir->idEntries[n];
739 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
740 int width, int height, int depth )
742 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
743 width, height, depth );
746 return &dir->idEntries[n];
749 /***********************************************************************
752 * A helper function that stretches a bitmap buffer into an HBITMAP.
755 * hDest [I] The handle of the destination bitmap.
756 * pDestInfo [I] The BITMAPINFO of the destination bitmap.
757 * pSrcInfo [I] The BITMAPINFO of the source bitmap.
758 * pSrcBits [I] A pointer to the source bitmap buffer.
760 static BOOL stretch_blt_icon(HBITMAP hDest, BITMAPINFO *pDestInfo, BITMAPINFO *pSrcInfo, char *pSrcBits)
764 HDC hdcMem = CreateCompatibleDC(screen_dc);
768 hOld = SelectObject(hdcMem, hDest);
769 res = StretchDIBits(hdcMem,
770 0, 0, pDestInfo->bmiHeader.biWidth, pDestInfo->bmiHeader.biHeight,
771 0, 0, pSrcInfo->bmiHeader.biWidth, pSrcInfo->bmiHeader.biHeight,
772 pSrcBits, pSrcInfo, DIB_RGB_COLORS, SRCCOPY);
773 SelectObject(hdcMem, hOld);
780 /***********************************************************************
783 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
786 BOOL has_alpha = FALSE;
787 const unsigned char *ptr = bits;
789 if (info->bmiHeader.biBitCount != 32) return FALSE;
790 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
791 if ((has_alpha = (ptr[3] != 0))) break;
795 /***********************************************************************
796 * create_alpha_bitmap
798 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
800 static HBITMAP create_alpha_bitmap( HBITMAP color, HBITMAP mask,
801 const BITMAPINFO *src_info, const void *color_bits )
804 BITMAPINFO *info = NULL;
811 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
812 if (bm.bmBitsPixel != 32) return 0;
814 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
815 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
816 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
817 info->bmiHeader.biWidth = bm.bmWidth;
818 info->bmiHeader.biHeight = -bm.bmHeight;
819 info->bmiHeader.biPlanes = 1;
820 info->bmiHeader.biBitCount = 32;
821 info->bmiHeader.biCompression = BI_RGB;
822 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
823 info->bmiHeader.biXPelsPerMeter = 0;
824 info->bmiHeader.biYPelsPerMeter = 0;
825 info->bmiHeader.biClrUsed = 0;
826 info->bmiHeader.biClrImportant = 0;
827 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
831 SelectObject( hdc, alpha );
832 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
833 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
834 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
839 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
840 if (!bmi_has_alpha( info, bits ))
842 DeleteObject( alpha );
848 /* pre-multiply by alpha */
849 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
851 unsigned int alpha = ptr[3];
852 ptr[0] = ptr[0] * alpha / 255;
853 ptr[1] = ptr[1] * alpha / 255;
854 ptr[2] = ptr[2] * alpha / 255;
859 HeapFree( GetProcessHeap(), 0, info );
864 /***********************************************************************
865 * create_icon_bitmaps
867 * Create the color, mask and alpha bitmaps from the DIB info.
869 static BOOL create_icon_bitmaps( const BITMAPINFO *bmi, int width, int height,
870 HBITMAP *color, HBITMAP *mask, HBITMAP *alpha )
872 BOOL monochrome = is_dib_monochrome( bmi );
873 unsigned int size = bitmap_info_size( bmi, DIB_RGB_COLORS );
875 void *color_bits, *mask_bits;
879 if (!(info = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
881 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
883 memcpy( info, bmi, size );
884 info->bmiHeader.biHeight /= 2;
886 color_bits = (char *)bmi + size;
887 mask_bits = (char *)color_bits +
888 get_dib_width_bytes( bmi->bmiHeader.biWidth,
889 bmi->bmiHeader.biBitCount ) * abs(info->bmiHeader.biHeight);
894 if (!(*mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
897 /* copy color data into second half of mask bitmap */
898 SelectObject( hdc, *mask );
899 StretchDIBits( hdc, 0, height, width, height,
900 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
901 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
905 if (!(*mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
906 if (!(*color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
907 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
909 DeleteObject( *mask );
912 SelectObject( hdc, *color );
913 StretchDIBits( hdc, 0, 0, width, height,
914 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
915 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
917 if (bmi_has_alpha( info, color_bits ))
918 *alpha = create_alpha_bitmap( *color, *mask, info, color_bits );
920 /* convert info to monochrome to copy the mask */
921 info->bmiHeader.biBitCount = 1;
922 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
924 RGBQUAD *rgb = info->bmiColors;
926 info->bmiHeader.biClrUsed = info->bmiHeader.biClrImportant = 2;
927 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
928 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
929 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
933 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)info) + 1);
935 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
936 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
940 SelectObject( hdc, *mask );
941 StretchDIBits( hdc, 0, 0, width, height,
942 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
943 mask_bits, info, DIB_RGB_COLORS, SRCCOPY );
948 HeapFree( GetProcessHeap(), 0, info );
952 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi,
953 POINT16 hotspot, BOOL bIcon,
955 INT width, INT height,
959 int sizeAnd, sizeXor;
960 HBITMAP color = 0, mask = 0, alpha = 0, hAndBits = 0, hXorBits = 0; /* error condition for later */
961 BITMAP bmpXor, bmpAnd;
964 BITMAPINFO *pSrcInfo, *pDestInfo;
966 if (dwVersion == 0x00020000)
968 FIXME_(cursor)("\t2.xx resources are not supported\n");
972 /* Check bitmap header */
974 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
975 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
976 bmi->bmiHeader.biCompression != BI_RGB) )
978 WARN_(cursor)("\tinvalid resource bitmap header.\n");
982 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
984 if (!width) width = bmi->bmiHeader.biWidth;
985 if (!height) height = bmi->bmiHeader.biHeight/2;
986 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
987 (bmi->bmiHeader.biWidth != width);
989 /* Scale the hotspot */
990 if (do_stretch && hotspot.x != ICON_HOTSPOT && hotspot.y != ICON_HOTSPOT)
992 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
993 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
996 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
997 if (!screen_dc) return 0;
999 if (create_icon_bitmaps( bmi, width, height, &color, &mask, &alpha ))
1001 /* Make sure we have room for the monochrome bitmap later on.
1002 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
1003 * up to and including the biBitCount. In-memory icon resource
1004 * format is as follows:
1006 * BITMAPINFOHEADER icHeader // DIB header
1007 * RGBQUAD icColors[] // Color table
1008 * BYTE icXOR[] // DIB bits for XOR mask
1009 * BYTE icAND[] // DIB bits for AND mask
1012 pSrcInfo = HeapAlloc( GetProcessHeap(), 0,
1013 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
1014 pDestInfo = HeapAlloc( GetProcessHeap(), 0,
1015 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)));
1016 if (pSrcInfo && pDestInfo)
1018 memcpy( pSrcInfo, bmi, size );
1019 pSrcInfo->bmiHeader.biHeight /= 2;
1021 memcpy( pDestInfo, bmi, size );
1022 pDestInfo->bmiHeader.biWidth = width;
1023 pDestInfo->bmiHeader.biHeight = height;
1024 pDestInfo->bmiHeader.biSizeImage = 0;
1026 /* Create the XOR bitmap */
1027 if(pSrcInfo->bmiHeader.biBitCount == 32)
1029 void *pDIBBuffer = NULL;
1030 hXorBits = CreateDIBSection(screen_dc, pDestInfo, DIB_RGB_COLORS, &pDIBBuffer, NULL, 0);
1034 if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
1036 DeleteObject(hXorBits);
1045 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
1048 if (!stretch_blt_icon(hXorBits, pDestInfo, pSrcInfo, (char*)bmi + size))
1050 DeleteObject(hXorBits);
1057 if (is_dib_monochrome(bmi))
1059 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
1060 SetDIBits(screen_dc, hXorBits, 0, height,
1061 (char *)bmi + size, pSrcInfo, DIB_RGB_COLORS);
1064 hXorBits = CreateDIBitmap(screen_dc, &pSrcInfo->bmiHeader,
1065 CBM_INIT, (char *)bmi + size, pSrcInfo, DIB_RGB_COLORS);
1071 char* xbits = (char *)bmi + size +
1072 get_dib_width_bytes( bmi->bmiHeader.biWidth,
1073 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
1075 pSrcInfo->bmiHeader.biBitCount = 1;
1076 if (pSrcInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
1078 RGBQUAD *rgb = pSrcInfo->bmiColors;
1080 pSrcInfo->bmiHeader.biClrUsed = pSrcInfo->bmiHeader.biClrImportant = 2;
1081 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
1082 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
1083 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
1087 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pSrcInfo) + 1);
1089 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
1090 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
1093 /* Create the AND bitmap */
1096 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
1098 if (!stretch_blt_icon(hAndBits, pDestInfo, pSrcInfo, xbits))
1100 DeleteObject(hAndBits);
1106 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
1107 SetDIBits(screen_dc, hAndBits, 0, height,
1108 xbits, pSrcInfo, DIB_RGB_COLORS);
1113 DeleteObject( hXorBits );
1119 HeapFree( GetProcessHeap(), 0, pSrcInfo );
1120 HeapFree( GetProcessHeap(), 0, pDestInfo );
1123 if( !hXorBits || !hAndBits )
1125 WARN_(cursor)("\tunable to create an icon bitmap.\n");
1126 DeleteObject( color );
1127 DeleteObject( alpha );
1128 DeleteObject( mask );
1132 /* Now create the CURSORICONINFO structure */
1133 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
1134 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
1135 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1136 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1138 hObj = alloc_icon_handle( sizeXor + sizeAnd );
1141 struct cursoricon_object *info = get_icon_ptr( hObj );
1143 info->color = color;
1145 info->alpha = alpha;
1146 info->data.ptHotSpot.x = hotspot.x;
1147 info->data.ptHotSpot.y = hotspot.y;
1148 info->data.nWidth = bmpXor.bmWidth;
1149 info->data.nHeight = bmpXor.bmHeight;
1150 info->data.nWidthBytes = bmpXor.bmWidthBytes;
1151 info->data.bPlanes = bmpXor.bmPlanes;
1152 info->data.bBitsPerPixel = bmpXor.bmBitsPixel;
1154 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1156 GetBitmapBits( hAndBits, sizeAnd, info + 1 );
1157 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
1158 release_icon_ptr( hObj, info );
1159 USER_Driver->pCreateCursorIcon( hObj, &info->data );
1163 DeleteObject( color );
1164 DeleteObject( alpha );
1165 DeleteObject( mask );
1168 DeleteObject( hAndBits );
1169 DeleteObject( hXorBits );
1174 /**********************************************************************
1175 * .ANI cursor support
1177 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
1178 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
1179 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
1181 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
1182 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
1183 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
1184 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
1185 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
1186 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
1188 #define ANI_FLAG_ICON 0x1
1189 #define ANI_FLAG_SEQUENCE 0x2
1205 const unsigned char *data;
1208 static void dump_ani_header( const ani_header *header )
1210 TRACE(" header size: %d\n", header->header_size);
1211 TRACE(" frames: %d\n", header->num_frames);
1212 TRACE(" steps: %d\n", header->num_steps);
1213 TRACE(" width: %d\n", header->width);
1214 TRACE(" height: %d\n", header->height);
1215 TRACE(" bpp: %d\n", header->bpp);
1216 TRACE(" planes: %d\n", header->num_planes);
1217 TRACE(" display rate: %d\n", header->display_rate);
1218 TRACE(" flags: 0x%08x\n", header->flags);
1240 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1242 const unsigned char *ptr = parent_chunk->data;
1243 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1245 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1249 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1250 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1252 ptr += sizeof(DWORD);
1253 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1254 ptr += sizeof(DWORD);
1255 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1261 ptr += sizeof(DWORD);
1262 ptr += (*(const DWORD *)ptr + 1) & ~1;
1263 ptr += sizeof(DWORD);
1271 * RIFF:'ACON' RIFF chunk
1272 * |- CHUNK:'anih' Header
1273 * |- CHUNK:'seq ' Sequence information (optional)
1274 * \- LIST:'fram' Frame list
1275 * |- CHUNK:icon Cursor frames
1280 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
1281 INT width, INT height, INT depth )
1284 ani_header header = {0};
1285 LPBYTE frame_bits = 0;
1287 CURSORICONFILEDIRENTRY *entry;
1289 riff_chunk_t root_chunk = { bits_size, bits };
1290 riff_chunk_t ACON_chunk = {0};
1291 riff_chunk_t anih_chunk = {0};
1292 riff_chunk_t fram_chunk = {0};
1293 const unsigned char *icon_data;
1295 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1297 if (!bits) return 0;
1299 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1300 if (!ACON_chunk.data)
1302 ERR("Failed to get root chunk.\n");
1306 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1307 if (!anih_chunk.data)
1309 ERR("Failed to get 'anih' chunk.\n");
1312 memcpy( &header, anih_chunk.data, sizeof(header) );
1313 dump_ani_header( &header );
1315 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1316 if (!fram_chunk.data)
1318 ERR("Failed to get icon list.\n");
1322 /* FIXME: For now, just load the first frame. Before we can load all the
1323 * frames, we need to write the needed code in wineserver, etc. to handle
1324 * cursors. Once this code is written, we can extend it to support .ani
1325 * cursors and then update user32 and winex11.drv to load all frames.
1327 * Hopefully this will at least make some games (C&C3, etc.) more playable
1330 FIXME("Loading all frames for .ani cursors not implemented.\n");
1331 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1333 entry = CURSORICON_FindBestIconFile( (CURSORICONFILEDIR *) icon_data,
1334 width, height, depth );
1336 frame_bits = HeapAlloc( GetProcessHeap(), 0, entry->dwDIBSize );
1337 memcpy( frame_bits, icon_data + entry->dwDIBOffset, entry->dwDIBSize );
1339 if (!header.width || !header.height)
1341 header.width = entry->bWidth;
1342 header.height = entry->bHeight;
1345 hotspot.x = entry->xHotspot;
1346 hotspot.y = entry->yHotspot;
1348 cursor = CURSORICON_CreateIconFromBMI( (BITMAPINFO *) frame_bits, hotspot,
1349 FALSE, 0x00030000, header.width, header.height, 0 );
1351 HeapFree( GetProcessHeap(), 0, frame_bits );
1357 /**********************************************************************
1358 * CreateIconFromResourceEx (USER32.@)
1360 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1361 * with cbSize parameter as well.
1363 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1364 BOOL bIcon, DWORD dwVersion,
1365 INT width, INT height,
1371 hotspot.x = ICON_HOTSPOT;
1372 hotspot.y = ICON_HOTSPOT;
1374 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1375 bits, cbSize, dwVersion, width, height,
1376 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1379 bmi = (BITMAPINFO *)bits;
1380 else /* get the hotspot */
1382 POINT16 *pt = (POINT16 *)bits;
1384 bmi = (BITMAPINFO *)(pt + 1);
1387 return CURSORICON_CreateIconFromBMI( bmi, hotspot, bIcon, dwVersion,
1388 width, height, cFlag );
1392 /**********************************************************************
1393 * CreateIconFromResource (USER32.@)
1395 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1396 BOOL bIcon, DWORD dwVersion)
1398 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1402 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1403 INT width, INT height, INT depth,
1404 BOOL fCursor, UINT loadflags)
1406 CURSORICONFILEDIRENTRY *entry;
1407 CURSORICONFILEDIR *dir;
1413 TRACE("loading %s\n", debugstr_w( filename ));
1415 bits = map_fileW( filename, &filesize );
1419 /* Check for .ani. */
1420 if (memcmp( bits, "RIFF", 4 ) == 0)
1422 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height,
1427 dir = (CURSORICONFILEDIR*) bits;
1428 if ( filesize < sizeof(*dir) )
1431 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1435 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth );
1437 entry = CURSORICON_FindBestIconFile( dir, width, height, depth );
1442 /* check that we don't run off the end of the file */
1443 if ( entry->dwDIBOffset > filesize )
1445 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1448 /* Set the actual hotspot for cursors and ICON_HOTSPOT for icons. */
1451 hotspot.x = entry->xHotspot;
1452 hotspot.y = entry->yHotspot;
1456 hotspot.x = ICON_HOTSPOT;
1457 hotspot.y = ICON_HOTSPOT;
1459 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset],
1460 hotspot, !fCursor, 0x00030000,
1461 width, height, loadflags );
1463 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1464 UnmapViewOfFile( bits );
1468 /**********************************************************************
1471 * Load a cursor or icon from resource or file.
1473 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1474 INT width, INT height, INT depth,
1475 BOOL fCursor, UINT loadflags)
1479 HRSRC hRsrc, hGroupRsrc;
1481 CURSORICONDIRENTRY *dirEntry;
1486 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1487 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1489 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1490 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1492 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1494 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1495 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1497 /* Get directory resource ID */
1499 if (!(hRsrc = FindResourceW( hInstance, name,
1500 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1504 /* Find the best entry in the directory */
1506 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1507 if (!(dir = LockResource( handle ))) return 0;
1509 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1511 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1512 if (!dirEntry) return 0;
1513 wResId = dirEntry->wResId;
1514 dwBytesInRes = dirEntry->dwBytesInRes;
1515 FreeResource( handle );
1517 /* Load the resource */
1519 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1520 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1522 /* If shared icon, check whether it was already loaded */
1523 if ( (loadflags & LR_SHARED)
1524 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
1527 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1528 bits = LockResource( handle );
1529 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1530 !fCursor, 0x00030000, width, height, loadflags);
1531 FreeResource( handle );
1533 /* If shared icon, add to icon cache */
1535 if ( hIcon && (loadflags & LR_SHARED) )
1536 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1542 /*************************************************************************
1543 * CURSORICON_ExtCopy
1545 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1548 * Handle [I] handle to an Image
1549 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1550 * iDesiredCX [I] The Desired width of the Image
1551 * iDesiredCY [I] The desired height of the Image
1552 * nFlags [I] The flags from CopyImage
1555 * Success: The new handle of the Image
1558 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1559 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1560 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1565 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1566 INT iDesiredCX, INT iDesiredCY,
1571 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1572 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1579 /* Best Fit or Monochrome */
1580 if( (nFlags & LR_COPYFROMRESOURCE
1581 && (iDesiredCX > 0 || iDesiredCY > 0))
1582 || nFlags & LR_MONOCHROME)
1584 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1586 /* Not Found in Cache, then do a straight copy
1588 if(pIconCache == NULL)
1590 hNew = CopyIcon( hIcon );
1591 if(nFlags & LR_COPYFROMRESOURCE)
1593 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1598 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1604 CURSORICONDIR *pDir;
1605 CURSORICONDIRENTRY *pDirEntry;
1606 BOOL bIsIcon = (nType == IMAGE_ICON);
1608 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1610 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1611 || (iDesiredCX == 0 && iDesiredCY == 0))
1613 iDesiredCY = GetSystemMetrics(bIsIcon ?
1614 SM_CYICON : SM_CYCURSOR);
1615 iDesiredCX = GetSystemMetrics(bIsIcon ?
1616 SM_CXICON : SM_CXCURSOR);
1619 /* Retrieve the CURSORICONDIRENTRY
1621 if (!(hMem = LoadResource( pIconCache->hModule ,
1622 pIconCache->hGroupRsrc)))
1626 if (!(pDir = LockResource( hMem )))
1635 pDirEntry = CURSORICON_FindBestIconRes(
1636 pDir, iDesiredCX, iDesiredCY, 256 );
1640 pDirEntry = CURSORICON_FindBestCursorRes(
1641 pDir, iDesiredCX, iDesiredCY, 1);
1644 wResId = pDirEntry->wResId;
1645 dwBytesInRes = pDirEntry->dwBytesInRes;
1648 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1649 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1650 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1654 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1655 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1659 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1664 pBits = LockResource( hMem );
1666 if(nFlags & LR_DEFAULTSIZE)
1668 iTargetCY = GetSystemMetrics(SM_CYICON);
1669 iTargetCX = GetSystemMetrics(SM_CXICON);
1672 /* Create a New Icon with the proper dimension
1674 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1675 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1679 else hNew = CopyIcon( hIcon );
1684 /***********************************************************************
1685 * CreateCursor (USER32.@)
1687 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1688 INT xHotSpot, INT yHotSpot,
1689 INT nWidth, INT nHeight,
1690 LPCVOID lpANDbits, LPCVOID lpXORbits )
1695 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1696 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1699 info.xHotspot = xHotSpot;
1700 info.yHotspot = yHotSpot;
1701 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1702 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1703 hCursor = CreateIconIndirect( &info );
1704 DeleteObject( info.hbmMask );
1705 DeleteObject( info.hbmColor );
1710 /***********************************************************************
1711 * CreateIcon (USER32.@)
1713 * Creates an icon based on the specified bitmaps. The bitmaps must be
1714 * provided in a device dependent format and will be resized to
1715 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1716 * depth. The provided bitmaps must be top-down bitmaps.
1717 * Although Windows does not support 15bpp(*) this API must support it
1718 * for Winelib applications.
1720 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1724 * Success: handle to an icon
1727 * FIXME: Do we need to resize the bitmaps?
1729 HICON WINAPI CreateIcon(
1730 HINSTANCE hInstance, /* [in] the application's hInstance */
1731 INT nWidth, /* [in] the width of the provided bitmaps */
1732 INT nHeight, /* [in] the height of the provided bitmaps */
1733 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1734 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1735 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1736 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1741 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1742 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1745 iinfo.xHotspot = ICON_HOTSPOT;
1746 iinfo.yHotspot = ICON_HOTSPOT;
1747 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1748 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1750 hIcon = CreateIconIndirect( &iinfo );
1752 DeleteObject( iinfo.hbmMask );
1753 DeleteObject( iinfo.hbmColor );
1759 /***********************************************************************
1760 * CopyIcon (USER32.@)
1762 HICON WINAPI CopyIcon( HICON hIcon )
1764 struct cursoricon_object *ptrOld, *ptrNew;
1768 if (!(ptrOld = get_icon_ptr( hIcon ))) return 0;
1769 size = ptrOld->data.nHeight * get_bitmap_width_bytes( ptrOld->data.nWidth, 1 ); /* and bitmap */
1770 size += ptrOld->data.nHeight * ptrOld->data.nWidthBytes; /* xor bitmap */
1771 hNew = alloc_icon_handle( size );
1772 ptrNew = get_icon_ptr( hNew );
1773 memcpy( &ptrNew->data, &ptrOld->data, sizeof(ptrNew->data) + size );
1774 ptrNew->color = copy_bitmap( ptrOld->color );
1775 ptrNew->alpha = copy_bitmap( ptrOld->alpha );
1776 ptrNew->mask = copy_bitmap( ptrOld->mask );
1777 release_icon_ptr( hIcon, ptrOld );
1778 release_icon_ptr( hNew, ptrNew );
1779 USER_Driver->pCreateCursorIcon( hNew, &ptrNew->data );
1784 /***********************************************************************
1785 * DestroyIcon (USER32.@)
1787 BOOL WINAPI DestroyIcon( HICON hIcon )
1789 TRACE_(icon)("%p\n", hIcon );
1791 if (CURSORICON_DelSharedIcon( hIcon ) == -1)
1792 free_icon_handle( hIcon );
1797 /***********************************************************************
1798 * DestroyCursor (USER32.@)
1800 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1802 if (GetCursor() == hCursor)
1804 WARN_(cursor)("Destroying active cursor!\n" );
1807 return DestroyIcon( hCursor );
1810 /***********************************************************************
1811 * DrawIcon (USER32.@)
1813 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1815 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1818 /***********************************************************************
1819 * SetCursor (USER32.@)
1821 * Set the cursor shape.
1824 * A handle to the previous cursor shape.
1826 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1832 TRACE("%p\n", hCursor);
1834 SERVER_START_REQ( set_cursor )
1836 req->flags = SET_CURSOR_HANDLE;
1837 req->handle = wine_server_user_handle( hCursor );
1838 if ((ret = !wine_server_call_err( req )))
1840 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1841 show_count = reply->prev_count;
1848 /* Change the cursor shape only if it is visible */
1849 if (show_count >= 0 && hOldCursor != hCursor) USER_Driver->pSetCursor( hCursor );
1853 /***********************************************************************
1854 * ShowCursor (USER32.@)
1856 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1859 int increment = bShow ? 1 : -1;
1862 SERVER_START_REQ( set_cursor )
1864 req->flags = SET_CURSOR_COUNT;
1865 req->show_count = increment;
1866 wine_server_call( req );
1867 cursor = wine_server_ptr_handle( reply->prev_handle );
1868 count = reply->prev_count + increment;
1872 TRACE("%d, count=%d\n", bShow, count );
1874 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1875 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1880 /***********************************************************************
1881 * GetCursor (USER32.@)
1883 HCURSOR WINAPI GetCursor(void)
1887 SERVER_START_REQ( set_cursor )
1890 wine_server_call( req );
1891 ret = wine_server_ptr_handle( reply->prev_handle );
1898 /***********************************************************************
1899 * ClipCursor (USER32.@)
1901 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1905 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1906 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1907 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1908 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1910 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1911 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1913 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1914 CURSOR_ClipRect = virt;
1916 USER_Driver->pClipCursor( rect );
1921 /***********************************************************************
1922 * GetClipCursor (USER32.@)
1924 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1926 /* If this is first time - initialize the rect */
1927 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1929 return CopyRect( rect, &CURSOR_ClipRect );
1933 /***********************************************************************
1934 * SetSystemCursor (USER32.@)
1936 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1938 FIXME("(%p,%08x),stub!\n", hcur, id);
1943 /**********************************************************************
1944 * LookupIconIdFromDirectoryEx (USER32.@)
1946 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1947 INT width, INT height, UINT cFlag )
1949 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1951 if( dir && !dir->idReserved && (dir->idType & 3) )
1953 CURSORICONDIRENTRY* entry;
1955 const HDC hdc = GetDC(0);
1956 const int depth = (cFlag & LR_MONOCHROME) ?
1957 1 : GetDeviceCaps(hdc, BITSPIXEL);
1961 entry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1963 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1965 if( entry ) retVal = entry->wResId;
1967 else WARN_(cursor)("invalid resource directory\n");
1971 /**********************************************************************
1972 * LookupIconIdFromDirectory (USER32.@)
1974 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1976 return LookupIconIdFromDirectoryEx( dir, bIcon,
1977 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1978 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1981 /***********************************************************************
1982 * LoadCursorW (USER32.@)
1984 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1986 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1988 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1989 LR_SHARED | LR_DEFAULTSIZE );
1992 /***********************************************************************
1993 * LoadCursorA (USER32.@)
1995 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1997 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1999 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
2000 LR_SHARED | LR_DEFAULTSIZE );
2003 /***********************************************************************
2004 * LoadCursorFromFileW (USER32.@)
2006 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
2008 TRACE("%s\n", debugstr_w(name));
2010 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
2011 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2014 /***********************************************************************
2015 * LoadCursorFromFileA (USER32.@)
2017 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
2019 TRACE("%s\n", debugstr_a(name));
2021 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
2022 LR_LOADFROMFILE | LR_DEFAULTSIZE );
2025 /***********************************************************************
2026 * LoadIconW (USER32.@)
2028 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
2030 TRACE("%p, %s\n", hInstance, debugstr_w(name));
2032 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
2033 LR_SHARED | LR_DEFAULTSIZE );
2036 /***********************************************************************
2037 * LoadIconA (USER32.@)
2039 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
2041 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2043 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
2044 LR_SHARED | LR_DEFAULTSIZE );
2047 /**********************************************************************
2048 * GetIconInfo (USER32.@)
2050 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
2052 struct cursoricon_object *ptr;
2054 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2056 TRACE("%p => %dx%d, %d bpp\n", hIcon,
2057 ptr->data.nWidth, ptr->data.nHeight, ptr->data.bBitsPerPixel);
2059 if ( (ptr->data.ptHotSpot.x == ICON_HOTSPOT) &&
2060 (ptr->data.ptHotSpot.y == ICON_HOTSPOT) )
2062 iconinfo->fIcon = TRUE;
2063 iconinfo->xHotspot = ptr->data.nWidth / 2;
2064 iconinfo->yHotspot = ptr->data.nHeight / 2;
2068 iconinfo->fIcon = FALSE;
2069 iconinfo->xHotspot = ptr->data.ptHotSpot.x;
2070 iconinfo->yHotspot = ptr->data.ptHotSpot.y;
2073 iconinfo->hbmColor = copy_bitmap( ptr->color );
2074 iconinfo->hbmMask = copy_bitmap( ptr->mask );
2075 release_icon_ptr( hIcon, ptr );
2080 /**********************************************************************
2081 * CreateIconIndirect (USER32.@)
2083 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2088 HBITMAP color = 0, mask;
2089 int xor_objsize = 0, sizeXor = 0, sizeAnd, width, height, planes, bpp;
2092 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2093 iconinfo->hbmColor, iconinfo->hbmMask,
2094 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2096 if (!iconinfo->hbmMask) return 0;
2098 planes = GetDeviceCaps( screen_dc, PLANES );
2099 bpp = GetDeviceCaps( screen_dc, BITSPIXEL );
2101 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2102 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2103 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2104 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2106 if (iconinfo->hbmColor)
2108 xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2109 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2110 bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
2111 bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
2112 /* we can use either depth 1 or screen depth for xor bitmap */
2113 if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
2114 sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
2116 width = bmpXor.dsBm.bmWidth;
2117 height = bmpXor.dsBm.bmHeight;
2118 if (bmpXor.dsBm.bmPlanes * bmpXor.dsBm.bmBitsPixel != 1)
2120 color = CreateCompatibleBitmap( screen_dc, width, height );
2121 mask = CreateBitmap( width, height, 1, 1, NULL );
2123 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2127 width = bmpAnd.bmWidth;
2128 height = bmpAnd.bmHeight;
2129 mask = CreateBitmap( width, height, 1, 1, NULL );
2132 sizeAnd = height * get_bitmap_width_bytes(width, 1);
2134 src = CreateCompatibleDC( 0 );
2135 dst = CreateCompatibleDC( 0 );
2137 SelectObject( src, iconinfo->hbmMask );
2138 SelectObject( dst, mask );
2139 StretchBlt( dst, 0, 0, width, height, src, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, SRCCOPY );
2143 SelectObject( src, iconinfo->hbmColor );
2144 SelectObject( dst, color );
2145 BitBlt( dst, 0, 0, width, height, src, 0, 0, SRCCOPY );
2147 else if (iconinfo->hbmColor)
2149 SelectObject( src, iconinfo->hbmColor );
2150 BitBlt( dst, 0, height, width, height, src, 0, 0, SRCCOPY );
2155 hObj = alloc_icon_handle( sizeXor + sizeAnd );
2158 struct cursoricon_object *info = get_icon_ptr( hObj );
2160 info->color = color;
2162 info->alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
2164 /* If we are creating an icon, the hotspot is unused */
2165 if (iconinfo->fIcon)
2167 info->data.ptHotSpot.x = ICON_HOTSPOT;
2168 info->data.ptHotSpot.y = ICON_HOTSPOT;
2172 info->data.ptHotSpot.x = iconinfo->xHotspot;
2173 info->data.ptHotSpot.y = iconinfo->yHotspot;
2176 if (iconinfo->hbmColor)
2178 info->data.nWidth = bmpXor.dsBm.bmWidth;
2179 info->data.nHeight = bmpXor.dsBm.bmHeight;
2180 info->data.nWidthBytes = bmpXor.dsBm.bmWidthBytes;
2181 info->data.bPlanes = planes;
2182 info->data.bBitsPerPixel = bpp;
2186 info->data.nWidth = bmpAnd.bmWidth;
2187 info->data.nHeight = bmpAnd.bmHeight / 2;
2188 info->data.nWidthBytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
2189 info->data.bPlanes = 1;
2190 info->data.bBitsPerPixel = 1;
2193 /* Transfer the bitmap bits to the CURSORICONINFO structure */
2195 GetBitmapBits( mask, sizeAnd, info + 1 );
2199 char *dst_bits = (char*)(info + 1) + sizeAnd;
2201 if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
2202 GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
2206 int dib_width = get_dib_width_bytes( info->data.nWidth, info->data.bBitsPerPixel );
2207 int bitmap_width = get_bitmap_width_bytes( info->data.nWidth, info->data.bBitsPerPixel );
2209 bminfo.bmiHeader.biSize = sizeof(bminfo);
2210 bminfo.bmiHeader.biWidth = info->data.nWidth;
2211 bminfo.bmiHeader.biHeight = info->data.nHeight;
2212 bminfo.bmiHeader.biPlanes = info->data.bPlanes;
2213 bminfo.bmiHeader.biBitCount = info->data.bBitsPerPixel;
2214 bminfo.bmiHeader.biCompression = BI_RGB;
2215 bminfo.bmiHeader.biSizeImage = info->data.nHeight * dib_width;
2216 bminfo.bmiHeader.biXPelsPerMeter = 0;
2217 bminfo.bmiHeader.biYPelsPerMeter = 0;
2218 bminfo.bmiHeader.biClrUsed = 0;
2219 bminfo.bmiHeader.biClrImportant = 0;
2221 /* swap lines for dib sections */
2222 if (xor_objsize == sizeof(DIBSECTION))
2223 bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;
2225 if (dib_width != bitmap_width) /* need to fixup alignment */
2227 char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );
2229 if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->data.nHeight,
2230 src_bits, &bminfo, DIB_RGB_COLORS ))
2233 for (y = 0; y < info->data.nHeight; y++)
2234 memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
2236 HeapFree( GetProcessHeap(), 0, src_bits );
2239 GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->data.nHeight,
2240 dst_bits, &bminfo, DIB_RGB_COLORS );
2243 release_icon_ptr( hObj, info );
2244 USER_Driver->pCreateCursorIcon( hObj, &info->data );
2249 /******************************************************************************
2250 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2253 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2256 * hdc [I] Handle to device context
2257 * x0 [I] X coordinate of upper left corner
2258 * y0 [I] Y coordinate of upper left corner
2259 * hIcon [I] Handle to icon to draw
2260 * cxWidth [I] Width of icon
2261 * cyWidth [I] Height of icon
2262 * istep [I] Index of frame in animated cursor
2263 * hbr [I] Handle to background brush
2264 * flags [I] Icon-drawing flags
2270 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2271 INT cxWidth, INT cyWidth, UINT istep,
2272 HBRUSH hbr, UINT flags )
2274 struct cursoricon_object *ptr;
2275 HDC hdc_dest, hMemDC;
2276 BOOL result = FALSE, DoOffscreen;
2278 COLORREF oldFg, oldBg;
2279 INT x, y, nStretchMode;
2281 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2282 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2284 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2285 if (!(hMemDC = CreateCompatibleDC( hdc )))
2287 release_icon_ptr( hIcon, ptr );
2292 FIXME_(icon)("Ignoring istep=%d\n", istep);
2293 if (flags & DI_NOMIRROR)
2294 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2296 /* Calculate the size of the destination image. */
2299 if (flags & DI_DEFAULTSIZE)
2300 cxWidth = GetSystemMetrics (SM_CXICON);
2302 cxWidth = ptr->data.nWidth;
2306 if (flags & DI_DEFAULTSIZE)
2307 cyWidth = GetSystemMetrics (SM_CYICON);
2309 cyWidth = ptr->data.nHeight;
2312 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2322 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto done;
2323 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2325 DeleteDC( hdc_dest );
2328 SelectObject(hdc_dest, hB_off);
2329 FillRect(hdc_dest, &r, hbr);
2339 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2341 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2342 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2344 if ((flags & DI_MASK) && (!ptr->alpha || !(flags & DI_IMAGE)))
2346 SelectObject( hMemDC, ptr->mask );
2347 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2348 hMemDC, 0, 0, ptr->data.nWidth, ptr->data.nHeight, SRCAND );
2351 if (flags & DI_IMAGE)
2355 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2357 SelectObject( hMemDC, ptr->alpha );
2358 GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2359 0, 0, ptr->data.nWidth, ptr->data.nHeight, pixelblend );
2361 else if (ptr->color)
2363 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2364 SelectObject( hMemDC, ptr->color );
2365 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2366 hMemDC, 0, 0, ptr->data.nWidth, ptr->data.nHeight, rop );
2370 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2371 SelectObject( hMemDC, ptr->mask );
2372 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2373 hMemDC, 0, ptr->data.nHeight, ptr->data.nWidth, ptr->data.nHeight, rop );
2377 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2379 SetTextColor( hdc, oldFg );
2380 SetBkColor( hdc, oldBg );
2381 SetStretchBltMode (hdc, nStretchMode);
2383 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2384 if (hB_off) DeleteObject(hB_off);
2387 release_icon_ptr( hIcon, ptr );
2391 /***********************************************************************
2392 * DIB_FixColorsToLoadflags
2394 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2397 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2400 COLORREF c_W, c_S, c_F, c_L, c_C;
2409 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2411 WARN_(resource)("Invalid bitmap\n");
2415 if (bpp > 8) return;
2417 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2425 colors = bmi->bmiHeader.biClrUsed;
2426 if (colors > 256) colors = 256;
2427 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2430 c_W = GetSysColor(COLOR_WINDOW);
2431 c_S = GetSysColor(COLOR_3DSHADOW);
2432 c_F = GetSysColor(COLOR_3DFACE);
2433 c_L = GetSysColor(COLOR_3DLIGHT);
2435 if (loadflags & LR_LOADTRANSPARENT) {
2437 case 1: pix = pix >> 7; break;
2438 case 4: pix = pix >> 4; break;
2441 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2444 if (pix >= colors) {
2445 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2448 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2449 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2450 ptr->rgbBlue = GetBValue(c_W);
2451 ptr->rgbGreen = GetGValue(c_W);
2452 ptr->rgbRed = GetRValue(c_W);
2454 if (loadflags & LR_LOADMAP3DCOLORS)
2455 for (i=0; i<colors; i++) {
2456 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2457 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2458 if (c_C == RGB(128, 128, 128)) {
2459 ptr->rgbRed = GetRValue(c_S);
2460 ptr->rgbGreen = GetGValue(c_S);
2461 ptr->rgbBlue = GetBValue(c_S);
2462 } else if (c_C == RGB(192, 192, 192)) {
2463 ptr->rgbRed = GetRValue(c_F);
2464 ptr->rgbGreen = GetGValue(c_F);
2465 ptr->rgbBlue = GetBValue(c_F);
2466 } else if (c_C == RGB(223, 223, 223)) {
2467 ptr->rgbRed = GetRValue(c_L);
2468 ptr->rgbGreen = GetGValue(c_L);
2469 ptr->rgbBlue = GetBValue(c_L);
2475 /**********************************************************************
2478 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2479 INT desiredx, INT desiredy, UINT loadflags )
2481 HBITMAP hbitmap = 0, orig_bm;
2485 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2489 LONG width, height, new_width, new_height;
2491 DWORD compr_dummy, offbits = 0;
2493 HDC screen_mem_dc = NULL;
2495 if (!(loadflags & LR_LOADFROMFILE))
2499 /* OEM bitmap: try to load the resource from user32.dll */
2500 instance = user32_module;
2503 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2504 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2506 if ((info = LockResource( handle )) == NULL) return 0;
2510 BITMAPFILEHEADER * bmfh;
2512 if (!(ptr = map_fileW( name, NULL ))) return 0;
2513 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2514 bmfh = (BITMAPFILEHEADER *)ptr;
2515 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2517 WARN("Invalid/unsupported bitmap format!\n");
2520 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2523 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2524 WARN("Broken BitmapInfoHeader!\n");
2528 size = bitmap_info_size(info, DIB_RGB_COLORS);
2529 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2530 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2532 if (!fix_info || !scaled_info) goto end;
2533 memcpy(fix_info, info, size);
2535 pix = *((LPBYTE)info + size);
2536 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2538 memcpy(scaled_info, fix_info, size);
2539 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2540 &bpp_dummy, &compr_dummy);
2542 new_width = desiredx;
2547 new_height = height > 0 ? desiredy : -desiredy;
2549 new_height = height;
2553 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2554 core->bcWidth = new_width;
2555 core->bcHeight = new_height;
2559 scaled_info->bmiHeader.biWidth = new_width;
2560 scaled_info->bmiHeader.biHeight = new_height;
2563 if (new_height < 0) new_height = -new_height;
2565 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2566 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2568 bits = (char *)info + (offbits ? offbits : size);
2570 if (loadflags & LR_CREATEDIBSECTION)
2572 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2573 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2577 if (is_dib_monochrome(fix_info))
2578 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2580 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2583 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2584 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2585 SelectObject(screen_mem_dc, orig_bm);
2588 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2589 HeapFree(GetProcessHeap(), 0, scaled_info);
2590 HeapFree(GetProcessHeap(), 0, fix_info);
2592 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2597 /**********************************************************************
2598 * LoadImageA (USER32.@)
2602 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2603 INT desiredx, INT desiredy, UINT loadflags)
2608 if (IS_INTRESOURCE(name))
2609 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2612 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2613 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2614 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2616 __EXCEPT_PAGE_FAULT {
2617 SetLastError( ERROR_INVALID_PARAMETER );
2621 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2622 HeapFree(GetProcessHeap(), 0, u_name);
2627 /******************************************************************************
2628 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2631 * hinst [I] Handle of instance that contains image
2632 * name [I] Name of image
2633 * type [I] Type of image
2634 * desiredx [I] Desired width
2635 * desiredy [I] Desired height
2636 * loadflags [I] Load flags
2639 * Success: Handle to newly loaded image
2642 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2644 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2645 INT desiredx, INT desiredy, UINT loadflags )
2647 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2648 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2650 if (loadflags & LR_DEFAULTSIZE) {
2651 if (type == IMAGE_ICON) {
2652 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2653 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2654 } else if (type == IMAGE_CURSOR) {
2655 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2656 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2659 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2662 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2665 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2668 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2669 GetDeviceCaps(screen_dc, BITSPIXEL),
2675 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2676 1, TRUE, loadflags);
2681 /******************************************************************************
2682 * CopyImage (USER32.@) Creates new image and copies attributes to it
2685 * hnd [I] Handle to image to copy
2686 * type [I] Type of image to copy
2687 * desiredx [I] Desired width of new image
2688 * desiredy [I] Desired height of new image
2689 * flags [I] Copy flags
2692 * Success: Handle to newly created image
2696 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2697 * all other versions (95/2000/XP have been tested) ignore it.
2700 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2701 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2702 * the copy will have the same depth as the screen.
2703 * The content of the image will only be copied if the bit depth of the
2704 * original image is compatible with the bit depth of the screen, or
2705 * if the source is a DIB section.
2706 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2708 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2709 INT desiredy, UINT flags )
2711 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2712 hnd, type, desiredx, desiredy, flags);
2723 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2724 if (!objSize) return 0;
2725 if ((desiredx < 0) || (desiredy < 0)) return 0;
2727 if (flags & LR_COPYFROMRESOURCE)
2729 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2732 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2733 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2735 /* Allocate memory for a BITMAPINFOHEADER structure and a
2736 color table. The maximum number of colors in a color table
2737 is 256 which corresponds to a bitmap with depth 8.
2738 Bitmaps with higher depths don't have color tables. */
2739 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2742 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2743 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2744 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2745 bi->bmiHeader.biCompression = BI_RGB;
2747 if (flags & LR_CREATEDIBSECTION)
2749 /* Create a DIB section. LR_MONOCHROME is ignored */
2751 HDC dc = CreateCompatibleDC(NULL);
2753 if (objSize == sizeof(DIBSECTION))
2755 /* The source bitmap is a DIB.
2756 Get its attributes to create an exact copy */
2757 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2760 /* Get the color table or the color masks */
2761 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2763 bi->bmiHeader.biWidth = desiredx;
2764 bi->bmiHeader.biHeight = desiredy;
2765 bi->bmiHeader.biSizeImage = 0;
2767 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2772 /* Create a device-dependent bitmap */
2774 BOOL monochrome = (flags & LR_MONOCHROME);
2776 if (objSize == sizeof(DIBSECTION))
2778 /* The source bitmap is a DIB section.
2779 Get its attributes */
2780 HDC dc = CreateCompatibleDC(NULL);
2781 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2782 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2783 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2786 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2788 /* Look if the colors of the DIB are black and white */
2791 (bi->bmiColors[0].rgbRed == 0xff
2792 && bi->bmiColors[0].rgbGreen == 0xff
2793 && bi->bmiColors[0].rgbBlue == 0xff
2794 && bi->bmiColors[0].rgbReserved == 0
2795 && bi->bmiColors[1].rgbRed == 0
2796 && bi->bmiColors[1].rgbGreen == 0
2797 && bi->bmiColors[1].rgbBlue == 0
2798 && bi->bmiColors[1].rgbReserved == 0)
2800 (bi->bmiColors[0].rgbRed == 0
2801 && bi->bmiColors[0].rgbGreen == 0
2802 && bi->bmiColors[0].rgbBlue == 0
2803 && bi->bmiColors[0].rgbReserved == 0
2804 && bi->bmiColors[1].rgbRed == 0xff
2805 && bi->bmiColors[1].rgbGreen == 0xff
2806 && bi->bmiColors[1].rgbBlue == 0xff
2807 && bi->bmiColors[1].rgbReserved == 0);
2810 else if (!monochrome)
2812 monochrome = ds.dsBm.bmBitsPixel == 1;
2817 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2821 HDC screenDC = GetDC(NULL);
2822 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2823 ReleaseDC(NULL, screenDC);
2829 /* Only copy the bitmap if it's a DIB section or if it's
2830 compatible to the screen */
2833 if (objSize == sizeof(DIBSECTION))
2835 copyContents = TRUE;
2839 HDC screenDC = GetDC(NULL);
2840 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2841 ReleaseDC(NULL, screenDC);
2843 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2848 /* The source bitmap may already be selected in a device context,
2849 use GetDIBits/StretchDIBits and not StretchBlt */
2854 dc = CreateCompatibleDC(NULL);
2856 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2857 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2858 bi->bmiHeader.biSizeImage = 0;
2859 bi->bmiHeader.biClrUsed = 0;
2860 bi->bmiHeader.biClrImportant = 0;
2862 /* Fill in biSizeImage */
2863 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2864 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2870 /* Get the image bits of the source bitmap */
2871 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2873 /* Copy it to the destination bitmap */
2874 oldBmp = SelectObject(dc, res);
2875 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2876 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2877 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2878 SelectObject(dc, oldBmp);
2880 HeapFree(GetProcessHeap(), 0, bits);
2886 if (flags & LR_COPYDELETEORG)
2891 HeapFree(GetProcessHeap(), 0, bi);
2895 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2897 /* Should call CURSORICON_ExtCopy but more testing
2898 * needs to be done before we change this
2900 if (flags) FIXME("Flags are ignored\n");
2901 return CopyCursor(hnd);
2907 /******************************************************************************
2908 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2911 * Success: Handle to specified bitmap
2914 HBITMAP WINAPI LoadBitmapW(
2915 HINSTANCE instance, /* [in] Handle to application instance */
2916 LPCWSTR name) /* [in] Address of bitmap resource name */
2918 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2921 /**********************************************************************
2922 * LoadBitmapA (USER32.@)
2926 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2928 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );