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"
37 #include "wine/exception.h"
38 #include "wine/server.h"
40 #include "user_private.h"
41 #include "wine/list.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(cursor
);
45 WINE_DECLARE_DEBUG_CHANNEL(icon
);
46 WINE_DECLARE_DEBUG_CHANNEL(resource
);
59 } CURSORICONFILEDIRENTRY
;
66 CURSORICONFILEDIRENTRY idEntries
[1];
71 static RECT CURSOR_ClipRect
; /* Cursor clipping rect */
75 static const WCHAR DISPLAYW
[] = {'D','I','S','P','L','A','Y',0};
78 /**********************************************************************
79 * ICONCACHE for cursors/icons loaded with LR_SHARED.
81 typedef struct tagICONCACHE
90 static struct list icon_cache
= LIST_INIT( icon_cache
);
92 static CRITICAL_SECTION IconCrst
;
93 static CRITICAL_SECTION_DEBUG critsect_debug
=
96 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
97 0, 0, { (DWORD_PTR
)(__FILE__
": IconCrst") }
99 static CRITICAL_SECTION IconCrst
= { &critsect_debug
, -1, 0, 0, 0, 0 };
102 /**********************************************************************
103 * User objects management
106 struct cursoricon_frame
108 HBITMAP color
; /* color bitmap */
109 HBITMAP alpha
; /* pre-multiplied alpha bitmap for 32-bpp icons */
110 HBITMAP mask
; /* mask bitmap (followed by color for 1-bpp icons) */
113 struct cursoricon_object
115 struct user_object obj
; /* object header */
116 ULONG_PTR param
; /* opaque param used by 16-bit code */
117 BOOL is_icon
; /* whether icon or cursor */
121 UINT num_frames
; /* number of frames in the icon/cursor */
122 UINT ms_delay
; /* delay between frames (in milliseconds) */
123 struct cursoricon_frame frames
[1]; /* icon frame information */
126 static HICON
alloc_icon_handle( UINT num_frames
)
128 struct cursoricon_object
*obj
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
129 FIELD_OFFSET( struct cursoricon_object
, frames
[num_frames
] ));
132 obj
->num_frames
= num_frames
;
133 return alloc_user_handle( &obj
->obj
, USER_ICON
);
136 static struct cursoricon_object
*get_icon_ptr( HICON handle
)
138 struct cursoricon_object
*obj
= get_user_handle_ptr( handle
, USER_ICON
);
139 if (obj
== OBJ_OTHER_PROCESS
)
141 WARN( "icon handle %p from other process\n", handle
);
147 static void release_icon_ptr( HICON handle
, struct cursoricon_object
*ptr
)
149 release_user_handle_ptr( ptr
);
152 static BOOL
free_icon_handle( HICON handle
)
154 struct cursoricon_object
*obj
= free_user_handle( handle
, USER_ICON
);
156 if (obj
== OBJ_OTHER_PROCESS
) WARN( "icon handle %p from other process\n", handle
);
159 ULONG_PTR param
= obj
->param
;
162 for (i
=0; i
<obj
->num_frames
; i
++)
164 if (obj
->frames
[i
].alpha
) DeleteObject( obj
->frames
[i
].alpha
);
165 if (obj
->frames
[i
].color
) DeleteObject( obj
->frames
[i
].color
);
166 DeleteObject( obj
->frames
[i
].mask
);
168 HeapFree( GetProcessHeap(), 0, obj
);
169 if (wow_handlers
.free_icon_param
&& param
) wow_handlers
.free_icon_param( param
);
170 USER_Driver
->pDestroyCursorIcon( handle
);
176 ULONG_PTR
get_icon_param( HICON handle
)
179 struct cursoricon_object
*obj
= get_user_handle_ptr( handle
, USER_ICON
);
181 if (obj
== OBJ_OTHER_PROCESS
) WARN( "icon handle %p from other process\n", handle
);
185 release_user_handle_ptr( obj
);
190 ULONG_PTR
set_icon_param( HICON handle
, ULONG_PTR param
)
193 struct cursoricon_object
*obj
= get_user_handle_ptr( handle
, USER_ICON
);
195 if (obj
== OBJ_OTHER_PROCESS
) WARN( "icon handle %p from other process\n", handle
);
200 release_user_handle_ptr( obj
);
206 /***********************************************************************
209 * Helper function to map a file to memory:
211 * [RETURN] ptr - pointer to mapped file
212 * [RETURN] filesize - pointer size of file to be stored if not NULL
214 static void *map_fileW( LPCWSTR name
, LPDWORD filesize
)
216 HANDLE hFile
, hMapping
;
219 hFile
= CreateFileW( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
220 OPEN_EXISTING
, FILE_FLAG_RANDOM_ACCESS
, 0 );
221 if (hFile
!= INVALID_HANDLE_VALUE
)
223 hMapping
= CreateFileMappingW( hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
226 ptr
= MapViewOfFile( hMapping
, FILE_MAP_READ
, 0, 0, 0 );
227 CloseHandle( hMapping
);
229 *filesize
= GetFileSize( hFile
, NULL
);
231 CloseHandle( hFile
);
237 /***********************************************************************
238 * get_dib_width_bytes
240 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
242 static int get_dib_width_bytes( int width
, int depth
)
248 case 1: words
= (width
+ 31) / 32; break;
249 case 4: words
= (width
+ 7) / 8; break;
250 case 8: words
= (width
+ 3) / 4; break;
252 case 16: words
= (width
+ 1) / 2; break;
253 case 24: words
= (width
* 3 + 3)/4; break;
255 WARN("(%d): Unsupported depth\n", depth
);
264 /***********************************************************************
267 * Return the size of the bitmap info structure including color table.
269 static int bitmap_info_size( const BITMAPINFO
* info
, WORD coloruse
)
271 unsigned int colors
, size
, masks
= 0;
273 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
275 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
276 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
277 return sizeof(BITMAPCOREHEADER
) + colors
*
278 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
280 else /* assume BITMAPINFOHEADER */
282 colors
= info
->bmiHeader
.biClrUsed
;
283 if (colors
> 256) /* buffer overflow otherwise */
285 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
286 colors
= 1 << info
->bmiHeader
.biBitCount
;
287 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
288 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
289 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
294 /***********************************************************************
297 * Helper function to duplicate a bitmap.
299 static HBITMAP
copy_bitmap( HBITMAP bitmap
)
305 if (!bitmap
) return 0;
306 if (!GetObjectW( bitmap
, sizeof(bmp
), &bmp
)) return 0;
308 src
= CreateCompatibleDC( 0 );
309 dst
= CreateCompatibleDC( 0 );
310 SelectObject( src
, bitmap
);
311 new_bitmap
= CreateCompatibleBitmap( src
, bmp
.bmWidth
, bmp
.bmHeight
);
312 SelectObject( dst
, new_bitmap
);
313 BitBlt( dst
, 0, 0, bmp
.bmWidth
, bmp
.bmHeight
, src
, 0, 0, SRCCOPY
);
320 /***********************************************************************
323 * Returns whether a DIB can be converted to a monochrome DDB.
325 * A DIB can be converted if its color table contains only black and
326 * white. Black must be the first color in the color table.
328 * Note : If the first color in the color table is white followed by
329 * black, we can't convert it to a monochrome DDB with
330 * SetDIBits, because black and white would be inverted.
332 static BOOL
is_dib_monochrome( const BITMAPINFO
* info
)
334 if (info
->bmiHeader
.biBitCount
!= 1) return FALSE
;
336 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
338 const RGBTRIPLE
*rgb
= ((const BITMAPCOREINFO
*)info
)->bmciColors
;
340 /* Check if the first color is black */
341 if ((rgb
->rgbtRed
== 0) && (rgb
->rgbtGreen
== 0) && (rgb
->rgbtBlue
== 0))
345 /* Check if the second color is white */
346 return ((rgb
->rgbtRed
== 0xff) && (rgb
->rgbtGreen
== 0xff)
347 && (rgb
->rgbtBlue
== 0xff));
351 else /* assume BITMAPINFOHEADER */
353 const RGBQUAD
*rgb
= info
->bmiColors
;
355 /* Check if the first color is black */
356 if ((rgb
->rgbRed
== 0) && (rgb
->rgbGreen
== 0) &&
357 (rgb
->rgbBlue
== 0) && (rgb
->rgbReserved
== 0))
361 /* Check if the second color is white */
362 return ((rgb
->rgbRed
== 0xff) && (rgb
->rgbGreen
== 0xff)
363 && (rgb
->rgbBlue
== 0xff) && (rgb
->rgbReserved
== 0));
369 /***********************************************************************
372 * Get the info from a bitmap header.
373 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
375 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
376 LONG
*height
, WORD
*bpp
, DWORD
*compr
)
378 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
380 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
381 *width
= core
->bcWidth
;
382 *height
= core
->bcHeight
;
383 *bpp
= core
->bcBitCount
;
387 else if (header
->biSize
>= sizeof(BITMAPINFOHEADER
))
389 *width
= header
->biWidth
;
390 *height
= header
->biHeight
;
391 *bpp
= header
->biBitCount
;
392 *compr
= header
->biCompression
;
395 ERR("(%d): unknown/wrong size for header\n", header
->biSize
);
399 /**********************************************************************
400 * CURSORICON_FindSharedIcon
402 static HICON
CURSORICON_FindSharedIcon( HMODULE hModule
, HRSRC hRsrc
)
407 EnterCriticalSection( &IconCrst
);
409 LIST_FOR_EACH_ENTRY( ptr
, &icon_cache
, ICONCACHE
, entry
)
410 if ( ptr
->hModule
== hModule
&& ptr
->hRsrc
== hRsrc
)
416 LeaveCriticalSection( &IconCrst
);
421 /*************************************************************************
422 * CURSORICON_FindCache
424 * Given a handle, find the corresponding cache element
427 * Handle [I] handle to an Image
430 * Success: The cache entry
434 static ICONCACHE
* CURSORICON_FindCache(HICON hIcon
)
437 ICONCACHE
*pRet
=NULL
;
439 EnterCriticalSection( &IconCrst
);
441 LIST_FOR_EACH_ENTRY( ptr
, &icon_cache
, ICONCACHE
, entry
)
443 if ( hIcon
== ptr
->hIcon
)
450 LeaveCriticalSection( &IconCrst
);
455 /**********************************************************************
456 * CURSORICON_AddSharedIcon
458 static void CURSORICON_AddSharedIcon( HMODULE hModule
, HRSRC hRsrc
, HRSRC hGroupRsrc
, HICON hIcon
)
460 ICONCACHE
*ptr
= HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE
) );
463 ptr
->hModule
= hModule
;
466 ptr
->hGroupRsrc
= hGroupRsrc
;
468 EnterCriticalSection( &IconCrst
);
469 list_add_head( &icon_cache
, &ptr
->entry
);
470 LeaveCriticalSection( &IconCrst
);
473 /**********************************************************************
476 BOOL
get_icon_size( HICON handle
, SIZE
*size
)
478 struct cursoricon_object
*info
;
480 if (!(info
= get_icon_ptr( handle
))) return FALSE
;
481 size
->cx
= info
->width
;
482 size
->cy
= info
->height
;
483 release_icon_ptr( handle
, info
);
488 * The following macro functions account for the irregularities of
489 * accessing cursor and icon resources in files and resource entries.
491 typedef BOOL (*fnGetCIEntry
)( LPVOID dir
, int n
,
492 int *width
, int *height
, int *bits
);
494 /**********************************************************************
495 * CURSORICON_FindBestIcon
497 * Find the icon closest to the requested size and bit depth.
499 static int CURSORICON_FindBestIcon( LPVOID dir
, fnGetCIEntry get_entry
,
500 int width
, int height
, int depth
)
502 int i
, cx
, cy
, bits
, bestEntry
= -1;
503 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
504 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
507 iTotalDiff
= 0xFFFFFFFF;
508 iColorDiff
= 0xFFFFFFFF;
509 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
511 iTempXDiff
= abs(width
- cx
);
512 iTempYDiff
= abs(height
- cy
);
514 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
518 iTotalDiff
= iXDiff
+ iYDiff
;
522 /* Find Best Colors for Best Fit */
523 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
525 if(abs(width
- cx
) == iXDiff
&& abs(height
- cy
) == iYDiff
)
527 iTempColorDiff
= abs(depth
- bits
);
528 if(iColorDiff
> iTempColorDiff
)
531 iColorDiff
= iTempColorDiff
;
539 static BOOL
CURSORICON_GetResIconEntry( LPVOID dir
, int n
,
540 int *width
, int *height
, int *bits
)
542 CURSORICONDIR
*resdir
= dir
;
545 if ( resdir
->idCount
<= n
)
547 icon
= &resdir
->idEntries
[n
].ResInfo
.icon
;
548 *width
= icon
->bWidth
;
549 *height
= icon
->bHeight
;
550 *bits
= resdir
->idEntries
[n
].wBitCount
;
554 /**********************************************************************
555 * CURSORICON_FindBestCursor
557 * Find the cursor closest to the requested size.
559 * FIXME: parameter 'color' ignored.
561 static int CURSORICON_FindBestCursor( LPVOID dir
, fnGetCIEntry get_entry
,
562 int width
, int height
, int depth
)
564 int i
, maxwidth
, maxheight
, cx
, cy
, bits
, bestEntry
= -1;
566 /* Double height to account for AND and XOR masks */
570 /* First find the largest one smaller than or equal to the requested size*/
572 maxwidth
= maxheight
= 0;
573 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
575 if ((cx
<= width
) && (cy
<= height
) &&
576 (cx
> maxwidth
) && (cy
> maxheight
))
583 if (bestEntry
!= -1) return bestEntry
;
585 /* Now find the smallest one larger than the requested size */
587 maxwidth
= maxheight
= 255;
588 for ( i
= 0; get_entry( dir
, i
, &cx
, &cy
, &bits
); i
++ )
590 if (((cx
< maxwidth
) && (cy
< maxheight
)) || (bestEntry
== -1))
601 static BOOL
CURSORICON_GetResCursorEntry( LPVOID dir
, int n
,
602 int *width
, int *height
, int *bits
)
604 CURSORICONDIR
*resdir
= dir
;
607 if ( resdir
->idCount
<= n
)
609 cursor
= &resdir
->idEntries
[n
].ResInfo
.cursor
;
610 *width
= cursor
->wWidth
;
611 *height
= cursor
->wHeight
;
612 *bits
= resdir
->idEntries
[n
].wBitCount
;
616 static CURSORICONDIRENTRY
*CURSORICON_FindBestIconRes( CURSORICONDIR
* dir
,
617 int width
, int height
, int depth
)
621 n
= CURSORICON_FindBestIcon( dir
, CURSORICON_GetResIconEntry
,
622 width
, height
, depth
);
625 return &dir
->idEntries
[n
];
628 static CURSORICONDIRENTRY
*CURSORICON_FindBestCursorRes( CURSORICONDIR
*dir
,
629 int width
, int height
, int depth
)
631 int n
= CURSORICON_FindBestCursor( dir
, CURSORICON_GetResCursorEntry
,
632 width
, height
, depth
);
635 return &dir
->idEntries
[n
];
638 static BOOL
CURSORICON_GetFileEntry( LPVOID dir
, int n
,
639 int *width
, int *height
, int *bits
)
641 CURSORICONFILEDIR
*filedir
= dir
;
642 CURSORICONFILEDIRENTRY
*entry
;
643 BITMAPINFOHEADER
*info
;
645 if ( filedir
->idCount
<= n
)
647 entry
= &filedir
->idEntries
[n
];
648 /* FIXME: check against file size */
649 info
= (BITMAPINFOHEADER
*)((char *)dir
+ entry
->dwDIBOffset
);
650 *width
= entry
->bWidth
;
651 *height
= entry
->bHeight
;
652 *bits
= info
->biBitCount
;
656 static CURSORICONFILEDIRENTRY
*CURSORICON_FindBestCursorFile( CURSORICONFILEDIR
*dir
,
657 int width
, int height
, int depth
)
659 int n
= CURSORICON_FindBestCursor( dir
, CURSORICON_GetFileEntry
,
660 width
, height
, depth
);
663 return &dir
->idEntries
[n
];
666 static CURSORICONFILEDIRENTRY
*CURSORICON_FindBestIconFile( CURSORICONFILEDIR
*dir
,
667 int width
, int height
, int depth
)
669 int n
= CURSORICON_FindBestIcon( dir
, CURSORICON_GetFileEntry
,
670 width
, height
, depth
);
673 return &dir
->idEntries
[n
];
676 /***********************************************************************
679 static BOOL
bmi_has_alpha( const BITMAPINFO
*info
, const void *bits
)
682 BOOL has_alpha
= FALSE
;
683 const unsigned char *ptr
= bits
;
685 if (info
->bmiHeader
.biBitCount
!= 32) return FALSE
;
686 for (i
= 0; i
< info
->bmiHeader
.biWidth
* abs(info
->bmiHeader
.biHeight
); i
++, ptr
+= 4)
687 if ((has_alpha
= (ptr
[3] != 0))) break;
691 /***********************************************************************
692 * create_alpha_bitmap
694 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
696 static HBITMAP
create_alpha_bitmap( HBITMAP color
, HBITMAP mask
,
697 const BITMAPINFO
*src_info
, const void *color_bits
)
700 BITMAPINFO
*info
= NULL
;
707 if (!GetObjectW( color
, sizeof(bm
), &bm
)) return 0;
708 if (bm
.bmBitsPixel
!= 32) return 0;
710 if (!(hdc
= CreateCompatibleDC( 0 ))) return 0;
711 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )))) goto done
;
712 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
713 info
->bmiHeader
.biWidth
= bm
.bmWidth
;
714 info
->bmiHeader
.biHeight
= -bm
.bmHeight
;
715 info
->bmiHeader
.biPlanes
= 1;
716 info
->bmiHeader
.biBitCount
= 32;
717 info
->bmiHeader
.biCompression
= BI_RGB
;
718 info
->bmiHeader
.biSizeImage
= bm
.bmWidth
* bm
.bmHeight
* 4;
719 info
->bmiHeader
.biXPelsPerMeter
= 0;
720 info
->bmiHeader
.biYPelsPerMeter
= 0;
721 info
->bmiHeader
.biClrUsed
= 0;
722 info
->bmiHeader
.biClrImportant
= 0;
723 if (!(alpha
= CreateDIBSection( hdc
, info
, DIB_RGB_COLORS
, &bits
, NULL
, 0 ))) goto done
;
727 SelectObject( hdc
, alpha
);
728 StretchDIBits( hdc
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
729 0, 0, src_info
->bmiHeader
.biWidth
, src_info
->bmiHeader
.biHeight
,
730 color_bits
, src_info
, DIB_RGB_COLORS
, SRCCOPY
);
735 GetDIBits( hdc
, color
, 0, bm
.bmHeight
, bits
, info
, DIB_RGB_COLORS
);
736 if (!bmi_has_alpha( info
, bits
))
738 DeleteObject( alpha
);
744 /* pre-multiply by alpha */
745 for (i
= 0, ptr
= bits
; i
< bm
.bmWidth
* bm
.bmHeight
; i
++, ptr
+= 4)
747 unsigned int alpha
= ptr
[3];
748 ptr
[0] = ptr
[0] * alpha
/ 255;
749 ptr
[1] = ptr
[1] * alpha
/ 255;
750 ptr
[2] = ptr
[2] * alpha
/ 255;
755 HeapFree( GetProcessHeap(), 0, info
);
760 /***********************************************************************
761 * create_icon_bitmaps
763 * Create the color, mask and alpha bitmaps from the DIB info.
765 static BOOL
create_icon_bitmaps( const BITMAPINFO
*bmi
, int width
, int height
,
766 HBITMAP
*color
, HBITMAP
*mask
, HBITMAP
*alpha
)
768 BOOL monochrome
= is_dib_monochrome( bmi
);
769 unsigned int size
= bitmap_info_size( bmi
, DIB_RGB_COLORS
);
771 void *color_bits
, *mask_bits
;
775 if (!(info
= HeapAlloc( GetProcessHeap(), 0, max( size
, FIELD_OFFSET( BITMAPINFO
, bmiColors
[2] )))))
777 if (!(hdc
= CreateCompatibleDC( 0 ))) goto done
;
779 memcpy( info
, bmi
, size
);
780 info
->bmiHeader
.biHeight
/= 2;
782 color_bits
= (char *)bmi
+ size
;
783 mask_bits
= (char *)color_bits
+
784 get_dib_width_bytes( bmi
->bmiHeader
.biWidth
,
785 bmi
->bmiHeader
.biBitCount
) * abs(info
->bmiHeader
.biHeight
);
790 if (!(*mask
= CreateBitmap( width
, height
* 2, 1, 1, NULL
))) goto done
;
793 /* copy color data into second half of mask bitmap */
794 SelectObject( hdc
, *mask
);
795 StretchDIBits( hdc
, 0, height
, width
, height
,
796 0, 0, info
->bmiHeader
.biWidth
, info
->bmiHeader
.biHeight
,
797 color_bits
, info
, DIB_RGB_COLORS
, SRCCOPY
);
801 if (!(*mask
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
802 if (!(*color
= CreateBitmap( width
, height
, GetDeviceCaps( screen_dc
, PLANES
),
803 GetDeviceCaps( screen_dc
, BITSPIXEL
), NULL
)))
805 DeleteObject( *mask
);
808 SelectObject( hdc
, *color
);
809 StretchDIBits( hdc
, 0, 0, width
, height
,
810 0, 0, info
->bmiHeader
.biWidth
, info
->bmiHeader
.biHeight
,
811 color_bits
, info
, DIB_RGB_COLORS
, SRCCOPY
);
813 if (bmi_has_alpha( info
, color_bits
))
814 *alpha
= create_alpha_bitmap( *color
, *mask
, info
, color_bits
);
816 /* convert info to monochrome to copy the mask */
817 info
->bmiHeader
.biBitCount
= 1;
818 if (info
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
))
820 RGBQUAD
*rgb
= info
->bmiColors
;
822 info
->bmiHeader
.biClrUsed
= info
->bmiHeader
.biClrImportant
= 2;
823 rgb
[0].rgbBlue
= rgb
[0].rgbGreen
= rgb
[0].rgbRed
= 0x00;
824 rgb
[1].rgbBlue
= rgb
[1].rgbGreen
= rgb
[1].rgbRed
= 0xff;
825 rgb
[0].rgbReserved
= rgb
[1].rgbReserved
= 0;
829 RGBTRIPLE
*rgb
= (RGBTRIPLE
*)(((BITMAPCOREHEADER
*)info
) + 1);
831 rgb
[0].rgbtBlue
= rgb
[0].rgbtGreen
= rgb
[0].rgbtRed
= 0x00;
832 rgb
[1].rgbtBlue
= rgb
[1].rgbtGreen
= rgb
[1].rgbtRed
= 0xff;
836 SelectObject( hdc
, *mask
);
837 StretchDIBits( hdc
, 0, 0, width
, height
,
838 0, 0, info
->bmiHeader
.biWidth
, info
->bmiHeader
.biHeight
,
839 mask_bits
, info
, DIB_RGB_COLORS
, SRCCOPY
);
844 HeapFree( GetProcessHeap(), 0, info
);
848 static HICON
CURSORICON_CreateIconFromBMI( BITMAPINFO
*bmi
,
849 POINT hotspot
, BOOL bIcon
,
851 INT width
, INT height
,
855 HBITMAP color
= 0, mask
= 0, alpha
= 0;
858 if (dwVersion
== 0x00020000)
860 FIXME_(cursor
)("\t2.xx resources are not supported\n");
864 /* Check bitmap header */
866 if ( (bmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) &&
867 (bmi
->bmiHeader
.biSize
!= sizeof(BITMAPINFOHEADER
) ||
868 bmi
->bmiHeader
.biCompression
!= BI_RGB
) )
870 WARN_(cursor
)("\tinvalid resource bitmap header.\n");
874 if (!width
) width
= bmi
->bmiHeader
.biWidth
;
875 if (!height
) height
= bmi
->bmiHeader
.biHeight
/2;
876 do_stretch
= (bmi
->bmiHeader
.biHeight
/2 != height
) ||
877 (bmi
->bmiHeader
.biWidth
!= width
);
879 /* Scale the hotspot */
882 hotspot
.x
= width
/ 2;
883 hotspot
.y
= height
/ 2;
887 hotspot
.x
= (hotspot
.x
* width
) / bmi
->bmiHeader
.biWidth
;
888 hotspot
.y
= (hotspot
.y
* height
) / (bmi
->bmiHeader
.biHeight
/ 2);
891 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
892 if (!screen_dc
) return 0;
894 if (!create_icon_bitmaps( bmi
, width
, height
, &color
, &mask
, &alpha
)) return 0;
896 hObj
= alloc_icon_handle(1);
899 struct cursoricon_object
*info
= get_icon_ptr( hObj
);
901 info
->is_icon
= bIcon
;
902 info
->hotspot
= hotspot
;
904 info
->height
= height
;
905 info
->frames
[0].color
= color
;
906 info
->frames
[0].mask
= mask
;
907 info
->frames
[0].alpha
= alpha
;
908 release_icon_ptr( hObj
, info
);
912 DeleteObject( color
);
913 DeleteObject( alpha
);
914 DeleteObject( mask
);
920 /**********************************************************************
921 * .ANI cursor support
923 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
924 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
925 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
927 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
928 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
929 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
930 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
931 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
932 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
934 #define ANI_FLAG_ICON 0x1
935 #define ANI_FLAG_SEQUENCE 0x2
951 const unsigned char *data
;
954 static void dump_ani_header( const ani_header
*header
)
956 TRACE(" header size: %d\n", header
->header_size
);
957 TRACE(" frames: %d\n", header
->num_frames
);
958 TRACE(" steps: %d\n", header
->num_steps
);
959 TRACE(" width: %d\n", header
->width
);
960 TRACE(" height: %d\n", header
->height
);
961 TRACE(" bpp: %d\n", header
->bpp
);
962 TRACE(" planes: %d\n", header
->num_planes
);
963 TRACE(" display rate: %d\n", header
->display_rate
);
964 TRACE(" flags: 0x%08x\n", header
->flags
);
986 static void riff_find_chunk( DWORD chunk_id
, DWORD chunk_type
, const riff_chunk_t
*parent_chunk
, riff_chunk_t
*chunk
)
988 const unsigned char *ptr
= parent_chunk
->data
;
989 const unsigned char *end
= parent_chunk
->data
+ (parent_chunk
->data_size
- (2 * sizeof(DWORD
)));
991 if (chunk_type
== ANI_LIST_ID
|| chunk_type
== ANI_RIFF_ID
) end
-= sizeof(DWORD
);
995 if ((!chunk_type
&& *(const DWORD
*)ptr
== chunk_id
)
996 || (chunk_type
&& *(const DWORD
*)ptr
== chunk_type
&& *((const DWORD
*)ptr
+ 2) == chunk_id
))
998 ptr
+= sizeof(DWORD
);
999 chunk
->data_size
= (*(const DWORD
*)ptr
+ 1) & ~1;
1000 ptr
+= sizeof(DWORD
);
1001 if (chunk_type
== ANI_LIST_ID
|| chunk_type
== ANI_RIFF_ID
) ptr
+= sizeof(DWORD
);
1007 ptr
+= sizeof(DWORD
);
1008 ptr
+= (*(const DWORD
*)ptr
+ 1) & ~1;
1009 ptr
+= sizeof(DWORD
);
1017 * RIFF:'ACON' RIFF chunk
1018 * |- CHUNK:'anih' Header
1019 * |- CHUNK:'seq ' Sequence information (optional)
1020 * \- LIST:'fram' Frame list
1021 * |- CHUNK:icon Cursor frames
1026 static HCURSOR
CURSORICON_CreateIconFromANI( const LPBYTE bits
, DWORD bits_size
,
1027 INT width
, INT height
, INT depth
)
1029 struct cursoricon_object
*info
;
1030 ani_header header
= {0};
1034 riff_chunk_t root_chunk
= { bits_size
, bits
};
1035 riff_chunk_t ACON_chunk
= {0};
1036 riff_chunk_t anih_chunk
= {0};
1037 riff_chunk_t fram_chunk
= {0};
1038 const unsigned char *icon_chunk
;
1039 const unsigned char *icon_data
;
1041 TRACE("bits %p, bits_size %d\n", bits
, bits_size
);
1043 riff_find_chunk( ANI_ACON_ID
, ANI_RIFF_ID
, &root_chunk
, &ACON_chunk
);
1044 if (!ACON_chunk
.data
)
1046 ERR("Failed to get root chunk.\n");
1050 riff_find_chunk( ANI_anih_ID
, 0, &ACON_chunk
, &anih_chunk
);
1051 if (!anih_chunk
.data
)
1053 ERR("Failed to get 'anih' chunk.\n");
1056 memcpy( &header
, anih_chunk
.data
, sizeof(header
) );
1057 dump_ani_header( &header
);
1059 riff_find_chunk( ANI_fram_ID
, ANI_LIST_ID
, &ACON_chunk
, &fram_chunk
);
1060 if (!fram_chunk
.data
)
1062 ERR("Failed to get icon list.\n");
1066 cursor
= alloc_icon_handle( header
.num_frames
);
1067 if (!cursor
) return 0;
1069 info
= get_icon_ptr( cursor
);
1070 info
->is_icon
= FALSE
;
1072 /* The .ANI stores the display rate in 1/60s, we store the delay between frames in ms */
1073 info
->ms_delay
= (100 * header
.display_rate
) / 6;
1075 icon_chunk
= fram_chunk
.data
;
1076 icon_data
= fram_chunk
.data
+ (2 * sizeof(DWORD
));
1077 for (i
=0; i
<header
.num_frames
; i
++)
1079 DWORD chunk_size
= *(DWORD
*)(icon_chunk
+ sizeof(DWORD
));
1080 struct cursoricon_frame
*frame
= &info
->frames
[i
];
1081 CURSORICONFILEDIRENTRY
*entry
;
1084 entry
= CURSORICON_FindBestIconFile( (CURSORICONFILEDIR
*) icon_data
,
1085 width
, height
, depth
);
1087 bmi
= (BITMAPINFO
*) (icon_data
+ entry
->dwDIBOffset
);
1088 info
->hotspot
.x
= entry
->xHotspot
;
1089 info
->hotspot
.y
= entry
->yHotspot
;
1090 if (!header
.width
|| !header
.height
)
1092 header
.width
= entry
->bWidth
;
1093 header
.height
= entry
->bHeight
;
1096 /* Grab a frame from the animation */
1097 if (!create_icon_bitmaps( bmi
, header
.width
, header
.height
,
1098 &frame
->color
, &frame
->mask
, &frame
->alpha
))
1100 FIXME_(cursor
)("failed to convert animated cursor frame.\n");
1104 FIXME_(cursor
)("Completely failed to create animated cursor!\n");
1105 info
->num_frames
= 0;
1106 release_icon_ptr( cursor
, info
);
1107 free_icon_handle( cursor
);
1113 /* Advance to the next chunk */
1114 icon_chunk
+= chunk_size
+ (2 * sizeof(DWORD
));
1115 icon_data
= icon_chunk
+ (2 * sizeof(DWORD
));
1118 /* There was an error but we at least decoded the first frame, so just use that frame */
1121 FIXME_(cursor
)("Error creating animated cursor, only using first frame!\n");
1122 for (i
=1; i
<info
->num_frames
; i
++)
1124 if (info
->frames
[i
].mask
) DeleteObject( info
->frames
[i
].mask
);
1125 if (info
->frames
[i
].color
) DeleteObject( info
->frames
[i
].color
);
1126 if (info
->frames
[i
].alpha
) DeleteObject( info
->frames
[i
].alpha
);
1128 info
->num_frames
= 1;
1131 info
->width
= header
.width
;
1132 info
->height
= header
.height
;
1133 release_icon_ptr( cursor
, info
);
1139 /**********************************************************************
1140 * CreateIconFromResourceEx (USER32.@)
1142 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1143 * with cbSize parameter as well.
1145 HICON WINAPI
CreateIconFromResourceEx( LPBYTE bits
, UINT cbSize
,
1146 BOOL bIcon
, DWORD dwVersion
,
1147 INT width
, INT height
,
1154 TRACE_(cursor
)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1155 bits
, cbSize
, dwVersion
, width
, height
,
1156 bIcon
? "icon" : "cursor", (cFlag
& LR_MONOCHROME
) ? "mono" : "" );
1158 if (!bits
) return 0;
1162 hotspot
.x
= width
/ 2;
1163 hotspot
.y
= height
/ 2;
1164 bmi
= (BITMAPINFO
*)bits
;
1166 else /* get the hotspot */
1168 SHORT
*pt
= (SHORT
*)bits
;
1171 bmi
= (BITMAPINFO
*)(pt
+ 2);
1174 icon
= CURSORICON_CreateIconFromBMI( bmi
, hotspot
, bIcon
, dwVersion
,
1175 width
, height
, cFlag
);
1176 if (icon
) USER_Driver
->pCreateCursorIcon( icon
);
1181 /**********************************************************************
1182 * CreateIconFromResource (USER32.@)
1184 HICON WINAPI
CreateIconFromResource( LPBYTE bits
, UINT cbSize
,
1185 BOOL bIcon
, DWORD dwVersion
)
1187 return CreateIconFromResourceEx( bits
, cbSize
, bIcon
, dwVersion
, 0,0,0);
1191 static HICON
CURSORICON_LoadFromFile( LPCWSTR filename
,
1192 INT width
, INT height
, INT depth
,
1193 BOOL fCursor
, UINT loadflags
)
1195 CURSORICONFILEDIRENTRY
*entry
;
1196 CURSORICONFILEDIR
*dir
;
1202 TRACE("loading %s\n", debugstr_w( filename
));
1204 bits
= map_fileW( filename
, &filesize
);
1208 /* Check for .ani. */
1209 if (memcmp( bits
, "RIFF", 4 ) == 0)
1211 hIcon
= CURSORICON_CreateIconFromANI( bits
, filesize
, width
, height
,
1216 dir
= (CURSORICONFILEDIR
*) bits
;
1217 if ( filesize
< sizeof(*dir
) )
1220 if ( filesize
< (sizeof(*dir
) + sizeof(dir
->idEntries
[0])*(dir
->idCount
-1)) )
1224 entry
= CURSORICON_FindBestCursorFile( dir
, width
, height
, depth
);
1226 entry
= CURSORICON_FindBestIconFile( dir
, width
, height
, depth
);
1231 /* check that we don't run off the end of the file */
1232 if ( entry
->dwDIBOffset
> filesize
)
1234 if ( entry
->dwDIBOffset
+ entry
->dwDIBSize
> filesize
)
1237 hotspot
.x
= entry
->xHotspot
;
1238 hotspot
.y
= entry
->yHotspot
;
1239 hIcon
= CURSORICON_CreateIconFromBMI( (BITMAPINFO
*)&bits
[entry
->dwDIBOffset
],
1240 hotspot
, !fCursor
, 0x00030000,
1241 width
, height
, loadflags
);
1243 TRACE("loaded %s -> %p\n", debugstr_w( filename
), hIcon
);
1244 UnmapViewOfFile( bits
);
1245 if (hIcon
) USER_Driver
->pCreateCursorIcon( hIcon
);
1249 /**********************************************************************
1252 * Load a cursor or icon from resource or file.
1254 static HICON
CURSORICON_Load(HINSTANCE hInstance
, LPCWSTR name
,
1255 INT width
, INT height
, INT depth
,
1256 BOOL fCursor
, UINT loadflags
)
1260 HRSRC hRsrc
, hGroupRsrc
;
1262 CURSORICONDIRENTRY
*dirEntry
;
1267 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1268 hInstance
, debugstr_w(name
), width
, height
, depth
, fCursor
, loadflags
);
1270 if ( loadflags
& LR_LOADFROMFILE
) /* Load from file */
1271 return CURSORICON_LoadFromFile( name
, width
, height
, depth
, fCursor
, loadflags
);
1273 if (!hInstance
) hInstance
= user32_module
; /* Load OEM cursor/icon */
1275 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1276 if ((ULONG_PTR
)hInstance
>> 16 == 0) loadflags
&= ~LR_SHARED
;
1278 /* Get directory resource ID */
1280 if (!(hRsrc
= FindResourceW( hInstance
, name
,
1281 (LPWSTR
)(fCursor
? RT_GROUP_CURSOR
: RT_GROUP_ICON
) )))
1285 /* Find the best entry in the directory */
1287 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
1288 if (!(dir
= LockResource( handle
))) return 0;
1290 dirEntry
= CURSORICON_FindBestCursorRes( dir
, width
, height
, depth
);
1292 dirEntry
= CURSORICON_FindBestIconRes( dir
, width
, height
, depth
);
1293 if (!dirEntry
) return 0;
1294 wResId
= dirEntry
->wResId
;
1295 dwBytesInRes
= dirEntry
->dwBytesInRes
;
1296 FreeResource( handle
);
1298 /* Load the resource */
1300 if (!(hRsrc
= FindResourceW(hInstance
,MAKEINTRESOURCEW(wResId
),
1301 (LPWSTR
)(fCursor
? RT_CURSOR
: RT_ICON
) ))) return 0;
1303 /* If shared icon, check whether it was already loaded */
1304 if ( (loadflags
& LR_SHARED
)
1305 && (hIcon
= CURSORICON_FindSharedIcon( hInstance
, hRsrc
) ) != 0 )
1308 if (!(handle
= LoadResource( hInstance
, hRsrc
))) return 0;
1309 bits
= LockResource( handle
);
1310 hIcon
= CreateIconFromResourceEx( bits
, dwBytesInRes
,
1311 !fCursor
, 0x00030000, width
, height
, loadflags
);
1312 FreeResource( handle
);
1314 /* If shared icon, add to icon cache */
1316 if ( hIcon
&& (loadflags
& LR_SHARED
) )
1317 CURSORICON_AddSharedIcon( hInstance
, hRsrc
, hGroupRsrc
, hIcon
);
1323 /*************************************************************************
1324 * CURSORICON_ExtCopy
1326 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1329 * Handle [I] handle to an Image
1330 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1331 * iDesiredCX [I] The Desired width of the Image
1332 * iDesiredCY [I] The desired height of the Image
1333 * nFlags [I] The flags from CopyImage
1336 * Success: The new handle of the Image
1339 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1340 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1341 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1346 static HICON
CURSORICON_ExtCopy(HICON hIcon
, UINT nType
,
1347 INT iDesiredCX
, INT iDesiredCY
,
1352 TRACE_(icon
)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1353 hIcon
, nType
, iDesiredCX
, iDesiredCY
, nFlags
);
1360 /* Best Fit or Monochrome */
1361 if( (nFlags
& LR_COPYFROMRESOURCE
1362 && (iDesiredCX
> 0 || iDesiredCY
> 0))
1363 || nFlags
& LR_MONOCHROME
)
1365 ICONCACHE
* pIconCache
= CURSORICON_FindCache(hIcon
);
1367 /* Not Found in Cache, then do a straight copy
1369 if(pIconCache
== NULL
)
1371 hNew
= CopyIcon( hIcon
);
1372 if(nFlags
& LR_COPYFROMRESOURCE
)
1374 TRACE_(icon
)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1379 int iTargetCY
= iDesiredCY
, iTargetCX
= iDesiredCX
;
1385 CURSORICONDIR
*pDir
;
1386 CURSORICONDIRENTRY
*pDirEntry
;
1387 BOOL bIsIcon
= (nType
== IMAGE_ICON
);
1389 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1391 if(((nFlags
& LR_MONOCHROME
) && !(nFlags
& LR_COPYFROMRESOURCE
))
1392 || (iDesiredCX
== 0 && iDesiredCY
== 0))
1394 iDesiredCY
= GetSystemMetrics(bIsIcon
?
1395 SM_CYICON
: SM_CYCURSOR
);
1396 iDesiredCX
= GetSystemMetrics(bIsIcon
?
1397 SM_CXICON
: SM_CXCURSOR
);
1400 /* Retrieve the CURSORICONDIRENTRY
1402 if (!(hMem
= LoadResource( pIconCache
->hModule
,
1403 pIconCache
->hGroupRsrc
)))
1407 if (!(pDir
= LockResource( hMem
)))
1416 pDirEntry
= CURSORICON_FindBestIconRes(
1417 pDir
, iDesiredCX
, iDesiredCY
, 256 );
1421 pDirEntry
= CURSORICON_FindBestCursorRes(
1422 pDir
, iDesiredCX
, iDesiredCY
, 1);
1425 wResId
= pDirEntry
->wResId
;
1426 dwBytesInRes
= pDirEntry
->dwBytesInRes
;
1429 TRACE_(icon
)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1430 wResId
, dwBytesInRes
, pDirEntry
->ResInfo
.icon
.bWidth
,
1431 pDirEntry
->ResInfo
.icon
.bHeight
, iDesiredCX
, iDesiredCY
);
1435 if (!(hRsrc
= FindResourceW(pIconCache
->hModule
,
1436 MAKEINTRESOURCEW(wResId
), (LPWSTR
)(bIsIcon
? RT_ICON
: RT_CURSOR
))))
1440 if (!(hMem
= LoadResource( pIconCache
->hModule
, hRsrc
)))
1445 pBits
= LockResource( hMem
);
1447 if(nFlags
& LR_DEFAULTSIZE
)
1449 iTargetCY
= GetSystemMetrics(SM_CYICON
);
1450 iTargetCX
= GetSystemMetrics(SM_CXICON
);
1453 /* Create a New Icon with the proper dimension
1455 hNew
= CreateIconFromResourceEx( pBits
, dwBytesInRes
,
1456 bIsIcon
, 0x00030000, iTargetCX
, iTargetCY
, nFlags
);
1460 else hNew
= CopyIcon( hIcon
);
1465 /***********************************************************************
1466 * CreateCursor (USER32.@)
1468 HCURSOR WINAPI
CreateCursor( HINSTANCE hInstance
,
1469 INT xHotSpot
, INT yHotSpot
,
1470 INT nWidth
, INT nHeight
,
1471 LPCVOID lpANDbits
, LPCVOID lpXORbits
)
1476 TRACE_(cursor
)("%dx%d spot=%d,%d xor=%p and=%p\n",
1477 nWidth
, nHeight
, xHotSpot
, yHotSpot
, lpXORbits
, lpANDbits
);
1480 info
.xHotspot
= xHotSpot
;
1481 info
.yHotspot
= yHotSpot
;
1482 info
.hbmMask
= CreateBitmap( nWidth
, nHeight
, 1, 1, lpANDbits
);
1483 info
.hbmColor
= CreateBitmap( nWidth
, nHeight
, 1, 1, lpXORbits
);
1484 hCursor
= CreateIconIndirect( &info
);
1485 DeleteObject( info
.hbmMask
);
1486 DeleteObject( info
.hbmColor
);
1491 /***********************************************************************
1492 * CreateIcon (USER32.@)
1494 * Creates an icon based on the specified bitmaps. The bitmaps must be
1495 * provided in a device dependent format and will be resized to
1496 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1497 * depth. The provided bitmaps must be top-down bitmaps.
1498 * Although Windows does not support 15bpp(*) this API must support it
1499 * for Winelib applications.
1501 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1505 * Success: handle to an icon
1508 * FIXME: Do we need to resize the bitmaps?
1510 HICON WINAPI
CreateIcon(
1511 HINSTANCE hInstance
, /* [in] the application's hInstance */
1512 INT nWidth
, /* [in] the width of the provided bitmaps */
1513 INT nHeight
, /* [in] the height of the provided bitmaps */
1514 BYTE bPlanes
, /* [in] the number of planes in the provided bitmaps */
1515 BYTE bBitsPixel
, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1516 LPCVOID lpANDbits
, /* [in] a monochrome bitmap representing the icon's mask */
1517 LPCVOID lpXORbits
) /* [in] the icon's 'color' bitmap */
1522 TRACE_(icon
)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1523 nWidth
, nHeight
, bPlanes
, bBitsPixel
, lpXORbits
, lpANDbits
);
1526 iinfo
.xHotspot
= nWidth
/ 2;
1527 iinfo
.yHotspot
= nHeight
/ 2;
1528 iinfo
.hbmMask
= CreateBitmap( nWidth
, nHeight
, 1, 1, lpANDbits
);
1529 iinfo
.hbmColor
= CreateBitmap( nWidth
, nHeight
, bPlanes
, bBitsPixel
, lpXORbits
);
1531 hIcon
= CreateIconIndirect( &iinfo
);
1533 DeleteObject( iinfo
.hbmMask
);
1534 DeleteObject( iinfo
.hbmColor
);
1540 /***********************************************************************
1541 * CopyIcon (USER32.@)
1543 HICON WINAPI
CopyIcon( HICON hIcon
)
1545 struct cursoricon_object
*ptrOld
, *ptrNew
;
1548 if (!(ptrOld
= get_icon_ptr( hIcon
))) return 0;
1549 if ((hNew
= alloc_icon_handle(1)))
1551 ptrNew
= get_icon_ptr( hNew
);
1552 ptrNew
->is_icon
= ptrOld
->is_icon
;
1553 ptrNew
->width
= ptrOld
->width
;
1554 ptrNew
->height
= ptrOld
->height
;
1555 ptrNew
->hotspot
= ptrOld
->hotspot
;
1556 ptrNew
->frames
[0].mask
= copy_bitmap( ptrOld
->frames
[0].mask
);
1557 ptrNew
->frames
[0].color
= copy_bitmap( ptrOld
->frames
[0].color
);
1558 ptrNew
->frames
[0].alpha
= copy_bitmap( ptrOld
->frames
[0].alpha
);
1559 release_icon_ptr( hNew
, ptrNew
);
1561 release_icon_ptr( hIcon
, ptrOld
);
1562 if (hNew
) USER_Driver
->pCreateCursorIcon( hNew
);
1567 /***********************************************************************
1568 * DestroyIcon (USER32.@)
1570 BOOL WINAPI
DestroyIcon( HICON hIcon
)
1572 TRACE_(icon
)("%p\n", hIcon
);
1574 if (!CURSORICON_FindCache( hIcon
)) free_icon_handle( hIcon
);
1579 /***********************************************************************
1580 * DestroyCursor (USER32.@)
1582 BOOL WINAPI
DestroyCursor( HCURSOR hCursor
)
1584 if (GetCursor() == hCursor
)
1586 WARN_(cursor
)("Destroying active cursor!\n" );
1589 return DestroyIcon( hCursor
);
1592 /***********************************************************************
1593 * DrawIcon (USER32.@)
1595 BOOL WINAPI
DrawIcon( HDC hdc
, INT x
, INT y
, HICON hIcon
)
1597 return DrawIconEx( hdc
, x
, y
, hIcon
, 0, 0, 0, 0, DI_NORMAL
| DI_COMPAT
| DI_DEFAULTSIZE
);
1600 /***********************************************************************
1601 * SetCursor (USER32.@)
1603 * Set the cursor shape.
1606 * A handle to the previous cursor shape.
1608 HCURSOR WINAPI DECLSPEC_HOTPATCH
SetCursor( HCURSOR hCursor
/* [in] Handle of cursor to show */ )
1614 TRACE("%p\n", hCursor
);
1616 SERVER_START_REQ( set_cursor
)
1618 req
->flags
= SET_CURSOR_HANDLE
;
1619 req
->handle
= wine_server_user_handle( hCursor
);
1620 if ((ret
= !wine_server_call_err( req
)))
1622 hOldCursor
= wine_server_ptr_handle( reply
->prev_handle
);
1623 show_count
= reply
->prev_count
;
1630 /* Change the cursor shape only if it is visible */
1631 if (show_count
>= 0 && hOldCursor
!= hCursor
) USER_Driver
->pSetCursor( hCursor
);
1635 /***********************************************************************
1636 * ShowCursor (USER32.@)
1638 INT WINAPI DECLSPEC_HOTPATCH
ShowCursor( BOOL bShow
)
1641 int increment
= bShow
? 1 : -1;
1644 SERVER_START_REQ( set_cursor
)
1646 req
->flags
= SET_CURSOR_COUNT
;
1647 req
->show_count
= increment
;
1648 wine_server_call( req
);
1649 cursor
= wine_server_ptr_handle( reply
->prev_handle
);
1650 count
= reply
->prev_count
+ increment
;
1654 TRACE("%d, count=%d\n", bShow
, count
);
1656 if (bShow
&& !count
) USER_Driver
->pSetCursor( cursor
);
1657 else if (!bShow
&& count
== -1) USER_Driver
->pSetCursor( 0 );
1662 /***********************************************************************
1663 * GetCursor (USER32.@)
1665 HCURSOR WINAPI
GetCursor(void)
1669 SERVER_START_REQ( set_cursor
)
1672 wine_server_call( req
);
1673 ret
= wine_server_ptr_handle( reply
->prev_handle
);
1680 /***********************************************************************
1681 * ClipCursor (USER32.@)
1683 BOOL WINAPI DECLSPEC_HOTPATCH
ClipCursor( const RECT
*rect
)
1687 SetRect( &virt
, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN
),
1688 GetSystemMetrics( SM_CYVIRTUALSCREEN
) );
1689 OffsetRect( &virt
, GetSystemMetrics( SM_XVIRTUALSCREEN
),
1690 GetSystemMetrics( SM_YVIRTUALSCREEN
) );
1692 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect
),
1693 wine_dbgstr_rect(&CURSOR_ClipRect
), wine_dbgstr_rect(&virt
) );
1695 if (!IntersectRect( &CURSOR_ClipRect
, &virt
, rect
))
1696 CURSOR_ClipRect
= virt
;
1698 USER_Driver
->pClipCursor( rect
);
1703 /***********************************************************************
1704 * GetClipCursor (USER32.@)
1706 BOOL WINAPI DECLSPEC_HOTPATCH
GetClipCursor( RECT
*rect
)
1708 /* If this is first time - initialize the rect */
1709 if (IsRectEmpty( &CURSOR_ClipRect
)) ClipCursor( NULL
);
1711 return CopyRect( rect
, &CURSOR_ClipRect
);
1715 /***********************************************************************
1716 * SetSystemCursor (USER32.@)
1718 BOOL WINAPI
SetSystemCursor(HCURSOR hcur
, DWORD id
)
1720 FIXME("(%p,%08x),stub!\n", hcur
, id
);
1725 /**********************************************************************
1726 * LookupIconIdFromDirectoryEx (USER32.@)
1728 INT WINAPI
LookupIconIdFromDirectoryEx( LPBYTE xdir
, BOOL bIcon
,
1729 INT width
, INT height
, UINT cFlag
)
1731 CURSORICONDIR
*dir
= (CURSORICONDIR
*)xdir
;
1733 if( dir
&& !dir
->idReserved
&& (dir
->idType
& 3) )
1735 CURSORICONDIRENTRY
* entry
;
1737 const HDC hdc
= GetDC(0);
1738 const int depth
= (cFlag
& LR_MONOCHROME
) ?
1739 1 : GetDeviceCaps(hdc
, BITSPIXEL
);
1743 entry
= CURSORICON_FindBestIconRes( dir
, width
, height
, depth
);
1745 entry
= CURSORICON_FindBestCursorRes( dir
, width
, height
, depth
);
1747 if( entry
) retVal
= entry
->wResId
;
1749 else WARN_(cursor
)("invalid resource directory\n");
1753 /**********************************************************************
1754 * LookupIconIdFromDirectory (USER32.@)
1756 INT WINAPI
LookupIconIdFromDirectory( LPBYTE dir
, BOOL bIcon
)
1758 return LookupIconIdFromDirectoryEx( dir
, bIcon
,
1759 bIcon
? GetSystemMetrics(SM_CXICON
) : GetSystemMetrics(SM_CXCURSOR
),
1760 bIcon
? GetSystemMetrics(SM_CYICON
) : GetSystemMetrics(SM_CYCURSOR
), bIcon
? 0 : LR_MONOCHROME
);
1763 /***********************************************************************
1764 * LoadCursorW (USER32.@)
1766 HCURSOR WINAPI
LoadCursorW(HINSTANCE hInstance
, LPCWSTR name
)
1768 TRACE("%p, %s\n", hInstance
, debugstr_w(name
));
1770 return LoadImageW( hInstance
, name
, IMAGE_CURSOR
, 0, 0,
1771 LR_SHARED
| LR_DEFAULTSIZE
);
1774 /***********************************************************************
1775 * LoadCursorA (USER32.@)
1777 HCURSOR WINAPI
LoadCursorA(HINSTANCE hInstance
, LPCSTR name
)
1779 TRACE("%p, %s\n", hInstance
, debugstr_a(name
));
1781 return LoadImageA( hInstance
, name
, IMAGE_CURSOR
, 0, 0,
1782 LR_SHARED
| LR_DEFAULTSIZE
);
1785 /***********************************************************************
1786 * LoadCursorFromFileW (USER32.@)
1788 HCURSOR WINAPI
LoadCursorFromFileW (LPCWSTR name
)
1790 TRACE("%s\n", debugstr_w(name
));
1792 return LoadImageW( 0, name
, IMAGE_CURSOR
, 0, 0,
1793 LR_LOADFROMFILE
| LR_DEFAULTSIZE
);
1796 /***********************************************************************
1797 * LoadCursorFromFileA (USER32.@)
1799 HCURSOR WINAPI
LoadCursorFromFileA (LPCSTR name
)
1801 TRACE("%s\n", debugstr_a(name
));
1803 return LoadImageA( 0, name
, IMAGE_CURSOR
, 0, 0,
1804 LR_LOADFROMFILE
| LR_DEFAULTSIZE
);
1807 /***********************************************************************
1808 * LoadIconW (USER32.@)
1810 HICON WINAPI
LoadIconW(HINSTANCE hInstance
, LPCWSTR name
)
1812 TRACE("%p, %s\n", hInstance
, debugstr_w(name
));
1814 return LoadImageW( hInstance
, name
, IMAGE_ICON
, 0, 0,
1815 LR_SHARED
| LR_DEFAULTSIZE
);
1818 /***********************************************************************
1819 * LoadIconA (USER32.@)
1821 HICON WINAPI
LoadIconA(HINSTANCE hInstance
, LPCSTR name
)
1823 TRACE("%p, %s\n", hInstance
, debugstr_a(name
));
1825 return LoadImageA( hInstance
, name
, IMAGE_ICON
, 0, 0,
1826 LR_SHARED
| LR_DEFAULTSIZE
);
1829 /**********************************************************************
1830 * GetIconInfo (USER32.@)
1832 BOOL WINAPI
GetIconInfo(HICON hIcon
, PICONINFO iconinfo
)
1836 infoW
.cbSize
= sizeof(infoW
);
1837 if (!GetIconInfoExW( hIcon
, &infoW
)) return FALSE
;
1838 iconinfo
->fIcon
= infoW
.fIcon
;
1839 iconinfo
->xHotspot
= infoW
.xHotspot
;
1840 iconinfo
->yHotspot
= infoW
.yHotspot
;
1841 iconinfo
->hbmColor
= infoW
.hbmColor
;
1842 iconinfo
->hbmMask
= infoW
.hbmMask
;
1846 /**********************************************************************
1847 * GetIconInfoExA (USER32.@)
1849 BOOL WINAPI
GetIconInfoExA( HICON icon
, ICONINFOEXA
*info
)
1853 if (info
->cbSize
!= sizeof(*info
))
1855 SetLastError( ERROR_INVALID_PARAMETER
);
1858 infoW
.cbSize
= sizeof(infoW
);
1859 if (!GetIconInfoExW( icon
, &infoW
)) return FALSE
;
1860 info
->fIcon
= infoW
.fIcon
;
1861 info
->xHotspot
= infoW
.xHotspot
;
1862 info
->yHotspot
= infoW
.yHotspot
;
1863 info
->hbmColor
= infoW
.hbmColor
;
1864 info
->hbmMask
= infoW
.hbmMask
;
1865 info
->wResID
= infoW
.wResID
;
1866 WideCharToMultiByte( CP_ACP
, 0, infoW
.szModName
, -1, info
->szModName
, MAX_PATH
, NULL
, NULL
);
1867 WideCharToMultiByte( CP_ACP
, 0, infoW
.szResName
, -1, info
->szResName
, MAX_PATH
, NULL
, NULL
);
1871 /**********************************************************************
1872 * GetIconInfoExW (USER32.@)
1874 BOOL WINAPI
GetIconInfoExW( HICON icon
, ICONINFOEXW
*info
)
1876 struct cursoricon_object
*ptr
;
1878 if (info
->cbSize
!= sizeof(*info
))
1880 SetLastError( ERROR_INVALID_PARAMETER
);
1883 if (!(ptr
= get_icon_ptr( icon
)))
1885 SetLastError( ERROR_INVALID_CURSOR_HANDLE
);
1889 TRACE("%p => %dx%d\n", icon
, ptr
->width
, ptr
->height
);
1891 info
->fIcon
= ptr
->is_icon
;
1892 info
->xHotspot
= ptr
->hotspot
.x
;
1893 info
->yHotspot
= ptr
->hotspot
.y
;
1894 info
->hbmColor
= copy_bitmap( ptr
->frames
[0].color
);
1895 info
->hbmMask
= copy_bitmap( ptr
->frames
[0].mask
);
1896 info
->wResID
= 0; /* FIXME */
1897 info
->szModName
[0] = 0; /* FIXME */
1898 info
->szResName
[0] = 0; /* FIXME */
1899 release_icon_ptr( icon
, ptr
);
1903 /* copy an icon bitmap, even when it can't be selected into a DC */
1904 /* helper for CreateIconIndirect */
1905 static void stretch_blt_icon( HDC hdc_dst
, int dst_x
, int dst_y
, int dst_width
, int dst_height
,
1906 HBITMAP src
, int width
, int height
)
1908 HDC hdc
= CreateCompatibleDC( 0 );
1910 if (!SelectObject( hdc
, src
)) /* do it the hard way */
1915 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )))) return;
1916 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1917 info
->bmiHeader
.biWidth
= width
;
1918 info
->bmiHeader
.biHeight
= height
;
1919 info
->bmiHeader
.biPlanes
= GetDeviceCaps( hdc_dst
, PLANES
);
1920 info
->bmiHeader
.biBitCount
= GetDeviceCaps( hdc_dst
, BITSPIXEL
);
1921 info
->bmiHeader
.biCompression
= BI_RGB
;
1922 info
->bmiHeader
.biSizeImage
= height
* get_dib_width_bytes( width
, info
->bmiHeader
.biBitCount
);
1923 info
->bmiHeader
.biXPelsPerMeter
= 0;
1924 info
->bmiHeader
.biYPelsPerMeter
= 0;
1925 info
->bmiHeader
.biClrUsed
= 0;
1926 info
->bmiHeader
.biClrImportant
= 0;
1927 bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
);
1928 if (bits
&& GetDIBits( hdc
, src
, 0, height
, bits
, info
, DIB_RGB_COLORS
))
1929 StretchDIBits( hdc_dst
, dst_x
, dst_y
, dst_width
, dst_height
,
1930 0, 0, width
, height
, bits
, info
, DIB_RGB_COLORS
, SRCCOPY
);
1932 HeapFree( GetProcessHeap(), 0, bits
);
1933 HeapFree( GetProcessHeap(), 0, info
);
1935 else StretchBlt( hdc_dst
, dst_x
, dst_y
, dst_width
, dst_height
, hdc
, 0, 0, width
, height
, SRCCOPY
);
1940 /**********************************************************************
1941 * CreateIconIndirect (USER32.@)
1943 HICON WINAPI
CreateIconIndirect(PICONINFO iconinfo
)
1945 BITMAP bmpXor
, bmpAnd
;
1947 HBITMAP color
= 0, mask
;
1951 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1952 iconinfo
->hbmColor
, iconinfo
->hbmMask
,
1953 iconinfo
->xHotspot
, iconinfo
->yHotspot
, iconinfo
->fIcon
);
1955 if (!iconinfo
->hbmMask
) return 0;
1957 GetObjectW( iconinfo
->hbmMask
, sizeof(bmpAnd
), &bmpAnd
);
1958 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1959 bmpAnd
.bmWidth
, bmpAnd
.bmHeight
, bmpAnd
.bmWidthBytes
,
1960 bmpAnd
.bmPlanes
, bmpAnd
.bmBitsPixel
);
1962 if (iconinfo
->hbmColor
)
1964 GetObjectW( iconinfo
->hbmColor
, sizeof(bmpXor
), &bmpXor
);
1965 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1966 bmpXor
.bmWidth
, bmpXor
.bmHeight
, bmpXor
.bmWidthBytes
,
1967 bmpXor
.bmPlanes
, bmpXor
.bmBitsPixel
);
1969 width
= bmpXor
.bmWidth
;
1970 height
= bmpXor
.bmHeight
;
1971 if (bmpXor
.bmPlanes
* bmpXor
.bmBitsPixel
!= 1)
1973 color
= CreateCompatibleBitmap( screen_dc
, width
, height
);
1974 mask
= CreateBitmap( width
, height
, 1, 1, NULL
);
1976 else mask
= CreateBitmap( width
, height
* 2, 1, 1, NULL
);
1980 width
= bmpAnd
.bmWidth
;
1981 height
= bmpAnd
.bmHeight
;
1982 mask
= CreateBitmap( width
, height
, 1, 1, NULL
);
1985 hdc
= CreateCompatibleDC( 0 );
1986 SelectObject( hdc
, mask
);
1987 stretch_blt_icon( hdc
, 0, 0, width
, height
, iconinfo
->hbmMask
, bmpAnd
.bmWidth
, bmpAnd
.bmHeight
);
1991 SelectObject( hdc
, color
);
1992 stretch_blt_icon( hdc
, 0, 0, width
, height
, iconinfo
->hbmColor
, width
, height
);
1994 else if (iconinfo
->hbmColor
)
1996 stretch_blt_icon( hdc
, 0, height
, width
, height
, iconinfo
->hbmColor
, width
, height
);
2002 hObj
= alloc_icon_handle(1);
2005 struct cursoricon_object
*info
= get_icon_ptr( hObj
);
2007 info
->is_icon
= iconinfo
->fIcon
;
2008 info
->width
= width
;
2009 info
->height
= height
;
2010 info
->frames
[0].color
= color
;
2011 info
->frames
[0].mask
= mask
;
2012 info
->frames
[0].alpha
= create_alpha_bitmap( iconinfo
->hbmColor
, mask
, NULL
, NULL
);
2015 info
->hotspot
.x
= width
/ 2;
2016 info
->hotspot
.y
= height
/ 2;
2020 info
->hotspot
.x
= iconinfo
->xHotspot
;
2021 info
->hotspot
.y
= iconinfo
->yHotspot
;
2024 release_icon_ptr( hObj
, info
);
2025 USER_Driver
->pCreateCursorIcon( hObj
);
2030 /******************************************************************************
2031 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2034 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2037 * hdc [I] Handle to device context
2038 * x0 [I] X coordinate of upper left corner
2039 * y0 [I] Y coordinate of upper left corner
2040 * hIcon [I] Handle to icon to draw
2041 * cxWidth [I] Width of icon
2042 * cyWidth [I] Height of icon
2043 * istep [I] Index of frame in animated cursor
2044 * hbr [I] Handle to background brush
2045 * flags [I] Icon-drawing flags
2051 BOOL WINAPI
DrawIconEx( HDC hdc
, INT x0
, INT y0
, HICON hIcon
,
2052 INT cxWidth
, INT cyWidth
, UINT istep
,
2053 HBRUSH hbr
, UINT flags
)
2055 struct cursoricon_object
*ptr
;
2056 HDC hdc_dest
, hMemDC
;
2057 BOOL result
= FALSE
, DoOffscreen
;
2059 COLORREF oldFg
, oldBg
;
2060 INT x
, y
, nStretchMode
;
2062 TRACE_(icon
)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2063 hdc
,x0
,y0
,hIcon
,cxWidth
,cyWidth
,istep
,hbr
,flags
);
2065 if (!(ptr
= get_icon_ptr( hIcon
))) return FALSE
;
2066 if (!(hMemDC
= CreateCompatibleDC( hdc
)))
2068 release_icon_ptr( hIcon
, ptr
);
2072 if (istep
>= ptr
->num_frames
)
2074 TRACE_(icon
)("Stepped past end of animated frames=%d\n", istep
);
2075 release_icon_ptr( hIcon
, ptr
);
2078 if (flags
& DI_NOMIRROR
)
2079 FIXME_(icon
)("Ignoring flag DI_NOMIRROR\n");
2081 /* Calculate the size of the destination image. */
2084 if (flags
& DI_DEFAULTSIZE
)
2085 cxWidth
= GetSystemMetrics (SM_CXICON
);
2087 cxWidth
= ptr
->width
;
2091 if (flags
& DI_DEFAULTSIZE
)
2092 cyWidth
= GetSystemMetrics (SM_CYICON
);
2094 cyWidth
= ptr
->height
;
2097 DoOffscreen
= (GetObjectType( hbr
) == OBJ_BRUSH
);
2107 if (!(hdc_dest
= CreateCompatibleDC(hdc
))) goto failed
;
2108 if (!(hB_off
= CreateCompatibleBitmap(hdc
, cxWidth
, cyWidth
)))
2110 DeleteDC( hdc_dest
);
2113 SelectObject(hdc_dest
, hB_off
);
2114 FillRect(hdc_dest
, &r
, hbr
);
2124 nStretchMode
= SetStretchBltMode (hdc
, STRETCH_DELETESCANS
);
2126 oldFg
= SetTextColor( hdc
, RGB(0,0,0) );
2127 oldBg
= SetBkColor( hdc
, RGB(255,255,255) );
2129 if (ptr
->frames
[istep
].alpha
&& (flags
& DI_IMAGE
))
2131 BOOL is_mono
= FALSE
;
2133 if (GetObjectType( hdc_dest
) == OBJ_MEMDC
)
2136 HBITMAP bmp
= GetCurrentObject( hdc_dest
, OBJ_BITMAP
);
2137 is_mono
= GetObjectW( bmp
, sizeof(bm
), &bm
) && bm
.bmBitsPixel
== 1;
2141 BLENDFUNCTION pixelblend
= { AC_SRC_OVER
, 0, 255, AC_SRC_ALPHA
};
2142 SelectObject( hMemDC
, ptr
->frames
[istep
].alpha
);
2143 if (GdiAlphaBlend( hdc_dest
, x
, y
, cxWidth
, cyWidth
, hMemDC
,
2144 0, 0, ptr
->width
, ptr
->height
, pixelblend
)) goto done
;
2148 if (flags
& DI_MASK
)
2150 SelectObject( hMemDC
, ptr
->frames
[istep
].mask
);
2151 StretchBlt( hdc_dest
, x
, y
, cxWidth
, cyWidth
,
2152 hMemDC
, 0, 0, ptr
->width
, ptr
->height
, SRCAND
);
2155 if (flags
& DI_IMAGE
)
2157 if (ptr
->frames
[istep
].color
)
2159 DWORD rop
= (flags
& DI_MASK
) ? SRCINVERT
: SRCCOPY
;
2160 SelectObject( hMemDC
, ptr
->frames
[istep
].color
);
2161 StretchBlt( hdc_dest
, x
, y
, cxWidth
, cyWidth
,
2162 hMemDC
, 0, 0, ptr
->width
, ptr
->height
, rop
);
2166 DWORD rop
= (flags
& DI_MASK
) ? SRCINVERT
: SRCCOPY
;
2167 SelectObject( hMemDC
, ptr
->frames
[istep
].mask
);
2168 StretchBlt( hdc_dest
, x
, y
, cxWidth
, cyWidth
,
2169 hMemDC
, 0, ptr
->height
, ptr
->width
, ptr
->height
, rop
);
2174 if (DoOffscreen
) BitBlt( hdc
, x0
, y0
, cxWidth
, cyWidth
, hdc_dest
, 0, 0, SRCCOPY
);
2176 SetTextColor( hdc
, oldFg
);
2177 SetBkColor( hdc
, oldBg
);
2178 SetStretchBltMode (hdc
, nStretchMode
);
2180 if (hdc_dest
!= hdc
) DeleteDC( hdc_dest
);
2181 if (hB_off
) DeleteObject(hB_off
);
2184 release_icon_ptr( hIcon
, ptr
);
2188 /***********************************************************************
2189 * DIB_FixColorsToLoadflags
2191 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2194 static void DIB_FixColorsToLoadflags(BITMAPINFO
* bmi
, UINT loadflags
, BYTE pix
)
2197 COLORREF c_W
, c_S
, c_F
, c_L
, c_C
;
2206 if (((bitmap_type
= DIB_GetBitmapInfo((BITMAPINFOHEADER
*) bmi
, &width
, &height
, &bpp
, &compr
)) == -1))
2208 WARN_(resource
)("Invalid bitmap\n");
2212 if (bpp
> 8) return;
2214 if (bitmap_type
== 0) /* BITMAPCOREHEADER */
2222 colors
= bmi
->bmiHeader
.biClrUsed
;
2223 if (colors
> 256) colors
= 256;
2224 if (!colors
&& (bpp
<= 8)) colors
= 1 << bpp
;
2227 c_W
= GetSysColor(COLOR_WINDOW
);
2228 c_S
= GetSysColor(COLOR_3DSHADOW
);
2229 c_F
= GetSysColor(COLOR_3DFACE
);
2230 c_L
= GetSysColor(COLOR_3DLIGHT
);
2232 if (loadflags
& LR_LOADTRANSPARENT
) {
2234 case 1: pix
= pix
>> 7; break;
2235 case 4: pix
= pix
>> 4; break;
2238 WARN_(resource
)("(%d): Unsupported depth\n", bpp
);
2241 if (pix
>= colors
) {
2242 WARN_(resource
)("pixel has color index greater than biClrUsed!\n");
2245 if (loadflags
& LR_LOADMAP3DCOLORS
) c_W
= c_F
;
2246 ptr
= (RGBQUAD
*)((char*)bmi
->bmiColors
+pix
*incr
);
2247 ptr
->rgbBlue
= GetBValue(c_W
);
2248 ptr
->rgbGreen
= GetGValue(c_W
);
2249 ptr
->rgbRed
= GetRValue(c_W
);
2251 if (loadflags
& LR_LOADMAP3DCOLORS
)
2252 for (i
=0; i
<colors
; i
++) {
2253 ptr
= (RGBQUAD
*)((char*)bmi
->bmiColors
+i
*incr
);
2254 c_C
= RGB(ptr
->rgbRed
, ptr
->rgbGreen
, ptr
->rgbBlue
);
2255 if (c_C
== RGB(128, 128, 128)) {
2256 ptr
->rgbRed
= GetRValue(c_S
);
2257 ptr
->rgbGreen
= GetGValue(c_S
);
2258 ptr
->rgbBlue
= GetBValue(c_S
);
2259 } else if (c_C
== RGB(192, 192, 192)) {
2260 ptr
->rgbRed
= GetRValue(c_F
);
2261 ptr
->rgbGreen
= GetGValue(c_F
);
2262 ptr
->rgbBlue
= GetBValue(c_F
);
2263 } else if (c_C
== RGB(223, 223, 223)) {
2264 ptr
->rgbRed
= GetRValue(c_L
);
2265 ptr
->rgbGreen
= GetGValue(c_L
);
2266 ptr
->rgbBlue
= GetBValue(c_L
);
2272 /**********************************************************************
2275 static HBITMAP
BITMAP_Load( HINSTANCE instance
, LPCWSTR name
,
2276 INT desiredx
, INT desiredy
, UINT loadflags
)
2278 HBITMAP hbitmap
= 0, orig_bm
;
2282 BITMAPINFO
*info
, *fix_info
= NULL
, *scaled_info
= NULL
;
2286 LONG width
, height
, new_width
, new_height
;
2288 DWORD compr_dummy
, offbits
= 0;
2290 HDC screen_mem_dc
= NULL
;
2292 if (!(loadflags
& LR_LOADFROMFILE
))
2296 /* OEM bitmap: try to load the resource from user32.dll */
2297 instance
= user32_module
;
2300 if (!(hRsrc
= FindResourceW( instance
, name
, (LPWSTR
)RT_BITMAP
))) return 0;
2301 if (!(handle
= LoadResource( instance
, hRsrc
))) return 0;
2303 if ((info
= LockResource( handle
)) == NULL
) return 0;
2307 BITMAPFILEHEADER
* bmfh
;
2309 if (!(ptr
= map_fileW( name
, NULL
))) return 0;
2310 info
= (BITMAPINFO
*)(ptr
+ sizeof(BITMAPFILEHEADER
));
2311 bmfh
= (BITMAPFILEHEADER
*)ptr
;
2312 if (bmfh
->bfType
!= 0x4d42 /* 'BM' */)
2314 WARN("Invalid/unsupported bitmap format!\n");
2317 if (bmfh
->bfOffBits
) offbits
= bmfh
->bfOffBits
- sizeof(BITMAPFILEHEADER
);
2320 size
= bitmap_info_size(info
, DIB_RGB_COLORS
);
2321 fix_info
= HeapAlloc(GetProcessHeap(), 0, size
);
2322 scaled_info
= HeapAlloc(GetProcessHeap(), 0, size
);
2324 if (!fix_info
|| !scaled_info
) goto end
;
2325 memcpy(fix_info
, info
, size
);
2327 pix
= *((LPBYTE
)info
+ size
);
2328 DIB_FixColorsToLoadflags(fix_info
, loadflags
, pix
);
2330 memcpy(scaled_info
, fix_info
, size
);
2331 bm_type
= DIB_GetBitmapInfo( &fix_info
->bmiHeader
, &width
, &height
,
2332 &bpp_dummy
, &compr_dummy
);
2335 WARN("Invalid bitmap format!\n");
2340 new_width
= desiredx
;
2345 new_height
= height
> 0 ? desiredy
: -desiredy
;
2347 new_height
= height
;
2351 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)&scaled_info
->bmiHeader
;
2352 core
->bcWidth
= new_width
;
2353 core
->bcHeight
= new_height
;
2357 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2358 if (info
->bmiHeader
.biHeight
> 65535 || info
->bmiHeader
.biWidth
> 65535) {
2359 WARN("Broken BitmapInfoHeader!\n");
2363 scaled_info
->bmiHeader
.biWidth
= new_width
;
2364 scaled_info
->bmiHeader
.biHeight
= new_height
;
2367 if (new_height
< 0) new_height
= -new_height
;
2369 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
2370 if (!(screen_mem_dc
= CreateCompatibleDC( screen_dc
))) goto end
;
2372 bits
= (char *)info
+ (offbits
? offbits
: size
);
2374 if (loadflags
& LR_CREATEDIBSECTION
)
2376 scaled_info
->bmiHeader
.biCompression
= 0; /* DIBSection can't be compressed */
2377 hbitmap
= CreateDIBSection(screen_dc
, scaled_info
, DIB_RGB_COLORS
, NULL
, 0, 0);
2381 if (is_dib_monochrome(fix_info
))
2382 hbitmap
= CreateBitmap(new_width
, new_height
, 1, 1, NULL
);
2384 hbitmap
= CreateCompatibleBitmap(screen_dc
, new_width
, new_height
);
2387 orig_bm
= SelectObject(screen_mem_dc
, hbitmap
);
2388 StretchDIBits(screen_mem_dc
, 0, 0, new_width
, new_height
, 0, 0, width
, height
, bits
, fix_info
, DIB_RGB_COLORS
, SRCCOPY
);
2389 SelectObject(screen_mem_dc
, orig_bm
);
2392 if (screen_mem_dc
) DeleteDC(screen_mem_dc
);
2393 HeapFree(GetProcessHeap(), 0, scaled_info
);
2394 HeapFree(GetProcessHeap(), 0, fix_info
);
2395 if (loadflags
& LR_LOADFROMFILE
) UnmapViewOfFile( ptr
);
2400 /**********************************************************************
2401 * LoadImageA (USER32.@)
2405 HANDLE WINAPI
LoadImageA( HINSTANCE hinst
, LPCSTR name
, UINT type
,
2406 INT desiredx
, INT desiredy
, UINT loadflags
)
2411 if (IS_INTRESOURCE(name
))
2412 return LoadImageW(hinst
, (LPCWSTR
)name
, type
, desiredx
, desiredy
, loadflags
);
2415 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
2416 u_name
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
2417 MultiByteToWideChar( CP_ACP
, 0, name
, -1, u_name
, len
);
2419 __EXCEPT_PAGE_FAULT
{
2420 SetLastError( ERROR_INVALID_PARAMETER
);
2424 res
= LoadImageW(hinst
, u_name
, type
, desiredx
, desiredy
, loadflags
);
2425 HeapFree(GetProcessHeap(), 0, u_name
);
2430 /******************************************************************************
2431 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2434 * hinst [I] Handle of instance that contains image
2435 * name [I] Name of image
2436 * type [I] Type of image
2437 * desiredx [I] Desired width
2438 * desiredy [I] Desired height
2439 * loadflags [I] Load flags
2442 * Success: Handle to newly loaded image
2445 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2447 HANDLE WINAPI
LoadImageW( HINSTANCE hinst
, LPCWSTR name
, UINT type
,
2448 INT desiredx
, INT desiredy
, UINT loadflags
)
2450 TRACE_(resource
)("(%p,%s,%d,%d,%d,0x%08x)\n",
2451 hinst
,debugstr_w(name
),type
,desiredx
,desiredy
,loadflags
);
2453 if (loadflags
& LR_DEFAULTSIZE
) {
2454 if (type
== IMAGE_ICON
) {
2455 if (!desiredx
) desiredx
= GetSystemMetrics(SM_CXICON
);
2456 if (!desiredy
) desiredy
= GetSystemMetrics(SM_CYICON
);
2457 } else if (type
== IMAGE_CURSOR
) {
2458 if (!desiredx
) desiredx
= GetSystemMetrics(SM_CXCURSOR
);
2459 if (!desiredy
) desiredy
= GetSystemMetrics(SM_CYCURSOR
);
2462 if (loadflags
& LR_LOADFROMFILE
) loadflags
&= ~LR_SHARED
;
2465 return BITMAP_Load( hinst
, name
, desiredx
, desiredy
, loadflags
);
2468 if (!screen_dc
) screen_dc
= CreateDCW( DISPLAYW
, NULL
, NULL
, NULL
);
2471 return CURSORICON_Load(hinst
, name
, desiredx
, desiredy
,
2472 GetDeviceCaps(screen_dc
, BITSPIXEL
),
2478 return CURSORICON_Load(hinst
, name
, desiredx
, desiredy
,
2479 1, TRUE
, loadflags
);
2484 /******************************************************************************
2485 * CopyImage (USER32.@) Creates new image and copies attributes to it
2488 * hnd [I] Handle to image to copy
2489 * type [I] Type of image to copy
2490 * desiredx [I] Desired width of new image
2491 * desiredy [I] Desired height of new image
2492 * flags [I] Copy flags
2495 * Success: Handle to newly created image
2499 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2500 * all other versions (95/2000/XP have been tested) ignore it.
2503 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2504 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2505 * the copy will have the same depth as the screen.
2506 * The content of the image will only be copied if the bit depth of the
2507 * original image is compatible with the bit depth of the screen, or
2508 * if the source is a DIB section.
2509 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2511 HANDLE WINAPI
CopyImage( HANDLE hnd
, UINT type
, INT desiredx
,
2512 INT desiredy
, UINT flags
)
2514 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2515 hnd
, type
, desiredx
, desiredy
, flags
);
2526 objSize
= GetObjectW( hnd
, sizeof(ds
), &ds
);
2527 if (!objSize
) return 0;
2528 if ((desiredx
< 0) || (desiredy
< 0)) return 0;
2530 if (flags
& LR_COPYFROMRESOURCE
)
2532 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2535 if (desiredx
== 0) desiredx
= ds
.dsBm
.bmWidth
;
2536 if (desiredy
== 0) desiredy
= ds
.dsBm
.bmHeight
;
2538 /* Allocate memory for a BITMAPINFOHEADER structure and a
2539 color table. The maximum number of colors in a color table
2540 is 256 which corresponds to a bitmap with depth 8.
2541 Bitmaps with higher depths don't have color tables. */
2542 bi
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
2545 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
2546 bi
->bmiHeader
.biPlanes
= ds
.dsBm
.bmPlanes
;
2547 bi
->bmiHeader
.biBitCount
= ds
.dsBm
.bmBitsPixel
;
2548 bi
->bmiHeader
.biCompression
= BI_RGB
;
2550 if (flags
& LR_CREATEDIBSECTION
)
2552 /* Create a DIB section. LR_MONOCHROME is ignored */
2554 HDC dc
= CreateCompatibleDC(NULL
);
2556 if (objSize
== sizeof(DIBSECTION
))
2558 /* The source bitmap is a DIB.
2559 Get its attributes to create an exact copy */
2560 memcpy(bi
, &ds
.dsBmih
, sizeof(BITMAPINFOHEADER
));
2563 /* Get the color table or the color masks */
2564 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
2566 bi
->bmiHeader
.biWidth
= desiredx
;
2567 bi
->bmiHeader
.biHeight
= desiredy
;
2568 bi
->bmiHeader
.biSizeImage
= 0;
2570 res
= CreateDIBSection(dc
, bi
, DIB_RGB_COLORS
, &bits
, NULL
, 0);
2575 /* Create a device-dependent bitmap */
2577 BOOL monochrome
= (flags
& LR_MONOCHROME
);
2579 if (objSize
== sizeof(DIBSECTION
))
2581 /* The source bitmap is a DIB section.
2582 Get its attributes */
2583 HDC dc
= CreateCompatibleDC(NULL
);
2584 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
2585 bi
->bmiHeader
.biBitCount
= ds
.dsBm
.bmBitsPixel
;
2586 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
2589 if (!monochrome
&& ds
.dsBm
.bmBitsPixel
== 1)
2591 /* Look if the colors of the DIB are black and white */
2594 (bi
->bmiColors
[0].rgbRed
== 0xff
2595 && bi
->bmiColors
[0].rgbGreen
== 0xff
2596 && bi
->bmiColors
[0].rgbBlue
== 0xff
2597 && bi
->bmiColors
[0].rgbReserved
== 0
2598 && bi
->bmiColors
[1].rgbRed
== 0
2599 && bi
->bmiColors
[1].rgbGreen
== 0
2600 && bi
->bmiColors
[1].rgbBlue
== 0
2601 && bi
->bmiColors
[1].rgbReserved
== 0)
2603 (bi
->bmiColors
[0].rgbRed
== 0
2604 && bi
->bmiColors
[0].rgbGreen
== 0
2605 && bi
->bmiColors
[0].rgbBlue
== 0
2606 && bi
->bmiColors
[0].rgbReserved
== 0
2607 && bi
->bmiColors
[1].rgbRed
== 0xff
2608 && bi
->bmiColors
[1].rgbGreen
== 0xff
2609 && bi
->bmiColors
[1].rgbBlue
== 0xff
2610 && bi
->bmiColors
[1].rgbReserved
== 0);
2613 else if (!monochrome
)
2615 monochrome
= ds
.dsBm
.bmBitsPixel
== 1;
2620 res
= CreateBitmap(desiredx
, desiredy
, 1, 1, NULL
);
2624 HDC screenDC
= GetDC(NULL
);
2625 res
= CreateCompatibleBitmap(screenDC
, desiredx
, desiredy
);
2626 ReleaseDC(NULL
, screenDC
);
2632 /* Only copy the bitmap if it's a DIB section or if it's
2633 compatible to the screen */
2636 if (objSize
== sizeof(DIBSECTION
))
2638 copyContents
= TRUE
;
2642 HDC screenDC
= GetDC(NULL
);
2643 int screen_depth
= GetDeviceCaps(screenDC
, BITSPIXEL
);
2644 ReleaseDC(NULL
, screenDC
);
2646 copyContents
= (ds
.dsBm
.bmBitsPixel
== 1 || ds
.dsBm
.bmBitsPixel
== screen_depth
);
2651 /* The source bitmap may already be selected in a device context,
2652 use GetDIBits/StretchDIBits and not StretchBlt */
2657 dc
= CreateCompatibleDC(NULL
);
2659 bi
->bmiHeader
.biWidth
= ds
.dsBm
.bmWidth
;
2660 bi
->bmiHeader
.biHeight
= ds
.dsBm
.bmHeight
;
2661 bi
->bmiHeader
.biSizeImage
= 0;
2662 bi
->bmiHeader
.biClrUsed
= 0;
2663 bi
->bmiHeader
.biClrImportant
= 0;
2665 /* Fill in biSizeImage */
2666 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, NULL
, bi
, DIB_RGB_COLORS
);
2667 bits
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, bi
->bmiHeader
.biSizeImage
);
2673 /* Get the image bits of the source bitmap */
2674 GetDIBits(dc
, hnd
, 0, ds
.dsBm
.bmHeight
, bits
, bi
, DIB_RGB_COLORS
);
2676 /* Copy it to the destination bitmap */
2677 oldBmp
= SelectObject(dc
, res
);
2678 StretchDIBits(dc
, 0, 0, desiredx
, desiredy
,
2679 0, 0, ds
.dsBm
.bmWidth
, ds
.dsBm
.bmHeight
,
2680 bits
, bi
, DIB_RGB_COLORS
, SRCCOPY
);
2681 SelectObject(dc
, oldBmp
);
2683 HeapFree(GetProcessHeap(), 0, bits
);
2689 if (flags
& LR_COPYDELETEORG
)
2694 HeapFree(GetProcessHeap(), 0, bi
);
2698 return CURSORICON_ExtCopy(hnd
,type
, desiredx
, desiredy
, flags
);
2700 /* Should call CURSORICON_ExtCopy but more testing
2701 * needs to be done before we change this
2703 if (flags
) FIXME("Flags are ignored\n");
2704 return CopyCursor(hnd
);
2710 /******************************************************************************
2711 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2714 * Success: Handle to specified bitmap
2717 HBITMAP WINAPI
LoadBitmapW(
2718 HINSTANCE instance
, /* [in] Handle to application instance */
2719 LPCWSTR name
) /* [in] Address of bitmap resource name */
2721 return LoadImageW( instance
, name
, IMAGE_BITMAP
, 0, 0, 0 );
2724 /**********************************************************************
2725 * LoadBitmapA (USER32.@)
2729 HBITMAP WINAPI
LoadBitmapA( HINSTANCE instance
, LPCSTR name
)
2731 return LoadImageA( instance
, name
, IMAGE_BITMAP
, 0, 0, 0 );