2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Martin Von Loewis
6 * Copyright 1997 Alex Korobka
7 * Copyright 1998 Turchanov Sergey
8 * Copyright 2007 Henri Verbeet
9 * Copyright 2009 Vincent Povirk for CodeWeavers
10 * Copyright 2016 Dmitry Timoshkov
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
38 #include "wine/exception.h"
39 #include "wine/server.h"
42 #include "user_private.h"
43 #include "wine/list.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(cursor
);
47 WINE_DECLARE_DEBUG_CHANNEL(icon
);
48 WINE_DECLARE_DEBUG_CHANNEL(resource
);
50 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
51 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
52 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
53 #define PNG_SIGN RIFF_FOURCC(0x89,'P','N','G')
55 static struct list icon_cache
= LIST_INIT( icon_cache
);
57 /**********************************************************************
58 * User objects management
61 struct cursoricon_frame
63 UINT width
; /* frame-specific width */
64 UINT height
; /* frame-specific height */
65 UINT delay
; /* frame-specific delay between this frame and the next (in jiffies) */
66 HBITMAP color
; /* color bitmap */
67 HBITMAP alpha
; /* pre-multiplied alpha bitmap for 32-bpp icons */
68 HBITMAP mask
; /* mask bitmap (followed by color for 1-bpp icons) */
72 struct cursoricon_object
74 struct user_object obj
; /* object header */
75 struct list entry
; /* entry in shared icons list */
76 ULONG_PTR param
; /* opaque param used by 16-bit code */
77 UNICODE_STRING module
; /* module for icons loaded from resources */
78 LPWSTR resname
; /* resource name for icons loaded from resources */
79 HRSRC rsrc
; /* resource for shared icons */
80 BOOL is_shared
; /* whether this object is shared */
81 BOOL is_icon
; /* whether icon or cursor */
82 BOOL is_ani
; /* whether this object is a static cursor or an animated cursor */
83 UINT delay
; /* delay between this frame and the next (in jiffies) */
86 struct cursoricon_frame frame
; /* frame-specific icon data */
89 UINT num_frames
; /* number of frames in the icon/cursor */
90 UINT num_steps
; /* number of sequence steps in the icon/cursor */
91 HICON
*frames
; /* list of animated cursor frames */
96 static HBITMAP
create_color_bitmap( int width
, int height
)
98 HDC hdc
= get_display_dc();
99 HBITMAP ret
= CreateCompatibleBitmap( hdc
, width
, height
);
100 release_display_dc( hdc
);
104 static int get_display_bpp(void)
106 HDC hdc
= get_display_dc();
107 int ret
= GetDeviceCaps( hdc
, BITSPIXEL
);
108 release_display_dc( hdc
);
112 static HICON
alloc_icon_handle( BOOL is_ani
, UINT num_steps
)
114 struct cursoricon_object
*obj
;
117 if (!(obj
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*obj
) ))) return NULL
;
119 obj
->is_ani
= is_ani
;
122 if (!(obj
->ani
.frames
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
123 num_steps
* sizeof(*obj
->ani
.frames
) )))
125 HeapFree( GetProcessHeap(), 0, obj
);
128 obj
->ani
.num_steps
= num_steps
;
129 obj
->ani
.num_frames
= num_steps
; /* changed later for some animated cursors */
132 if (!(handle
= alloc_user_handle( &obj
->obj
, NTUSER_OBJ_ICON
)))
134 if (obj
->is_ani
) HeapFree( GetProcessHeap(), 0, obj
->ani
.frames
);
135 HeapFree( GetProcessHeap(), 0, obj
);
140 static struct cursoricon_object
*get_icon_ptr( HICON handle
)
142 struct cursoricon_object
*obj
= get_user_handle_ptr( handle
, NTUSER_OBJ_ICON
);
143 if (obj
== OBJ_OTHER_PROCESS
)
145 WARN( "icon handle %p from other process\n", handle
);
151 static struct cursoricon_frame
*get_icon_frame( struct cursoricon_object
*obj
, int istep
)
153 struct cursoricon_object
*req_frame
;
155 if (!obj
->is_ani
) return &obj
->frame
;
156 if (!(req_frame
= get_icon_ptr( obj
->ani
.frames
[istep
] ))) return 0;
157 return &req_frame
->frame
;
160 static void release_icon_frame( struct cursoricon_object
*obj
, struct cursoricon_frame
*frame
)
164 struct cursoricon_object
*frameobj
;
166 frameobj
= (struct cursoricon_object
*) (((char *)frame
) - FIELD_OFFSET(struct cursoricon_object
, frame
));
167 release_user_handle_ptr( frameobj
);
171 static UINT
get_icon_steps( struct cursoricon_object
*obj
)
173 return obj
->is_ani
? obj
->ani
.num_steps
: 1;
176 static void free_icon_frame( struct cursoricon_frame
*frame
)
178 if (frame
->color
) DeleteObject( frame
->color
);
179 if (frame
->alpha
) DeleteObject( frame
->alpha
);
180 if (frame
->mask
) DeleteObject( frame
->mask
);
183 static BOOL
free_icon_handle( HICON handle
)
185 struct cursoricon_object
*obj
= free_user_handle( handle
, NTUSER_OBJ_ICON
);
187 if (obj
== OBJ_OTHER_PROCESS
) WARN( "icon handle %p from other process\n", handle
);
190 ULONG_PTR param
= obj
->param
;
193 assert( !obj
->rsrc
); /* shared icons can't be freed */
197 free_icon_frame( &obj
->frame
);
201 for (i
= 0; i
< obj
->ani
.num_steps
; i
++)
203 HICON hFrame
= obj
->ani
.frames
[i
];
209 free_icon_handle( obj
->ani
.frames
[i
] );
210 for (j
=0; j
< obj
->ani
.num_steps
; j
++)
212 if (obj
->ani
.frames
[j
] == hFrame
) obj
->ani
.frames
[j
] = 0;
216 HeapFree( GetProcessHeap(), 0, obj
->ani
.frames
);
218 if (!IS_INTRESOURCE( obj
->resname
)) HeapFree( GetProcessHeap(), 0, obj
->resname
);
219 HeapFree( GetProcessHeap(), 0, obj
);
220 if (wow_handlers
.free_icon_param
&& param
) wow_handlers
.free_icon_param( param
);
221 USER_Driver
->pDestroyCursorIcon( handle
);
227 ULONG_PTR
get_icon_param( HICON handle
)
230 struct cursoricon_object
*obj
= get_user_handle_ptr( handle
, NTUSER_OBJ_ICON
);
232 if (obj
== OBJ_OTHER_PROCESS
) WARN( "icon handle %p from other process\n", handle
);
236 release_user_handle_ptr( obj
);
241 ULONG_PTR
set_icon_param( HICON handle
, ULONG_PTR param
)
244 struct cursoricon_object
*obj
= get_user_handle_ptr( handle
, NTUSER_OBJ_ICON
);
246 if (obj
== OBJ_OTHER_PROCESS
) WARN( "icon handle %p from other process\n", handle
);
251 release_user_handle_ptr( obj
);
257 /***********************************************************************
260 * Helper function to map a file to memory:
262 * [RETURN] ptr - pointer to mapped file
263 * [RETURN] filesize - pointer size of file to be stored if not NULL
265 static const void *map_fileW( LPCWSTR name
, LPDWORD filesize
)
267 HANDLE hFile
, hMapping
;
270 hFile
= CreateFileW( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
271 OPEN_EXISTING
, FILE_FLAG_RANDOM_ACCESS
, 0 );
272 if (hFile
!= INVALID_HANDLE_VALUE
)
274 hMapping
= CreateFileMappingW( hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
277 ptr
= MapViewOfFile( hMapping
, FILE_MAP_READ
, 0, 0, 0 );
278 CloseHandle( hMapping
);
280 *filesize
= GetFileSize( hFile
, NULL
);
282 CloseHandle( hFile
);
288 /***********************************************************************
291 * Return the size of a DIB bitmap in bytes.
293 static int get_dib_image_size( int width
, int height
, int depth
)
295 return (((width
* depth
+ 31) / 8) & ~3) * abs( height
);
299 /***********************************************************************
302 * Return the size of the bitmap info structure including color table.
304 int bitmap_info_size( const BITMAPINFO
* info
, WORD coloruse
)
306 unsigned int colors
, size
, masks
= 0;
308 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
310 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
311 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
312 return sizeof(BITMAPCOREHEADER
) + colors
*
313 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
315 else /* assume BITMAPINFOHEADER */
317 colors
= info
->bmiHeader
.biClrUsed
;
318 if (colors
> 256) /* buffer overflow otherwise */
320 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
321 colors
= 1 << info
->bmiHeader
.biBitCount
;
322 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
323 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
324 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
329 /***********************************************************************
332 * Helper function to duplicate a bitmap.
334 static HBITMAP
copy_bitmap( HBITMAP bitmap
)
337 HBITMAP new_bitmap
= 0;
340 if (!bitmap
) return 0;
341 if (!GetObjectW( bitmap
, sizeof(bmp
), &bmp
)) return 0;
343 if ((src
= CreateCompatibleDC( 0 )) && (dst
= CreateCompatibleDC( 0 )))
345 SelectObject( src
, bitmap
);
346 if ((new_bitmap
= CreateCompatibleBitmap( src
, bmp
.bmWidth
, bmp
.bmHeight
)))
348 SelectObject( dst
, new_bitmap
);
349 BitBlt( dst
, 0, 0, bmp
.bmWidth
, bmp
.bmHeight
, src
, 0, 0, SRCCOPY
);
358 /***********************************************************************
361 * Returns whether a DIB can be converted to a monochrome DDB.
363 * A DIB can be converted if its color table contains only black and
364 * white. Black must be the first color in the color table.
366 * Note : If the first color in the color table is white followed by
367 * black, we can't convert it to a monochrome DDB with
368 * SetDIBits, because black and white would be inverted.
370 static BOOL
is_dib_monochrome( const BITMAPINFO
* info
)
372 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
374 const RGBTRIPLE
*rgb
= ((const BITMAPCOREINFO
*)info
)->bmciColors
;
376 if (((const BITMAPCOREINFO
*)info
)->bmciHeader
.bcBitCount
!= 1) return FALSE
;
378 /* Check if the first color is black */
379 if ((rgb
->rgbtRed
== 0) && (rgb
->rgbtGreen
== 0) && (rgb
->rgbtBlue
== 0))
383 /* Check if the second color is white */
384 return ((rgb
->rgbtRed
== 0xff) && (rgb
->rgbtGreen
== 0xff)
385 && (rgb
->rgbtBlue
== 0xff));
389 else /* assume BITMAPINFOHEADER */
391 const RGBQUAD
*rgb
= info
->bmiColors
;
393 if (info
->bmiHeader
.biBitCount
!= 1) return FALSE
;
395 /* Check if the first color is black */
396 if ((rgb
->rgbRed
== 0) && (rgb
->rgbGreen
== 0) &&
397 (rgb
->rgbBlue
== 0) && (rgb
->rgbReserved
== 0))
401 /* Check if the second color is white */
402 return ((rgb
->rgbRed
== 0xff) && (rgb
->rgbGreen
== 0xff)
403 && (rgb
->rgbBlue
== 0xff) && (rgb
->rgbReserved
== 0));
409 /***********************************************************************
412 * Get the info from a bitmap header.
413 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
415 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
416 LONG
*height
, WORD
*bpp
, DWORD
*compr
)
418 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
420 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
421 *width
= core
->bcWidth
;
422 *height
= core
->bcHeight
;
423 *bpp
= core
->bcBitCount
;
427 else if (header
->biSize
== sizeof(BITMAPINFOHEADER
) ||
428 header
->biSize
== sizeof(BITMAPV4HEADER
) ||
429 header
->biSize
== sizeof(BITMAPV5HEADER
))
431 *width
= header
->biWidth
;
432 *height
= header
->biHeight
;
433 *bpp
= header
->biBitCount
;
434 *compr
= header
->biCompression
;
437 WARN("unknown/wrong size (%u) for header\n", header
->biSize
);
441 /**********************************************************************
444 BOOL
get_icon_size( HICON handle
, SIZE
*size
)
446 struct cursoricon_object
*info
;
447 struct cursoricon_frame
*frame
;
449 if (!(info
= get_icon_ptr( handle
))) return FALSE
;
450 frame
= get_icon_frame( info
, 0 );
451 size
->cx
= frame
->width
;
452 size
->cy
= frame
->height
;
453 release_icon_frame( info
, frame
);
454 release_user_handle_ptr( info
);
464 static void user_read_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
466 struct png_wrapper
*png
= png_get_io_ptr(png_ptr
);
468 if (png
->size
- png
->pos
>= length
)
470 memcpy(data
, png
->buffer
+ png
->pos
, length
);
475 png_error(png_ptr
, "failed to read PNG data");
479 static unsigned be_uint(unsigned val
)
488 return (u
.c
[0] << 24) | (u
.c
[1] << 16) | (u
.c
[2] << 8) | u
.c
[3];
491 static BOOL
get_png_info(const void *png_data
, DWORD size
, int *width
, int *height
, int *bpp
)
493 static const char png_sig
[8] = { 0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a };
494 static const char png_IHDR
[8] = { 0,0,0,0x0d,'I','H','D','R' };
499 unsigned width
, height
;
500 char bit_depth
, color_type
, compression
, filter
, interlace
;
503 if (size
< sizeof(*png
)) return FALSE
;
504 if (memcmp(png
->png_sig
, png_sig
, sizeof(png_sig
)) != 0) return FALSE
;
505 if (memcmp(png
->ihdr_sig
, png_IHDR
, sizeof(png_IHDR
)) != 0) return FALSE
;
507 *bpp
= (png
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) ? 32 : 24;
508 *width
= be_uint(png
->width
);
509 *height
= be_uint(png
->height
);
514 static BITMAPINFO
*load_png(const char *png_data
, DWORD
*size
)
516 struct png_wrapper png
;
519 png_bytep
*row_pointers
= NULL
;
520 int color_type
, bit_depth
, bpp
, width
, height
;
521 int rowbytes
, image_size
, mask_size
= 0, i
;
522 BITMAPINFO
*info
= NULL
;
523 unsigned char *image_data
;
525 if (!get_png_info(png_data
, *size
, &width
, &height
, &bpp
)) return NULL
;
527 png
.buffer
= png_data
;
531 /* initialize libpng */
532 png_ptr
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
533 if (!png_ptr
) return NULL
;
535 info_ptr
= png_create_info_struct(png_ptr
);
538 png_destroy_read_struct(&png_ptr
, NULL
, NULL
);
542 /* set up setjmp/longjmp error handling */
543 if (setjmp(png_jmpbuf(png_ptr
)))
546 RtlFreeHeap(GetProcessHeap(), 0, info
);
547 png_destroy_read_struct(&png_ptr
, &info_ptr
, NULL
);
551 png_set_crc_action(png_ptr
, PNG_CRC_QUIET_USE
, PNG_CRC_QUIET_USE
);
553 /* set up custom i/o handling */
554 png_set_read_fn(png_ptr
, &png
, user_read_data
);
556 /* read the header */
557 png_read_info(png_ptr
, info_ptr
);
559 color_type
= png_get_color_type(png_ptr
, info_ptr
);
560 bit_depth
= png_get_bit_depth(png_ptr
, info_ptr
);
562 /* expand grayscale image data to rgb */
563 if (color_type
== PNG_COLOR_TYPE_GRAY
|| color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
564 png_set_gray_to_rgb(png_ptr
);
566 /* expand palette image data to rgb */
567 if (color_type
== PNG_COLOR_TYPE_PALETTE
|| bit_depth
< 8)
568 png_set_expand(png_ptr
);
570 /* update color type information */
571 png_read_update_info(png_ptr
, info_ptr
);
573 color_type
= png_get_color_type(png_ptr
, info_ptr
);
574 bit_depth
= png_get_bit_depth(png_ptr
, info_ptr
);
580 case PNG_COLOR_TYPE_RGB
:
585 case PNG_COLOR_TYPE_RGB_ALPHA
:
588 png_set_bgr(png_ptr
);
599 FIXME("unsupported PNG color format %d, %d bpp\n", color_type
, bit_depth
);
600 png_destroy_read_struct(&png_ptr
, &info_ptr
, NULL
);
604 width
= png_get_image_width(png_ptr
, info_ptr
);
605 height
= png_get_image_height(png_ptr
, info_ptr
);
607 rowbytes
= (width
* bpp
+ 7) / 8;
608 image_size
= height
* rowbytes
;
609 if (bpp
!= 32) /* add a mask if there is no alpha */
610 mask_size
= (width
+ 7) / 8 * height
;
612 info
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER
) + image_size
+ mask_size
);
615 png_destroy_read_struct(&png_ptr
, &info_ptr
, NULL
);
619 image_data
= (unsigned char *)info
+ sizeof(BITMAPINFOHEADER
);
620 memset(image_data
+ image_size
, 0, mask_size
);
622 row_pointers
= malloc(height
* sizeof(png_bytep
));
625 RtlFreeHeap(GetProcessHeap(), 0, info
);
626 png_destroy_read_struct(&png_ptr
, &info_ptr
, NULL
);
631 for (i
= 0; i
< height
; i
++)
632 row_pointers
[i
] = image_data
+ (height
- i
- 1) * rowbytes
;
634 png_read_image(png_ptr
, row_pointers
);
636 png_destroy_read_struct(&png_ptr
, &info_ptr
, NULL
);
638 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
639 info
->bmiHeader
.biWidth
= width
;
640 info
->bmiHeader
.biHeight
= height
* 2;
641 info
->bmiHeader
.biPlanes
= 1;
642 info
->bmiHeader
.biBitCount
= bpp
;
643 info
->bmiHeader
.biCompression
= BI_RGB
;
644 info
->bmiHeader
.biSizeImage
= image_size
;
645 info
->bmiHeader
.biXPelsPerMeter
= 0;
646 info
->bmiHeader
.biYPelsPerMeter
= 0;
647 info
->bmiHeader
.biClrUsed
= 0;
648 info
->bmiHeader
.biClrImportant
= 0;
650 *size
= sizeof(BITMAPINFOHEADER
) + image_size
+ mask_size
;
656 * The following macro functions account for the irregularities of
657 * accessing cursor and icon resources in files and resource entries.
659 typedef BOOL (*fnGetCIEntry
)( LPCVOID dir
, DWORD size
, int n
,
660 int *width
, int *height
, int *bits
);
662 /**********************************************************************
663 * CURSORICON_FindBestIcon
665 * Find the icon closest to the requested size and bit depth.
667 static int CURSORICON_FindBestIcon( LPCVOID dir
, DWORD size
, fnGetCIEntry get_entry
,
668 int width
, int height
, int depth
, UINT loadflags
)
670 int i
, cx
, cy
, bits
, bestEntry
= -1;
671 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
672 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
675 iTotalDiff
= 0xFFFFFFFF;
676 iColorDiff
= 0xFFFFFFFF;
678 if (loadflags
& LR_DEFAULTSIZE
)
680 if (!width
) width
= GetSystemMetrics( SM_CXICON
);
681 if (!height
) height
= GetSystemMetrics( SM_CYICON
);
683 else if (!width
&& !height
)
685 /* use the size of the first entry */
686 if (!get_entry( dir
, size
, 0, &width
, &height
, &bits
)) return -1;
690 for ( i
= 0; iTotalDiff
&& get_entry( dir
, size
, i
, &cx
, &cy
, &bits
); i
++ )
692 iTempXDiff
= abs(width
- cx
);
693 iTempYDiff
= abs(height
- cy
);
695 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
699 iTotalDiff
= iXDiff
+ iYDiff
;
703 /* Find Best Colors for Best Fit */
704 for ( i
= 0; get_entry( dir
, size
, i
, &cx
, &cy
, &bits
); i
++ )
706 TRACE("entry %d: %d x %d, %d bpp\n", i
, cx
, cy
, bits
);
708 if(abs(width
- cx
) == iXDiff
&& abs(height
- cy
) == iYDiff
)
710 iTempColorDiff
= abs(depth
- bits
);
711 if(iColorDiff
> iTempColorDiff
)
714 iColorDiff
= iTempColorDiff
;
722 static BOOL
CURSORICON_GetResIconEntry( LPCVOID dir
, DWORD size
, int n
,
723 int *width
, int *height
, int *bits
)
725 const CURSORICONDIR
*resdir
= dir
;
726 const ICONRESDIR
*icon
;
728 if ( resdir
->idCount
<= n
)
730 if ((const char *)&resdir
->idEntries
[n
+ 1] - (const char *)dir
> size
)
732 icon
= &resdir
->idEntries
[n
].ResInfo
.icon
;
733 *width
= icon
->bWidth
;
734 *height
= icon
->bHeight
;
735 *bits
= resdir
->idEntries
[n
].wBitCount
;
736 if (!*width
&& !*height
) *width
= *height
= 256;
740 /**********************************************************************
741 * CURSORICON_FindBestCursor
743 * Find the cursor closest to the requested size.
745 * FIXME: parameter 'color' ignored.
747 static int CURSORICON_FindBestCursor( LPCVOID dir
, DWORD size
, fnGetCIEntry get_entry
,
748 int width
, int height
, int depth
, UINT loadflags
)
750 int i
, maxwidth
, maxheight
, maxbits
, cx
, cy
, bits
, bestEntry
= -1;
752 if (loadflags
& LR_DEFAULTSIZE
)
754 if (!width
) width
= GetSystemMetrics( SM_CXCURSOR
);
755 if (!height
) height
= GetSystemMetrics( SM_CYCURSOR
);
757 else if (!width
&& !height
)
759 /* use the first entry */
760 if (!get_entry( dir
, size
, 0, &width
, &height
, &bits
)) return -1;
764 /* First find the largest one smaller than or equal to the requested size*/
766 maxwidth
= maxheight
= maxbits
= 0;
767 for ( i
= 0; get_entry( dir
, size
, i
, &cx
, &cy
, &bits
); i
++ )
769 if (cx
> width
|| cy
> height
) continue;
770 if (cx
< maxwidth
|| cy
< maxheight
) continue;
771 if (cx
== maxwidth
&& cy
== maxheight
)
773 if (loadflags
& LR_MONOCHROME
)
775 if (maxbits
&& bits
>= maxbits
) continue;
777 else if (bits
<= maxbits
) continue;
784 if (bestEntry
!= -1) return bestEntry
;
786 /* Now find the smallest one larger than the requested size */
788 maxwidth
= maxheight
= 255;
789 for ( i
= 0; get_entry( dir
, size
, i
, &cx
, &cy
, &bits
); i
++ )
791 if (cx
> maxwidth
|| cy
> maxheight
) continue;
792 if (cx
== maxwidth
&& cy
== maxheight
)
794 if (loadflags
& LR_MONOCHROME
)
796 if (maxbits
&& bits
>= maxbits
) continue;
798 else if (bits
<= maxbits
) continue;
805 if (bestEntry
== -1) bestEntry
= 0;
810 static BOOL
CURSORICON_GetResCursorEntry( LPCVOID dir
, DWORD size
, int n
,
811 int *width
, int *height
, int *bits
)
813 const CURSORICONDIR
*resdir
= dir
;
814 const CURSORDIR
*cursor
;
816 if ( resdir
->idCount
<= n
)
818 if ((const char *)&resdir
->idEntries
[n
+ 1] - (const char *)dir
> size
)
820 cursor
= &resdir
->idEntries
[n
].ResInfo
.cursor
;
821 *width
= cursor
->wWidth
;
822 *height
= cursor
->wHeight
;
823 *bits
= resdir
->idEntries
[n
].wBitCount
;
824 if (*height
== *width
* 2) *height
/= 2;
828 static const CURSORICONDIRENTRY
*CURSORICON_FindBestIconRes( const CURSORICONDIR
* dir
, DWORD size
,
829 int width
, int height
, int depth
,
834 n
= CURSORICON_FindBestIcon( dir
, size
, CURSORICON_GetResIconEntry
,
835 width
, height
, depth
, loadflags
);
838 return &dir
->idEntries
[n
];
841 static const CURSORICONDIRENTRY
*CURSORICON_FindBestCursorRes( const CURSORICONDIR
*dir
, DWORD size
,
842 int width
, int height
, int depth
,
845 int n
= CURSORICON_FindBestCursor( dir
, size
, CURSORICON_GetResCursorEntry
,
846 width
, height
, depth
, loadflags
);
849 return &dir
->idEntries
[n
];
852 static BOOL
CURSORICON_GetFileEntry( LPCVOID dir
, DWORD size
, int n
,
853 int *width
, int *height
, int *bits
)
855 const CURSORICONFILEDIR
*filedir
= dir
;
856 const CURSORICONFILEDIRENTRY
*entry
;
857 const BITMAPINFOHEADER
*info
;
859 if ( filedir
->idCount
<= n
)
861 if ((const char *)&filedir
->idEntries
[n
+ 1] - (const char *)dir
> size
)
863 entry
= &filedir
->idEntries
[n
];
864 if (entry
->dwDIBOffset
> size
- sizeof(info
->biSize
)) return FALSE
;
865 info
= (const BITMAPINFOHEADER
*)((const char *)dir
+ entry
->dwDIBOffset
);
867 if (info
->biSize
== PNG_SIGN
) return get_png_info(info
, size
, width
, height
, bits
);
869 if (info
->biSize
!= sizeof(BITMAPCOREHEADER
))
871 if ((const char *)(info
+ 1) - (const char *)dir
> size
) return FALSE
;
872 *bits
= info
->biBitCount
;
876 const BITMAPCOREHEADER
*coreinfo
= (const BITMAPCOREHEADER
*)((const char *)dir
+ entry
->dwDIBOffset
);
877 if ((const char *)(coreinfo
+ 1) - (const char *)dir
> size
) return FALSE
;
878 *bits
= coreinfo
->bcBitCount
;
880 *width
= entry
->bWidth
;
881 *height
= entry
->bHeight
;
885 static const CURSORICONFILEDIRENTRY
*CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR
*dir
, DWORD size
,
886 int width
, int height
, int depth
,
889 int n
= CURSORICON_FindBestCursor( dir
, size
, CURSORICON_GetFileEntry
,
890 width
, height
, depth
, loadflags
);
893 return &dir
->idEntries
[n
];
896 static const CURSORICONFILEDIRENTRY
*CURSORICON_FindBestIconFile( const CURSORICONFILEDIR
*dir
, DWORD size
,
897 int width
, int height
, int depth
,
900 int n
= CURSORICON_FindBestIcon( dir
, size
, CURSORICON_GetFileEntry
,
901 width
, height
, depth
, loadflags
);
904 return &dir
->idEntries
[n
];
907 static HICON
alloc_cursoricon_handle( BOOL is_icon
)
909 struct cursoricon_object
*obj
;
912 if (!(obj
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*obj
) ))) return NULL
;
913 obj
->is_icon
= is_icon
;
915 if (!(handle
= alloc_user_handle( &obj
->obj
, NTUSER_OBJ_ICON
))) free( obj
);
919 /***********************************************************************
920 * NtUserSetCursorIconData (win32u.@)
922 BOOL WINAPI
NtUserSetCursorIconData( HCURSOR cursor
, UNICODE_STRING
*module
, UNICODE_STRING
*res_name
,
923 struct cursoricon_desc
*desc
)
925 struct cursoricon_object
*obj
;
928 if (!(obj
= get_icon_ptr( cursor
))) return FALSE
;
930 if (obj
->is_ani
|| obj
->frame
.width
)
932 /* already initialized */
933 release_user_handle_ptr( obj
);
934 SetLastError( ERROR_INVALID_CURSOR_HANDLE
);
938 obj
->delay
= desc
->delay
;
942 if (!(obj
->ani
.frames
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
943 desc
->num_steps
* sizeof(*obj
->ani
.frames
) )))
945 release_user_handle_ptr( obj
);
949 obj
->ani
.num_steps
= desc
->num_steps
;
950 obj
->ani
.num_frames
= desc
->num_frames
;
952 else obj
->frame
= desc
->frames
[0];
956 else if (res_name
->Length
)
958 obj
->resname
= HeapAlloc( GetProcessHeap(), 0, res_name
->Length
+ sizeof(WCHAR
) );
961 memcpy( obj
->resname
, res_name
->Buffer
, res_name
->Length
);
962 obj
->resname
[res_name
->Length
/ sizeof(WCHAR
)] = 0;
966 obj
->resname
= MAKEINTRESOURCEW( LOWORD(res_name
->Buffer
) );
968 if (module
&& module
->Length
&& (obj
->module
.Buffer
= HeapAlloc( GetProcessHeap(), 0, module
->Length
)))
970 memcpy( obj
->module
.Buffer
, module
->Buffer
, module
->Length
);
971 obj
->module
.Length
= module
->Length
;
976 /* Setup the animated frames in the correct sequence */
977 for (i
= 0; i
< desc
->num_steps
; i
++)
979 struct cursoricon_desc frame_desc
;
982 if (obj
->ani
.frames
[i
]) continue; /* already set */
984 frame_id
= desc
->frame_seq
? desc
->frame_seq
[i
] : i
;
985 if (frame_id
>= obj
->ani
.num_frames
)
987 frame_id
= obj
->ani
.num_frames
- 1;
988 ERR_(cursor
)( "Sequence indicates frame past end of list, corrupt?\n" );
990 memset( &frame_desc
, 0, sizeof(frame_desc
) );
991 frame_desc
.delay
= desc
->frame_rates
? desc
->frame_rates
[i
] : desc
->delay
;
992 frame_desc
.frames
= &desc
->frames
[frame_id
];
993 if (!(obj
->ani
.frames
[i
] = alloc_cursoricon_handle( obj
->is_icon
)) ||
994 !NtUserSetCursorIconData( obj
->ani
.frames
[i
], NULL
, NULL
, &frame_desc
))
996 release_user_handle_ptr( obj
);
1000 if (desc
->frame_seq
)
1002 for (j
= i
+ 1; j
< obj
->ani
.num_steps
; j
++)
1004 if (desc
->frame_seq
[j
] == frame_id
) obj
->ani
.frames
[j
] = obj
->ani
.frames
[i
];
1010 if (desc
->flags
& LR_SHARED
)
1012 obj
->is_shared
= TRUE
;
1013 if (obj
->module
.Length
)
1015 obj
->rsrc
= desc
->rsrc
;
1016 list_add_head( &icon_cache
, &obj
->entry
);
1020 release_user_handle_ptr( obj
);
1024 /***********************************************************************
1027 static BOOL
bmi_has_alpha( const BITMAPINFO
*info
, const void *bits
)
1030 BOOL has_alpha
= FALSE
;
1031 const unsigned char *ptr
= bits
;
1033 if (info
->bmiHeader
.biBitCount
!= 32) return FALSE
;
1034 for (i
= 0; i
< info
->bmiHeader
.biWidth
* abs(info
->bmiHeader
.biHeight
); i
++, ptr
+= 4)
1035 if ((has_alpha
= (ptr
[3] != 0))) break;
1039 /***********************************************************************
1040 * create_alpha_bitmap
1042 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
1044 static HBITMAP
create_alpha_bitmap( HBITMAP color
, const BITMAPINFO
*src_info
, const void *color_bits
)
1047 BITMAPINFO
*info
= NULL
;
1054 if (!GetObjectW( color
, sizeof(bm
), &bm
)) return 0;
1055 if (bm
.bmBitsPixel
!= 32) return 0;
1057 if (!(hdc
= CreateCompatibleDC( 0 ))) return 0;
1058 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )))) goto done
;
1059 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1060 info
->bmiHeader
.biWidth
= bm
.bmWidth
;
1061 info
->bmiHeader
.biHeight
= -bm
.bmHeight
;
1062 info
->bmiHeader
.biPlanes
= 1;
1063 info
->bmiHeader
.biBitCount
= 32;
1064 info
->bmiHeader
.biCompression
= BI_RGB
;
1065 info
->bmiHeader
.biSizeImage
= bm
.bmWidth
* bm
.bmHeight
* 4;
1066 info
->bmiHeader
.biXPelsPerMeter
= 0;
1067 info
->bmiHeader
.biYPelsPerMeter
= 0;
1068 info
->bmiHeader
.biClrUsed
= 0;
1069 info
->bmiHeader
.biClrImportant
= 0;
1070 if (!(alpha
= CreateDIBSection( hdc
, info
, DIB_RGB_COLORS
, &bits
, NULL
, 0 ))) goto done
;
1074 SelectObject( hdc
, alpha
);
1075 StretchDIBits( hdc
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
1076 0, 0, src_info
->bmiHeader
.biWidth
, src_info
->bmiHeader
.biHeight
,
1077 color_bits
, src_info
, DIB_RGB_COLORS
, SRCCOPY
);
1082 GetDIBits( hdc
, color
, 0, bm
.bmHeight
, bits
, info
, DIB_RGB_COLORS
);
1083 if (!bmi_has_alpha( info
, bits
))
1085 DeleteObject( alpha
);
1091 /* pre-multiply by alpha */
1092 for (i
= 0, ptr
= bits
; i
< bm
.bmWidth
* bm
.bmHeight
; i
++, ptr
+= 4)
1094 unsigned int alpha
= ptr
[3];
1095 ptr
[0] = ptr
[0] * alpha
/ 255;
1096 ptr
[1] = ptr
[1] * alpha
/ 255;
1097 ptr
[2] = ptr
[2] * alpha
/ 255;
1102 HeapFree( GetProcessHeap(), 0, info
);
1106 static BOOL
create_icon_frame( const BITMAPINFO
*bmi
, DWORD maxsize
, POINT hotspot
, BOOL is_icon
,
1107 INT width
, INT height
, UINT flags
, struct cursoricon_frame
*frame
)
1109 DWORD size
, color_size
, mask_size
, compr
;
1110 const void *color_bits
, *mask_bits
;
1111 void *alpha_mask_bits
= NULL
;
1112 LONG bmi_width
, bmi_height
;
1113 BITMAPINFO
*bmi_copy
;
1119 memset( frame
, 0, sizeof(*frame
) );
1121 /* Check bitmap header */
1123 if (bmi
->bmiHeader
.biSize
== PNG_SIGN
)
1125 BITMAPINFO
*bmi_png
;
1127 if (!(bmi_png
= load_png( (const char *)bmi
, &maxsize
))) return FALSE
;
1128 ret
= create_icon_frame( bmi_png
, maxsize
, hotspot
, is_icon
, width
, height
, flags
, frame
);
1129 HeapFree( GetProcessHeap(), 0, bmi_png
);
1133 if (maxsize
< sizeof(BITMAPCOREHEADER
))
1135 WARN( "invalid size %u\n", maxsize
);
1138 if (maxsize
< bmi
->bmiHeader
.biSize
)
1140 WARN( "invalid header size %u\n", bmi
->bmiHeader
.biSize
);
1143 if ( (bmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) &&
1144 (bmi
->bmiHeader
.biSize
!= sizeof(BITMAPINFOHEADER
) ||
1145 (bmi
->bmiHeader
.biCompression
!= BI_RGB
&&
1146 bmi
->bmiHeader
.biCompression
!= BI_BITFIELDS
)) )
1148 WARN( "invalid bitmap header %u\n", bmi
->bmiHeader
.biSize
);
1152 size
= bitmap_info_size( bmi
, DIB_RGB_COLORS
);
1153 DIB_GetBitmapInfo(&bmi
->bmiHeader
, &bmi_width
, &bmi_height
, &bpp
, &compr
);
1154 color_size
= get_dib_image_size( bmi_width
, bmi_height
/ 2,
1156 mask_size
= get_dib_image_size( bmi_width
, bmi_height
/ 2, 1 );
1157 if (size
> maxsize
|| color_size
> maxsize
- size
)
1159 WARN( "truncated file %u < %u+%u+%u\n", maxsize
, size
, color_size
, mask_size
);
1162 if (mask_size
> maxsize
- size
- color_size
) mask_size
= 0; /* no mask */
1164 if (flags
& LR_DEFAULTSIZE
)
1166 if (!width
) width
= GetSystemMetrics( is_icon
? SM_CXICON
: SM_CXCURSOR
);
1167 if (!height
) height
= GetSystemMetrics( is_icon
? SM_CYICON
: SM_CYCURSOR
);
1171 if (!width
) width
= bmi_width
;
1172 if (!height
) height
= bmi_height
/2;
1174 do_stretch
= (bmi_height
/2 != height
) || (bmi_width
!= width
);
1176 /* Scale the hotspot */
1179 hotspot
.x
= width
/ 2;
1180 hotspot
.y
= height
/ 2;
1182 else if (do_stretch
)
1184 hotspot
.x
= (hotspot
.x
* width
) / bmi_width
;
1185 hotspot
.y
= (hotspot
.y
* height
) / (bmi_height
/ 2);
1188 if (!(bmi_copy
= HeapAlloc( GetProcessHeap(), 0, max( size
, FIELD_OFFSET( BITMAPINFO
, bmiColors
[2] )))))
1190 if (!(hdc
= CreateCompatibleDC( 0 ))) goto done
;
1192 memcpy( bmi_copy
, bmi
, size
);
1193 if (bmi_copy
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
))
1194 bmi_copy
->bmiHeader
.biHeight
/= 2;
1196 ((BITMAPCOREINFO
*)bmi_copy
)->bmciHeader
.bcHeight
/= 2;
1199 color_bits
= (const char*)bmi
+ size
;
1200 mask_bits
= (const char*)color_bits
+ color_size
;
1202 if (is_dib_monochrome( bmi
))
1204 if (!(frame
->mask
= CreateBitmap( width
, height
* 2, 1, 1, NULL
))) goto done
;
1206 /* copy color data into second half of mask bitmap */
1207 SelectObject( hdc
, frame
->mask
);
1208 StretchDIBits( hdc
, 0, height
, width
, height
,
1209 0, 0, bmi_width
, bmi_height
,
1210 color_bits
, bmi_copy
, DIB_RGB_COLORS
, SRCCOPY
);
1214 if (!(frame
->mask
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
1215 if (!(frame
->color
= create_color_bitmap( width
, height
))) goto done
;
1216 SelectObject( hdc
, frame
->color
);
1217 StretchDIBits( hdc
, 0, 0, width
, height
,
1218 0, 0, bmi_width
, bmi_height
,
1219 color_bits
, bmi_copy
, DIB_RGB_COLORS
, SRCCOPY
);
1221 if (bmi_has_alpha( bmi_copy
, color_bits
))
1223 frame
->alpha
= create_alpha_bitmap( frame
->color
, bmi_copy
, color_bits
);
1224 if (!mask_size
) /* generate mask from alpha */
1226 LONG x
, y
, dst_stride
= ((bmi_width
+ 31) / 8) & ~3;
1228 if ((alpha_mask_bits
= heap_calloc( bmi_height
, dst_stride
)))
1230 static const unsigned char masks
[] = { 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1 };
1231 const DWORD
*src
= color_bits
;
1232 unsigned char *dst
= alpha_mask_bits
;
1234 for (y
= 0; y
< bmi_height
; y
++, src
+= bmi_width
, dst
+= dst_stride
)
1235 for (x
= 0; x
< bmi_width
; x
++)
1236 if (src
[x
] >> 24 != 0xff) dst
[x
>> 3] |= masks
[x
& 7];
1238 mask_bits
= alpha_mask_bits
;
1239 mask_size
= bmi_height
* dst_stride
;
1244 /* convert info to monochrome to copy the mask */
1245 if (bmi_copy
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
))
1247 RGBQUAD
*rgb
= bmi_copy
->bmiColors
;
1249 bmi_copy
->bmiHeader
.biBitCount
= 1;
1250 bmi_copy
->bmiHeader
.biClrUsed
= bmi_copy
->bmiHeader
.biClrImportant
= 2;
1251 rgb
[0].rgbBlue
= rgb
[0].rgbGreen
= rgb
[0].rgbRed
= 0x00;
1252 rgb
[1].rgbBlue
= rgb
[1].rgbGreen
= rgb
[1].rgbRed
= 0xff;
1253 rgb
[0].rgbReserved
= rgb
[1].rgbReserved
= 0;
1257 RGBTRIPLE
*rgb
= (RGBTRIPLE
*)(((BITMAPCOREHEADER
*)bmi_copy
) + 1);
1259 ((BITMAPCOREINFO
*)bmi_copy
)->bmciHeader
.bcBitCount
= 1;
1260 rgb
[0].rgbtBlue
= rgb
[0].rgbtGreen
= rgb
[0].rgbtRed
= 0x00;
1261 rgb
[1].rgbtBlue
= rgb
[1].rgbtGreen
= rgb
[1].rgbtRed
= 0xff;
1267 SelectObject( hdc
, frame
->mask
);
1268 StretchDIBits( hdc
, 0, 0, width
, height
,
1269 0, 0, bmi_width
, bmi_height
,
1270 mask_bits
, bmi_copy
, DIB_RGB_COLORS
, SRCCOPY
);
1274 frame
->width
= width
;
1275 frame
->height
= height
;
1276 frame
->hotspot
= hotspot
;
1280 if (!ret
) free_icon_frame( frame
);
1282 HeapFree( GetProcessHeap(), 0, bmi_copy
);
1283 HeapFree( GetProcessHeap(), 0, alpha_mask_bits
);
1287 static HICON
create_cursoricon_object( struct cursoricon_desc
*desc
, BOOL is_icon
, HINSTANCE module
,
1288 const WCHAR
*resname
, HRSRC rsrc
)
1290 WCHAR buf
[MAX_PATH
];
1291 UNICODE_STRING module_name
= { 0, sizeof(buf
), buf
};
1292 UNICODE_STRING res_str
= { 0 };
1295 if (!(handle
= alloc_cursoricon_handle( is_icon
))) return 0;
1297 if (module
) LdrGetDllFullName( module
, &module_name
);
1299 res_str
.Buffer
= (WCHAR
*)resname
;
1300 if (!IS_INTRESOURCE(resname
))
1302 res_str
.Length
= lstrlenW( resname
) * sizeof(WCHAR
);
1303 res_str
.MaximumLength
= res_str
.Length
+ sizeof(WCHAR
);
1305 desc
->rsrc
= rsrc
; /* FIXME: we should probably avoid storing rsrc */
1307 if (!NtUserSetCursorIconData( handle
, &module_name
, &res_str
, desc
))
1309 DestroyCursor( handle
);
1316 /***********************************************************************
1317 * create_icon_from_bmi
1319 * Create an icon from its BITMAPINFO.
1321 static HICON
create_icon_from_bmi( const BITMAPINFO
*bmi
, DWORD maxsize
, HMODULE module
, LPCWSTR resname
,
1322 HRSRC rsrc
, POINT hotspot
, BOOL bIcon
, INT width
, INT height
,
1325 struct cursoricon_frame frame
;
1326 struct cursoricon_desc desc
=
1333 if (!create_icon_frame( bmi
, maxsize
, hotspot
, bIcon
, width
, height
, flags
, &frame
)) return 0;
1335 ret
= create_cursoricon_object( &desc
, bIcon
, module
, resname
, rsrc
);
1336 if (!ret
) free_icon_frame( &frame
);
1341 /**********************************************************************
1342 * .ANI cursor support
1344 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
1345 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
1346 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
1347 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
1348 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
1349 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
1350 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
1352 #define ANI_FLAG_ICON 0x1
1353 #define ANI_FLAG_SEQUENCE 0x2
1369 const unsigned char *data
;
1372 static void dump_ani_header( const ani_header
*header
)
1374 TRACE(" header size: %d\n", header
->header_size
);
1375 TRACE(" frames: %d\n", header
->num_frames
);
1376 TRACE(" steps: %d\n", header
->num_steps
);
1377 TRACE(" width: %d\n", header
->width
);
1378 TRACE(" height: %d\n", header
->height
);
1379 TRACE(" bpp: %d\n", header
->bpp
);
1380 TRACE(" planes: %d\n", header
->num_planes
);
1381 TRACE(" display rate: %d\n", header
->display_rate
);
1382 TRACE(" flags: 0x%08x\n", header
->flags
);
1404 static void riff_find_chunk( DWORD chunk_id
, DWORD chunk_type
, const riff_chunk_t
*parent_chunk
, riff_chunk_t
*chunk
)
1406 const unsigned char *ptr
= parent_chunk
->data
;
1407 const unsigned char *end
= parent_chunk
->data
+ (parent_chunk
->data_size
- (2 * sizeof(DWORD
)));
1409 if (chunk_type
== ANI_LIST_ID
|| chunk_type
== ANI_RIFF_ID
) end
-= sizeof(DWORD
);
1413 if ((!chunk_type
&& *(const DWORD
*)ptr
== chunk_id
)
1414 || (chunk_type
&& *(const DWORD
*)ptr
== chunk_type
&& *((const DWORD
*)ptr
+ 2) == chunk_id
))
1416 ptr
+= sizeof(DWORD
);
1417 chunk
->data_size
= (*(const DWORD
*)ptr
+ 1) & ~1;
1418 ptr
+= sizeof(DWORD
);
1419 if (chunk_type
== ANI_LIST_ID
|| chunk_type
== ANI_RIFF_ID
) ptr
+= sizeof(DWORD
);
1425 ptr
+= sizeof(DWORD
);
1428 ptr
+= (*(const DWORD
*)ptr
+ 1) & ~1;
1429 ptr
+= sizeof(DWORD
);
1437 * RIFF:'ACON' RIFF chunk
1438 * |- CHUNK:'anih' Header
1439 * |- CHUNK:'seq ' Sequence information (optional)
1440 * \- LIST:'fram' Frame list
1441 * |- CHUNK:icon Cursor frames
1446 static HCURSOR
CURSORICON_CreateIconFromANI( const BYTE
*bits
, DWORD bits_size
, INT width
, INT height
,
1447 INT depth
, BOOL is_icon
, UINT loadflags
)
1449 struct cursoricon_object
*info
;
1450 DWORD
*frame_rates
= NULL
;
1451 DWORD
*frame_seq
= NULL
;
1453 BOOL use_seq
= FALSE
;
1459 riff_chunk_t root_chunk
= { bits_size
, bits
};
1460 riff_chunk_t ACON_chunk
= {0};
1461 riff_chunk_t anih_chunk
= {0};
1462 riff_chunk_t fram_chunk
= {0};
1463 riff_chunk_t rate_chunk
= {0};
1464 riff_chunk_t seq_chunk
= {0};
1465 const unsigned char *icon_chunk
;
1466 const unsigned char *icon_data
;
1468 TRACE("bits %p, bits_size %d\n", bits
, bits_size
);
1470 riff_find_chunk( ANI_ACON_ID
, ANI_RIFF_ID
, &root_chunk
, &ACON_chunk
);
1471 if (!ACON_chunk
.data
)
1473 ERR("Failed to get root chunk.\n");
1477 riff_find_chunk( ANI_anih_ID
, 0, &ACON_chunk
, &anih_chunk
);
1478 if (!anih_chunk
.data
)
1480 ERR("Failed to get 'anih' chunk.\n");
1483 memcpy( &header
, anih_chunk
.data
, sizeof(header
) );
1484 dump_ani_header( &header
);
1486 if (!(header
.flags
& ANI_FLAG_ICON
))
1488 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1492 if (header
.flags
& ANI_FLAG_SEQUENCE
)
1494 riff_find_chunk( ANI_seq__ID
, 0, &ACON_chunk
, &seq_chunk
);
1497 frame_seq
= (DWORD
*) seq_chunk
.data
;
1502 FIXME("Sequence data expected but not found, assuming steps == frames.\n");
1503 header
.num_steps
= header
.num_frames
;
1507 riff_find_chunk( ANI_rate_ID
, 0, &ACON_chunk
, &rate_chunk
);
1508 if (rate_chunk
.data
)
1509 frame_rates
= (DWORD
*) rate_chunk
.data
;
1511 riff_find_chunk( ANI_fram_ID
, ANI_LIST_ID
, &ACON_chunk
, &fram_chunk
);
1512 if (!fram_chunk
.data
)
1514 ERR("Failed to get icon list.\n");
1518 cursor
= alloc_icon_handle( TRUE
, header
.num_steps
);
1519 if (!cursor
) return 0;
1520 frames
= HeapAlloc( GetProcessHeap(), 0, sizeof(*frames
) * header
.num_frames
);
1523 free_icon_handle( cursor
);
1527 info
= get_icon_ptr( cursor
);
1528 info
->is_icon
= is_icon
;
1529 info
->ani
.num_frames
= header
.num_frames
;
1531 /* The .ANI stores the display rate in jiffies (1/60s) */
1532 info
->delay
= header
.display_rate
;
1534 icon_chunk
= fram_chunk
.data
;
1535 icon_data
= fram_chunk
.data
+ (2 * sizeof(DWORD
));
1536 for (i
=0; i
<header
.num_frames
; i
++)
1538 const DWORD chunk_size
= *(const DWORD
*)(icon_chunk
+ sizeof(DWORD
));
1539 const CURSORICONFILEDIRENTRY
*entry
;
1540 INT frameWidth
, frameHeight
;
1541 const BITMAPINFO
*bmi
;
1544 entry
= CURSORICON_FindBestIconFile((const CURSORICONFILEDIR
*) icon_data
,
1545 bits
+ bits_size
- icon_data
,
1546 width
, height
, depth
, loadflags
);
1548 hotspot
.x
= entry
->xHotspot
;
1549 hotspot
.y
= entry
->yHotspot
;
1550 if (!header
.width
|| !header
.height
)
1552 frameWidth
= entry
->bWidth
;
1553 frameHeight
= entry
->bHeight
;
1557 frameWidth
= header
.width
;
1558 frameHeight
= header
.height
;
1562 if (entry
->dwDIBOffset
< bits
+ bits_size
- icon_data
)
1564 bmi
= (const BITMAPINFO
*) (icon_data
+ entry
->dwDIBOffset
);
1565 /* Grab a frame from the animation */
1566 frames
[i
] = create_icon_from_bmi( bmi
, bits
+ bits_size
- (const BYTE
*)bmi
,
1567 NULL
, NULL
, NULL
, hotspot
,
1568 is_icon
, frameWidth
, frameHeight
, loadflags
);
1573 FIXME_(cursor
)("failed to convert animated cursor frame.\n");
1577 FIXME_(cursor
)("Completely failed to create animated cursor!\n");
1578 info
->ani
.num_frames
= 0;
1579 release_user_handle_ptr( info
);
1580 free_icon_handle( cursor
);
1581 HeapFree( GetProcessHeap(), 0, frames
);
1587 /* Advance to the next chunk */
1588 icon_chunk
+= chunk_size
+ (2 * sizeof(DWORD
));
1589 icon_data
= icon_chunk
+ (2 * sizeof(DWORD
));
1592 /* There was an error but we at least decoded the first frame, so just use that frame */
1595 FIXME_(cursor
)("Error creating animated cursor, only using first frame!\n");
1596 for (i
=1; i
< info
->ani
.num_frames
; i
++)
1597 free_icon_handle( info
->ani
.frames
[i
] );
1600 info
->ani
.num_steps
= 1;
1601 info
->ani
.num_frames
= 1;
1604 /* Setup the animated frames in the correct sequence */
1605 for (i
=0; i
< info
->ani
.num_steps
; i
++)
1607 DWORD frame_id
= use_seq
? frame_seq
[i
] : i
;
1608 struct cursoricon_frame
*frame
;
1610 if (frame_id
>= info
->ani
.num_frames
)
1612 frame_id
= info
->ani
.num_frames
-1;
1613 ERR_(cursor
)("Sequence indicates frame past end of list, corrupt?\n");
1615 info
->ani
.frames
[i
] = frames
[frame_id
];
1616 frame
= get_icon_frame( info
, i
);
1618 frame
->delay
= frame_rates
[i
];
1621 release_icon_frame( info
, frame
);
1624 HeapFree( GetProcessHeap(), 0, frames
);
1625 release_user_handle_ptr( info
);
1631 /**********************************************************************
1632 * CreateIconFromResourceEx (USER32.@)
1634 * FIXME: Convert to mono when cFlag is LR_MONOCHROME.
1636 HICON WINAPI
CreateIconFromResourceEx( LPBYTE bits
, UINT cbSize
,
1637 BOOL bIcon
, DWORD dwVersion
,
1638 INT width
, INT height
,
1642 const BITMAPINFO
*bmi
;
1644 TRACE_(cursor
)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1645 bits
, cbSize
, dwVersion
, width
, height
,
1646 bIcon
? "icon" : "cursor", (cFlag
& LR_MONOCHROME
) ? "mono" : "" );
1648 if (!bits
) return 0;
1650 if (dwVersion
== 0x00020000)
1652 FIXME_(cursor
)("\t2.xx resources are not supported\n");
1656 /* Check if the resource is an animated icon/cursor */
1657 if (!memcmp(bits
, "RIFF", 4))
1658 return CURSORICON_CreateIconFromANI( bits
, cbSize
, width
, height
,
1659 0 /* default depth */, bIcon
, cFlag
);
1663 hotspot
.x
= width
/ 2;
1664 hotspot
.y
= height
/ 2;
1665 bmi
= (BITMAPINFO
*)bits
;
1667 else /* get the hotspot */
1669 const SHORT
*pt
= (const SHORT
*)bits
;
1672 bmi
= (const BITMAPINFO
*)(pt
+ 2);
1673 cbSize
-= 2 * sizeof(*pt
);
1676 return create_icon_from_bmi( bmi
, cbSize
, NULL
, NULL
, NULL
, hotspot
, bIcon
, width
, height
, cFlag
);
1680 /**********************************************************************
1681 * CreateIconFromResource (USER32.@)
1683 HICON WINAPI
CreateIconFromResource( LPBYTE bits
, UINT cbSize
,
1684 BOOL bIcon
, DWORD dwVersion
)
1686 return CreateIconFromResourceEx( bits
, cbSize
, bIcon
, dwVersion
, 0, 0, LR_DEFAULTSIZE
| LR_SHARED
);
1690 static HICON
CURSORICON_LoadFromFile( LPCWSTR filename
,
1691 INT width
, INT height
, INT depth
,
1692 BOOL fCursor
, UINT loadflags
)
1694 const CURSORICONFILEDIRENTRY
*entry
;
1695 const CURSORICONFILEDIR
*dir
;
1701 TRACE("loading %s\n", debugstr_w( filename
));
1703 bits
= map_fileW( filename
, &filesize
);
1707 /* Check for .ani. */
1708 if (memcmp( bits
, "RIFF", 4 ) == 0)
1710 hIcon
= CURSORICON_CreateIconFromANI( bits
, filesize
, width
, height
, depth
, !fCursor
, loadflags
);
1714 dir
= (const CURSORICONFILEDIR
*) bits
;
1715 if ( filesize
< FIELD_OFFSET( CURSORICONFILEDIR
, idEntries
[dir
->idCount
] ))
1719 entry
= CURSORICON_FindBestCursorFile( dir
, filesize
, width
, height
, depth
, loadflags
);
1721 entry
= CURSORICON_FindBestIconFile( dir
, filesize
, width
, height
, depth
, loadflags
);
1726 /* check that we don't run off the end of the file */
1727 if ( entry
->dwDIBOffset
> filesize
)
1729 if ( entry
->dwDIBOffset
+ entry
->dwDIBSize
> filesize
)
1732 hotspot
.x
= entry
->xHotspot
;
1733 hotspot
.y
= entry
->yHotspot
;
1734 hIcon
= create_icon_from_bmi( (const BITMAPINFO
*)&bits
[entry
->dwDIBOffset
], filesize
- entry
->dwDIBOffset
,
1735 NULL
, NULL
, NULL
, hotspot
, !fCursor
, width
, height
, loadflags
);
1737 TRACE("loaded %s -> %p\n", debugstr_w( filename
), hIcon
);
1738 UnmapViewOfFile( bits
);
1742 /**********************************************************************
1745 * Load a cursor or icon from resource or file.
1747 static HICON
CURSORICON_Load(HINSTANCE hInstance
, LPCWSTR name
,
1748 INT width
, INT height
, INT depth
,
1749 BOOL fCursor
, UINT loadflags
)
1755 const CURSORICONDIR
*dir
;
1756 const CURSORICONDIRENTRY
*dirEntry
;
1761 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1762 hInstance
, debugstr_w(name
), width
, height
, depth
, fCursor
, loadflags
);
1764 if ( loadflags
& LR_LOADFROMFILE
) /* Load from file */
1765 return CURSORICON_LoadFromFile( name
, width
, height
, depth
, fCursor
, loadflags
);
1767 if (!hInstance
) hInstance
= user32_module
; /* Load OEM cursor/icon */
1769 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1770 if ((ULONG_PTR
)hInstance
>> 16 == 0) loadflags
&= ~LR_SHARED
;
1772 /* Get directory resource ID */
1774 if (!(hRsrc
= FindResourceW( hInstance
, name
,
1775 (LPWSTR
)(fCursor
? RT_GROUP_CURSOR
: RT_GROUP_ICON
) )))
1777 /* try animated resource */
1778 if (!(hRsrc
= FindResourceW( hInstance
, name
,
1779 (LPWSTR
)(fCursor
? RT_ANICURSOR
: RT_ANIICON
) ))) return 0;
1780 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
1781 bits
= LockResource( handle
);
1782 return CURSORICON_CreateIconFromANI( bits
, SizeofResource( hInstance
, handle
),
1783 width
, height
, depth
, !fCursor
, loadflags
);
1786 /* Find the best entry in the directory */
1788 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
1789 if (!(dir
= LockResource( handle
))) return 0;
1790 size
= SizeofResource( hInstance
, hRsrc
);
1792 dirEntry
= CURSORICON_FindBestCursorRes( dir
, size
, width
, height
, depth
, loadflags
);
1794 dirEntry
= CURSORICON_FindBestIconRes( dir
, size
, width
, height
, depth
, loadflags
);
1795 if (!dirEntry
) return 0;
1796 wResId
= dirEntry
->wResId
;
1797 FreeResource( handle
);
1799 /* Load the resource */
1801 if (!(hRsrc
= FindResourceW(hInstance
,MAKEINTRESOURCEW(wResId
),
1802 (LPWSTR
)(fCursor
? RT_CURSOR
: RT_ICON
) ))) return 0;
1804 /* If shared icon, check whether it was already loaded */
1805 if (loadflags
& LR_SHARED
)
1807 WCHAR buf
[MAX_PATH
];
1808 UNICODE_STRING module
= { 0, sizeof(buf
) - sizeof(WCHAR
), buf
};
1809 struct cursoricon_object
*ptr
;
1811 if (!LdrGetDllFullName( hInstance
, &module
))
1814 LIST_FOR_EACH_ENTRY( ptr
, &icon_cache
, struct cursoricon_object
, entry
)
1816 if (ptr
->module
.Length
!= module
.Length
) continue;
1817 if (memcmp( ptr
->module
.Buffer
, module
.Buffer
, module
.Length
)) continue;
1818 if (ptr
->rsrc
!= hRsrc
) continue;
1819 hIcon
= ptr
->obj
.handle
;
1823 if (hIcon
) return hIcon
;
1827 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
1828 size
= SizeofResource( hInstance
, hRsrc
);
1829 bits
= LockResource( handle
);
1833 hotspot
.x
= width
/ 2;
1834 hotspot
.y
= height
/ 2;
1836 else /* get the hotspot */
1838 const SHORT
*pt
= (const SHORT
*)bits
;
1841 bits
+= 2 * sizeof(SHORT
);
1842 size
-= 2 * sizeof(SHORT
);
1844 hIcon
= create_icon_from_bmi( (const BITMAPINFO
*)bits
, size
, hInstance
, name
, hRsrc
,
1845 hotspot
, !fCursor
, width
, height
, loadflags
);
1846 FreeResource( handle
);
1851 static HBITMAP
create_masked_bitmap( int width
, int height
, const void *and, const void *xor )
1853 HBITMAP and_bitmap
, xor_bitmap
, bitmap
;
1856 and_bitmap
= CreateBitmap( width
, height
, 1, 1, and );
1857 xor_bitmap
= CreateBitmap( width
, height
, 1, 1, xor );
1858 bitmap
= CreateBitmap( width
, height
* 2, 1, 1, NULL
);
1859 src_dc
= CreateCompatibleDC( 0 );
1860 dst_dc
= CreateCompatibleDC( 0 );
1862 SelectObject( dst_dc
, bitmap
);
1863 SelectObject( src_dc
, and_bitmap
);
1864 BitBlt( dst_dc
, 0, 0, width
, height
, src_dc
, 0, 0, SRCCOPY
);
1865 SelectObject( src_dc
, xor_bitmap
);
1866 BitBlt( dst_dc
, 0, height
, width
, height
, src_dc
, 0, 0, SRCCOPY
);
1868 DeleteObject( and_bitmap
);
1869 DeleteObject( xor_bitmap
);
1876 /***********************************************************************
1877 * CreateCursor (USER32.@)
1879 HCURSOR WINAPI
CreateCursor( HINSTANCE instance
, int hotspot_x
, int hotspot_y
,
1880 int width
, int height
, const void *and, const void *xor )
1885 TRACE( "hotspot (%d,%d), size %dx%d\n", hotspot_x
, hotspot_y
, width
, height
);
1888 info
.xHotspot
= hotspot_x
;
1889 info
.yHotspot
= hotspot_y
;
1890 info
.hbmColor
= NULL
;
1891 info
.hbmMask
= create_masked_bitmap( width
, height
, and, xor );
1892 cursor
= CreateIconIndirect( &info
);
1893 DeleteObject( info
.hbmMask
);
1898 /***********************************************************************
1899 * CreateIcon (USER32.@)
1901 * Creates an icon based on the specified bitmaps. The bitmaps must be
1902 * provided in a device dependent format and will be resized to
1903 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1904 * depth. The provided bitmaps must be top-down bitmaps.
1905 * Although Windows does not support 15bpp(*) this API must support it
1906 * for Winelib applications.
1908 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1912 * Success: handle to an icon
1915 * FIXME: Do we need to resize the bitmaps?
1917 HICON WINAPI
CreateIcon( HINSTANCE instance
, int width
, int height
, BYTE planes
,
1918 BYTE depth
, const void *and, const void *xor )
1923 TRACE_(icon
)( "%dx%d, planes %d, depth %d\n", width
, height
, planes
, depth
);
1926 info
.xHotspot
= width
/ 2;
1927 info
.yHotspot
= height
/ 2;
1930 info
.hbmColor
= NULL
;
1931 info
.hbmMask
= create_masked_bitmap( width
, height
, and, xor );
1935 info
.hbmColor
= CreateBitmap( width
, height
, planes
, depth
, xor );
1936 info
.hbmMask
= CreateBitmap( width
, height
, 1, 1, and );
1939 icon
= CreateIconIndirect( &info
);
1941 DeleteObject( info
.hbmMask
);
1942 DeleteObject( info
.hbmColor
);
1948 /***********************************************************************
1949 * CopyIcon (USER32.@)
1951 HICON WINAPI
CopyIcon( HICON icon
)
1956 info
.cbSize
= sizeof(info
);
1957 if (!GetIconInfoExW( icon
, &info
))
1960 res
= CopyImage( icon
, info
.fIcon
? IMAGE_ICON
: IMAGE_CURSOR
, 0, 0, 0 );
1961 DeleteObject( info
.hbmColor
);
1962 DeleteObject( info
.hbmMask
);
1967 /***********************************************************************
1968 * DestroyIcon (USER32.@)
1970 BOOL WINAPI
DestroyIcon( HICON hIcon
)
1973 struct cursoricon_object
*obj
= get_icon_ptr( hIcon
);
1975 TRACE_(icon
)("%p\n", hIcon
);
1979 BOOL shared
= obj
->is_shared
;
1980 release_user_handle_ptr( obj
);
1981 ret
= (NtUserGetCursor() != hIcon
);
1982 if (!shared
) free_icon_handle( hIcon
);
1988 /***********************************************************************
1989 * DestroyCursor (USER32.@)
1991 BOOL WINAPI
DestroyCursor( HCURSOR hCursor
)
1993 return DestroyIcon( hCursor
);
1996 /***********************************************************************
1997 * DrawIcon (USER32.@)
1999 BOOL WINAPI
DrawIcon( HDC hdc
, INT x
, INT y
, HICON hIcon
)
2001 return DrawIconEx( hdc
, x
, y
, hIcon
, 0, 0, 0, 0, DI_NORMAL
| DI_COMPAT
| DI_DEFAULTSIZE
);
2004 /***********************************************************************
2005 * SetCursor (USER32.@)
2007 * Set the cursor shape.
2010 * A handle to the previous cursor shape.
2012 HCURSOR WINAPI DECLSPEC_HOTPATCH
SetCursor( HCURSOR hCursor
/* [in] Handle of cursor to show */ )
2014 struct cursoricon_object
*obj
;
2019 TRACE("%p\n", hCursor
);
2021 SERVER_START_REQ( set_cursor
)
2023 req
->flags
= SET_CURSOR_HANDLE
;
2024 req
->handle
= wine_server_user_handle( hCursor
);
2025 if ((ret
= !wine_server_call_err( req
)))
2027 hOldCursor
= wine_server_ptr_handle( reply
->prev_handle
);
2028 show_count
= reply
->prev_count
;
2034 USER_Driver
->pSetCursor( show_count
>= 0 ? hCursor
: 0 );
2036 if (!(obj
= get_icon_ptr( hOldCursor
))) return 0;
2037 release_user_handle_ptr( obj
);
2042 /***********************************************************************
2043 * GetClipCursor (USER32.@)
2045 BOOL WINAPI DECLSPEC_HOTPATCH
GetClipCursor( RECT
*rect
)
2047 return NtUserCallOneParam( (UINT_PTR
)rect
, NtUserGetClipCursor
);
2051 /***********************************************************************
2052 * SetSystemCursor (USER32.@)
2054 BOOL WINAPI
SetSystemCursor(HCURSOR hcur
, DWORD id
)
2056 FIXME("(%p,%08x),stub!\n", hcur
, id
);
2061 /**********************************************************************
2062 * LookupIconIdFromDirectoryEx (USER32.@)
2064 INT WINAPI
LookupIconIdFromDirectoryEx( LPBYTE xdir
, BOOL bIcon
,
2065 INT width
, INT height
, UINT cFlag
)
2067 const CURSORICONDIR
*dir
= (const CURSORICONDIR
*)xdir
;
2069 if( dir
&& !dir
->idReserved
&& (dir
->idType
& 3) )
2071 const CURSORICONDIRENTRY
* entry
;
2072 int depth
= (cFlag
& LR_MONOCHROME
) ? 1 : get_display_bpp();
2075 entry
= CURSORICON_FindBestIconRes( dir
, ~0u, width
, height
, depth
, LR_DEFAULTSIZE
);
2077 entry
= CURSORICON_FindBestCursorRes( dir
, ~0u, width
, height
, depth
, LR_DEFAULTSIZE
);
2079 if( entry
) retVal
= entry
->wResId
;
2081 else WARN_(cursor
)("invalid resource directory\n");
2085 /**********************************************************************
2086 * LookupIconIdFromDirectory (USER32.@)
2088 INT WINAPI
LookupIconIdFromDirectory( LPBYTE dir
, BOOL bIcon
)
2090 return LookupIconIdFromDirectoryEx( dir
, bIcon
, 0, 0, bIcon
? 0 : LR_MONOCHROME
);
2093 /***********************************************************************
2094 * LoadCursorW (USER32.@)
2096 HCURSOR WINAPI
LoadCursorW(HINSTANCE hInstance
, LPCWSTR name
)
2098 TRACE("%p, %s\n", hInstance
, debugstr_w(name
));
2100 return LoadImageW( hInstance
, name
, IMAGE_CURSOR
, 0, 0,
2101 LR_SHARED
| LR_DEFAULTSIZE
);
2104 /***********************************************************************
2105 * LoadCursorA (USER32.@)
2107 HCURSOR WINAPI
LoadCursorA(HINSTANCE hInstance
, LPCSTR name
)
2109 TRACE("%p, %s\n", hInstance
, debugstr_a(name
));
2111 return LoadImageA( hInstance
, name
, IMAGE_CURSOR
, 0, 0,
2112 LR_SHARED
| LR_DEFAULTSIZE
);
2115 /***********************************************************************
2116 * LoadCursorFromFileW (USER32.@)
2118 HCURSOR WINAPI
LoadCursorFromFileW (LPCWSTR name
)
2120 TRACE("%s\n", debugstr_w(name
));
2122 return LoadImageW( 0, name
, IMAGE_CURSOR
, 0, 0,
2123 LR_LOADFROMFILE
| LR_DEFAULTSIZE
);
2126 /***********************************************************************
2127 * LoadCursorFromFileA (USER32.@)
2129 HCURSOR WINAPI
LoadCursorFromFileA (LPCSTR name
)
2131 TRACE("%s\n", debugstr_a(name
));
2133 return LoadImageA( 0, name
, IMAGE_CURSOR
, 0, 0,
2134 LR_LOADFROMFILE
| LR_DEFAULTSIZE
);
2137 /***********************************************************************
2138 * LoadIconW (USER32.@)
2140 HICON WINAPI
LoadIconW(HINSTANCE hInstance
, LPCWSTR name
)
2142 TRACE("%p, %s\n", hInstance
, debugstr_w(name
));
2144 return LoadImageW( hInstance
, name
, IMAGE_ICON
, 0, 0,
2145 LR_SHARED
| LR_DEFAULTSIZE
);
2148 /***********************************************************************
2149 * LoadIconA (USER32.@)
2151 HICON WINAPI
LoadIconA(HINSTANCE hInstance
, LPCSTR name
)
2153 TRACE("%p, %s\n", hInstance
, debugstr_a(name
));
2155 return LoadImageA( hInstance
, name
, IMAGE_ICON
, 0, 0,
2156 LR_SHARED
| LR_DEFAULTSIZE
);
2159 /**********************************************************************
2160 * GetCursorFrameInfo (USER32.@)
2163 * So far no use has been found for the second parameter, it is currently presumed
2164 * that this parameter is reserved for future use.
2167 * hCursor [I] Handle to cursor for which to retrieve information
2168 * reserved [I] No purpose has been found for this parameter (may be NULL)
2169 * istep [I] The step of the cursor for which to retrieve information
2170 * rate_jiffies [O] Pointer to DWORD that receives the frame-specific delay (cannot be NULL)
2171 * num_steps [O] Pointer to DWORD that receives the number of steps in the cursor (cannot be NULL)
2174 * Success: Handle to a frame of the cursor (specified by istep)
2175 * Failure: NULL cursor (0)
2177 HCURSOR WINAPI
GetCursorFrameInfo(HCURSOR hCursor
, DWORD reserved
, DWORD istep
, DWORD
*rate_jiffies
, DWORD
*num_steps
)
2179 struct cursoricon_object
*ptr
;
2183 if (rate_jiffies
== NULL
|| num_steps
== NULL
) return 0;
2185 if (!(ptr
= get_icon_ptr( hCursor
))) return 0;
2187 TRACE("%p => %d %d %p %p\n", hCursor
, reserved
, istep
, rate_jiffies
, num_steps
);
2189 FIXME("Second parameter non-zero (%d), please report this!\n", reserved
);
2191 icon_steps
= get_icon_steps(ptr
);
2192 if (istep
< icon_steps
|| !ptr
->is_ani
)
2194 UINT icon_frames
= 1;
2197 icon_frames
= ptr
->ani
.num_frames
;
2198 if (ptr
->is_ani
&& icon_frames
> 1)
2199 ret
= ptr
->ani
.frames
[istep
];
2202 if (icon_frames
== 1)
2207 else if (icon_steps
== 1)
2210 *rate_jiffies
= ptr
->delay
;
2212 else if (istep
< icon_steps
)
2214 struct cursoricon_frame
*frame
;
2216 *num_steps
= icon_steps
;
2217 frame
= get_icon_frame( ptr
, istep
);
2218 if (get_icon_steps(ptr
) == 1)
2221 *num_steps
= get_icon_steps(ptr
);
2222 /* If this specific frame does not have a delay then use the global delay */
2223 if (frame
->delay
== ~0)
2224 *rate_jiffies
= ptr
->delay
;
2226 *rate_jiffies
= frame
->delay
;
2227 release_icon_frame( ptr
, frame
);
2231 release_user_handle_ptr( ptr
);
2236 /**********************************************************************
2237 * GetIconInfo (USER32.@)
2239 BOOL WINAPI
GetIconInfo(HICON hIcon
, PICONINFO iconinfo
)
2243 infoW
.cbSize
= sizeof(infoW
);
2244 if (!GetIconInfoExW( hIcon
, &infoW
)) return FALSE
;
2245 iconinfo
->fIcon
= infoW
.fIcon
;
2246 iconinfo
->xHotspot
= infoW
.xHotspot
;
2247 iconinfo
->yHotspot
= infoW
.yHotspot
;
2248 iconinfo
->hbmColor
= infoW
.hbmColor
;
2249 iconinfo
->hbmMask
= infoW
.hbmMask
;
2253 /**********************************************************************
2254 * GetIconInfoExA (USER32.@)
2256 BOOL WINAPI
GetIconInfoExA( HICON icon
, ICONINFOEXA
*info
)
2260 if (info
->cbSize
!= sizeof(*info
))
2262 SetLastError( ERROR_INVALID_PARAMETER
);
2265 infoW
.cbSize
= sizeof(infoW
);
2266 if (!GetIconInfoExW( icon
, &infoW
)) return FALSE
;
2267 info
->fIcon
= infoW
.fIcon
;
2268 info
->xHotspot
= infoW
.xHotspot
;
2269 info
->yHotspot
= infoW
.yHotspot
;
2270 info
->hbmColor
= infoW
.hbmColor
;
2271 info
->hbmMask
= infoW
.hbmMask
;
2272 info
->wResID
= infoW
.wResID
;
2273 WideCharToMultiByte( CP_ACP
, 0, infoW
.szModName
, -1, info
->szModName
, MAX_PATH
, NULL
, NULL
);
2274 WideCharToMultiByte( CP_ACP
, 0, infoW
.szResName
, -1, info
->szResName
, MAX_PATH
, NULL
, NULL
);
2278 /**********************************************************************
2279 * GetIconInfoExW (USER32.@)
2281 BOOL WINAPI
GetIconInfoExW( HICON icon
, ICONINFOEXW
*info
)
2283 struct cursoricon_frame
*frame
;
2284 struct cursoricon_object
*ptr
;
2287 if (info
->cbSize
!= sizeof(*info
))
2289 SetLastError( ERROR_INVALID_PARAMETER
);
2292 if (!(ptr
= get_icon_ptr( icon
)))
2294 SetLastError( ERROR_INVALID_CURSOR_HANDLE
);
2298 frame
= get_icon_frame( ptr
, 0 );
2301 release_user_handle_ptr( ptr
);
2302 SetLastError( ERROR_INVALID_CURSOR_HANDLE
);
2306 TRACE("%p => %dx%d\n", icon
, frame
->width
, frame
->height
);
2308 info
->fIcon
= ptr
->is_icon
;
2309 info
->xHotspot
= frame
->hotspot
.x
;
2310 info
->yHotspot
= frame
->hotspot
.y
;
2311 info
->hbmColor
= copy_bitmap( frame
->color
);
2312 info
->hbmMask
= copy_bitmap( frame
->mask
);
2314 info
->szModName
[0] = 0;
2315 info
->szResName
[0] = 0;
2316 if (ptr
->module
.Length
)
2318 if (IS_INTRESOURCE( ptr
->resname
)) info
->wResID
= LOWORD( ptr
->resname
);
2319 else lstrcpynW( info
->szResName
, ptr
->resname
, MAX_PATH
);
2320 memcpy( info
->szModName
, ptr
->module
.Buffer
, ptr
->module
.Length
);
2321 info
->szModName
[ptr
->module
.Length
/ sizeof(WCHAR
)] = 0;
2323 if (!info
->hbmMask
|| (!info
->hbmColor
&& frame
->color
))
2325 DeleteObject( info
->hbmMask
);
2326 DeleteObject( info
->hbmColor
);
2329 release_icon_frame( ptr
, frame
);
2330 release_user_handle_ptr( ptr
);
2334 /* copy an icon bitmap, even when it can't be selected into a DC */
2335 /* helper for CreateIconIndirect */
2336 static void stretch_blt_icon( HDC hdc_dst
, int dst_x
, int dst_y
, int dst_width
, int dst_height
,
2337 HBITMAP src
, int width
, int height
)
2339 HDC hdc
= CreateCompatibleDC( 0 );
2341 if (!SelectObject( hdc
, src
)) /* do it the hard way */
2346 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )))) return;
2347 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
2348 info
->bmiHeader
.biWidth
= width
;
2349 info
->bmiHeader
.biHeight
= height
;
2350 info
->bmiHeader
.biPlanes
= GetDeviceCaps( hdc_dst
, PLANES
);
2351 info
->bmiHeader
.biBitCount
= GetDeviceCaps( hdc_dst
, BITSPIXEL
);
2352 info
->bmiHeader
.biCompression
= BI_RGB
;
2353 info
->bmiHeader
.biSizeImage
= get_dib_image_size( width
, height
, info
->bmiHeader
.biBitCount
);
2354 info
->bmiHeader
.biXPelsPerMeter
= 0;
2355 info
->bmiHeader
.biYPelsPerMeter
= 0;
2356 info
->bmiHeader
.biClrUsed
= 0;
2357 info
->bmiHeader
.biClrImportant
= 0;
2358 bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
);
2359 if (bits
&& GetDIBits( hdc
, src
, 0, height
, bits
, info
, DIB_RGB_COLORS
))
2360 StretchDIBits( hdc_dst
, dst_x
, dst_y
, dst_width
, dst_height
,
2361 0, 0, width
, height
, bits
, info
, DIB_RGB_COLORS
, SRCCOPY
);
2363 HeapFree( GetProcessHeap(), 0, bits
);
2364 HeapFree( GetProcessHeap(), 0, info
);
2366 else StretchBlt( hdc_dst
, dst_x
, dst_y
, dst_width
, dst_height
, hdc
, 0, 0, width
, height
, SRCCOPY
);
2371 /**********************************************************************
2372 * CreateIconIndirect (USER32.@)
2374 HICON WINAPI
CreateIconIndirect(PICONINFO iconinfo
)
2376 struct cursoricon_frame frame
= { 0 };
2377 struct cursoricon_desc desc
= { .frames
= &frame
};
2378 BITMAP bmpXor
, bmpAnd
;
2382 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2383 iconinfo
->hbmColor
, iconinfo
->hbmMask
,
2384 iconinfo
->xHotspot
, iconinfo
->yHotspot
, iconinfo
->fIcon
);
2386 if (!iconinfo
->hbmMask
) return 0;
2388 GetObjectW( iconinfo
->hbmMask
, sizeof(bmpAnd
), &bmpAnd
);
2389 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2390 bmpAnd
.bmWidth
, bmpAnd
.bmHeight
, bmpAnd
.bmWidthBytes
,
2391 bmpAnd
.bmPlanes
, bmpAnd
.bmBitsPixel
);
2393 if (iconinfo
->hbmColor
)
2395 GetObjectW( iconinfo
->hbmColor
, sizeof(bmpXor
), &bmpXor
);
2396 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2397 bmpXor
.bmWidth
, bmpXor
.bmHeight
, bmpXor
.bmWidthBytes
,
2398 bmpXor
.bmPlanes
, bmpXor
.bmBitsPixel
);
2400 frame
.width
= bmpXor
.bmWidth
;
2401 frame
.height
= bmpXor
.bmHeight
;
2402 frame
.color
= create_color_bitmap( frame
.width
, frame
.height
);
2406 frame
.width
= bmpAnd
.bmWidth
;
2407 frame
.height
= bmpAnd
.bmHeight
;
2409 frame
.mask
= CreateBitmap( frame
.width
, frame
.height
, 1, 1, NULL
);
2411 hdc
= CreateCompatibleDC( 0 );
2412 SelectObject( hdc
, frame
.mask
);
2413 stretch_blt_icon( hdc
, 0, 0, frame
.width
, frame
.height
, iconinfo
->hbmMask
,
2414 bmpAnd
.bmWidth
, bmpAnd
.bmHeight
);
2418 SelectObject( hdc
, frame
.color
);
2419 stretch_blt_icon( hdc
, 0, 0, frame
.width
, frame
.height
, iconinfo
->hbmColor
,
2420 frame
.width
, frame
.height
);
2422 else frame
.height
/= 2;
2426 frame
.alpha
= create_alpha_bitmap( iconinfo
->hbmColor
, NULL
, NULL
);
2428 if (iconinfo
->fIcon
)
2430 frame
.hotspot
.x
= frame
.width
/ 2;
2431 frame
.hotspot
.y
= frame
.height
/ 2;
2435 frame
.hotspot
.x
= iconinfo
->xHotspot
;
2436 frame
.hotspot
.y
= iconinfo
->yHotspot
;
2439 ret
= create_cursoricon_object( &desc
, iconinfo
->fIcon
, 0, 0, 0 );
2440 if (!ret
) free_icon_frame( &frame
);
2444 /******************************************************************************
2445 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2448 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2451 * hdc [I] Handle to device context
2452 * x0 [I] X coordinate of upper left corner
2453 * y0 [I] Y coordinate of upper left corner
2454 * hIcon [I] Handle to icon to draw
2455 * cxWidth [I] Width of icon
2456 * cyWidth [I] Height of icon
2457 * istep [I] Index of frame in animated cursor
2458 * hbr [I] Handle to background brush
2459 * flags [I] Icon-drawing flags
2465 BOOL WINAPI
DrawIconEx( HDC hdc
, INT x0
, INT y0
, HICON hIcon
,
2466 INT cxWidth
, INT cyWidth
, UINT istep
,
2467 HBRUSH hbr
, UINT flags
)
2469 struct cursoricon_frame
*frame
;
2470 struct cursoricon_object
*ptr
;
2471 HDC hdc_dest
, hMemDC
;
2472 BOOL result
= FALSE
, DoOffscreen
;
2474 COLORREF oldFg
, oldBg
;
2475 INT x
, y
, nStretchMode
;
2477 TRACE_(icon
)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2478 hdc
,x0
,y0
,hIcon
,cxWidth
,cyWidth
,istep
,hbr
,flags
);
2480 if (!(ptr
= get_icon_ptr( hIcon
))) return FALSE
;
2481 if (istep
>= get_icon_steps( ptr
))
2483 TRACE_(icon
)("Stepped past end of animated frames=%d\n", istep
);
2484 release_user_handle_ptr( ptr
);
2487 if (!(frame
= get_icon_frame( ptr
, istep
)))
2489 FIXME_(icon
)("Error retrieving icon frame %d\n", istep
);
2490 release_user_handle_ptr( ptr
);
2493 if (!(hMemDC
= CreateCompatibleDC( hdc
)))
2495 release_icon_frame( ptr
, frame
);
2496 release_user_handle_ptr( ptr
);
2500 if (flags
& DI_NOMIRROR
)
2501 FIXME_(icon
)("Ignoring flag DI_NOMIRROR\n");
2503 /* Calculate the size of the destination image. */
2506 if (flags
& DI_DEFAULTSIZE
)
2507 cxWidth
= GetSystemMetrics (SM_CXICON
);
2509 cxWidth
= frame
->width
;
2513 if (flags
& DI_DEFAULTSIZE
)
2514 cyWidth
= GetSystemMetrics (SM_CYICON
);
2516 cyWidth
= frame
->height
;
2519 DoOffscreen
= (GetObjectType( hbr
) == OBJ_BRUSH
);
2524 SetRect(&r
, 0, 0, cxWidth
, cxWidth
);
2526 if (!(hdc_dest
= CreateCompatibleDC(hdc
))) goto failed
;
2527 if (!(hB_off
= CreateCompatibleBitmap(hdc
, cxWidth
, cyWidth
)))
2529 DeleteDC( hdc_dest
);
2532 SelectObject(hdc_dest
, hB_off
);
2533 FillRect(hdc_dest
, &r
, hbr
);
2543 nStretchMode
= SetStretchBltMode (hdc
, STRETCH_DELETESCANS
);
2545 oldFg
= SetTextColor( hdc
, RGB(0,0,0) );
2546 oldBg
= SetBkColor( hdc
, RGB(255,255,255) );
2548 if (frame
->alpha
&& (flags
& DI_IMAGE
))
2550 BOOL alpha_blend
= TRUE
;
2552 if (GetObjectType( hdc_dest
) == OBJ_MEMDC
)
2555 HBITMAP bmp
= GetCurrentObject( hdc_dest
, OBJ_BITMAP
);
2556 alpha_blend
= GetObjectW( bmp
, sizeof(bm
), &bm
) && bm
.bmBitsPixel
> 8;
2560 BLENDFUNCTION pixelblend
= { AC_SRC_OVER
, 0, 255, AC_SRC_ALPHA
};
2561 SelectObject( hMemDC
, frame
->alpha
);
2562 if (GdiAlphaBlend( hdc_dest
, x
, y
, cxWidth
, cyWidth
, hMemDC
,
2563 0, 0, frame
->width
, frame
->height
,
2564 pixelblend
)) goto done
;
2568 if (flags
& DI_MASK
)
2570 DWORD rop
= (flags
& DI_IMAGE
) ? SRCAND
: SRCCOPY
;
2571 SelectObject( hMemDC
, frame
->mask
);
2572 StretchBlt( hdc_dest
, x
, y
, cxWidth
, cyWidth
,
2573 hMemDC
, 0, 0, frame
->width
, frame
->height
, rop
);
2576 if (flags
& DI_IMAGE
)
2580 DWORD rop
= (flags
& DI_MASK
) ? SRCINVERT
: SRCCOPY
;
2581 SelectObject( hMemDC
, frame
->color
);
2582 StretchBlt( hdc_dest
, x
, y
, cxWidth
, cyWidth
,
2583 hMemDC
, 0, 0, frame
->width
, frame
->height
, rop
);
2587 DWORD rop
= (flags
& DI_MASK
) ? SRCINVERT
: SRCCOPY
;
2588 SelectObject( hMemDC
, frame
->mask
);
2589 StretchBlt( hdc_dest
, x
, y
, cxWidth
, cyWidth
,
2590 hMemDC
, 0, frame
->height
, frame
->width
,
2591 frame
->height
, rop
);
2596 if (DoOffscreen
) BitBlt( hdc
, x0
, y0
, cxWidth
, cyWidth
, hdc_dest
, 0, 0, SRCCOPY
);
2598 SetTextColor( hdc
, oldFg
);
2599 SetBkColor( hdc
, oldBg
);
2600 SetStretchBltMode (hdc
, nStretchMode
);
2602 if (hdc_dest
!= hdc
) DeleteDC( hdc_dest
);
2603 if (hB_off
) DeleteObject(hB_off
);
2606 release_icon_frame( ptr
, frame
);
2607 release_user_handle_ptr( ptr
);
2611 /***********************************************************************
2612 * DIB_FixColorsToLoadflags
2614 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2617 static void DIB_FixColorsToLoadflags(BITMAPINFO
* bmi
, UINT loadflags
, BYTE pix
)
2620 COLORREF c_W
, c_S
, c_F
, c_L
, c_C
;
2629 if (((bitmap_type
= DIB_GetBitmapInfo((BITMAPINFOHEADER
*) bmi
, &width
, &height
, &bpp
, &compr
)) == -1))
2631 WARN_(resource
)("Invalid bitmap\n");
2635 if (bpp
> 8) return;
2637 if (bitmap_type
== 0) /* BITMAPCOREHEADER */
2645 colors
= bmi
->bmiHeader
.biClrUsed
;
2646 if (colors
> 256) colors
= 256;
2647 if (!colors
&& (bpp
<= 8)) colors
= 1 << bpp
;
2650 c_W
= GetSysColor(COLOR_WINDOW
);
2651 c_S
= GetSysColor(COLOR_3DSHADOW
);
2652 c_F
= GetSysColor(COLOR_3DFACE
);
2653 c_L
= GetSysColor(COLOR_3DLIGHT
);
2655 if (loadflags
& LR_LOADTRANSPARENT
) {
2657 case 1: pix
= pix
>> 7; break;
2658 case 4: pix
= pix
>> 4; break;
2661 WARN_(resource
)("(%d): Unsupported depth\n", bpp
);
2664 if (pix
>= colors
) {
2665 WARN_(resource
)("pixel has color index greater than biClrUsed!\n");
2668 if (loadflags
& LR_LOADMAP3DCOLORS
) c_W
= c_F
;
2669 ptr
= (RGBQUAD
*)((char*)bmi
->bmiColors
+pix
*incr
);
2670 ptr
->rgbBlue
= GetBValue(c_W
);
2671 ptr
->rgbGreen
= GetGValue(c_W
);
2672 ptr
->rgbRed
= GetRValue(c_W
);
2674 if (loadflags
& LR_LOADMAP3DCOLORS
)
2675 for (i
=0; i
<colors
; i
++) {
2676 ptr
= (RGBQUAD
*)((char*)bmi
->bmiColors
+i
*incr
);
2677 c_C
= RGB(ptr
->rgbRed
, ptr
->rgbGreen
, ptr
->rgbBlue
);
2678 if (c_C
== RGB(128, 128, 128)) {
2679 ptr
->rgbRed
= GetRValue(c_S
);
2680 ptr
->rgbGreen
= GetGValue(c_S
);
2681 ptr
->rgbBlue
= GetBValue(c_S
);
2682 } else if (c_C
== RGB(192, 192, 192)) {
2683 ptr
->rgbRed
= GetRValue(c_F
);
2684 ptr
->rgbGreen
= GetGValue(c_F
);
2685 ptr
->rgbBlue
= GetBValue(c_F
);
2686 } else if (c_C
== RGB(223, 223, 223)) {
2687 ptr
->rgbRed
= GetRValue(c_L
);
2688 ptr
->rgbGreen
= GetGValue(c_L
);
2689 ptr
->rgbBlue
= GetBValue(c_L
);
2695 /**********************************************************************
2698 static HBITMAP
BITMAP_Load( HINSTANCE instance
, LPCWSTR name
,
2699 INT desiredx
, INT desiredy
, UINT loadflags
)
2701 HBITMAP hbitmap
= 0, orig_bm
;
2704 const char *ptr
= NULL
;
2705 BITMAPINFO
*info
, *fix_info
= NULL
, *scaled_info
= NULL
;
2709 LONG width
, height
, new_width
, new_height
;
2711 DWORD compr_dummy
, offbits
= 0;
2713 HDC screen_mem_dc
= NULL
;
2715 if (!(loadflags
& LR_LOADFROMFILE
))
2719 /* OEM bitmap: try to load the resource from user32.dll */
2720 instance
= user32_module
;
2723 if (!(hRsrc
= FindResourceW( instance
, name
, (LPWSTR
)RT_BITMAP
))) return 0;
2724 if (!(handle
= LoadResource( instance
, hRsrc
))) return 0;
2726 if ((info
= LockResource( handle
)) == NULL
) return 0;
2730 BITMAPFILEHEADER
* bmfh
;
2732 if (!(ptr
= map_fileW( name
, NULL
))) return 0;
2733 info
= (BITMAPINFO
*)(ptr
+ sizeof(BITMAPFILEHEADER
));
2734 bmfh
= (BITMAPFILEHEADER
*)ptr
;
2735 if (bmfh
->bfType
!= 0x4d42 /* 'BM' */)
2737 WARN("Invalid/unsupported bitmap format!\n");
2740 if (bmfh
->bfOffBits
) offbits
= bmfh
->bfOffBits
- sizeof(BITMAPFILEHEADER
);
2743 bm_type
= DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
,
2744 &bpp_dummy
, &compr_dummy
);
2747 WARN("Invalid bitmap format!\n");
2751 size
= bitmap_info_size(info
, DIB_RGB_COLORS
);
2752 fix_info
= HeapAlloc(GetProcessHeap(), 0, size
);
2753 scaled_info
= HeapAlloc(GetProcessHeap(), 0, size
);
2755 if (!fix_info
|| !scaled_info
) goto end
;
2756 memcpy(fix_info
, info
, size
);
2758 pix
= *((LPBYTE
)info
+ size
);
2759 DIB_FixColorsToLoadflags(fix_info
, loadflags
, pix
);
2761 memcpy(scaled_info
, fix_info
, size
);
2764 new_width
= desiredx
;
2769 new_height
= height
> 0 ? desiredy
: -desiredy
;
2771 new_height
= height
;
2775 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)&scaled_info
->bmiHeader
;
2776 core
->bcWidth
= new_width
;
2777 core
->bcHeight
= new_height
;
2781 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2782 if (info
->bmiHeader
.biHeight
> 65535 || info
->bmiHeader
.biWidth
> 65535) {
2783 WARN("Broken BitmapInfoHeader!\n");
2787 scaled_info
->bmiHeader
.biWidth
= new_width
;
2788 scaled_info
->bmiHeader
.biHeight
= new_height
;
2791 if (new_height
< 0) new_height
= -new_height
;
2793 if (!(screen_mem_dc
= CreateCompatibleDC( 0 ))) goto end
;
2795 bits
= (char *)info
+ (offbits
? offbits
: size
);
2797 if (loadflags
& LR_CREATEDIBSECTION
)
2799 scaled_info
->bmiHeader
.biCompression
= 0; /* DIBSection can't be compressed */
2800 hbitmap
= CreateDIBSection(0, scaled_info
, DIB_RGB_COLORS
, NULL
, 0, 0);
2804 if (is_dib_monochrome(fix_info
))
2805 hbitmap
= CreateBitmap(new_width
, new_height
, 1, 1, NULL
);
2807 hbitmap
= create_color_bitmap(new_width
, new_height
);
2810 orig_bm
= SelectObject(screen_mem_dc
, hbitmap
);
2811 if (info
->bmiHeader
.biBitCount
> 1)
2812 SetStretchBltMode(screen_mem_dc
, HALFTONE
);
2813 StretchDIBits(screen_mem_dc
, 0, 0, new_width
, new_height
, 0, 0, width
, height
, bits
, fix_info
, DIB_RGB_COLORS
, SRCCOPY
);
2814 SelectObject(screen_mem_dc
, orig_bm
);
2817 if (screen_mem_dc
) DeleteDC(screen_mem_dc
);
2818 HeapFree(GetProcessHeap(), 0, scaled_info
);
2819 HeapFree(GetProcessHeap(), 0, fix_info
);
2820 if (loadflags
& LR_LOADFROMFILE
) UnmapViewOfFile( ptr
);
2825 /**********************************************************************
2826 * LoadImageA (USER32.@)
2830 HANDLE WINAPI
LoadImageA( HINSTANCE hinst
, LPCSTR name
, UINT type
,
2831 INT desiredx
, INT desiredy
, UINT loadflags
)
2836 if (IS_INTRESOURCE(name
))
2837 return LoadImageW(hinst
, (LPCWSTR
)name
, type
, desiredx
, desiredy
, loadflags
);
2840 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
2841 u_name
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
2842 MultiByteToWideChar( CP_ACP
, 0, name
, -1, u_name
, len
);
2844 __EXCEPT_PAGE_FAULT
{
2845 SetLastError( ERROR_INVALID_PARAMETER
);
2849 res
= LoadImageW(hinst
, u_name
, type
, desiredx
, desiredy
, loadflags
);
2850 HeapFree(GetProcessHeap(), 0, u_name
);
2855 /******************************************************************************
2856 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2859 * hinst [I] Handle of instance that contains image
2860 * name [I] Name of image
2861 * type [I] Type of image
2862 * desiredx [I] Desired width
2863 * desiredy [I] Desired height
2864 * loadflags [I] Load flags
2867 * Success: Handle to newly loaded image
2870 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2872 HANDLE WINAPI
LoadImageW( HINSTANCE hinst
, LPCWSTR name
, UINT type
,
2873 INT desiredx
, INT desiredy
, UINT loadflags
)
2876 WCHAR path
[MAX_PATH
];
2878 TRACE_(resource
)("(%p,%s,%d,%d,%d,0x%08x)\n",
2879 hinst
,debugstr_w(name
),type
,desiredx
,desiredy
,loadflags
);
2881 if (loadflags
& LR_LOADFROMFILE
)
2883 loadflags
&= ~LR_SHARED
;
2884 /* relative paths are not only relative to the current working directory */
2885 if (SearchPathW(NULL
, name
, NULL
, ARRAY_SIZE(path
), path
, NULL
)) name
= path
;
2889 return BITMAP_Load( hinst
, name
, desiredx
, desiredy
, loadflags
);
2894 if (!(loadflags
& LR_MONOCHROME
)) depth
= get_display_bpp();
2895 return CURSORICON_Load(hinst
, name
, desiredx
, desiredy
, depth
, (type
== IMAGE_CURSOR
), loadflags
);
2901 /* StretchBlt from src to dest; helper for CopyImage(). */
2902 static void stretch_bitmap( HBITMAP dst
, HBITMAP src
, int dst_width
, int dst_height
, int src_width
, int src_height
)
2904 HDC src_dc
= CreateCompatibleDC( 0 ), dst_dc
= CreateCompatibleDC( 0 );
2906 SelectObject( src_dc
, src
);
2907 SelectObject( dst_dc
, dst
);
2908 StretchBlt( dst_dc
, 0, 0, dst_width
, dst_height
, src_dc
, 0, 0, src_width
, src_height
, SRCCOPY
);
2915 /******************************************************************************
2916 * CopyImage (USER32.@) Creates new image and copies attributes to it
2919 * hnd [I] Handle to image to copy
2920 * type [I] Type of image to copy
2921 * desiredx [I] Desired width of new image
2922 * desiredy [I] Desired height of new image
2923 * flags [I] Copy flags
2926 * Success: Handle to newly created image
2930 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2931 * all other versions (95/2000/XP have been tested) ignore it.
2934 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2935 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2936 * the copy will have the same depth as the screen.
2937 * The content of the image will only be copied if the bit depth of the
2938 * original image is compatible with the bit depth of the screen, or
2939 * if the source is a DIB section.
2940 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2942 HANDLE WINAPI
CopyImage( HANDLE hnd
, UINT type
, INT desiredx
,
2943 INT desiredy
, UINT flags
)
2945 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2946 hnd
, type
, desiredx
, desiredy
, flags
);
2957 objSize
= GetObjectW( hnd
, sizeof(ds
), &ds
);
2958 if (!objSize
) return 0;
2959 if ((desiredx
< 0) || (desiredy
< 0)) return 0;
2961 if (flags
& LR_COPYFROMRESOURCE
)
2963 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2966 if (desiredx
== 0) desiredx
= ds
.dsBm
.bmWidth
;
2967 if (desiredy
== 0) desiredy
= ds
.dsBm
.bmHeight
;
2969 /* Allocate memory for a BITMAPINFOHEADER structure and a
2970 color table. The maximum number of colors in a color table
2971 is 256 which corresponds to a bitmap with depth 8.
2972 Bitmaps with higher depths don't have color tables. */
2973 bi
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
2976 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
2977 bi
->bmiHeader
.biPlanes
= ds
.dsBm
.bmPlanes
;
2978 bi
->bmiHeader
.biBitCount
= ds
.dsBm
.bmBitsPixel
;
2979 bi
->bmiHeader
.biCompression
= BI_RGB
;
2981 if (flags
& LR_CREATEDIBSECTION
)
2983 /* Create a DIB section. LR_MONOCHROME is ignored */
2985 HDC dc
= CreateCompatibleDC(NULL
);
2987 if (objSize
== sizeof(DIBSECTION
))
2989 /* The source bitmap is a DIB.
2990 Get its attributes to create an exact copy */
2991 memcpy(bi
, &ds
.dsBmih
, sizeof(BITMAPINFOHEADER
));
2994 bi
->bmiHeader
.biWidth
= desiredx
;
2995 bi
->bmiHeader
.biHeight
= desiredy
;
2997 /* Get the color table or the color masks */
2998 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
3000 res
= CreateDIBSection(dc
, bi
, DIB_RGB_COLORS
, &bits
, NULL
, 0);
3005 /* Create a device-dependent bitmap */
3007 BOOL monochrome
= (flags
& LR_MONOCHROME
);
3009 if (objSize
== sizeof(DIBSECTION
))
3011 /* The source bitmap is a DIB section.
3012 Get its attributes */
3013 HDC dc
= CreateCompatibleDC(NULL
);
3014 bi
->bmiHeader
.biWidth
= ds
.dsBm
.bmWidth
;
3015 bi
->bmiHeader
.biHeight
= ds
.dsBm
.bmHeight
;
3016 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
3019 if (!monochrome
&& ds
.dsBm
.bmBitsPixel
== 1)
3021 /* Look if the colors of the DIB are black and white */
3024 (bi
->bmiColors
[0].rgbRed
== 0xff
3025 && bi
->bmiColors
[0].rgbGreen
== 0xff
3026 && bi
->bmiColors
[0].rgbBlue
== 0xff
3027 && bi
->bmiColors
[0].rgbReserved
== 0
3028 && bi
->bmiColors
[1].rgbRed
== 0
3029 && bi
->bmiColors
[1].rgbGreen
== 0
3030 && bi
->bmiColors
[1].rgbBlue
== 0
3031 && bi
->bmiColors
[1].rgbReserved
== 0)
3033 (bi
->bmiColors
[0].rgbRed
== 0
3034 && bi
->bmiColors
[0].rgbGreen
== 0
3035 && bi
->bmiColors
[0].rgbBlue
== 0
3036 && bi
->bmiColors
[0].rgbReserved
== 0
3037 && bi
->bmiColors
[1].rgbRed
== 0xff
3038 && bi
->bmiColors
[1].rgbGreen
== 0xff
3039 && bi
->bmiColors
[1].rgbBlue
== 0xff
3040 && bi
->bmiColors
[1].rgbReserved
== 0);
3043 else if (!monochrome
)
3045 monochrome
= ds
.dsBm
.bmBitsPixel
== 1;
3049 res
= CreateBitmap(desiredx
, desiredy
, 1, 1, NULL
);
3051 res
= create_color_bitmap(desiredx
, desiredy
);
3056 /* Only copy the bitmap if it's a DIB section or if it's
3057 compatible to the screen */
3058 if (objSize
== sizeof(DIBSECTION
) ||
3059 ds
.dsBm
.bmBitsPixel
== 1 ||
3060 ds
.dsBm
.bmBitsPixel
== get_display_bpp())
3062 /* The source bitmap may already be selected in a device context,
3063 use GetDIBits/StretchDIBits and not StretchBlt */
3068 dc
= CreateCompatibleDC(NULL
);
3069 if (ds
.dsBm
.bmBitsPixel
> 1)
3070 SetStretchBltMode(dc
, HALFTONE
);
3072 bi
->bmiHeader
.biWidth
= ds
.dsBm
.bmWidth
;
3073 bi
->bmiHeader
.biHeight
= ds
.dsBm
.bmHeight
;
3074 bi
->bmiHeader
.biSizeImage
= 0;
3075 bi
->bmiHeader
.biClrUsed
= 0;
3076 bi
->bmiHeader
.biClrImportant
= 0;
3078 /* Fill in biSizeImage */
3079 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
3080 bits
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, bi
->bmiHeader
.biSizeImage
);
3086 /* Get the image bits of the source bitmap */
3087 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, bits
, bi
, DIB_RGB_COLORS
);
3089 /* Copy it to the destination bitmap */
3090 oldBmp
= SelectObject(dc
, res
);
3091 StretchDIBits(dc
, 0, 0, desiredx
, desiredy
,
3092 0, 0, ds
.dsBm
.bmWidth
, ds
.dsBm
.bmHeight
,
3093 bits
, bi
, DIB_RGB_COLORS
, SRCCOPY
);
3094 SelectObject(dc
, oldBmp
);
3096 HeapFree(GetProcessHeap(), 0, bits
);
3102 if (flags
& LR_COPYDELETEORG
)
3107 HeapFree(GetProcessHeap(), 0, bi
);
3113 struct cursoricon_frame
*frame
;
3114 struct cursoricon_object
*icon
;
3115 int depth
= (flags
& LR_MONOCHROME
) ? 1 : get_display_bpp();
3116 HICON resource_icon
= NULL
;
3121 if (!(icon
= get_icon_ptr( hnd
))) return 0;
3123 if (icon
->rsrc
&& (flags
& LR_COPYFROMRESOURCE
) && icon
->module
.Length
&&
3124 !LdrGetDllHandle( NULL
, 0, &icon
->module
, &module
))
3126 resource_icon
= CURSORICON_Load( module
, icon
->resname
, desiredx
,
3127 desiredy
, depth
, !icon
->is_icon
, flags
);
3128 release_user_handle_ptr( icon
);
3129 if (!(icon
= get_icon_ptr( resource_icon
)))
3131 if (resource_icon
) DestroyIcon( resource_icon
);
3135 frame
= get_icon_frame( icon
, 0 );
3137 if (flags
& LR_DEFAULTSIZE
)
3139 if (!desiredx
) desiredx
= GetSystemMetrics( type
== IMAGE_ICON
? SM_CXICON
: SM_CXCURSOR
);
3140 if (!desiredy
) desiredy
= GetSystemMetrics( type
== IMAGE_ICON
? SM_CYICON
: SM_CYCURSOR
);
3144 if (!desiredx
) desiredx
= frame
->width
;
3145 if (!desiredy
) desiredy
= frame
->height
;
3148 info
.fIcon
= icon
->is_icon
;
3149 info
.xHotspot
= frame
->hotspot
.x
;
3150 info
.yHotspot
= frame
->hotspot
.y
;
3152 if (desiredx
== frame
->width
&& desiredy
== frame
->height
)
3154 info
.hbmColor
= frame
->color
;
3155 info
.hbmMask
= frame
->mask
;
3156 res
= CreateIconIndirect( &info
);
3162 if (!(info
.hbmColor
= create_color_bitmap( desiredx
, desiredy
)))
3164 release_icon_frame( icon
, frame
);
3165 release_user_handle_ptr( icon
);
3166 if (resource_icon
) DestroyIcon( resource_icon
);
3169 stretch_bitmap( info
.hbmColor
, frame
->color
, desiredx
, desiredy
,
3170 frame
->width
, frame
->height
);
3172 if (!(info
.hbmMask
= CreateBitmap( desiredx
, desiredy
, 1, 1, NULL
)))
3174 DeleteObject( info
.hbmColor
);
3175 release_icon_frame( icon
, frame
);
3176 release_user_handle_ptr( icon
);
3177 if (resource_icon
) DestroyIcon( resource_icon
);
3180 stretch_bitmap( info
.hbmMask
, frame
->mask
, desiredx
, desiredy
,
3181 frame
->width
, frame
->height
);
3185 info
.hbmColor
= NULL
;
3187 if (!(info
.hbmMask
= CreateBitmap( desiredx
, desiredy
* 2, 1, 1, NULL
)))
3189 release_user_handle_ptr( icon
);
3190 if (resource_icon
) DestroyIcon( resource_icon
);
3193 stretch_bitmap( info
.hbmMask
, frame
->mask
, desiredx
, desiredy
* 2,
3194 frame
->width
, frame
->height
* 2 );
3197 res
= CreateIconIndirect( &info
);
3199 DeleteObject( info
.hbmColor
);
3200 DeleteObject( info
.hbmMask
);
3203 release_icon_frame( icon
, frame
);
3204 release_user_handle_ptr( icon
);
3206 if (res
&& (flags
& LR_COPYDELETEORG
)) DestroyIcon( hnd
);
3207 if (resource_icon
) DestroyIcon( resource_icon
);
3215 /******************************************************************************
3216 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
3219 * Success: Handle to specified bitmap
3222 HBITMAP WINAPI
LoadBitmapW(
3223 HINSTANCE instance
, /* [in] Handle to application instance */
3224 LPCWSTR name
) /* [in] Address of bitmap resource name */
3226 return LoadImageW( instance
, name
, IMAGE_BITMAP
, 0, 0, 0 );
3229 /**********************************************************************
3230 * LoadBitmapA (USER32.@)
3234 HBITMAP WINAPI
LoadBitmapA( HINSTANCE instance
, LPCSTR name
)
3236 return LoadImageA( instance
, name
, IMAGE_BITMAP
, 0, 0, 0 );