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
26 #include "wine/port.h"
38 #include "wine/exception.h"
39 #include "wine/server.h"
42 #include "user_private.h"
43 #include "wine/list.h"
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(cursor
);
48 WINE_DECLARE_DEBUG_CHANNEL(icon
);
49 WINE_DECLARE_DEBUG_CHANNEL(resource
);
62 } CURSORICONFILEDIRENTRY
;
69 CURSORICONFILEDIRENTRY idEntries
[1];
76 static const WCHAR DISPLAYW
[] = {'D','I','S','P','L','A','Y',0};
78 static struct list icon_cache
= LIST_INIT( icon_cache
);
80 /**********************************************************************
81 * User objects management
84 struct cursoricon_frame
86 UINT width
; /* frame-specific width */
87 UINT height
; /* frame-specific height */
88 UINT delay
; /* frame-specific delay between this frame and the next (in jiffies) */
89 HBITMAP color
; /* color bitmap */
90 HBITMAP alpha
; /* pre-multiplied alpha bitmap for 32-bpp icons */
91 HBITMAP mask
; /* mask bitmap (followed by color for 1-bpp icons) */
94 struct cursoricon_object
96 struct user_object obj
; /* object header */
97 struct list entry
; /* entry in shared icons list */
98 ULONG_PTR param
; /* opaque param used by 16-bit code */
99 HMODULE module
; /* module for icons loaded from resources */
100 LPWSTR resname
; /* resource name for icons loaded from resources */
101 HRSRC rsrc
; /* resource for shared icons */
102 BOOL is_icon
; /* whether icon or cursor */
103 BOOL is_ani
; /* whether this object is a static cursor or an animated cursor */
104 UINT delay
; /* delay between this frame and the next (in jiffies) */
108 struct static_cursoricon_object
110 struct cursoricon_object shared
;
111 struct cursoricon_frame frame
; /* frame-specific icon data */
114 struct animated_cursoricon_object
116 struct cursoricon_object shared
;
117 UINT num_frames
; /* number of frames in the icon/cursor */
118 UINT num_steps
; /* number of sequence steps in the icon/cursor */
119 HICON frames
[1]; /* list of animated cursor frames */
122 static HICON
alloc_icon_handle( BOOL is_ani
, UINT num_steps
)
124 struct cursoricon_object
*obj
;
128 icon_size
= FIELD_OFFSET( struct animated_cursoricon_object
, frames
[num_steps
] );
130 icon_size
= sizeof( struct static_cursoricon_object
);
131 obj
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, icon_size
);
134 obj
->is_ani
= is_ani
;
137 struct animated_cursoricon_object
*ani_icon_data
= (struct animated_cursoricon_object
*) obj
;
139 ani_icon_data
->num_steps
= num_steps
;
140 ani_icon_data
->num_frames
= num_steps
; /* changed later for some animated cursors */
142 return alloc_user_handle( &obj
->obj
, USER_ICON
);
145 static struct cursoricon_object
*get_icon_ptr( HICON handle
)
147 struct cursoricon_object
*obj
= get_user_handle_ptr( handle
, USER_ICON
);
148 if (obj
== OBJ_OTHER_PROCESS
)
150 WARN( "icon handle %p from other process\n", handle
);
156 static void release_icon_ptr( HICON handle
, struct cursoricon_object
*ptr
)
158 release_user_handle_ptr( ptr
);
161 static struct cursoricon_frame
*get_icon_frame( struct cursoricon_object
*obj
, int istep
)
163 struct static_cursoricon_object
*req_frame
;
167 struct animated_cursoricon_object
*ani_icon_data
;
168 struct cursoricon_object
*frameobj
;
170 ani_icon_data
= (struct animated_cursoricon_object
*) obj
;
171 if (!(frameobj
= get_icon_ptr( ani_icon_data
->frames
[istep
] )))
173 req_frame
= (struct static_cursoricon_object
*) frameobj
;
176 req_frame
= (struct static_cursoricon_object
*) obj
;
178 return &req_frame
->frame
;
181 static void release_icon_frame( struct cursoricon_object
*obj
, int istep
, struct cursoricon_frame
*frame
)
185 struct animated_cursoricon_object
*ani_icon_data
;
186 struct cursoricon_object
*frameobj
;
188 ani_icon_data
= (struct animated_cursoricon_object
*) obj
;
189 frameobj
= (struct cursoricon_object
*) (((char *)frame
) - FIELD_OFFSET(struct static_cursoricon_object
, frame
));
190 release_icon_ptr( ani_icon_data
->frames
[istep
], frameobj
);
194 static UINT
get_icon_steps( struct cursoricon_object
*obj
)
198 struct animated_cursoricon_object
*ani_icon_data
;
200 ani_icon_data
= (struct animated_cursoricon_object
*) obj
;
201 return ani_icon_data
->num_steps
;
206 static BOOL
free_icon_handle( HICON handle
)
208 struct cursoricon_object
*obj
= free_user_handle( handle
, USER_ICON
);
210 if (obj
== OBJ_OTHER_PROCESS
) WARN( "icon handle %p from other process\n", handle
);
213 ULONG_PTR param
= obj
->param
;
216 assert( !obj
->rsrc
); /* shared icons can't be freed */
220 struct cursoricon_frame
*frame
= get_icon_frame( obj
, 0 );
222 if (frame
->alpha
) DeleteObject( frame
->alpha
);
223 if (frame
->color
) DeleteObject( frame
->color
);
224 DeleteObject( frame
->mask
);
225 release_icon_frame( obj
, 0, frame
);
229 struct animated_cursoricon_object
*ani_icon_data
= (struct animated_cursoricon_object
*) obj
;
231 for (i
=0; i
<ani_icon_data
->num_steps
; i
++)
233 HICON hFrame
= ani_icon_data
->frames
[i
];
239 free_icon_handle( ani_icon_data
->frames
[i
] );
240 for (j
=0; j
<ani_icon_data
->num_steps
; j
++)
242 if (ani_icon_data
->frames
[j
] == hFrame
)
243 ani_icon_data
->frames
[j
] = 0;
248 if (!IS_INTRESOURCE( obj
->resname
)) HeapFree( GetProcessHeap(), 0, obj
->resname
);
249 HeapFree( GetProcessHeap(), 0, obj
);
250 if (wow_handlers
.free_icon_param
&& param
) wow_handlers
.free_icon_param( param
);
251 USER_Driver
->pDestroyCursorIcon( handle
);
257 ULONG_PTR
get_icon_param( HICON handle
)
260 struct cursoricon_object
*obj
= get_user_handle_ptr( handle
, USER_ICON
);
262 if (obj
== OBJ_OTHER_PROCESS
) WARN( "icon handle %p from other process\n", handle
);
266 release_user_handle_ptr( obj
);
271 ULONG_PTR
set_icon_param( HICON handle
, ULONG_PTR param
)
274 struct cursoricon_object
*obj
= get_user_handle_ptr( handle
, USER_ICON
);
276 if (obj
== OBJ_OTHER_PROCESS
) WARN( "icon handle %p from other process\n", handle
);
281 release_user_handle_ptr( obj
);
287 /***********************************************************************
290 * Helper function to map a file to memory:
292 * [RETURN] ptr - pointer to mapped file
293 * [RETURN] filesize - pointer size of file to be stored if not NULL
295 static const void *map_fileW( LPCWSTR name
, LPDWORD filesize
)
297 HANDLE hFile
, hMapping
;
300 hFile
= CreateFileW( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
301 OPEN_EXISTING
, FILE_FLAG_RANDOM_ACCESS
, 0 );
302 if (hFile
!= INVALID_HANDLE_VALUE
)
304 hMapping
= CreateFileMappingW( hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
307 ptr
= MapViewOfFile( hMapping
, FILE_MAP_READ
, 0, 0, 0 );
308 CloseHandle( hMapping
);
310 *filesize
= GetFileSize( hFile
, NULL
);
312 CloseHandle( hFile
);
318 /***********************************************************************
321 * Return the size of a DIB bitmap in bytes.
323 static int get_dib_image_size( int width
, int height
, int depth
)
325 return (((width
* depth
+ 31) / 8) & ~3) * abs( height
);
329 /***********************************************************************
332 * Return the size of the bitmap info structure including color table.
334 static int bitmap_info_size( const BITMAPINFO
* info
, WORD coloruse
)
336 unsigned int colors
, size
, masks
= 0;
338 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
340 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
341 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
342 return sizeof(BITMAPCOREHEADER
) + colors
*
343 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
345 else /* assume BITMAPINFOHEADER */
347 colors
= info
->bmiHeader
.biClrUsed
;
348 if (colors
> 256) /* buffer overflow otherwise */
350 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
351 colors
= 1 << info
->bmiHeader
.biBitCount
;
352 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
353 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
354 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
359 /***********************************************************************
362 * Helper function to duplicate a bitmap.
364 static HBITMAP
copy_bitmap( HBITMAP bitmap
)
367 HBITMAP new_bitmap
= 0;
370 if (!bitmap
) return 0;
371 if (!GetObjectW( bitmap
, sizeof(bmp
), &bmp
)) return 0;
373 if ((src
= CreateCompatibleDC( 0 )) && (dst
= CreateCompatibleDC( 0 )))
375 SelectObject( src
, bitmap
);
376 if ((new_bitmap
= CreateCompatibleBitmap( src
, bmp
.bmWidth
, bmp
.bmHeight
)))
378 SelectObject( dst
, new_bitmap
);
379 BitBlt( dst
, 0, 0, bmp
.bmWidth
, bmp
.bmHeight
, src
, 0, 0, SRCCOPY
);
388 /***********************************************************************
391 * Returns whether a DIB can be converted to a monochrome DDB.
393 * A DIB can be converted if its color table contains only black and
394 * white. Black must be the first color in the color table.
396 * Note : If the first color in the color table is white followed by
397 * black, we can't convert it to a monochrome DDB with
398 * SetDIBits, because black and white would be inverted.
400 static BOOL
is_dib_monochrome( const BITMAPINFO
* info
)
402 if (info
->bmiHeader
.biBitCount
!= 1) return FALSE
;
404 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
406 const RGBTRIPLE
*rgb
= ((const BITMAPCOREINFO
*)info
)->bmciColors
;
408 /* Check if the first color is black */
409 if ((rgb
->rgbtRed
== 0) && (rgb
->rgbtGreen
== 0) && (rgb
->rgbtBlue
== 0))
413 /* Check if the second color is white */
414 return ((rgb
->rgbtRed
== 0xff) && (rgb
->rgbtGreen
== 0xff)
415 && (rgb
->rgbtBlue
== 0xff));
419 else /* assume BITMAPINFOHEADER */
421 const RGBQUAD
*rgb
= info
->bmiColors
;
423 /* Check if the first color is black */
424 if ((rgb
->rgbRed
== 0) && (rgb
->rgbGreen
== 0) &&
425 (rgb
->rgbBlue
== 0) && (rgb
->rgbReserved
== 0))
429 /* Check if the second color is white */
430 return ((rgb
->rgbRed
== 0xff) && (rgb
->rgbGreen
== 0xff)
431 && (rgb
->rgbBlue
== 0xff) && (rgb
->rgbReserved
== 0));
437 /***********************************************************************
440 * Get the info from a bitmap header.
441 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
443 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
444 LONG
*height
, WORD
*bpp
, DWORD
*compr
)
446 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
448 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
449 *width
= core
->bcWidth
;
450 *height
= core
->bcHeight
;
451 *bpp
= core
->bcBitCount
;
455 else if (header
->biSize
== sizeof(BITMAPINFOHEADER
) ||
456 header
->biSize
== sizeof(BITMAPV4HEADER
) ||
457 header
->biSize
== sizeof(BITMAPV5HEADER
))
459 *width
= header
->biWidth
;
460 *height
= header
->biHeight
;
461 *bpp
= header
->biBitCount
;
462 *compr
= header
->biCompression
;
465 WARN("unknown/wrong size (%u) for header\n", header
->biSize
);
469 /**********************************************************************
472 BOOL
get_icon_size( HICON handle
, SIZE
*size
)
474 struct cursoricon_object
*info
;
475 struct cursoricon_frame
*frame
;
477 if (!(info
= get_icon_ptr( handle
))) return FALSE
;
478 frame
= get_icon_frame( info
, 0 );
479 size
->cx
= frame
->width
;
480 size
->cy
= frame
->height
;
481 release_icon_frame( info
, 0, frame
);
482 release_icon_ptr( handle
, info
);
487 * The following macro functions account for the irregularities of
488 * accessing cursor and icon resources in files and resource entries.
490 typedef BOOL (*fnGetCIEntry
)( LPCVOID dir
, DWORD size
, int n
,
491 int *width
, int *height
, int *bits
);
493 /**********************************************************************
494 * CURSORICON_FindBestIcon
496 * Find the icon closest to the requested size and bit depth.
498 static int CURSORICON_FindBestIcon( LPCVOID dir
, DWORD size
, fnGetCIEntry get_entry
,
499 int width
, int height
, int depth
, UINT loadflags
)
501 int i
, cx
, cy
, bits
, bestEntry
= -1;
502 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
503 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
506 iTotalDiff
= 0xFFFFFFFF;
507 iColorDiff
= 0xFFFFFFFF;
509 if (loadflags
& LR_DEFAULTSIZE
)
511 if (!width
) width
= GetSystemMetrics( SM_CXICON
);
512 if (!height
) height
= GetSystemMetrics( SM_CYICON
);
514 else if (!width
&& !height
)
516 /* use the size of the first entry */
517 if (!get_entry( dir
, size
, 0, &width
, &height
, &bits
)) return -1;
521 for ( i
= 0; iTotalDiff
&& get_entry( dir
, size
, i
, &cx
, &cy
, &bits
); i
++ )
523 iTempXDiff
= abs(width
- cx
);
524 iTempYDiff
= abs(height
- cy
);
526 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
530 iTotalDiff
= iXDiff
+ iYDiff
;
534 /* Find Best Colors for Best Fit */
535 for ( i
= 0; get_entry( dir
, size
, i
, &cx
, &cy
, &bits
); i
++ )
537 if(abs(width
- cx
) == iXDiff
&& abs(height
- cy
) == iYDiff
)
539 iTempColorDiff
= abs(depth
- bits
);
540 if(iColorDiff
> iTempColorDiff
)
543 iColorDiff
= iTempColorDiff
;
551 static BOOL
CURSORICON_GetResIconEntry( LPCVOID dir
, DWORD size
, int n
,
552 int *width
, int *height
, int *bits
)
554 const CURSORICONDIR
*resdir
= dir
;
555 const ICONRESDIR
*icon
;
557 if ( resdir
->idCount
<= n
)
559 if ((const char *)&resdir
->idEntries
[n
+ 1] - (const char *)dir
> size
)
561 icon
= &resdir
->idEntries
[n
].ResInfo
.icon
;
562 *width
= icon
->bWidth
;
563 *height
= icon
->bHeight
;
564 *bits
= resdir
->idEntries
[n
].wBitCount
;
568 /**********************************************************************
569 * CURSORICON_FindBestCursor
571 * Find the cursor closest to the requested size.
573 * FIXME: parameter 'color' ignored.
575 static int CURSORICON_FindBestCursor( LPCVOID dir
, DWORD size
, fnGetCIEntry get_entry
,
576 int width
, int height
, int depth
, UINT loadflags
)
578 int i
, maxwidth
, maxheight
, cx
, cy
, bits
, bestEntry
= -1;
580 if (loadflags
& LR_DEFAULTSIZE
)
582 if (!width
) width
= GetSystemMetrics( SM_CXCURSOR
);
583 if (!height
) height
= GetSystemMetrics( SM_CYCURSOR
);
585 else if (!width
&& !height
)
587 /* use the first entry */
588 if (!get_entry( dir
, size
, 0, &width
, &height
, &bits
)) return -1;
592 /* Double height to account for AND and XOR masks */
596 /* First find the largest one smaller than or equal to the requested size*/
598 maxwidth
= maxheight
= 0;
599 for ( i
= 0; get_entry( dir
, size
, i
, &cx
, &cy
, &bits
); i
++ )
601 if ((cx
<= width
) && (cy
<= height
) &&
602 (cx
> maxwidth
) && (cy
> maxheight
))
609 if (bestEntry
!= -1) return bestEntry
;
611 /* Now find the smallest one larger than the requested size */
613 maxwidth
= maxheight
= 255;
614 for ( i
= 0; get_entry( dir
, size
, i
, &cx
, &cy
, &bits
); i
++ )
616 if (((cx
< maxwidth
) && (cy
< maxheight
)) || (bestEntry
== -1))
627 static BOOL
CURSORICON_GetResCursorEntry( LPCVOID dir
, DWORD size
, int n
,
628 int *width
, int *height
, int *bits
)
630 const CURSORICONDIR
*resdir
= dir
;
631 const CURSORDIR
*cursor
;
633 if ( resdir
->idCount
<= n
)
635 if ((const char *)&resdir
->idEntries
[n
+ 1] - (const char *)dir
> size
)
637 cursor
= &resdir
->idEntries
[n
].ResInfo
.cursor
;
638 *width
= cursor
->wWidth
;
639 *height
= cursor
->wHeight
;
640 *bits
= resdir
->idEntries
[n
].wBitCount
;
644 static const CURSORICONDIRENTRY
*CURSORICON_FindBestIconRes( const CURSORICONDIR
* dir
, DWORD size
,
645 int width
, int height
, int depth
,
650 n
= CURSORICON_FindBestIcon( dir
, size
, CURSORICON_GetResIconEntry
,
651 width
, height
, depth
, loadflags
);
654 return &dir
->idEntries
[n
];
657 static const CURSORICONDIRENTRY
*CURSORICON_FindBestCursorRes( const CURSORICONDIR
*dir
, DWORD size
,
658 int width
, int height
, int depth
,
661 int n
= CURSORICON_FindBestCursor( dir
, size
, CURSORICON_GetResCursorEntry
,
662 width
, height
, depth
, loadflags
);
665 return &dir
->idEntries
[n
];
668 static BOOL
CURSORICON_GetFileEntry( LPCVOID dir
, DWORD size
, int n
,
669 int *width
, int *height
, int *bits
)
671 const CURSORICONFILEDIR
*filedir
= dir
;
672 const CURSORICONFILEDIRENTRY
*entry
;
673 const BITMAPINFOHEADER
*info
;
675 if ( filedir
->idCount
<= n
)
677 if ((const char *)&filedir
->idEntries
[n
+ 1] - (const char *)dir
> size
)
679 entry
= &filedir
->idEntries
[n
];
680 info
= (const BITMAPINFOHEADER
*)((const char *)dir
+ entry
->dwDIBOffset
);
681 if ((const char *)(info
+ 1) - (const char *)dir
> size
) return FALSE
;
682 *width
= entry
->bWidth
;
683 *height
= entry
->bHeight
;
684 *bits
= info
->biBitCount
;
688 static const CURSORICONFILEDIRENTRY
*CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR
*dir
, DWORD size
,
689 int width
, int height
, int depth
,
692 int n
= CURSORICON_FindBestCursor( dir
, size
, CURSORICON_GetFileEntry
,
693 width
, height
, depth
, loadflags
);
696 return &dir
->idEntries
[n
];
699 static const CURSORICONFILEDIRENTRY
*CURSORICON_FindBestIconFile( const CURSORICONFILEDIR
*dir
, DWORD size
,
700 int width
, int height
, int depth
,
703 int n
= CURSORICON_FindBestIcon( dir
, size
, CURSORICON_GetFileEntry
,
704 width
, height
, depth
, loadflags
);
707 return &dir
->idEntries
[n
];
710 /***********************************************************************
713 static BOOL
bmi_has_alpha( const BITMAPINFO
*info
, const void *bits
)
716 BOOL has_alpha
= FALSE
;
717 const unsigned char *ptr
= bits
;
719 if (info
->bmiHeader
.biBitCount
!= 32) return FALSE
;
720 for (i
= 0; i
< info
->bmiHeader
.biWidth
* abs(info
->bmiHeader
.biHeight
); i
++, ptr
+= 4)
721 if ((has_alpha
= (ptr
[3] != 0))) break;
725 /***********************************************************************
726 * create_alpha_bitmap
728 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
730 static HBITMAP
create_alpha_bitmap( HBITMAP color
, HBITMAP mask
,
731 const BITMAPINFO
*src_info
, const void *color_bits
)
734 BITMAPINFO
*info
= NULL
;
741 if (!GetObjectW( color
, sizeof(bm
), &bm
)) return 0;
742 if (bm
.bmBitsPixel
!= 32) return 0;
744 if (!(hdc
= CreateCompatibleDC( 0 ))) return 0;
745 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )))) goto done
;
746 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
747 info
->bmiHeader
.biWidth
= bm
.bmWidth
;
748 info
->bmiHeader
.biHeight
= -bm
.bmHeight
;
749 info
->bmiHeader
.biPlanes
= 1;
750 info
->bmiHeader
.biBitCount
= 32;
751 info
->bmiHeader
.biCompression
= BI_RGB
;
752 info
->bmiHeader
.biSizeImage
= bm
.bmWidth
* bm
.bmHeight
* 4;
753 info
->bmiHeader
.biXPelsPerMeter
= 0;
754 info
->bmiHeader
.biYPelsPerMeter
= 0;
755 info
->bmiHeader
.biClrUsed
= 0;
756 info
->bmiHeader
.biClrImportant
= 0;
757 if (!(alpha
= CreateDIBSection( hdc
, info
, DIB_RGB_COLORS
, &bits
, NULL
, 0 ))) goto done
;
761 SelectObject( hdc
, alpha
);
762 StretchDIBits( hdc
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
763 0, 0, src_info
->bmiHeader
.biWidth
, src_info
->bmiHeader
.biHeight
,
764 color_bits
, src_info
, DIB_RGB_COLORS
, SRCCOPY
);
769 GetDIBits( hdc
, color
, 0, bm
.bmHeight
, bits
, info
, DIB_RGB_COLORS
);
770 if (!bmi_has_alpha( info
, bits
))
772 DeleteObject( alpha
);
778 /* pre-multiply by alpha */
779 for (i
= 0, ptr
= bits
; i
< bm
.bmWidth
* bm
.bmHeight
; i
++, ptr
+= 4)
781 unsigned int alpha
= ptr
[3];
782 ptr
[0] = ptr
[0] * alpha
/ 255;
783 ptr
[1] = ptr
[1] * alpha
/ 255;
784 ptr
[2] = ptr
[2] * alpha
/ 255;
789 HeapFree( GetProcessHeap(), 0, info
);
794 /***********************************************************************
795 * create_icon_from_bmi
797 * Create an icon from its BITMAPINFO.
799 static HICON
create_icon_from_bmi( const BITMAPINFO
*bmi
, DWORD maxsize
, HMODULE module
, LPCWSTR resname
,
800 HRSRC rsrc
, POINT hotspot
, BOOL bIcon
, INT width
, INT height
,
803 DWORD size
, color_size
, mask_size
;
804 HBITMAP color
= 0, mask
= 0, alpha
= 0;
805 const void *color_bits
, *mask_bits
;
806 BITMAPINFO
*bmi_copy
;
812 /* Check bitmap header */
814 if (maxsize
< sizeof(BITMAPCOREHEADER
))
816 WARN( "invalid size %u\n", maxsize
);
819 if (maxsize
< bmi
->bmiHeader
.biSize
)
821 WARN( "invalid header size %u\n", bmi
->bmiHeader
.biSize
);
824 if ( (bmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) &&
825 (bmi
->bmiHeader
.biSize
!= sizeof(BITMAPINFOHEADER
) ||
826 (bmi
->bmiHeader
.biCompression
!= BI_RGB
&&
827 bmi
->bmiHeader
.biCompression
!= BI_BITFIELDS
)) )
829 WARN( "invalid bitmap header %u\n", bmi
->bmiHeader
.biSize
);
833 size
= bitmap_info_size( bmi
, DIB_RGB_COLORS
);
834 color_size
= get_dib_image_size( bmi
->bmiHeader
.biWidth
, bmi
->bmiHeader
.biHeight
/ 2,
835 bmi
->bmiHeader
.biBitCount
);
836 mask_size
= get_dib_image_size( bmi
->bmiHeader
.biWidth
, bmi
->bmiHeader
.biHeight
/ 2, 1 );
837 if (size
> maxsize
|| color_size
> maxsize
- size
)
839 WARN( "truncated file %u < %u+%u+%u\n", maxsize
, size
, color_size
, mask_size
);
842 if (mask_size
> maxsize
- size
- color_size
) mask_size
= 0; /* no mask */
844 if (cFlag
& LR_DEFAULTSIZE
)
846 if (!width
) width
= GetSystemMetrics( bIcon
? SM_CXICON
: SM_CXCURSOR
);
847 if (!height
) height
= GetSystemMetrics( bIcon
? SM_CYICON
: SM_CYCURSOR
);
851 if (!width
) width
= bmi
->bmiHeader
.biWidth
;
852 if (!height
) height
= bmi
->bmiHeader
.biHeight
/2;
854 do_stretch
= (bmi
->bmiHeader
.biHeight
/2 != height
) ||
855 (bmi
->bmiHeader
.biWidth
!= width
);
857 /* Scale the hotspot */
860 hotspot
.x
= width
/ 2;
861 hotspot
.y
= height
/ 2;
865 hotspot
.x
= (hotspot
.x
* width
) / bmi
->bmiHeader
.biWidth
;
866 hotspot
.y
= (hotspot
.y
* height
) / (bmi
->bmiHeader
.biHeight
/ 2);
869 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
870 if (!screen_dc
) return 0;
872 if (!(bmi_copy
= HeapAlloc( GetProcessHeap(), 0, max( size
, FIELD_OFFSET( BITMAPINFO
, bmiColors
[2] )))))
874 if (!(hdc
= CreateCompatibleDC( 0 ))) goto done
;
876 memcpy( bmi_copy
, bmi
, size
);
877 bmi_copy
->bmiHeader
.biHeight
/= 2;
879 color_bits
= (const char*)bmi
+ size
;
880 mask_bits
= (const char*)color_bits
+ color_size
;
883 if (is_dib_monochrome( bmi
))
885 if (!(mask
= CreateBitmap( width
, height
* 2, 1, 1, NULL
))) goto done
;
888 /* copy color data into second half of mask bitmap */
889 SelectObject( hdc
, mask
);
890 StretchDIBits( hdc
, 0, height
, width
, height
,
891 0, 0, bmi_copy
->bmiHeader
.biWidth
, bmi_copy
->bmiHeader
.biHeight
,
892 color_bits
, bmi_copy
, DIB_RGB_COLORS
, SRCCOPY
);
896 if (!(mask
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
897 if (!(color
= CreateBitmap( width
, height
, GetDeviceCaps( screen_dc
, PLANES
),
898 GetDeviceCaps( screen_dc
, BITSPIXEL
), NULL
)))
900 DeleteObject( mask
);
903 SelectObject( hdc
, color
);
904 StretchDIBits( hdc
, 0, 0, width
, height
,
905 0, 0, bmi_copy
->bmiHeader
.biWidth
, bmi_copy
->bmiHeader
.biHeight
,
906 color_bits
, bmi_copy
, DIB_RGB_COLORS
, SRCCOPY
);
908 if (bmi_has_alpha( bmi_copy
, color_bits
))
909 alpha
= create_alpha_bitmap( color
, mask
, bmi_copy
, color_bits
);
911 /* convert info to monochrome to copy the mask */
912 bmi_copy
->bmiHeader
.biBitCount
= 1;
913 if (bmi_copy
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
))
915 RGBQUAD
*rgb
= bmi_copy
->bmiColors
;
917 bmi_copy
->bmiHeader
.biClrUsed
= bmi_copy
->bmiHeader
.biClrImportant
= 2;
918 rgb
[0].rgbBlue
= rgb
[0].rgbGreen
= rgb
[0].rgbRed
= 0x00;
919 rgb
[1].rgbBlue
= rgb
[1].rgbGreen
= rgb
[1].rgbRed
= 0xff;
920 rgb
[0].rgbReserved
= rgb
[1].rgbReserved
= 0;
924 RGBTRIPLE
*rgb
= (RGBTRIPLE
*)(((BITMAPCOREHEADER
*)bmi_copy
) + 1);
926 rgb
[0].rgbtBlue
= rgb
[0].rgbtGreen
= rgb
[0].rgbtRed
= 0x00;
927 rgb
[1].rgbtBlue
= rgb
[1].rgbtGreen
= rgb
[1].rgbtRed
= 0xff;
933 SelectObject( hdc
, mask
);
934 StretchDIBits( hdc
, 0, 0, width
, height
,
935 0, 0, bmi_copy
->bmiHeader
.biWidth
, bmi_copy
->bmiHeader
.biHeight
,
936 mask_bits
, bmi_copy
, DIB_RGB_COLORS
, SRCCOPY
);
942 HeapFree( GetProcessHeap(), 0, bmi_copy
);
945 hObj
= alloc_icon_handle( FALSE
, 1 );
948 struct cursoricon_object
*info
= get_icon_ptr( hObj
);
949 struct cursoricon_frame
*frame
;
951 info
->is_icon
= bIcon
;
952 info
->module
= module
;
953 info
->hotspot
= hotspot
;
954 frame
= get_icon_frame( info
, 0 );
956 frame
->width
= width
;
957 frame
->height
= height
;
958 frame
->color
= color
;
960 frame
->alpha
= alpha
;
961 release_icon_frame( info
, 0, frame
);
962 if (!IS_INTRESOURCE(resname
))
964 info
->resname
= HeapAlloc( GetProcessHeap(), 0, (strlenW(resname
) + 1) * sizeof(WCHAR
) );
965 if (info
->resname
) strcpyW( info
->resname
, resname
);
967 else info
->resname
= MAKEINTRESOURCEW( LOWORD(resname
) );
969 if (module
&& (cFlag
& LR_SHARED
))
972 list_add_head( &icon_cache
, &info
->entry
);
974 release_icon_ptr( hObj
, info
);
978 DeleteObject( color
);
979 DeleteObject( alpha
);
980 DeleteObject( mask
);
986 /**********************************************************************
987 * .ANI cursor support
989 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
990 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
991 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
993 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
994 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
995 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
996 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
997 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
998 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
999 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
1001 #define ANI_FLAG_ICON 0x1
1002 #define ANI_FLAG_SEQUENCE 0x2
1018 const unsigned char *data
;
1021 static void dump_ani_header( const ani_header
*header
)
1023 TRACE(" header size: %d\n", header
->header_size
);
1024 TRACE(" frames: %d\n", header
->num_frames
);
1025 TRACE(" steps: %d\n", header
->num_steps
);
1026 TRACE(" width: %d\n", header
->width
);
1027 TRACE(" height: %d\n", header
->height
);
1028 TRACE(" bpp: %d\n", header
->bpp
);
1029 TRACE(" planes: %d\n", header
->num_planes
);
1030 TRACE(" display rate: %d\n", header
->display_rate
);
1031 TRACE(" flags: 0x%08x\n", header
->flags
);
1053 static void riff_find_chunk( DWORD chunk_id
, DWORD chunk_type
, const riff_chunk_t
*parent_chunk
, riff_chunk_t
*chunk
)
1055 const unsigned char *ptr
= parent_chunk
->data
;
1056 const unsigned char *end
= parent_chunk
->data
+ (parent_chunk
->data_size
- (2 * sizeof(DWORD
)));
1058 if (chunk_type
== ANI_LIST_ID
|| chunk_type
== ANI_RIFF_ID
) end
-= sizeof(DWORD
);
1062 if ((!chunk_type
&& *(const DWORD
*)ptr
== chunk_id
)
1063 || (chunk_type
&& *(const DWORD
*)ptr
== chunk_type
&& *((const DWORD
*)ptr
+ 2) == chunk_id
))
1065 ptr
+= sizeof(DWORD
);
1066 chunk
->data_size
= (*(const DWORD
*)ptr
+ 1) & ~1;
1067 ptr
+= sizeof(DWORD
);
1068 if (chunk_type
== ANI_LIST_ID
|| chunk_type
== ANI_RIFF_ID
) ptr
+= sizeof(DWORD
);
1074 ptr
+= sizeof(DWORD
);
1075 ptr
+= (*(const DWORD
*)ptr
+ 1) & ~1;
1076 ptr
+= sizeof(DWORD
);
1084 * RIFF:'ACON' RIFF chunk
1085 * |- CHUNK:'anih' Header
1086 * |- CHUNK:'seq ' Sequence information (optional)
1087 * \- LIST:'fram' Frame list
1088 * |- CHUNK:icon Cursor frames
1093 static HCURSOR
CURSORICON_CreateIconFromANI( const BYTE
*bits
, DWORD bits_size
, INT width
, INT height
,
1094 INT depth
, BOOL is_icon
, UINT loadflags
)
1096 struct animated_cursoricon_object
*ani_icon_data
;
1097 struct cursoricon_object
*info
;
1098 DWORD
*frame_rates
= NULL
;
1099 DWORD
*frame_seq
= NULL
;
1100 ani_header header
= {0};
1101 BOOL use_seq
= FALSE
;
1107 riff_chunk_t root_chunk
= { bits_size
, bits
};
1108 riff_chunk_t ACON_chunk
= {0};
1109 riff_chunk_t anih_chunk
= {0};
1110 riff_chunk_t fram_chunk
= {0};
1111 riff_chunk_t rate_chunk
= {0};
1112 riff_chunk_t seq_chunk
= {0};
1113 const unsigned char *icon_chunk
;
1114 const unsigned char *icon_data
;
1116 TRACE("bits %p, bits_size %d\n", bits
, bits_size
);
1118 riff_find_chunk( ANI_ACON_ID
, ANI_RIFF_ID
, &root_chunk
, &ACON_chunk
);
1119 if (!ACON_chunk
.data
)
1121 ERR("Failed to get root chunk.\n");
1125 riff_find_chunk( ANI_anih_ID
, 0, &ACON_chunk
, &anih_chunk
);
1126 if (!anih_chunk
.data
)
1128 ERR("Failed to get 'anih' chunk.\n");
1131 memcpy( &header
, anih_chunk
.data
, sizeof(header
) );
1132 dump_ani_header( &header
);
1134 if (!(header
.flags
& ANI_FLAG_ICON
))
1136 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1140 if (header
.flags
& ANI_FLAG_SEQUENCE
)
1142 riff_find_chunk( ANI_seq__ID
, 0, &ACON_chunk
, &seq_chunk
);
1145 frame_seq
= (DWORD
*) seq_chunk
.data
;
1150 FIXME("Sequence data expected but not found, assuming steps == frames.\n");
1151 header
.num_steps
= header
.num_frames
;
1155 riff_find_chunk( ANI_rate_ID
, 0, &ACON_chunk
, &rate_chunk
);
1156 if (rate_chunk
.data
)
1157 frame_rates
= (DWORD
*) rate_chunk
.data
;
1159 riff_find_chunk( ANI_fram_ID
, ANI_LIST_ID
, &ACON_chunk
, &fram_chunk
);
1160 if (!fram_chunk
.data
)
1162 ERR("Failed to get icon list.\n");
1166 cursor
= alloc_icon_handle( TRUE
, header
.num_steps
);
1167 if (!cursor
) return 0;
1168 frames
= HeapAlloc( GetProcessHeap(), 0, sizeof(*frames
) * header
.num_frames
);
1171 free_icon_handle( cursor
);
1175 info
= get_icon_ptr( cursor
);
1176 ani_icon_data
= (struct animated_cursoricon_object
*) info
;
1177 info
->is_icon
= is_icon
;
1178 ani_icon_data
->num_frames
= header
.num_frames
;
1180 /* The .ANI stores the display rate in jiffies (1/60s) */
1181 info
->delay
= header
.display_rate
;
1183 icon_chunk
= fram_chunk
.data
;
1184 icon_data
= fram_chunk
.data
+ (2 * sizeof(DWORD
));
1185 for (i
=0; i
<header
.num_frames
; i
++)
1187 const DWORD chunk_size
= *(const DWORD
*)(icon_chunk
+ sizeof(DWORD
));
1188 const CURSORICONFILEDIRENTRY
*entry
;
1189 INT frameWidth
, frameHeight
;
1190 const BITMAPINFO
*bmi
;
1192 entry
= CURSORICON_FindBestIconFile((const CURSORICONFILEDIR
*) icon_data
,
1193 bits
+ bits_size
- icon_data
,
1194 width
, height
, depth
, loadflags
);
1196 info
->hotspot
.x
= entry
->xHotspot
;
1197 info
->hotspot
.y
= entry
->yHotspot
;
1198 if (!header
.width
|| !header
.height
)
1200 frameWidth
= entry
->bWidth
;
1201 frameHeight
= entry
->bHeight
;
1205 frameWidth
= header
.width
;
1206 frameHeight
= header
.height
;
1210 if (entry
->dwDIBOffset
< bits
+ bits_size
- icon_data
)
1212 bmi
= (const BITMAPINFO
*) (icon_data
+ entry
->dwDIBOffset
);
1213 /* Grab a frame from the animation */
1214 frames
[i
] = create_icon_from_bmi( bmi
, bits
+ bits_size
- (const BYTE
*)bmi
,
1215 NULL
, NULL
, NULL
, info
->hotspot
,
1216 is_icon
, frameWidth
, frameHeight
, loadflags
);
1221 FIXME_(cursor
)("failed to convert animated cursor frame.\n");
1225 FIXME_(cursor
)("Completely failed to create animated cursor!\n");
1226 ani_icon_data
->num_frames
= 0;
1227 release_icon_ptr( cursor
, info
);
1228 free_icon_handle( cursor
);
1229 HeapFree( GetProcessHeap(), 0, frames
);
1235 /* Advance to the next chunk */
1236 icon_chunk
+= chunk_size
+ (2 * sizeof(DWORD
));
1237 icon_data
= icon_chunk
+ (2 * sizeof(DWORD
));
1240 /* There was an error but we at least decoded the first frame, so just use that frame */
1243 FIXME_(cursor
)("Error creating animated cursor, only using first frame!\n");
1244 for (i
=1; i
<ani_icon_data
->num_frames
; i
++)
1245 free_icon_handle( ani_icon_data
->frames
[i
] );
1248 ani_icon_data
->num_steps
= 1;
1249 ani_icon_data
->num_frames
= 1;
1252 /* Setup the animated frames in the correct sequence */
1253 for (i
=0; i
<ani_icon_data
->num_steps
; i
++)
1255 DWORD frame_id
= use_seq
? frame_seq
[i
] : i
;
1256 struct cursoricon_frame
*frame
;
1258 if (frame_id
>= ani_icon_data
->num_frames
)
1260 frame_id
= ani_icon_data
->num_frames
-1;
1261 ERR_(cursor
)("Sequence indicates frame past end of list, corrupt?\n");
1263 ani_icon_data
->frames
[i
] = frames
[frame_id
];
1264 frame
= get_icon_frame( info
, i
);
1266 frame
->delay
= frame_rates
[i
];
1269 release_icon_frame( info
, i
, frame
);
1272 HeapFree( GetProcessHeap(), 0, frames
);
1273 release_icon_ptr( cursor
, info
);
1279 /**********************************************************************
1280 * CreateIconFromResourceEx (USER32.@)
1282 * FIXME: Convert to mono when cFlag is LR_MONOCHROME.
1284 HICON WINAPI
CreateIconFromResourceEx( LPBYTE bits
, UINT cbSize
,
1285 BOOL bIcon
, DWORD dwVersion
,
1286 INT width
, INT height
,
1290 const BITMAPINFO
*bmi
;
1292 TRACE_(cursor
)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1293 bits
, cbSize
, dwVersion
, width
, height
,
1294 bIcon
? "icon" : "cursor", (cFlag
& LR_MONOCHROME
) ? "mono" : "" );
1296 if (!bits
) return 0;
1298 if (dwVersion
== 0x00020000)
1300 FIXME_(cursor
)("\t2.xx resources are not supported\n");
1304 /* Check if the resource is an animated icon/cursor */
1305 if (!memcmp(bits
, "RIFF", 4))
1306 return CURSORICON_CreateIconFromANI( bits
, cbSize
, width
, height
,
1307 0 /* default depth */, bIcon
, cFlag
);
1311 hotspot
.x
= width
/ 2;
1312 hotspot
.y
= height
/ 2;
1313 bmi
= (BITMAPINFO
*)bits
;
1315 else /* get the hotspot */
1317 const SHORT
*pt
= (const SHORT
*)bits
;
1320 bmi
= (const BITMAPINFO
*)(pt
+ 2);
1321 cbSize
-= 2 * sizeof(*pt
);
1324 return create_icon_from_bmi( bmi
, cbSize
, NULL
, NULL
, NULL
, hotspot
, bIcon
, width
, height
, cFlag
);
1328 /**********************************************************************
1329 * CreateIconFromResource (USER32.@)
1331 HICON WINAPI
CreateIconFromResource( LPBYTE bits
, UINT cbSize
,
1332 BOOL bIcon
, DWORD dwVersion
)
1334 return CreateIconFromResourceEx( bits
, cbSize
, bIcon
, dwVersion
, 0,0,0);
1338 static HICON
CURSORICON_LoadFromFile( LPCWSTR filename
,
1339 INT width
, INT height
, INT depth
,
1340 BOOL fCursor
, UINT loadflags
)
1342 const CURSORICONFILEDIRENTRY
*entry
;
1343 const CURSORICONFILEDIR
*dir
;
1349 TRACE("loading %s\n", debugstr_w( filename
));
1351 bits
= map_fileW( filename
, &filesize
);
1355 /* Check for .ani. */
1356 if (memcmp( bits
, "RIFF", 4 ) == 0)
1358 hIcon
= CURSORICON_CreateIconFromANI( bits
, filesize
, width
, height
, depth
, !fCursor
, loadflags
);
1362 dir
= (const CURSORICONFILEDIR
*) bits
;
1363 if ( filesize
< FIELD_OFFSET( CURSORICONFILEDIR
, idEntries
[dir
->idCount
] ))
1367 entry
= CURSORICON_FindBestCursorFile( dir
, filesize
, width
, height
, depth
, loadflags
);
1369 entry
= CURSORICON_FindBestIconFile( dir
, filesize
, width
, height
, depth
, loadflags
);
1374 /* check that we don't run off the end of the file */
1375 if ( entry
->dwDIBOffset
> filesize
)
1377 if ( entry
->dwDIBOffset
+ entry
->dwDIBSize
> filesize
)
1380 hotspot
.x
= entry
->xHotspot
;
1381 hotspot
.y
= entry
->yHotspot
;
1382 hIcon
= create_icon_from_bmi( (const BITMAPINFO
*)&bits
[entry
->dwDIBOffset
], filesize
- entry
->dwDIBOffset
,
1383 NULL
, NULL
, NULL
, hotspot
, !fCursor
, width
, height
, loadflags
);
1385 TRACE("loaded %s -> %p\n", debugstr_w( filename
), hIcon
);
1386 UnmapViewOfFile( bits
);
1390 /**********************************************************************
1393 * Load a cursor or icon from resource or file.
1395 static HICON
CURSORICON_Load(HINSTANCE hInstance
, LPCWSTR name
,
1396 INT width
, INT height
, INT depth
,
1397 BOOL fCursor
, UINT loadflags
)
1403 const CURSORICONDIR
*dir
;
1404 const CURSORICONDIRENTRY
*dirEntry
;
1409 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1410 hInstance
, debugstr_w(name
), width
, height
, depth
, fCursor
, loadflags
);
1412 if ( loadflags
& LR_LOADFROMFILE
) /* Load from file */
1413 return CURSORICON_LoadFromFile( name
, width
, height
, depth
, fCursor
, loadflags
);
1415 if (!hInstance
) hInstance
= user32_module
; /* Load OEM cursor/icon */
1417 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1418 if ((ULONG_PTR
)hInstance
>> 16 == 0) loadflags
&= ~LR_SHARED
;
1420 /* Get directory resource ID */
1422 if (!(hRsrc
= FindResourceW( hInstance
, name
,
1423 (LPWSTR
)(fCursor
? RT_GROUP_CURSOR
: RT_GROUP_ICON
) )))
1425 /* try animated resource */
1426 if (!(hRsrc
= FindResourceW( hInstance
, name
,
1427 (LPWSTR
)(fCursor
? RT_ANICURSOR
: RT_ANIICON
) ))) return 0;
1428 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
1429 bits
= LockResource( handle
);
1430 return CURSORICON_CreateIconFromANI( bits
, SizeofResource( hInstance
, handle
),
1431 width
, height
, depth
, !fCursor
, loadflags
);
1434 /* Find the best entry in the directory */
1436 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
1437 if (!(dir
= LockResource( handle
))) return 0;
1438 size
= SizeofResource( hInstance
, hRsrc
);
1440 dirEntry
= CURSORICON_FindBestCursorRes( dir
, size
, width
, height
, depth
, loadflags
);
1442 dirEntry
= CURSORICON_FindBestIconRes( dir
, size
, width
, height
, depth
, loadflags
);
1443 if (!dirEntry
) return 0;
1444 wResId
= dirEntry
->wResId
;
1445 FreeResource( handle
);
1447 /* Load the resource */
1449 if (!(hRsrc
= FindResourceW(hInstance
,MAKEINTRESOURCEW(wResId
),
1450 (LPWSTR
)(fCursor
? RT_CURSOR
: RT_ICON
) ))) return 0;
1452 /* If shared icon, check whether it was already loaded */
1453 if (loadflags
& LR_SHARED
)
1455 struct cursoricon_object
*ptr
;
1458 LIST_FOR_EACH_ENTRY( ptr
, &icon_cache
, struct cursoricon_object
, entry
)
1460 if (ptr
->module
!= hInstance
) continue;
1461 if (ptr
->rsrc
!= hRsrc
) continue;
1462 hIcon
= ptr
->obj
.handle
;
1466 if (hIcon
) return hIcon
;
1469 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
1470 size
= SizeofResource( hInstance
, hRsrc
);
1471 bits
= LockResource( handle
);
1475 hotspot
.x
= width
/ 2;
1476 hotspot
.y
= height
/ 2;
1478 else /* get the hotspot */
1480 const SHORT
*pt
= (const SHORT
*)bits
;
1483 bits
+= 2 * sizeof(SHORT
);
1484 size
-= 2 * sizeof(SHORT
);
1486 hIcon
= create_icon_from_bmi( (const BITMAPINFO
*)bits
, size
, hInstance
, name
, hRsrc
,
1487 hotspot
, !fCursor
, width
, height
, loadflags
);
1488 FreeResource( handle
);
1493 /***********************************************************************
1494 * CreateCursor (USER32.@)
1496 HCURSOR WINAPI
CreateCursor( HINSTANCE hInstance
,
1497 INT xHotSpot
, INT yHotSpot
,
1498 INT nWidth
, INT nHeight
,
1499 LPCVOID lpANDbits
, LPCVOID lpXORbits
)
1504 TRACE_(cursor
)("%dx%d spot=%d,%d xor=%p and=%p\n",
1505 nWidth
, nHeight
, xHotSpot
, yHotSpot
, lpXORbits
, lpANDbits
);
1508 info
.xHotspot
= xHotSpot
;
1509 info
.yHotspot
= yHotSpot
;
1510 info
.hbmMask
= CreateBitmap( nWidth
, nHeight
, 1, 1, lpANDbits
);
1511 info
.hbmColor
= CreateBitmap( nWidth
, nHeight
, 1, 1, lpXORbits
);
1512 hCursor
= CreateIconIndirect( &info
);
1513 DeleteObject( info
.hbmMask
);
1514 DeleteObject( info
.hbmColor
);
1519 /***********************************************************************
1520 * CreateIcon (USER32.@)
1522 * Creates an icon based on the specified bitmaps. The bitmaps must be
1523 * provided in a device dependent format and will be resized to
1524 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1525 * depth. The provided bitmaps must be top-down bitmaps.
1526 * Although Windows does not support 15bpp(*) this API must support it
1527 * for Winelib applications.
1529 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1533 * Success: handle to an icon
1536 * FIXME: Do we need to resize the bitmaps?
1538 HICON WINAPI
CreateIcon(
1539 HINSTANCE hInstance
, /* [in] the application's hInstance */
1540 INT nWidth
, /* [in] the width of the provided bitmaps */
1541 INT nHeight
, /* [in] the height of the provided bitmaps */
1542 BYTE bPlanes
, /* [in] the number of planes in the provided bitmaps */
1543 BYTE bBitsPixel
, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1544 LPCVOID lpANDbits
, /* [in] a monochrome bitmap representing the icon's mask */
1545 LPCVOID lpXORbits
) /* [in] the icon's 'color' bitmap */
1550 TRACE_(icon
)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1551 nWidth
, nHeight
, bPlanes
, bBitsPixel
, lpXORbits
, lpANDbits
);
1554 iinfo
.xHotspot
= nWidth
/ 2;
1555 iinfo
.yHotspot
= nHeight
/ 2;
1556 iinfo
.hbmMask
= CreateBitmap( nWidth
, nHeight
, 1, 1, lpANDbits
);
1557 iinfo
.hbmColor
= CreateBitmap( nWidth
, nHeight
, bPlanes
, bBitsPixel
, lpXORbits
);
1559 hIcon
= CreateIconIndirect( &iinfo
);
1561 DeleteObject( iinfo
.hbmMask
);
1562 DeleteObject( iinfo
.hbmColor
);
1568 /***********************************************************************
1569 * CopyIcon (USER32.@)
1571 HICON WINAPI
CopyIcon( HICON hIcon
)
1573 struct cursoricon_object
*ptrOld
, *ptrNew
;
1576 if (!(ptrOld
= get_icon_ptr( hIcon
)))
1578 SetLastError( ERROR_INVALID_CURSOR_HANDLE
);
1581 if ((hNew
= alloc_icon_handle( FALSE
, 1 )))
1583 struct cursoricon_frame
*frameOld
, *frameNew
;
1585 ptrNew
= get_icon_ptr( hNew
);
1586 ptrNew
->is_icon
= ptrOld
->is_icon
;
1587 ptrNew
->hotspot
= ptrOld
->hotspot
;
1588 if (!(frameOld
= get_icon_frame( ptrOld
, 0 )))
1590 release_icon_ptr( hIcon
, ptrOld
);
1591 SetLastError( ERROR_INVALID_CURSOR_HANDLE
);
1594 if (!(frameNew
= get_icon_frame( ptrNew
, 0 )))
1596 release_icon_frame( ptrOld
, 0, frameOld
);
1597 release_icon_ptr( hIcon
, ptrOld
);
1598 SetLastError( ERROR_INVALID_CURSOR_HANDLE
);
1601 frameNew
->delay
= 0;
1602 frameNew
->width
= frameOld
->width
;
1603 frameNew
->height
= frameOld
->height
;
1604 frameNew
->mask
= copy_bitmap( frameOld
->mask
);
1605 frameNew
->color
= copy_bitmap( frameOld
->color
);
1606 frameNew
->alpha
= copy_bitmap( frameOld
->alpha
);
1607 release_icon_frame( ptrOld
, 0, frameOld
);
1608 release_icon_frame( ptrNew
, 0, frameNew
);
1609 release_icon_ptr( hNew
, ptrNew
);
1611 release_icon_ptr( hIcon
, ptrOld
);
1616 /***********************************************************************
1617 * DestroyIcon (USER32.@)
1619 BOOL WINAPI
DestroyIcon( HICON hIcon
)
1622 struct cursoricon_object
*obj
= get_icon_ptr( hIcon
);
1624 TRACE_(icon
)("%p\n", hIcon
);
1628 BOOL shared
= (obj
->rsrc
!= NULL
);
1629 release_icon_ptr( hIcon
, obj
);
1630 ret
= (GetCursor() != hIcon
);
1631 if (!shared
) free_icon_handle( hIcon
);
1637 /***********************************************************************
1638 * DestroyCursor (USER32.@)
1640 BOOL WINAPI
DestroyCursor( HCURSOR hCursor
)
1642 return DestroyIcon( hCursor
);
1645 /***********************************************************************
1646 * DrawIcon (USER32.@)
1648 BOOL WINAPI
DrawIcon( HDC hdc
, INT x
, INT y
, HICON hIcon
)
1650 return DrawIconEx( hdc
, x
, y
, hIcon
, 0, 0, 0, 0, DI_NORMAL
| DI_COMPAT
| DI_DEFAULTSIZE
);
1653 /***********************************************************************
1654 * SetCursor (USER32.@)
1656 * Set the cursor shape.
1659 * A handle to the previous cursor shape.
1661 HCURSOR WINAPI DECLSPEC_HOTPATCH
SetCursor( HCURSOR hCursor
/* [in] Handle of cursor to show */ )
1663 struct cursoricon_object
*obj
;
1668 TRACE("%p\n", hCursor
);
1670 SERVER_START_REQ( set_cursor
)
1672 req
->flags
= SET_CURSOR_HANDLE
;
1673 req
->handle
= wine_server_user_handle( hCursor
);
1674 if ((ret
= !wine_server_call_err( req
)))
1676 hOldCursor
= wine_server_ptr_handle( reply
->prev_handle
);
1677 show_count
= reply
->prev_count
;
1683 USER_Driver
->pSetCursor( show_count
>= 0 ? hCursor
: 0 );
1685 if (!(obj
= get_icon_ptr( hOldCursor
))) return 0;
1686 release_icon_ptr( hOldCursor
, obj
);
1690 /***********************************************************************
1691 * ShowCursor (USER32.@)
1693 INT WINAPI DECLSPEC_HOTPATCH
ShowCursor( BOOL bShow
)
1696 int increment
= bShow
? 1 : -1;
1699 SERVER_START_REQ( set_cursor
)
1701 req
->flags
= SET_CURSOR_COUNT
;
1702 req
->show_count
= increment
;
1703 wine_server_call( req
);
1704 cursor
= wine_server_ptr_handle( reply
->prev_handle
);
1705 count
= reply
->prev_count
+ increment
;
1709 TRACE("%d, count=%d\n", bShow
, count
);
1711 if (bShow
&& !count
) USER_Driver
->pSetCursor( cursor
);
1712 else if (!bShow
&& count
== -1) USER_Driver
->pSetCursor( 0 );
1717 /***********************************************************************
1718 * GetCursor (USER32.@)
1720 HCURSOR WINAPI
GetCursor(void)
1724 SERVER_START_REQ( set_cursor
)
1727 wine_server_call( req
);
1728 ret
= wine_server_ptr_handle( reply
->prev_handle
);
1735 /***********************************************************************
1736 * ClipCursor (USER32.@)
1738 BOOL WINAPI DECLSPEC_HOTPATCH
ClipCursor( const RECT
*rect
)
1743 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect
) );
1745 if (rect
&& (rect
->left
> rect
->right
|| rect
->top
> rect
->bottom
)) return FALSE
;
1747 SERVER_START_REQ( set_cursor
)
1749 req
->clip_msg
= WM_WINE_CLIPCURSOR
;
1752 req
->flags
= SET_CURSOR_CLIP
;
1753 req
->clip
.left
= rect
->left
;
1754 req
->clip
.top
= rect
->top
;
1755 req
->clip
.right
= rect
->right
;
1756 req
->clip
.bottom
= rect
->bottom
;
1758 else req
->flags
= SET_CURSOR_NOCLIP
;
1760 if ((ret
= !wine_server_call( req
)))
1762 new_rect
.left
= reply
->new_clip
.left
;
1763 new_rect
.top
= reply
->new_clip
.top
;
1764 new_rect
.right
= reply
->new_clip
.right
;
1765 new_rect
.bottom
= reply
->new_clip
.bottom
;
1769 if (ret
) USER_Driver
->pClipCursor( &new_rect
);
1774 /***********************************************************************
1775 * GetClipCursor (USER32.@)
1777 BOOL WINAPI DECLSPEC_HOTPATCH
GetClipCursor( RECT
*rect
)
1781 if (!rect
) return FALSE
;
1783 SERVER_START_REQ( set_cursor
)
1786 if ((ret
= !wine_server_call( req
)))
1788 rect
->left
= reply
->new_clip
.left
;
1789 rect
->top
= reply
->new_clip
.top
;
1790 rect
->right
= reply
->new_clip
.right
;
1791 rect
->bottom
= reply
->new_clip
.bottom
;
1799 /***********************************************************************
1800 * SetSystemCursor (USER32.@)
1802 BOOL WINAPI
SetSystemCursor(HCURSOR hcur
, DWORD id
)
1804 FIXME("(%p,%08x),stub!\n", hcur
, id
);
1809 /**********************************************************************
1810 * LookupIconIdFromDirectoryEx (USER32.@)
1812 INT WINAPI
LookupIconIdFromDirectoryEx( LPBYTE xdir
, BOOL bIcon
,
1813 INT width
, INT height
, UINT cFlag
)
1815 const CURSORICONDIR
*dir
= (const CURSORICONDIR
*)xdir
;
1817 if( dir
&& !dir
->idReserved
&& (dir
->idType
& 3) )
1819 const CURSORICONDIRENTRY
* entry
;
1821 const HDC hdc
= GetDC(0);
1822 const int depth
= (cFlag
& LR_MONOCHROME
) ?
1823 1 : GetDeviceCaps(hdc
, BITSPIXEL
);
1827 entry
= CURSORICON_FindBestIconRes( dir
, ~0u, width
, height
, depth
, LR_DEFAULTSIZE
);
1829 entry
= CURSORICON_FindBestCursorRes( dir
, ~0u, width
, height
, depth
, LR_DEFAULTSIZE
);
1831 if( entry
) retVal
= entry
->wResId
;
1833 else WARN_(cursor
)("invalid resource directory\n");
1837 /**********************************************************************
1838 * LookupIconIdFromDirectory (USER32.@)
1840 INT WINAPI
LookupIconIdFromDirectory( LPBYTE dir
, BOOL bIcon
)
1842 return LookupIconIdFromDirectoryEx( dir
, bIcon
, 0, 0, bIcon
? 0 : LR_MONOCHROME
);
1845 /***********************************************************************
1846 * LoadCursorW (USER32.@)
1848 HCURSOR WINAPI
LoadCursorW(HINSTANCE hInstance
, LPCWSTR name
)
1850 TRACE("%p, %s\n", hInstance
, debugstr_w(name
));
1852 return LoadImageW( hInstance
, name
, IMAGE_CURSOR
, 0, 0,
1853 LR_SHARED
| LR_DEFAULTSIZE
);
1856 /***********************************************************************
1857 * LoadCursorA (USER32.@)
1859 HCURSOR WINAPI
LoadCursorA(HINSTANCE hInstance
, LPCSTR name
)
1861 TRACE("%p, %s\n", hInstance
, debugstr_a(name
));
1863 return LoadImageA( hInstance
, name
, IMAGE_CURSOR
, 0, 0,
1864 LR_SHARED
| LR_DEFAULTSIZE
);
1867 /***********************************************************************
1868 * LoadCursorFromFileW (USER32.@)
1870 HCURSOR WINAPI
LoadCursorFromFileW (LPCWSTR name
)
1872 TRACE("%s\n", debugstr_w(name
));
1874 return LoadImageW( 0, name
, IMAGE_CURSOR
, 0, 0,
1875 LR_LOADFROMFILE
| LR_DEFAULTSIZE
);
1878 /***********************************************************************
1879 * LoadCursorFromFileA (USER32.@)
1881 HCURSOR WINAPI
LoadCursorFromFileA (LPCSTR name
)
1883 TRACE("%s\n", debugstr_a(name
));
1885 return LoadImageA( 0, name
, IMAGE_CURSOR
, 0, 0,
1886 LR_LOADFROMFILE
| LR_DEFAULTSIZE
);
1889 /***********************************************************************
1890 * LoadIconW (USER32.@)
1892 HICON WINAPI
LoadIconW(HINSTANCE hInstance
, LPCWSTR name
)
1894 TRACE("%p, %s\n", hInstance
, debugstr_w(name
));
1896 return LoadImageW( hInstance
, name
, IMAGE_ICON
, 0, 0,
1897 LR_SHARED
| LR_DEFAULTSIZE
);
1900 /***********************************************************************
1901 * LoadIconA (USER32.@)
1903 HICON WINAPI
LoadIconA(HINSTANCE hInstance
, LPCSTR name
)
1905 TRACE("%p, %s\n", hInstance
, debugstr_a(name
));
1907 return LoadImageA( hInstance
, name
, IMAGE_ICON
, 0, 0,
1908 LR_SHARED
| LR_DEFAULTSIZE
);
1911 /**********************************************************************
1912 * GetCursorFrameInfo (USER32.@)
1915 * So far no use has been found for the second parameter, it is currently presumed
1916 * that this parameter is reserved for future use.
1919 * hCursor [I] Handle to cursor for which to retrieve information
1920 * reserved [I] No purpose has been found for this parameter (may be NULL)
1921 * istep [I] The step of the cursor for which to retrieve information
1922 * rate_jiffies [O] Pointer to DWORD that receives the frame-specific delay (cannot be NULL)
1923 * num_steps [O] Pointer to DWORD that receives the number of steps in the cursor (cannot be NULL)
1926 * Success: Handle to a frame of the cursor (specified by istep)
1927 * Failure: NULL cursor (0)
1929 HCURSOR WINAPI
GetCursorFrameInfo(HCURSOR hCursor
, DWORD reserved
, DWORD istep
, DWORD
*rate_jiffies
, DWORD
*num_steps
)
1931 struct cursoricon_object
*ptr
;
1935 if (rate_jiffies
== NULL
|| num_steps
== NULL
) return 0;
1937 if (!(ptr
= get_icon_ptr( hCursor
))) return 0;
1939 TRACE("%p => %d %d %p %p\n", hCursor
, reserved
, istep
, rate_jiffies
, num_steps
);
1941 FIXME("Second parameter non-zero (%d), please report this!\n", reserved
);
1943 icon_steps
= get_icon_steps(ptr
);
1944 if (istep
< icon_steps
|| !ptr
->is_ani
)
1946 struct animated_cursoricon_object
*ani_icon_data
= (struct animated_cursoricon_object
*) ptr
;
1947 UINT icon_frames
= 1;
1950 icon_frames
= ani_icon_data
->num_frames
;
1951 if (ptr
->is_ani
&& icon_frames
> 1)
1952 ret
= ani_icon_data
->frames
[istep
];
1955 if (icon_frames
== 1)
1960 else if (icon_steps
== 1)
1963 *rate_jiffies
= ptr
->delay
;
1965 else if (istep
< icon_steps
)
1967 struct cursoricon_frame
*frame
;
1969 *num_steps
= icon_steps
;
1970 frame
= get_icon_frame( ptr
, istep
);
1971 if (get_icon_steps(ptr
) == 1)
1974 *num_steps
= get_icon_steps(ptr
);
1975 /* If this specific frame does not have a delay then use the global delay */
1976 if (frame
->delay
== ~0)
1977 *rate_jiffies
= ptr
->delay
;
1979 *rate_jiffies
= frame
->delay
;
1980 release_icon_frame( ptr
, istep
, frame
);
1984 release_icon_ptr( hCursor
, ptr
);
1989 /**********************************************************************
1990 * GetIconInfo (USER32.@)
1992 BOOL WINAPI
GetIconInfo(HICON hIcon
, PICONINFO iconinfo
)
1996 infoW
.cbSize
= sizeof(infoW
);
1997 if (!GetIconInfoExW( hIcon
, &infoW
)) return FALSE
;
1998 iconinfo
->fIcon
= infoW
.fIcon
;
1999 iconinfo
->xHotspot
= infoW
.xHotspot
;
2000 iconinfo
->yHotspot
= infoW
.yHotspot
;
2001 iconinfo
->hbmColor
= infoW
.hbmColor
;
2002 iconinfo
->hbmMask
= infoW
.hbmMask
;
2006 /**********************************************************************
2007 * GetIconInfoExA (USER32.@)
2009 BOOL WINAPI
GetIconInfoExA( HICON icon
, ICONINFOEXA
*info
)
2013 if (info
->cbSize
!= sizeof(*info
))
2015 SetLastError( ERROR_INVALID_PARAMETER
);
2018 infoW
.cbSize
= sizeof(infoW
);
2019 if (!GetIconInfoExW( icon
, &infoW
)) return FALSE
;
2020 info
->fIcon
= infoW
.fIcon
;
2021 info
->xHotspot
= infoW
.xHotspot
;
2022 info
->yHotspot
= infoW
.yHotspot
;
2023 info
->hbmColor
= infoW
.hbmColor
;
2024 info
->hbmMask
= infoW
.hbmMask
;
2025 info
->wResID
= infoW
.wResID
;
2026 WideCharToMultiByte( CP_ACP
, 0, infoW
.szModName
, -1, info
->szModName
, MAX_PATH
, NULL
, NULL
);
2027 WideCharToMultiByte( CP_ACP
, 0, infoW
.szResName
, -1, info
->szResName
, MAX_PATH
, NULL
, NULL
);
2031 /**********************************************************************
2032 * GetIconInfoExW (USER32.@)
2034 BOOL WINAPI
GetIconInfoExW( HICON icon
, ICONINFOEXW
*info
)
2036 struct cursoricon_frame
*frame
;
2037 struct cursoricon_object
*ptr
;
2041 if (info
->cbSize
!= sizeof(*info
))
2043 SetLastError( ERROR_INVALID_PARAMETER
);
2046 if (!(ptr
= get_icon_ptr( icon
)))
2048 SetLastError( ERROR_INVALID_CURSOR_HANDLE
);
2052 frame
= get_icon_frame( ptr
, 0 );
2055 release_icon_ptr( icon
, ptr
);
2056 SetLastError( ERROR_INVALID_CURSOR_HANDLE
);
2060 TRACE("%p => %dx%d\n", icon
, frame
->width
, frame
->height
);
2062 info
->fIcon
= ptr
->is_icon
;
2063 info
->xHotspot
= ptr
->hotspot
.x
;
2064 info
->yHotspot
= ptr
->hotspot
.y
;
2065 info
->hbmColor
= copy_bitmap( frame
->color
);
2066 info
->hbmMask
= copy_bitmap( frame
->mask
);
2068 info
->szModName
[0] = 0;
2069 info
->szResName
[0] = 0;
2072 if (IS_INTRESOURCE( ptr
->resname
)) info
->wResID
= LOWORD( ptr
->resname
);
2073 else lstrcpynW( info
->szResName
, ptr
->resname
, MAX_PATH
);
2075 if (!info
->hbmMask
|| (!info
->hbmColor
&& frame
->color
))
2077 DeleteObject( info
->hbmMask
);
2078 DeleteObject( info
->hbmColor
);
2081 module
= ptr
->module
;
2082 release_icon_frame( ptr
, 0, frame
);
2083 release_icon_ptr( icon
, ptr
);
2084 if (ret
&& module
) GetModuleFileNameW( module
, info
->szModName
, MAX_PATH
);
2088 /* copy an icon bitmap, even when it can't be selected into a DC */
2089 /* helper for CreateIconIndirect */
2090 static void stretch_blt_icon( HDC hdc_dst
, int dst_x
, int dst_y
, int dst_width
, int dst_height
,
2091 HBITMAP src
, int width
, int height
)
2093 HDC hdc
= CreateCompatibleDC( 0 );
2095 if (!SelectObject( hdc
, src
)) /* do it the hard way */
2100 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )))) return;
2101 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
2102 info
->bmiHeader
.biWidth
= width
;
2103 info
->bmiHeader
.biHeight
= height
;
2104 info
->bmiHeader
.biPlanes
= GetDeviceCaps( hdc_dst
, PLANES
);
2105 info
->bmiHeader
.biBitCount
= GetDeviceCaps( hdc_dst
, BITSPIXEL
);
2106 info
->bmiHeader
.biCompression
= BI_RGB
;
2107 info
->bmiHeader
.biSizeImage
= get_dib_image_size( width
, height
, info
->bmiHeader
.biBitCount
);
2108 info
->bmiHeader
.biXPelsPerMeter
= 0;
2109 info
->bmiHeader
.biYPelsPerMeter
= 0;
2110 info
->bmiHeader
.biClrUsed
= 0;
2111 info
->bmiHeader
.biClrImportant
= 0;
2112 bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
);
2113 if (bits
&& GetDIBits( hdc
, src
, 0, height
, bits
, info
, DIB_RGB_COLORS
))
2114 StretchDIBits( hdc_dst
, dst_x
, dst_y
, dst_width
, dst_height
,
2115 0, 0, width
, height
, bits
, info
, DIB_RGB_COLORS
, SRCCOPY
);
2117 HeapFree( GetProcessHeap(), 0, bits
);
2118 HeapFree( GetProcessHeap(), 0, info
);
2120 else StretchBlt( hdc_dst
, dst_x
, dst_y
, dst_width
, dst_height
, hdc
, 0, 0, width
, height
, SRCCOPY
);
2125 /**********************************************************************
2126 * CreateIconIndirect (USER32.@)
2128 HICON WINAPI
CreateIconIndirect(PICONINFO iconinfo
)
2130 BITMAP bmpXor
, bmpAnd
;
2132 HBITMAP color
= 0, mask
;
2136 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2137 iconinfo
->hbmColor
, iconinfo
->hbmMask
,
2138 iconinfo
->xHotspot
, iconinfo
->yHotspot
, iconinfo
->fIcon
);
2140 if (!iconinfo
->hbmMask
) return 0;
2142 GetObjectW( iconinfo
->hbmMask
, sizeof(bmpAnd
), &bmpAnd
);
2143 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2144 bmpAnd
.bmWidth
, bmpAnd
.bmHeight
, bmpAnd
.bmWidthBytes
,
2145 bmpAnd
.bmPlanes
, bmpAnd
.bmBitsPixel
);
2147 if (iconinfo
->hbmColor
)
2149 GetObjectW( iconinfo
->hbmColor
, sizeof(bmpXor
), &bmpXor
);
2150 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2151 bmpXor
.bmWidth
, bmpXor
.bmHeight
, bmpXor
.bmWidthBytes
,
2152 bmpXor
.bmPlanes
, bmpXor
.bmBitsPixel
);
2154 width
= bmpXor
.bmWidth
;
2155 height
= bmpXor
.bmHeight
;
2156 if (bmpXor
.bmPlanes
* bmpXor
.bmBitsPixel
!= 1 || bmpAnd
.bmPlanes
* bmpAnd
.bmBitsPixel
!= 1)
2158 color
= CreateCompatibleBitmap( screen_dc
, width
, height
);
2159 mask
= CreateBitmap( width
, height
, 1, 1, NULL
);
2161 else mask
= CreateBitmap( width
, height
* 2, 1, 1, NULL
);
2165 width
= bmpAnd
.bmWidth
;
2166 height
= bmpAnd
.bmHeight
;
2167 mask
= CreateBitmap( width
, height
, 1, 1, NULL
);
2170 hdc
= CreateCompatibleDC( 0 );
2171 SelectObject( hdc
, mask
);
2172 stretch_blt_icon( hdc
, 0, 0, width
, height
, iconinfo
->hbmMask
, bmpAnd
.bmWidth
, bmpAnd
.bmHeight
);
2176 SelectObject( hdc
, color
);
2177 stretch_blt_icon( hdc
, 0, 0, width
, height
, iconinfo
->hbmColor
, width
, height
);
2179 else if (iconinfo
->hbmColor
)
2181 stretch_blt_icon( hdc
, 0, height
, width
, height
, iconinfo
->hbmColor
, width
, height
);
2187 hObj
= alloc_icon_handle( FALSE
, 1 );
2190 struct cursoricon_object
*info
= get_icon_ptr( hObj
);
2191 struct cursoricon_frame
*frame
;
2193 info
->is_icon
= iconinfo
->fIcon
;
2194 frame
= get_icon_frame( info
, 0 );
2196 frame
->width
= width
;
2197 frame
->height
= height
;
2198 frame
->color
= color
;
2200 frame
->alpha
= create_alpha_bitmap( iconinfo
->hbmColor
, mask
, NULL
, NULL
);
2201 release_icon_frame( info
, 0, frame
);
2204 info
->hotspot
.x
= width
/ 2;
2205 info
->hotspot
.y
= height
/ 2;
2209 info
->hotspot
.x
= iconinfo
->xHotspot
;
2210 info
->hotspot
.y
= iconinfo
->yHotspot
;
2213 release_icon_ptr( hObj
, info
);
2218 /******************************************************************************
2219 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2222 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2225 * hdc [I] Handle to device context
2226 * x0 [I] X coordinate of upper left corner
2227 * y0 [I] Y coordinate of upper left corner
2228 * hIcon [I] Handle to icon to draw
2229 * cxWidth [I] Width of icon
2230 * cyWidth [I] Height of icon
2231 * istep [I] Index of frame in animated cursor
2232 * hbr [I] Handle to background brush
2233 * flags [I] Icon-drawing flags
2239 BOOL WINAPI
DrawIconEx( HDC hdc
, INT x0
, INT y0
, HICON hIcon
,
2240 INT cxWidth
, INT cyWidth
, UINT istep
,
2241 HBRUSH hbr
, UINT flags
)
2243 struct cursoricon_frame
*frame
;
2244 struct cursoricon_object
*ptr
;
2245 HDC hdc_dest
, hMemDC
;
2246 BOOL result
= FALSE
, DoOffscreen
;
2248 COLORREF oldFg
, oldBg
;
2249 INT x
, y
, nStretchMode
;
2251 TRACE_(icon
)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2252 hdc
,x0
,y0
,hIcon
,cxWidth
,cyWidth
,istep
,hbr
,flags
);
2254 if (!(ptr
= get_icon_ptr( hIcon
))) return FALSE
;
2255 if (istep
>= get_icon_steps( ptr
))
2257 TRACE_(icon
)("Stepped past end of animated frames=%d\n", istep
);
2258 release_icon_ptr( hIcon
, ptr
);
2261 if (!(frame
= get_icon_frame( ptr
, istep
)))
2263 FIXME_(icon
)("Error retrieving icon frame %d\n", istep
);
2264 release_icon_ptr( hIcon
, ptr
);
2267 if (!(hMemDC
= CreateCompatibleDC( hdc
)))
2269 release_icon_frame( ptr
, istep
, frame
);
2270 release_icon_ptr( hIcon
, ptr
);
2274 if (flags
& DI_NOMIRROR
)
2275 FIXME_(icon
)("Ignoring flag DI_NOMIRROR\n");
2277 /* Calculate the size of the destination image. */
2280 if (flags
& DI_DEFAULTSIZE
)
2281 cxWidth
= GetSystemMetrics (SM_CXICON
);
2283 cxWidth
= frame
->width
;
2287 if (flags
& DI_DEFAULTSIZE
)
2288 cyWidth
= GetSystemMetrics (SM_CYICON
);
2290 cyWidth
= frame
->height
;
2293 DoOffscreen
= (GetObjectType( hbr
) == OBJ_BRUSH
);
2303 if (!(hdc_dest
= CreateCompatibleDC(hdc
))) goto failed
;
2304 if (!(hB_off
= CreateCompatibleBitmap(hdc
, cxWidth
, cyWidth
)))
2306 DeleteDC( hdc_dest
);
2309 SelectObject(hdc_dest
, hB_off
);
2310 FillRect(hdc_dest
, &r
, hbr
);
2320 nStretchMode
= SetStretchBltMode (hdc
, STRETCH_DELETESCANS
);
2322 oldFg
= SetTextColor( hdc
, RGB(0,0,0) );
2323 oldBg
= SetBkColor( hdc
, RGB(255,255,255) );
2325 if (frame
->alpha
&& (flags
& DI_IMAGE
))
2327 BOOL alpha_blend
= TRUE
;
2329 if (GetObjectType( hdc_dest
) == OBJ_MEMDC
)
2332 HBITMAP bmp
= GetCurrentObject( hdc_dest
, OBJ_BITMAP
);
2333 alpha_blend
= GetObjectW( bmp
, sizeof(bm
), &bm
) && bm
.bmBitsPixel
> 8;
2337 BLENDFUNCTION pixelblend
= { AC_SRC_OVER
, 0, 255, AC_SRC_ALPHA
};
2338 SelectObject( hMemDC
, frame
->alpha
);
2339 if (GdiAlphaBlend( hdc_dest
, x
, y
, cxWidth
, cyWidth
, hMemDC
,
2340 0, 0, frame
->width
, frame
->height
,
2341 pixelblend
)) goto done
;
2345 if (flags
& DI_MASK
)
2347 DWORD rop
= (flags
& DI_IMAGE
) ? SRCAND
: SRCCOPY
;
2348 SelectObject( hMemDC
, frame
->mask
);
2349 StretchBlt( hdc_dest
, x
, y
, cxWidth
, cyWidth
,
2350 hMemDC
, 0, 0, frame
->width
, frame
->height
, rop
);
2353 if (flags
& DI_IMAGE
)
2357 DWORD rop
= (flags
& DI_MASK
) ? SRCINVERT
: SRCCOPY
;
2358 SelectObject( hMemDC
, frame
->color
);
2359 StretchBlt( hdc_dest
, x
, y
, cxWidth
, cyWidth
,
2360 hMemDC
, 0, 0, frame
->width
, frame
->height
, rop
);
2364 DWORD rop
= (flags
& DI_MASK
) ? SRCINVERT
: SRCCOPY
;
2365 SelectObject( hMemDC
, frame
->mask
);
2366 StretchBlt( hdc_dest
, x
, y
, cxWidth
, cyWidth
,
2367 hMemDC
, 0, frame
->height
, frame
->width
,
2368 frame
->height
, rop
);
2373 if (DoOffscreen
) BitBlt( hdc
, x0
, y0
, cxWidth
, cyWidth
, hdc_dest
, 0, 0, SRCCOPY
);
2375 SetTextColor( hdc
, oldFg
);
2376 SetBkColor( hdc
, oldBg
);
2377 SetStretchBltMode (hdc
, nStretchMode
);
2379 if (hdc_dest
!= hdc
) DeleteDC( hdc_dest
);
2380 if (hB_off
) DeleteObject(hB_off
);
2383 release_icon_frame( ptr
, istep
, frame
);
2384 release_icon_ptr( hIcon
, ptr
);
2388 /***********************************************************************
2389 * DIB_FixColorsToLoadflags
2391 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2394 static void DIB_FixColorsToLoadflags(BITMAPINFO
* bmi
, UINT loadflags
, BYTE pix
)
2397 COLORREF c_W
, c_S
, c_F
, c_L
, c_C
;
2406 if (((bitmap_type
= DIB_GetBitmapInfo((BITMAPINFOHEADER
*) bmi
, &width
, &height
, &bpp
, &compr
)) == -1))
2408 WARN_(resource
)("Invalid bitmap\n");
2412 if (bpp
> 8) return;
2414 if (bitmap_type
== 0) /* BITMAPCOREHEADER */
2422 colors
= bmi
->bmiHeader
.biClrUsed
;
2423 if (colors
> 256) colors
= 256;
2424 if (!colors
&& (bpp
<= 8)) colors
= 1 << bpp
;
2427 c_W
= GetSysColor(COLOR_WINDOW
);
2428 c_S
= GetSysColor(COLOR_3DSHADOW
);
2429 c_F
= GetSysColor(COLOR_3DFACE
);
2430 c_L
= GetSysColor(COLOR_3DLIGHT
);
2432 if (loadflags
& LR_LOADTRANSPARENT
) {
2434 case 1: pix
= pix
>> 7; break;
2435 case 4: pix
= pix
>> 4; break;
2438 WARN_(resource
)("(%d): Unsupported depth\n", bpp
);
2441 if (pix
>= colors
) {
2442 WARN_(resource
)("pixel has color index greater than biClrUsed!\n");
2445 if (loadflags
& LR_LOADMAP3DCOLORS
) c_W
= c_F
;
2446 ptr
= (RGBQUAD
*)((char*)bmi
->bmiColors
+pix
*incr
);
2447 ptr
->rgbBlue
= GetBValue(c_W
);
2448 ptr
->rgbGreen
= GetGValue(c_W
);
2449 ptr
->rgbRed
= GetRValue(c_W
);
2451 if (loadflags
& LR_LOADMAP3DCOLORS
)
2452 for (i
=0; i
<colors
; i
++) {
2453 ptr
= (RGBQUAD
*)((char*)bmi
->bmiColors
+i
*incr
);
2454 c_C
= RGB(ptr
->rgbRed
, ptr
->rgbGreen
, ptr
->rgbBlue
);
2455 if (c_C
== RGB(128, 128, 128)) {
2456 ptr
->rgbRed
= GetRValue(c_S
);
2457 ptr
->rgbGreen
= GetGValue(c_S
);
2458 ptr
->rgbBlue
= GetBValue(c_S
);
2459 } else if (c_C
== RGB(192, 192, 192)) {
2460 ptr
->rgbRed
= GetRValue(c_F
);
2461 ptr
->rgbGreen
= GetGValue(c_F
);
2462 ptr
->rgbBlue
= GetBValue(c_F
);
2463 } else if (c_C
== RGB(223, 223, 223)) {
2464 ptr
->rgbRed
= GetRValue(c_L
);
2465 ptr
->rgbGreen
= GetGValue(c_L
);
2466 ptr
->rgbBlue
= GetBValue(c_L
);
2472 /**********************************************************************
2475 static HBITMAP
BITMAP_Load( HINSTANCE instance
, LPCWSTR name
,
2476 INT desiredx
, INT desiredy
, UINT loadflags
)
2478 HBITMAP hbitmap
= 0, orig_bm
;
2481 const char *ptr
= NULL
;
2482 BITMAPINFO
*info
, *fix_info
= NULL
, *scaled_info
= NULL
;
2486 LONG width
, height
, new_width
, new_height
;
2488 DWORD compr_dummy
, offbits
= 0;
2490 HDC screen_mem_dc
= NULL
;
2492 if (!(loadflags
& LR_LOADFROMFILE
))
2496 /* OEM bitmap: try to load the resource from user32.dll */
2497 instance
= user32_module
;
2500 if (!(hRsrc
= FindResourceW( instance
, name
, (LPWSTR
)RT_BITMAP
))) return 0;
2501 if (!(handle
= LoadResource( instance
, hRsrc
))) return 0;
2503 if ((info
= LockResource( handle
)) == NULL
) return 0;
2507 BITMAPFILEHEADER
* bmfh
;
2509 if (!(ptr
= map_fileW( name
, NULL
))) return 0;
2510 info
= (BITMAPINFO
*)(ptr
+ sizeof(BITMAPFILEHEADER
));
2511 bmfh
= (BITMAPFILEHEADER
*)ptr
;
2512 if (bmfh
->bfType
!= 0x4d42 /* 'BM' */)
2514 WARN("Invalid/unsupported bitmap format!\n");
2517 if (bmfh
->bfOffBits
) offbits
= bmfh
->bfOffBits
- sizeof(BITMAPFILEHEADER
);
2520 bm_type
= DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
,
2521 &bpp_dummy
, &compr_dummy
);
2524 WARN("Invalid bitmap format!\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
);
2541 new_width
= desiredx
;
2546 new_height
= height
> 0 ? desiredy
: -desiredy
;
2548 new_height
= height
;
2552 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)&scaled_info
->bmiHeader
;
2553 core
->bcWidth
= new_width
;
2554 core
->bcHeight
= new_height
;
2558 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2559 if (info
->bmiHeader
.biHeight
> 65535 || info
->bmiHeader
.biWidth
> 65535) {
2560 WARN("Broken BitmapInfoHeader!\n");
2564 scaled_info
->bmiHeader
.biWidth
= new_width
;
2565 scaled_info
->bmiHeader
.biHeight
= new_height
;
2568 if (new_height
< 0) new_height
= -new_height
;
2570 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
2571 if (!(screen_mem_dc
= CreateCompatibleDC( screen_dc
))) goto end
;
2573 bits
= (char *)info
+ (offbits
? offbits
: size
);
2575 if (loadflags
& LR_CREATEDIBSECTION
)
2577 scaled_info
->bmiHeader
.biCompression
= 0; /* DIBSection can't be compressed */
2578 hbitmap
= CreateDIBSection(screen_dc
, scaled_info
, DIB_RGB_COLORS
, NULL
, 0, 0);
2582 if (is_dib_monochrome(fix_info
))
2583 hbitmap
= CreateBitmap(new_width
, new_height
, 1, 1, NULL
);
2585 hbitmap
= CreateCompatibleBitmap(screen_dc
, new_width
, new_height
);
2588 orig_bm
= SelectObject(screen_mem_dc
, hbitmap
);
2589 StretchDIBits(screen_mem_dc
, 0, 0, new_width
, new_height
, 0, 0, width
, height
, bits
, fix_info
, DIB_RGB_COLORS
, SRCCOPY
);
2590 SelectObject(screen_mem_dc
, orig_bm
);
2593 if (screen_mem_dc
) DeleteDC(screen_mem_dc
);
2594 HeapFree(GetProcessHeap(), 0, scaled_info
);
2595 HeapFree(GetProcessHeap(), 0, fix_info
);
2596 if (loadflags
& LR_LOADFROMFILE
) UnmapViewOfFile( ptr
);
2601 /**********************************************************************
2602 * LoadImageA (USER32.@)
2606 HANDLE WINAPI
LoadImageA( HINSTANCE hinst
, LPCSTR name
, UINT type
,
2607 INT desiredx
, INT desiredy
, UINT loadflags
)
2612 if (IS_INTRESOURCE(name
))
2613 return LoadImageW(hinst
, (LPCWSTR
)name
, type
, desiredx
, desiredy
, loadflags
);
2616 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
2617 u_name
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
2618 MultiByteToWideChar( CP_ACP
, 0, name
, -1, u_name
, len
);
2620 __EXCEPT_PAGE_FAULT
{
2621 SetLastError( ERROR_INVALID_PARAMETER
);
2625 res
= LoadImageW(hinst
, u_name
, type
, desiredx
, desiredy
, loadflags
);
2626 HeapFree(GetProcessHeap(), 0, u_name
);
2631 /******************************************************************************
2632 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2635 * hinst [I] Handle of instance that contains image
2636 * name [I] Name of image
2637 * type [I] Type of image
2638 * desiredx [I] Desired width
2639 * desiredy [I] Desired height
2640 * loadflags [I] Load flags
2643 * Success: Handle to newly loaded image
2646 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2648 HANDLE WINAPI
LoadImageW( HINSTANCE hinst
, LPCWSTR name
, UINT type
,
2649 INT desiredx
, INT desiredy
, UINT loadflags
)
2653 TRACE_(resource
)("(%p,%s,%d,%d,%d,0x%08x)\n",
2654 hinst
,debugstr_w(name
),type
,desiredx
,desiredy
,loadflags
);
2656 if (loadflags
& LR_LOADFROMFILE
) loadflags
&= ~LR_SHARED
;
2659 return BITMAP_Load( hinst
, name
, desiredx
, desiredy
, loadflags
);
2664 if (!(loadflags
& LR_MONOCHROME
))
2666 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
2667 if (screen_dc
) depth
= GetDeviceCaps( screen_dc
, BITSPIXEL
);
2669 return CURSORICON_Load(hinst
, name
, desiredx
, desiredy
, depth
, (type
== IMAGE_CURSOR
), loadflags
);
2674 /******************************************************************************
2675 * CopyImage (USER32.@) Creates new image and copies attributes to it
2678 * hnd [I] Handle to image to copy
2679 * type [I] Type of image to copy
2680 * desiredx [I] Desired width of new image
2681 * desiredy [I] Desired height of new image
2682 * flags [I] Copy flags
2685 * Success: Handle to newly created image
2689 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2690 * all other versions (95/2000/XP have been tested) ignore it.
2693 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2694 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2695 * the copy will have the same depth as the screen.
2696 * The content of the image will only be copied if the bit depth of the
2697 * original image is compatible with the bit depth of the screen, or
2698 * if the source is a DIB section.
2699 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2701 HANDLE WINAPI
CopyImage( HANDLE hnd
, UINT type
, INT desiredx
,
2702 INT desiredy
, UINT flags
)
2704 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2705 hnd
, type
, desiredx
, desiredy
, flags
);
2716 objSize
= GetObjectW( hnd
, sizeof(ds
), &ds
);
2717 if (!objSize
) return 0;
2718 if ((desiredx
< 0) || (desiredy
< 0)) return 0;
2720 if (flags
& LR_COPYFROMRESOURCE
)
2722 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2725 if (desiredx
== 0) desiredx
= ds
.dsBm
.bmWidth
;
2726 if (desiredy
== 0) desiredy
= ds
.dsBm
.bmHeight
;
2728 /* Allocate memory for a BITMAPINFOHEADER structure and a
2729 color table. The maximum number of colors in a color table
2730 is 256 which corresponds to a bitmap with depth 8.
2731 Bitmaps with higher depths don't have color tables. */
2732 bi
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
2735 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
2736 bi
->bmiHeader
.biPlanes
= ds
.dsBm
.bmPlanes
;
2737 bi
->bmiHeader
.biBitCount
= ds
.dsBm
.bmBitsPixel
;
2738 bi
->bmiHeader
.biCompression
= BI_RGB
;
2740 if (flags
& LR_CREATEDIBSECTION
)
2742 /* Create a DIB section. LR_MONOCHROME is ignored */
2744 HDC dc
= CreateCompatibleDC(NULL
);
2746 if (objSize
== sizeof(DIBSECTION
))
2748 /* The source bitmap is a DIB.
2749 Get its attributes to create an exact copy */
2750 memcpy(bi
, &ds
.dsBmih
, sizeof(BITMAPINFOHEADER
));
2753 bi
->bmiHeader
.biWidth
= desiredx
;
2754 bi
->bmiHeader
.biHeight
= desiredy
;
2756 /* Get the color table or the color masks */
2757 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
2759 res
= CreateDIBSection(dc
, bi
, DIB_RGB_COLORS
, &bits
, NULL
, 0);
2764 /* Create a device-dependent bitmap */
2766 BOOL monochrome
= (flags
& LR_MONOCHROME
);
2768 if (objSize
== sizeof(DIBSECTION
))
2770 /* The source bitmap is a DIB section.
2771 Get its attributes */
2772 HDC dc
= CreateCompatibleDC(NULL
);
2773 bi
->bmiHeader
.biWidth
= ds
.dsBm
.bmWidth
;
2774 bi
->bmiHeader
.biHeight
= ds
.dsBm
.bmHeight
;
2775 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
2778 if (!monochrome
&& ds
.dsBm
.bmBitsPixel
== 1)
2780 /* Look if the colors of the DIB are black and white */
2783 (bi
->bmiColors
[0].rgbRed
== 0xff
2784 && bi
->bmiColors
[0].rgbGreen
== 0xff
2785 && bi
->bmiColors
[0].rgbBlue
== 0xff
2786 && bi
->bmiColors
[0].rgbReserved
== 0
2787 && bi
->bmiColors
[1].rgbRed
== 0
2788 && bi
->bmiColors
[1].rgbGreen
== 0
2789 && bi
->bmiColors
[1].rgbBlue
== 0
2790 && bi
->bmiColors
[1].rgbReserved
== 0)
2792 (bi
->bmiColors
[0].rgbRed
== 0
2793 && bi
->bmiColors
[0].rgbGreen
== 0
2794 && bi
->bmiColors
[0].rgbBlue
== 0
2795 && bi
->bmiColors
[0].rgbReserved
== 0
2796 && bi
->bmiColors
[1].rgbRed
== 0xff
2797 && bi
->bmiColors
[1].rgbGreen
== 0xff
2798 && bi
->bmiColors
[1].rgbBlue
== 0xff
2799 && bi
->bmiColors
[1].rgbReserved
== 0);
2802 else if (!monochrome
)
2804 monochrome
= ds
.dsBm
.bmBitsPixel
== 1;
2809 res
= CreateBitmap(desiredx
, desiredy
, 1, 1, NULL
);
2813 HDC screenDC
= GetDC(NULL
);
2814 res
= CreateCompatibleBitmap(screenDC
, desiredx
, desiredy
);
2815 ReleaseDC(NULL
, screenDC
);
2821 /* Only copy the bitmap if it's a DIB section or if it's
2822 compatible to the screen */
2825 if (objSize
== sizeof(DIBSECTION
))
2827 copyContents
= TRUE
;
2831 HDC screenDC
= GetDC(NULL
);
2832 int screen_depth
= GetDeviceCaps(screenDC
, BITSPIXEL
);
2833 ReleaseDC(NULL
, screenDC
);
2835 copyContents
= (ds
.dsBm
.bmBitsPixel
== 1 || ds
.dsBm
.bmBitsPixel
== screen_depth
);
2840 /* The source bitmap may already be selected in a device context,
2841 use GetDIBits/StretchDIBits and not StretchBlt */
2846 dc
= CreateCompatibleDC(NULL
);
2848 bi
->bmiHeader
.biWidth
= ds
.dsBm
.bmWidth
;
2849 bi
->bmiHeader
.biHeight
= ds
.dsBm
.bmHeight
;
2850 bi
->bmiHeader
.biSizeImage
= 0;
2851 bi
->bmiHeader
.biClrUsed
= 0;
2852 bi
->bmiHeader
.biClrImportant
= 0;
2854 /* Fill in biSizeImage */
2855 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
2856 bits
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, bi
->bmiHeader
.biSizeImage
);
2862 /* Get the image bits of the source bitmap */
2863 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, bits
, bi
, DIB_RGB_COLORS
);
2865 /* Copy it to the destination bitmap */
2866 oldBmp
= SelectObject(dc
, res
);
2867 StretchDIBits(dc
, 0, 0, desiredx
, desiredy
,
2868 0, 0, ds
.dsBm
.bmWidth
, ds
.dsBm
.bmHeight
,
2869 bits
, bi
, DIB_RGB_COLORS
, SRCCOPY
);
2870 SelectObject(dc
, oldBmp
);
2872 HeapFree(GetProcessHeap(), 0, bits
);
2878 if (flags
& LR_COPYDELETEORG
)
2883 HeapFree(GetProcessHeap(), 0, bi
);
2889 struct cursoricon_object
*icon
;
2891 int depth
= (flags
& LR_MONOCHROME
) ? 1 : GetDeviceCaps( screen_dc
, BITSPIXEL
);
2893 if (flags
& LR_DEFAULTSIZE
)
2895 if (!desiredx
) desiredx
= GetSystemMetrics( type
== IMAGE_ICON
? SM_CXICON
: SM_CXCURSOR
);
2896 if (!desiredy
) desiredy
= GetSystemMetrics( type
== IMAGE_ICON
? SM_CYICON
: SM_CYCURSOR
);
2899 if (!(icon
= get_icon_ptr( hnd
))) return 0;
2901 if (icon
->rsrc
&& (flags
& LR_COPYFROMRESOURCE
))
2902 res
= CURSORICON_Load( icon
->module
, icon
->resname
, desiredx
, desiredy
, depth
,
2903 !icon
->is_icon
, flags
);
2905 res
= CopyIcon( hnd
); /* FIXME: change size if necessary */
2906 release_icon_ptr( hnd
, icon
);
2908 if (res
&& (flags
& LR_COPYDELETEORG
)) DeleteObject( hnd
);
2916 /******************************************************************************
2917 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2920 * Success: Handle to specified bitmap
2923 HBITMAP WINAPI
LoadBitmapW(
2924 HINSTANCE instance
, /* [in] Handle to application instance */
2925 LPCWSTR name
) /* [in] Address of bitmap resource name */
2927 return LoadImageW( instance
, name
, IMAGE_BITMAP
, 0, 0, 0 );
2930 /**********************************************************************
2931 * LoadBitmapA (USER32.@)
2935 HBITMAP WINAPI
LoadBitmapA( HINSTANCE instance
, LPCSTR name
)
2937 return LoadImageA( instance
, name
, IMAGE_BITMAP
, 0, 0, 0 );