crypt32: Make helper for copying CMSG_CMS_SIGNER_INFO attributes more generic.
[wine.git] / dlls / user32 / cursoricon.c
blobe772d5412355274d7544e0b09b644893a1148ae2
1 /*
2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Martin Von Loewis
6 * Copyright 1997 Alex Korobka
7 * Copyright 1998 Turchanov Sergey
8 * Copyright 2007 Henri Verbeet
9 * Copyright 2009 Vincent Povirk for CodeWeavers
10 * Copyright 2016 Dmitry Timoshkov
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include <assert.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <stdlib.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winerror.h"
36 #include "winnls.h"
37 #include "wine/exception.h"
38 #include "wine/server.h"
39 #include "controls.h"
40 #include "win.h"
41 #include "user_private.h"
42 #include "wine/list.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
46 WINE_DECLARE_DEBUG_CHANNEL(icon);
47 WINE_DECLARE_DEBUG_CHANNEL(resource);
49 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
50 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
51 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
52 #define PNG_SIGN RIFF_FOURCC(0x89,'P','N','G')
54 static struct list icon_cache = LIST_INIT( icon_cache );
56 /**********************************************************************
57 * User objects management
60 struct cursoricon_frame
62 UINT width; /* frame-specific width */
63 UINT height; /* frame-specific height */
64 UINT delay; /* frame-specific delay between this frame and the next (in jiffies) */
65 HBITMAP color; /* color bitmap */
66 HBITMAP alpha; /* pre-multiplied alpha bitmap for 32-bpp icons */
67 HBITMAP mask; /* mask bitmap (followed by color for 1-bpp icons) */
70 struct cursoricon_object
72 struct user_object obj; /* object header */
73 struct list entry; /* entry in shared icons list */
74 ULONG_PTR param; /* opaque param used by 16-bit code */
75 HMODULE module; /* module for icons loaded from resources */
76 LPWSTR resname; /* resource name for icons loaded from resources */
77 HRSRC rsrc; /* resource for shared icons */
78 BOOL is_icon; /* whether icon or cursor */
79 BOOL is_ani; /* whether this object is a static cursor or an animated cursor */
80 UINT delay; /* delay between this frame and the next (in jiffies) */
81 POINT hotspot;
84 struct static_cursoricon_object
86 struct cursoricon_object shared;
87 struct cursoricon_frame frame; /* frame-specific icon data */
90 struct animated_cursoricon_object
92 struct cursoricon_object shared;
93 UINT num_frames; /* number of frames in the icon/cursor */
94 UINT num_steps; /* number of sequence steps in the icon/cursor */
95 HICON frames[1]; /* list of animated cursor frames */
98 static HBITMAP create_color_bitmap( int width, int height )
100 HDC hdc = get_display_dc();
101 HBITMAP ret = CreateCompatibleBitmap( hdc, width, height );
102 release_display_dc( hdc );
103 return ret;
106 static int get_display_bpp(void)
108 HDC hdc = get_display_dc();
109 int ret = GetDeviceCaps( hdc, BITSPIXEL );
110 release_display_dc( hdc );
111 return ret;
114 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
116 static const struct png_funcs *png_funcs;
118 static BOOL WINAPI load_libpng( INIT_ONCE *once, void *param, void **context )
120 __wine_init_unix_lib( user32_module, DLL_PROCESS_ATTACH, NULL, &png_funcs );
121 return TRUE;
124 static BOOL have_libpng(void)
126 return InitOnceExecuteOnce( &init_once, load_libpng, NULL, NULL ) && png_funcs;
129 static HICON alloc_icon_handle( BOOL is_ani, UINT num_steps )
131 struct cursoricon_object *obj;
132 int icon_size;
133 HICON handle;
135 if (is_ani)
136 icon_size = FIELD_OFFSET( struct animated_cursoricon_object, frames[num_steps] );
137 else
138 icon_size = sizeof( struct static_cursoricon_object );
139 obj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, icon_size );
140 if (!obj) return NULL;
142 obj->delay = 0;
143 obj->is_ani = is_ani;
144 if (is_ani)
146 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
148 ani_icon_data->num_steps = num_steps;
149 ani_icon_data->num_frames = num_steps; /* changed later for some animated cursors */
152 if (!(handle = alloc_user_handle( &obj->obj, USER_ICON )))
153 HeapFree( GetProcessHeap(), 0, obj );
154 return handle;
157 static struct cursoricon_object *get_icon_ptr( HICON handle )
159 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
160 if (obj == OBJ_OTHER_PROCESS)
162 WARN( "icon handle %p from other process\n", handle );
163 obj = NULL;
165 return obj;
168 static struct cursoricon_frame *get_icon_frame( struct cursoricon_object *obj, int istep )
170 struct static_cursoricon_object *req_frame;
172 if (obj->is_ani)
174 struct animated_cursoricon_object *ani_icon_data;
175 struct cursoricon_object *frameobj;
177 ani_icon_data = (struct animated_cursoricon_object *) obj;
178 if (!(frameobj = get_icon_ptr( ani_icon_data->frames[istep] )))
179 return 0;
180 req_frame = (struct static_cursoricon_object *) frameobj;
182 else
183 req_frame = (struct static_cursoricon_object *) obj;
185 return &req_frame->frame;
188 static void release_icon_frame( struct cursoricon_object *obj, struct cursoricon_frame *frame )
190 if (obj->is_ani)
192 struct cursoricon_object *frameobj;
194 frameobj = (struct cursoricon_object *) (((char *)frame) - FIELD_OFFSET(struct static_cursoricon_object, frame));
195 release_user_handle_ptr( frameobj );
199 static UINT get_icon_steps( struct cursoricon_object *obj )
201 if (obj->is_ani)
203 struct animated_cursoricon_object *ani_icon_data;
205 ani_icon_data = (struct animated_cursoricon_object *) obj;
206 return ani_icon_data->num_steps;
208 return 1;
211 static BOOL free_icon_handle( HICON handle )
213 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
215 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
216 else if (obj)
218 ULONG_PTR param = obj->param;
219 UINT i;
221 assert( !obj->rsrc ); /* shared icons can't be freed */
223 if (!obj->is_ani)
225 struct cursoricon_frame *frame = get_icon_frame( obj, 0 );
227 if (frame->alpha) DeleteObject( frame->alpha );
228 if (frame->color) DeleteObject( frame->color );
229 DeleteObject( frame->mask );
230 release_icon_frame( obj, frame );
232 else
234 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
236 for (i=0; i<ani_icon_data->num_steps; i++)
238 HICON hFrame = ani_icon_data->frames[i];
240 if (hFrame)
242 UINT j;
244 free_icon_handle( ani_icon_data->frames[i] );
245 for (j=0; j<ani_icon_data->num_steps; j++)
247 if (ani_icon_data->frames[j] == hFrame)
248 ani_icon_data->frames[j] = 0;
253 if (!IS_INTRESOURCE( obj->resname )) HeapFree( GetProcessHeap(), 0, obj->resname );
254 HeapFree( GetProcessHeap(), 0, obj );
255 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
256 USER_Driver->pDestroyCursorIcon( handle );
257 return TRUE;
259 return FALSE;
262 ULONG_PTR get_icon_param( HICON handle )
264 ULONG_PTR ret = 0;
265 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
267 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
268 else if (obj)
270 ret = obj->param;
271 release_user_handle_ptr( obj );
273 return ret;
276 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
278 ULONG_PTR ret = 0;
279 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
281 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
282 else if (obj)
284 ret = obj->param;
285 obj->param = param;
286 release_user_handle_ptr( obj );
288 return ret;
292 /***********************************************************************
293 * map_fileW
295 * Helper function to map a file to memory:
296 * name - file name
297 * [RETURN] ptr - pointer to mapped file
298 * [RETURN] filesize - pointer size of file to be stored if not NULL
300 static const void *map_fileW( LPCWSTR name, LPDWORD filesize )
302 HANDLE hFile, hMapping;
303 LPVOID ptr = NULL;
305 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
306 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
307 if (hFile != INVALID_HANDLE_VALUE)
309 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
310 if (hMapping)
312 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
313 CloseHandle( hMapping );
314 if (filesize)
315 *filesize = GetFileSize( hFile, NULL );
317 CloseHandle( hFile );
319 return ptr;
323 /***********************************************************************
324 * get_dib_image_size
326 * Return the size of a DIB bitmap in bytes.
328 static int get_dib_image_size( int width, int height, int depth )
330 return (((width * depth + 31) / 8) & ~3) * abs( height );
334 /***********************************************************************
335 * bitmap_info_size
337 * Return the size of the bitmap info structure including color table.
339 int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
341 unsigned int colors, size, masks = 0;
343 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
345 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
346 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
347 return sizeof(BITMAPCOREHEADER) + colors *
348 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
350 else /* assume BITMAPINFOHEADER */
352 colors = info->bmiHeader.biClrUsed;
353 if (colors > 256) /* buffer overflow otherwise */
354 colors = 256;
355 if (!colors && (info->bmiHeader.biBitCount <= 8))
356 colors = 1 << info->bmiHeader.biBitCount;
357 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
358 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
359 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
364 /***********************************************************************
365 * copy_bitmap
367 * Helper function to duplicate a bitmap.
369 static HBITMAP copy_bitmap( HBITMAP bitmap )
371 HDC src, dst = 0;
372 HBITMAP new_bitmap = 0;
373 BITMAP bmp;
375 if (!bitmap) return 0;
376 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
378 if ((src = CreateCompatibleDC( 0 )) && (dst = CreateCompatibleDC( 0 )))
380 SelectObject( src, bitmap );
381 if ((new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight )))
383 SelectObject( dst, new_bitmap );
384 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
387 DeleteDC( dst );
388 DeleteDC( src );
389 return new_bitmap;
393 /***********************************************************************
394 * is_dib_monochrome
396 * Returns whether a DIB can be converted to a monochrome DDB.
398 * A DIB can be converted if its color table contains only black and
399 * white. Black must be the first color in the color table.
401 * Note : If the first color in the color table is white followed by
402 * black, we can't convert it to a monochrome DDB with
403 * SetDIBits, because black and white would be inverted.
405 static BOOL is_dib_monochrome( const BITMAPINFO* info )
407 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
409 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
411 if (((const BITMAPCOREINFO*)info)->bmciHeader.bcBitCount != 1) return FALSE;
413 /* Check if the first color is black */
414 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
416 rgb++;
418 /* Check if the second color is white */
419 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
420 && (rgb->rgbtBlue == 0xff));
422 else return FALSE;
424 else /* assume BITMAPINFOHEADER */
426 const RGBQUAD *rgb = info->bmiColors;
428 if (info->bmiHeader.biBitCount != 1) return FALSE;
430 /* Check if the first color is black */
431 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
432 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
434 rgb++;
436 /* Check if the second color is white */
437 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
438 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
440 else return FALSE;
444 /***********************************************************************
445 * DIB_GetBitmapInfo
447 * Get the info from a bitmap header.
448 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
450 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
451 LONG *height, WORD *bpp, DWORD *compr )
453 if (header->biSize == sizeof(BITMAPCOREHEADER))
455 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
456 *width = core->bcWidth;
457 *height = core->bcHeight;
458 *bpp = core->bcBitCount;
459 *compr = 0;
460 return 0;
462 else if (header->biSize == sizeof(BITMAPINFOHEADER) ||
463 header->biSize == sizeof(BITMAPV4HEADER) ||
464 header->biSize == sizeof(BITMAPV5HEADER))
466 *width = header->biWidth;
467 *height = header->biHeight;
468 *bpp = header->biBitCount;
469 *compr = header->biCompression;
470 return 1;
472 WARN("unknown/wrong size (%u) for header\n", header->biSize);
473 return -1;
476 /**********************************************************************
477 * get_icon_size
479 BOOL get_icon_size( HICON handle, SIZE *size )
481 struct cursoricon_object *info;
482 struct cursoricon_frame *frame;
484 if (!(info = get_icon_ptr( handle ))) return FALSE;
485 frame = get_icon_frame( info, 0 );
486 size->cx = frame->width;
487 size->cy = frame->height;
488 release_icon_frame( info, frame);
489 release_user_handle_ptr( info );
490 return TRUE;
494 * The following macro functions account for the irregularities of
495 * accessing cursor and icon resources in files and resource entries.
497 typedef BOOL (*fnGetCIEntry)( LPCVOID dir, DWORD size, int n,
498 int *width, int *height, int *bits );
500 /**********************************************************************
501 * CURSORICON_FindBestIcon
503 * Find the icon closest to the requested size and bit depth.
505 static int CURSORICON_FindBestIcon( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
506 int width, int height, int depth, UINT loadflags )
508 int i, cx, cy, bits, bestEntry = -1;
509 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
510 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
512 /* Find Best Fit */
513 iTotalDiff = 0xFFFFFFFF;
514 iColorDiff = 0xFFFFFFFF;
516 if (loadflags & LR_DEFAULTSIZE)
518 if (!width) width = GetSystemMetrics( SM_CXICON );
519 if (!height) height = GetSystemMetrics( SM_CYICON );
521 else if (!width && !height)
523 /* use the size of the first entry */
524 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
525 iTotalDiff = 0;
528 for ( i = 0; iTotalDiff && get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
530 iTempXDiff = abs(width - cx);
531 iTempYDiff = abs(height - cy);
533 if(iTotalDiff > (iTempXDiff + iTempYDiff))
535 iXDiff = iTempXDiff;
536 iYDiff = iTempYDiff;
537 iTotalDiff = iXDiff + iYDiff;
541 /* Find Best Colors for Best Fit */
542 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
544 TRACE("entry %d: %d x %d, %d bpp\n", i, cx, cy, bits);
546 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
548 iTempColorDiff = abs(depth - bits);
549 if(iColorDiff > iTempColorDiff)
551 bestEntry = i;
552 iColorDiff = iTempColorDiff;
557 return bestEntry;
560 static BOOL CURSORICON_GetResIconEntry( LPCVOID dir, DWORD size, int n,
561 int *width, int *height, int *bits )
563 const CURSORICONDIR *resdir = dir;
564 const ICONRESDIR *icon;
566 if ( resdir->idCount <= n )
567 return FALSE;
568 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
569 return FALSE;
570 icon = &resdir->idEntries[n].ResInfo.icon;
571 *width = icon->bWidth;
572 *height = icon->bHeight;
573 *bits = resdir->idEntries[n].wBitCount;
574 if (!*width && !*height && have_libpng()) *width = *height = 256;
575 return TRUE;
578 /**********************************************************************
579 * CURSORICON_FindBestCursor
581 * Find the cursor closest to the requested size.
583 * FIXME: parameter 'color' ignored.
585 static int CURSORICON_FindBestCursor( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
586 int width, int height, int depth, UINT loadflags )
588 int i, maxwidth, maxheight, maxbits, cx, cy, bits, bestEntry = -1;
590 if (loadflags & LR_DEFAULTSIZE)
592 if (!width) width = GetSystemMetrics( SM_CXCURSOR );
593 if (!height) height = GetSystemMetrics( SM_CYCURSOR );
595 else if (!width && !height)
597 /* use the first entry */
598 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
599 return 0;
602 /* First find the largest one smaller than or equal to the requested size*/
604 maxwidth = maxheight = maxbits = 0;
605 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
607 if (cx > width || cy > height) continue;
608 if (cx < maxwidth || cy < maxheight) continue;
609 if (cx == maxwidth && cy == maxheight)
611 if (loadflags & LR_MONOCHROME)
613 if (maxbits && bits >= maxbits) continue;
615 else if (bits <= maxbits) continue;
617 bestEntry = i;
618 maxwidth = cx;
619 maxheight = cy;
620 maxbits = bits;
622 if (bestEntry != -1) return bestEntry;
624 /* Now find the smallest one larger than the requested size */
626 maxwidth = maxheight = 255;
627 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
629 if (cx > maxwidth || cy > maxheight) continue;
630 if (cx == maxwidth && cy == maxheight)
632 if (loadflags & LR_MONOCHROME)
634 if (maxbits && bits >= maxbits) continue;
636 else if (bits <= maxbits) continue;
638 bestEntry = i;
639 maxwidth = cx;
640 maxheight = cy;
641 maxbits = bits;
643 if (bestEntry == -1) bestEntry = 0;
645 return bestEntry;
648 static BOOL CURSORICON_GetResCursorEntry( LPCVOID dir, DWORD size, int n,
649 int *width, int *height, int *bits )
651 const CURSORICONDIR *resdir = dir;
652 const CURSORDIR *cursor;
654 if ( resdir->idCount <= n )
655 return FALSE;
656 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
657 return FALSE;
658 cursor = &resdir->idEntries[n].ResInfo.cursor;
659 *width = cursor->wWidth;
660 *height = cursor->wHeight;
661 *bits = resdir->idEntries[n].wBitCount;
662 if (*height == *width * 2) *height /= 2;
663 return TRUE;
666 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir, DWORD size,
667 int width, int height, int depth,
668 UINT loadflags )
670 int n;
672 n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetResIconEntry,
673 width, height, depth, loadflags );
674 if ( n < 0 )
675 return NULL;
676 return &dir->idEntries[n];
679 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir, DWORD size,
680 int width, int height, int depth,
681 UINT loadflags )
683 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetResCursorEntry,
684 width, height, depth, loadflags );
685 if ( n < 0 )
686 return NULL;
687 return &dir->idEntries[n];
690 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, DWORD size, int n,
691 int *width, int *height, int *bits )
693 const CURSORICONFILEDIR *filedir = dir;
694 const CURSORICONFILEDIRENTRY *entry;
695 const BITMAPINFOHEADER *info;
697 if ( filedir->idCount <= n )
698 return FALSE;
699 if ((const char *)&filedir->idEntries[n + 1] - (const char *)dir > size)
700 return FALSE;
701 entry = &filedir->idEntries[n];
702 if (entry->dwDIBOffset > size - sizeof(info->biSize)) return FALSE;
703 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
705 if (info->biSize == PNG_SIGN)
707 if (have_libpng()) return png_funcs->get_png_info(info, size, width, height, bits);
708 *width = *height = *bits = 0;
709 return TRUE;
712 if (info->biSize != sizeof(BITMAPCOREHEADER))
714 if ((const char *)(info + 1) - (const char *)dir > size) return FALSE;
715 *bits = info->biBitCount;
717 else
719 const BITMAPCOREHEADER *coreinfo = (const BITMAPCOREHEADER *)((const char *)dir + entry->dwDIBOffset);
720 if ((const char *)(coreinfo + 1) - (const char *)dir > size) return FALSE;
721 *bits = coreinfo->bcBitCount;
723 *width = entry->bWidth;
724 *height = entry->bHeight;
725 return TRUE;
728 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir, DWORD size,
729 int width, int height, int depth,
730 UINT loadflags )
732 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetFileEntry,
733 width, height, depth, loadflags );
734 if ( n < 0 )
735 return NULL;
736 return &dir->idEntries[n];
739 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir, DWORD size,
740 int width, int height, int depth,
741 UINT loadflags )
743 int n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetFileEntry,
744 width, height, depth, loadflags );
745 if ( n < 0 )
746 return NULL;
747 return &dir->idEntries[n];
750 /***********************************************************************
751 * bmi_has_alpha
753 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
755 int i;
756 BOOL has_alpha = FALSE;
757 const unsigned char *ptr = bits;
759 if (info->bmiHeader.biBitCount != 32) return FALSE;
760 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
761 if ((has_alpha = (ptr[3] != 0))) break;
762 return has_alpha;
765 /***********************************************************************
766 * create_alpha_bitmap
768 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
770 static HBITMAP create_alpha_bitmap( HBITMAP color, const BITMAPINFO *src_info, const void *color_bits )
772 HBITMAP alpha = 0;
773 BITMAPINFO *info = NULL;
774 BITMAP bm;
775 HDC hdc;
776 void *bits;
777 unsigned char *ptr;
778 int i;
780 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
781 if (bm.bmBitsPixel != 32) return 0;
783 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
784 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
785 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
786 info->bmiHeader.biWidth = bm.bmWidth;
787 info->bmiHeader.biHeight = -bm.bmHeight;
788 info->bmiHeader.biPlanes = 1;
789 info->bmiHeader.biBitCount = 32;
790 info->bmiHeader.biCompression = BI_RGB;
791 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
792 info->bmiHeader.biXPelsPerMeter = 0;
793 info->bmiHeader.biYPelsPerMeter = 0;
794 info->bmiHeader.biClrUsed = 0;
795 info->bmiHeader.biClrImportant = 0;
796 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
798 if (src_info)
800 SelectObject( hdc, alpha );
801 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
802 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
803 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
806 else
808 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
809 if (!bmi_has_alpha( info, bits ))
811 DeleteObject( alpha );
812 alpha = 0;
813 goto done;
817 /* pre-multiply by alpha */
818 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
820 unsigned int alpha = ptr[3];
821 ptr[0] = ptr[0] * alpha / 255;
822 ptr[1] = ptr[1] * alpha / 255;
823 ptr[2] = ptr[2] * alpha / 255;
826 done:
827 DeleteDC( hdc );
828 HeapFree( GetProcessHeap(), 0, info );
829 return alpha;
833 /***********************************************************************
834 * create_icon_from_bmi
836 * Create an icon from its BITMAPINFO.
838 static HICON create_icon_from_bmi( const BITMAPINFO *bmi, DWORD maxsize, HMODULE module, LPCWSTR resname,
839 HRSRC rsrc, POINT hotspot, BOOL bIcon, INT width, INT height,
840 UINT cFlag )
842 DWORD size, color_size, mask_size;
843 HBITMAP color = 0, mask = 0, alpha = 0;
844 const void *color_bits, *mask_bits;
845 void *alpha_mask_bits = NULL;
846 BITMAPINFO *bmi_copy;
847 BOOL ret = FALSE;
848 BOOL do_stretch;
849 HICON hObj = 0;
850 HDC hdc = 0;
851 LONG bmi_width, bmi_height;
852 WORD bpp;
853 DWORD compr;
855 /* Check bitmap header */
857 if (bmi->bmiHeader.biSize == PNG_SIGN)
859 BITMAPINFO *bmi_png;
861 if (!have_libpng()) return 0;
862 bmi_png = png_funcs->load_png( (const char *)bmi, &maxsize );
863 if (bmi_png)
865 hObj = create_icon_from_bmi( bmi_png, maxsize, module, resname,
866 rsrc, hotspot, bIcon, width, height, cFlag );
867 HeapFree( GetProcessHeap(), 0, bmi_png );
868 return hObj;
870 return 0;
873 if (maxsize < sizeof(BITMAPCOREHEADER))
875 WARN( "invalid size %u\n", maxsize );
876 return 0;
878 if (maxsize < bmi->bmiHeader.biSize)
880 WARN( "invalid header size %u\n", bmi->bmiHeader.biSize );
881 return 0;
883 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
884 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
885 (bmi->bmiHeader.biCompression != BI_RGB &&
886 bmi->bmiHeader.biCompression != BI_BITFIELDS)) )
888 WARN( "invalid bitmap header %u\n", bmi->bmiHeader.biSize );
889 return 0;
892 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
893 DIB_GetBitmapInfo(&bmi->bmiHeader, &bmi_width, &bmi_height, &bpp, &compr);
894 color_size = get_dib_image_size( bmi_width, bmi_height / 2,
895 bpp );
896 mask_size = get_dib_image_size( bmi_width, bmi_height / 2, 1 );
897 if (size > maxsize || color_size > maxsize - size)
899 WARN( "truncated file %u < %u+%u+%u\n", maxsize, size, color_size, mask_size );
900 return 0;
902 if (mask_size > maxsize - size - color_size) mask_size = 0; /* no mask */
904 if (cFlag & LR_DEFAULTSIZE)
906 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
907 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
909 else
911 if (!width) width = bmi_width;
912 if (!height) height = bmi_height/2;
914 do_stretch = (bmi_height/2 != height) ||
915 (bmi_width != width);
917 /* Scale the hotspot */
918 if (bIcon)
920 hotspot.x = width / 2;
921 hotspot.y = height / 2;
923 else if (do_stretch)
925 hotspot.x = (hotspot.x * width) / bmi_width;
926 hotspot.y = (hotspot.y * height) / (bmi_height / 2);
929 if (!(bmi_copy = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
930 return 0;
931 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
933 memcpy( bmi_copy, bmi, size );
934 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
935 bmi_copy->bmiHeader.biHeight /= 2;
936 else
937 ((BITMAPCOREINFO *)bmi_copy)->bmciHeader.bcHeight /= 2;
938 bmi_height /= 2;
940 color_bits = (const char*)bmi + size;
941 mask_bits = (const char*)color_bits + color_size;
943 alpha = 0;
944 if (is_dib_monochrome( bmi ))
946 if (!(mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
947 color = 0;
949 /* copy color data into second half of mask bitmap */
950 SelectObject( hdc, mask );
951 StretchDIBits( hdc, 0, height, width, height,
952 0, 0, bmi_width, bmi_height,
953 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
955 else
957 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
958 if (!(color = create_color_bitmap( width, height )))
960 DeleteObject( mask );
961 goto done;
963 SelectObject( hdc, color );
964 StretchDIBits( hdc, 0, 0, width, height,
965 0, 0, bmi_width, bmi_height,
966 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
968 if (bmi_has_alpha( bmi_copy, color_bits ))
970 alpha = create_alpha_bitmap( color, bmi_copy, color_bits );
971 if (!mask_size) /* generate mask from alpha */
973 LONG x, y, dst_stride = ((bmi_width + 31) / 8) & ~3;
975 if ((alpha_mask_bits = heap_calloc( bmi_height, dst_stride )))
977 static const unsigned char masks[] = { 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1 };
978 const DWORD *src = color_bits;
979 unsigned char *dst = alpha_mask_bits;
981 for (y = 0; y < bmi_height; y++, src += bmi_width, dst += dst_stride)
982 for (x = 0; x < bmi_width; x++)
983 if (src[x] >> 24 != 0xff) dst[x >> 3] |= masks[x & 7];
985 mask_bits = alpha_mask_bits;
986 mask_size = bmi_height * dst_stride;
991 /* convert info to monochrome to copy the mask */
992 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
994 RGBQUAD *rgb = bmi_copy->bmiColors;
996 bmi_copy->bmiHeader.biBitCount = 1;
997 bmi_copy->bmiHeader.biClrUsed = bmi_copy->bmiHeader.biClrImportant = 2;
998 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
999 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
1000 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
1002 else
1004 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)bmi_copy) + 1);
1006 ((BITMAPCOREINFO *)bmi_copy)->bmciHeader.bcBitCount = 1;
1007 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
1008 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
1012 if (mask_size)
1014 SelectObject( hdc, mask );
1015 StretchDIBits( hdc, 0, 0, width, height,
1016 0, 0, bmi_width, bmi_height,
1017 mask_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
1019 ret = TRUE;
1021 done:
1022 DeleteDC( hdc );
1023 HeapFree( GetProcessHeap(), 0, bmi_copy );
1024 HeapFree( GetProcessHeap(), 0, alpha_mask_bits );
1026 if (ret)
1027 hObj = alloc_icon_handle( FALSE, 0 );
1028 if (hObj)
1030 struct cursoricon_object *info = get_icon_ptr( hObj );
1031 struct cursoricon_frame *frame;
1033 info->is_icon = bIcon;
1034 info->module = module;
1035 info->hotspot = hotspot;
1036 frame = get_icon_frame( info, 0 );
1037 frame->delay = ~0;
1038 frame->width = width;
1039 frame->height = height;
1040 frame->color = color;
1041 frame->mask = mask;
1042 frame->alpha = alpha;
1043 release_icon_frame( info, frame );
1044 if (!IS_INTRESOURCE(resname))
1046 info->resname = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(resname) + 1) * sizeof(WCHAR) );
1047 if (info->resname) lstrcpyW( info->resname, resname );
1049 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
1051 if (module && (cFlag & LR_SHARED))
1053 info->rsrc = rsrc;
1054 list_add_head( &icon_cache, &info->entry );
1056 release_user_handle_ptr( info );
1058 else
1060 DeleteObject( color );
1061 DeleteObject( alpha );
1062 DeleteObject( mask );
1064 return hObj;
1068 /**********************************************************************
1069 * .ANI cursor support
1071 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
1072 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
1073 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
1074 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
1075 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
1076 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
1077 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
1079 #define ANI_FLAG_ICON 0x1
1080 #define ANI_FLAG_SEQUENCE 0x2
1082 typedef struct {
1083 DWORD header_size;
1084 DWORD num_frames;
1085 DWORD num_steps;
1086 DWORD width;
1087 DWORD height;
1088 DWORD bpp;
1089 DWORD num_planes;
1090 DWORD display_rate;
1091 DWORD flags;
1092 } ani_header;
1094 typedef struct {
1095 DWORD data_size;
1096 const unsigned char *data;
1097 } riff_chunk_t;
1099 static void dump_ani_header( const ani_header *header )
1101 TRACE(" header size: %d\n", header->header_size);
1102 TRACE(" frames: %d\n", header->num_frames);
1103 TRACE(" steps: %d\n", header->num_steps);
1104 TRACE(" width: %d\n", header->width);
1105 TRACE(" height: %d\n", header->height);
1106 TRACE(" bpp: %d\n", header->bpp);
1107 TRACE(" planes: %d\n", header->num_planes);
1108 TRACE(" display rate: %d\n", header->display_rate);
1109 TRACE(" flags: 0x%08x\n", header->flags);
1114 * RIFF:
1115 * DWORD "RIFF"
1116 * DWORD size
1117 * DWORD riff_id
1118 * BYTE[] data
1120 * LIST:
1121 * DWORD "LIST"
1122 * DWORD size
1123 * DWORD list_id
1124 * BYTE[] data
1126 * CHUNK:
1127 * DWORD chunk_id
1128 * DWORD size
1129 * BYTE[] data
1131 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1133 const unsigned char *ptr = parent_chunk->data;
1134 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1136 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1138 while (ptr < end)
1140 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1141 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1143 ptr += sizeof(DWORD);
1144 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1145 ptr += sizeof(DWORD);
1146 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1147 chunk->data = ptr;
1149 return;
1152 ptr += sizeof(DWORD);
1153 if (ptr >= end)
1154 break;
1155 ptr += (*(const DWORD *)ptr + 1) & ~1;
1156 ptr += sizeof(DWORD);
1162 * .ANI layout:
1164 * RIFF:'ACON' RIFF chunk
1165 * |- CHUNK:'anih' Header
1166 * |- CHUNK:'seq ' Sequence information (optional)
1167 * \- LIST:'fram' Frame list
1168 * |- CHUNK:icon Cursor frames
1169 * |- CHUNK:icon
1170 * |- ...
1171 * \- CHUNK:icon
1173 static HCURSOR CURSORICON_CreateIconFromANI( const BYTE *bits, DWORD bits_size, INT width, INT height,
1174 INT depth, BOOL is_icon, UINT loadflags )
1176 struct animated_cursoricon_object *ani_icon_data;
1177 struct cursoricon_object *info;
1178 DWORD *frame_rates = NULL;
1179 DWORD *frame_seq = NULL;
1180 ani_header header;
1181 BOOL use_seq = FALSE;
1182 HCURSOR cursor;
1183 UINT i;
1184 BOOL error = FALSE;
1185 HICON *frames;
1187 riff_chunk_t root_chunk = { bits_size, bits };
1188 riff_chunk_t ACON_chunk = {0};
1189 riff_chunk_t anih_chunk = {0};
1190 riff_chunk_t fram_chunk = {0};
1191 riff_chunk_t rate_chunk = {0};
1192 riff_chunk_t seq_chunk = {0};
1193 const unsigned char *icon_chunk;
1194 const unsigned char *icon_data;
1196 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1198 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1199 if (!ACON_chunk.data)
1201 ERR("Failed to get root chunk.\n");
1202 return 0;
1205 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1206 if (!anih_chunk.data)
1208 ERR("Failed to get 'anih' chunk.\n");
1209 return 0;
1211 memcpy( &header, anih_chunk.data, sizeof(header) );
1212 dump_ani_header( &header );
1214 if (!(header.flags & ANI_FLAG_ICON))
1216 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1217 return 0;
1220 if (header.flags & ANI_FLAG_SEQUENCE)
1222 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1223 if (seq_chunk.data)
1225 frame_seq = (DWORD *) seq_chunk.data;
1226 use_seq = TRUE;
1228 else
1230 FIXME("Sequence data expected but not found, assuming steps == frames.\n");
1231 header.num_steps = header.num_frames;
1235 riff_find_chunk( ANI_rate_ID, 0, &ACON_chunk, &rate_chunk );
1236 if (rate_chunk.data)
1237 frame_rates = (DWORD *) rate_chunk.data;
1239 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1240 if (!fram_chunk.data)
1242 ERR("Failed to get icon list.\n");
1243 return 0;
1246 cursor = alloc_icon_handle( TRUE, header.num_steps );
1247 if (!cursor) return 0;
1248 frames = HeapAlloc( GetProcessHeap(), 0, sizeof(*frames) * header.num_frames );
1249 if (!frames)
1251 free_icon_handle( cursor );
1252 return 0;
1255 info = get_icon_ptr( cursor );
1256 ani_icon_data = (struct animated_cursoricon_object *) info;
1257 info->is_icon = is_icon;
1258 ani_icon_data->num_frames = header.num_frames;
1260 /* The .ANI stores the display rate in jiffies (1/60s) */
1261 info->delay = header.display_rate;
1263 icon_chunk = fram_chunk.data;
1264 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1265 for (i=0; i<header.num_frames; i++)
1267 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1268 const CURSORICONFILEDIRENTRY *entry;
1269 INT frameWidth, frameHeight;
1270 const BITMAPINFO *bmi;
1272 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1273 bits + bits_size - icon_data,
1274 width, height, depth, loadflags );
1276 info->hotspot.x = entry->xHotspot;
1277 info->hotspot.y = entry->yHotspot;
1278 if (!header.width || !header.height)
1280 frameWidth = entry->bWidth;
1281 frameHeight = entry->bHeight;
1283 else
1285 frameWidth = header.width;
1286 frameHeight = header.height;
1289 frames[i] = NULL;
1290 if (entry->dwDIBOffset < bits + bits_size - icon_data)
1292 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1293 /* Grab a frame from the animation */
1294 frames[i] = create_icon_from_bmi( bmi, bits + bits_size - (const BYTE *)bmi,
1295 NULL, NULL, NULL, info->hotspot,
1296 is_icon, frameWidth, frameHeight, loadflags );
1299 if (!frames[i])
1301 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1302 error = TRUE;
1303 if (i == 0)
1305 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1306 ani_icon_data->num_frames = 0;
1307 release_user_handle_ptr( info );
1308 free_icon_handle( cursor );
1309 HeapFree( GetProcessHeap(), 0, frames );
1310 return 0;
1312 break;
1315 /* Advance to the next chunk */
1316 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1317 icon_data = icon_chunk + (2 * sizeof(DWORD));
1320 /* There was an error but we at least decoded the first frame, so just use that frame */
1321 if (error)
1323 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1324 for (i=1; i<ani_icon_data->num_frames; i++)
1325 free_icon_handle( ani_icon_data->frames[i] );
1326 use_seq = FALSE;
1327 info->delay = 0;
1328 ani_icon_data->num_steps = 1;
1329 ani_icon_data->num_frames = 1;
1332 /* Setup the animated frames in the correct sequence */
1333 for (i=0; i<ani_icon_data->num_steps; i++)
1335 DWORD frame_id = use_seq ? frame_seq[i] : i;
1336 struct cursoricon_frame *frame;
1338 if (frame_id >= ani_icon_data->num_frames)
1340 frame_id = ani_icon_data->num_frames-1;
1341 ERR_(cursor)("Sequence indicates frame past end of list, corrupt?\n");
1343 ani_icon_data->frames[i] = frames[frame_id];
1344 frame = get_icon_frame( info, i );
1345 if (frame_rates)
1346 frame->delay = frame_rates[i];
1347 else
1348 frame->delay = ~0;
1349 release_icon_frame( info, frame );
1352 HeapFree( GetProcessHeap(), 0, frames );
1353 release_user_handle_ptr( info );
1355 return cursor;
1359 /**********************************************************************
1360 * CreateIconFromResourceEx (USER32.@)
1362 * FIXME: Convert to mono when cFlag is LR_MONOCHROME.
1364 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1365 BOOL bIcon, DWORD dwVersion,
1366 INT width, INT height,
1367 UINT cFlag )
1369 POINT hotspot;
1370 const BITMAPINFO *bmi;
1372 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1373 bits, cbSize, dwVersion, width, height,
1374 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1376 if (!bits) return 0;
1378 if (dwVersion == 0x00020000)
1380 FIXME_(cursor)("\t2.xx resources are not supported\n");
1381 return 0;
1384 /* Check if the resource is an animated icon/cursor */
1385 if (!memcmp(bits, "RIFF", 4))
1386 return CURSORICON_CreateIconFromANI( bits, cbSize, width, height,
1387 0 /* default depth */, bIcon, cFlag );
1389 if (bIcon)
1391 hotspot.x = width / 2;
1392 hotspot.y = height / 2;
1393 bmi = (BITMAPINFO *)bits;
1395 else /* get the hotspot */
1397 const SHORT *pt = (const SHORT *)bits;
1398 hotspot.x = pt[0];
1399 hotspot.y = pt[1];
1400 bmi = (const BITMAPINFO *)(pt + 2);
1401 cbSize -= 2 * sizeof(*pt);
1404 return create_icon_from_bmi( bmi, cbSize, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1408 /**********************************************************************
1409 * CreateIconFromResource (USER32.@)
1411 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1412 BOOL bIcon, DWORD dwVersion)
1414 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1418 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1419 INT width, INT height, INT depth,
1420 BOOL fCursor, UINT loadflags)
1422 const CURSORICONFILEDIRENTRY *entry;
1423 const CURSORICONFILEDIR *dir;
1424 DWORD filesize = 0;
1425 HICON hIcon = 0;
1426 const BYTE *bits;
1427 POINT hotspot;
1429 TRACE("loading %s\n", debugstr_w( filename ));
1431 bits = map_fileW( filename, &filesize );
1432 if (!bits)
1433 return hIcon;
1435 /* Check for .ani. */
1436 if (memcmp( bits, "RIFF", 4 ) == 0)
1438 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, !fCursor, loadflags );
1439 goto end;
1442 dir = (const CURSORICONFILEDIR*) bits;
1443 if ( filesize < FIELD_OFFSET( CURSORICONFILEDIR, idEntries[dir->idCount] ))
1444 goto end;
1446 if ( fCursor )
1447 entry = CURSORICON_FindBestCursorFile( dir, filesize, width, height, depth, loadflags );
1448 else
1449 entry = CURSORICON_FindBestIconFile( dir, filesize, width, height, depth, loadflags );
1451 if ( !entry )
1452 goto end;
1454 /* check that we don't run off the end of the file */
1455 if ( entry->dwDIBOffset > filesize )
1456 goto end;
1457 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1458 goto end;
1460 hotspot.x = entry->xHotspot;
1461 hotspot.y = entry->yHotspot;
1462 hIcon = create_icon_from_bmi( (const BITMAPINFO *)&bits[entry->dwDIBOffset], filesize - entry->dwDIBOffset,
1463 NULL, NULL, NULL, hotspot, !fCursor, width, height, loadflags );
1464 end:
1465 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1466 UnmapViewOfFile( bits );
1467 return hIcon;
1470 /**********************************************************************
1471 * CURSORICON_Load
1473 * Load a cursor or icon from resource or file.
1475 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1476 INT width, INT height, INT depth,
1477 BOOL fCursor, UINT loadflags)
1479 HANDLE handle = 0;
1480 HICON hIcon = 0;
1481 HRSRC hRsrc;
1482 DWORD size;
1483 const CURSORICONDIR *dir;
1484 const CURSORICONDIRENTRY *dirEntry;
1485 const BYTE *bits;
1486 WORD wResId;
1487 POINT hotspot;
1489 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1490 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1492 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1493 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1495 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1497 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1498 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1500 /* Get directory resource ID */
1502 if (!(hRsrc = FindResourceW( hInstance, name,
1503 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1505 /* try animated resource */
1506 if (!(hRsrc = FindResourceW( hInstance, name,
1507 (LPWSTR)(fCursor ? RT_ANICURSOR : RT_ANIICON) ))) return 0;
1508 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1509 bits = LockResource( handle );
1510 return CURSORICON_CreateIconFromANI( bits, SizeofResource( hInstance, handle ),
1511 width, height, depth, !fCursor, loadflags );
1514 /* Find the best entry in the directory */
1516 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1517 if (!(dir = LockResource( handle ))) return 0;
1518 size = SizeofResource( hInstance, hRsrc );
1519 if (fCursor)
1520 dirEntry = CURSORICON_FindBestCursorRes( dir, size, width, height, depth, loadflags );
1521 else
1522 dirEntry = CURSORICON_FindBestIconRes( dir, size, width, height, depth, loadflags );
1523 if (!dirEntry) return 0;
1524 wResId = dirEntry->wResId;
1525 FreeResource( handle );
1527 /* Load the resource */
1529 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1530 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1532 /* If shared icon, check whether it was already loaded */
1533 if (loadflags & LR_SHARED)
1535 struct cursoricon_object *ptr;
1537 USER_Lock();
1538 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1540 if (ptr->module != hInstance) continue;
1541 if (ptr->rsrc != hRsrc) continue;
1542 hIcon = ptr->obj.handle;
1543 break;
1545 USER_Unlock();
1546 if (hIcon) return hIcon;
1549 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1550 size = SizeofResource( hInstance, hRsrc );
1551 bits = LockResource( handle );
1553 if (!fCursor)
1555 hotspot.x = width / 2;
1556 hotspot.y = height / 2;
1558 else /* get the hotspot */
1560 const SHORT *pt = (const SHORT *)bits;
1561 hotspot.x = pt[0];
1562 hotspot.y = pt[1];
1563 bits += 2 * sizeof(SHORT);
1564 size -= 2 * sizeof(SHORT);
1566 hIcon = create_icon_from_bmi( (const BITMAPINFO *)bits, size, hInstance, name, hRsrc,
1567 hotspot, !fCursor, width, height, loadflags );
1568 FreeResource( handle );
1569 return hIcon;
1573 /***********************************************************************
1574 * CreateCursor (USER32.@)
1576 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1577 INT xHotSpot, INT yHotSpot,
1578 INT nWidth, INT nHeight,
1579 LPCVOID lpANDbits, LPCVOID lpXORbits )
1581 ICONINFO info;
1582 HCURSOR hCursor;
1584 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1585 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1587 info.fIcon = FALSE;
1588 info.xHotspot = xHotSpot;
1589 info.yHotspot = yHotSpot;
1590 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1591 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1592 hCursor = CreateIconIndirect( &info );
1593 DeleteObject( info.hbmMask );
1594 DeleteObject( info.hbmColor );
1595 return hCursor;
1599 /***********************************************************************
1600 * CreateIcon (USER32.@)
1602 * Creates an icon based on the specified bitmaps. The bitmaps must be
1603 * provided in a device dependent format and will be resized to
1604 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1605 * depth. The provided bitmaps must be top-down bitmaps.
1606 * Although Windows does not support 15bpp(*) this API must support it
1607 * for Winelib applications.
1609 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1610 * format!
1612 * RETURNS
1613 * Success: handle to an icon
1614 * Failure: NULL
1616 * FIXME: Do we need to resize the bitmaps?
1618 HICON WINAPI CreateIcon(
1619 HINSTANCE hInstance, /* [in] the application's hInstance */
1620 INT nWidth, /* [in] the width of the provided bitmaps */
1621 INT nHeight, /* [in] the height of the provided bitmaps */
1622 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1623 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1624 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1625 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1627 ICONINFO iinfo;
1628 HICON hIcon;
1630 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1631 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1633 iinfo.fIcon = TRUE;
1634 iinfo.xHotspot = nWidth / 2;
1635 iinfo.yHotspot = nHeight / 2;
1636 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1637 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1639 hIcon = CreateIconIndirect( &iinfo );
1641 DeleteObject( iinfo.hbmMask );
1642 DeleteObject( iinfo.hbmColor );
1644 return hIcon;
1648 /***********************************************************************
1649 * CopyIcon (USER32.@)
1651 HICON WINAPI CopyIcon( HICON hIcon )
1653 struct cursoricon_object *ptrOld, *ptrNew;
1654 HICON hNew;
1656 if (!(ptrOld = get_icon_ptr( hIcon )))
1658 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1659 return 0;
1661 if ((hNew = alloc_icon_handle( FALSE, 0 )))
1663 struct cursoricon_frame *frameOld, *frameNew;
1665 ptrNew = get_icon_ptr( hNew );
1666 ptrNew->is_icon = ptrOld->is_icon;
1667 ptrNew->hotspot = ptrOld->hotspot;
1668 if (!(frameOld = get_icon_frame( ptrOld, 0 )))
1670 release_user_handle_ptr( ptrOld );
1671 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1672 return 0;
1674 if (!(frameNew = get_icon_frame( ptrNew, 0 )))
1676 release_icon_frame( ptrOld, frameOld );
1677 release_user_handle_ptr( ptrOld );
1678 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1679 return 0;
1681 frameNew->delay = 0;
1682 frameNew->width = frameOld->width;
1683 frameNew->height = frameOld->height;
1684 frameNew->mask = copy_bitmap( frameOld->mask );
1685 frameNew->color = copy_bitmap( frameOld->color );
1686 frameNew->alpha = copy_bitmap( frameOld->alpha );
1687 release_icon_frame( ptrOld, frameOld );
1688 release_icon_frame( ptrNew, frameNew );
1689 release_user_handle_ptr( ptrNew );
1691 release_user_handle_ptr( ptrOld );
1692 return hNew;
1696 /***********************************************************************
1697 * DestroyIcon (USER32.@)
1699 BOOL WINAPI DestroyIcon( HICON hIcon )
1701 BOOL ret = FALSE;
1702 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1704 TRACE_(icon)("%p\n", hIcon );
1706 if (obj)
1708 BOOL shared = (obj->rsrc != NULL);
1709 release_user_handle_ptr( obj );
1710 ret = (GetCursor() != hIcon);
1711 if (!shared) free_icon_handle( hIcon );
1713 return ret;
1717 /***********************************************************************
1718 * DestroyCursor (USER32.@)
1720 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1722 return DestroyIcon( hCursor );
1725 /***********************************************************************
1726 * DrawIcon (USER32.@)
1728 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1730 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1733 /***********************************************************************
1734 * SetCursor (USER32.@)
1736 * Set the cursor shape.
1738 * RETURNS
1739 * A handle to the previous cursor shape.
1741 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1743 struct cursoricon_object *obj;
1744 HCURSOR hOldCursor;
1745 int show_count;
1746 BOOL ret;
1748 TRACE("%p\n", hCursor);
1750 SERVER_START_REQ( set_cursor )
1752 req->flags = SET_CURSOR_HANDLE;
1753 req->handle = wine_server_user_handle( hCursor );
1754 if ((ret = !wine_server_call_err( req )))
1756 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1757 show_count = reply->prev_count;
1760 SERVER_END_REQ;
1762 if (!ret) return 0;
1763 USER_Driver->pSetCursor( show_count >= 0 ? hCursor : 0 );
1765 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1766 release_user_handle_ptr( obj );
1767 return hOldCursor;
1770 /***********************************************************************
1771 * ShowCursor (USER32.@)
1773 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1775 HCURSOR cursor;
1776 int increment = bShow ? 1 : -1;
1777 int count;
1779 SERVER_START_REQ( set_cursor )
1781 req->flags = SET_CURSOR_COUNT;
1782 req->show_count = increment;
1783 wine_server_call( req );
1784 cursor = wine_server_ptr_handle( reply->prev_handle );
1785 count = reply->prev_count + increment;
1787 SERVER_END_REQ;
1789 TRACE("%d, count=%d\n", bShow, count );
1791 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1792 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1794 return count;
1797 /***********************************************************************
1798 * GetCursor (USER32.@)
1800 HCURSOR WINAPI GetCursor(void)
1802 HCURSOR ret;
1804 SERVER_START_REQ( set_cursor )
1806 req->flags = 0;
1807 wine_server_call( req );
1808 ret = wine_server_ptr_handle( reply->prev_handle );
1810 SERVER_END_REQ;
1811 return ret;
1815 /***********************************************************************
1816 * ClipCursor (USER32.@)
1818 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1820 UINT dpi;
1821 BOOL ret;
1822 RECT new_rect;
1824 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1826 if (rect)
1828 if (rect->left > rect->right || rect->top > rect->bottom) return FALSE;
1829 if ((dpi = get_thread_dpi()))
1831 new_rect = map_dpi_rect( *rect, dpi,
1832 get_monitor_dpi( MonitorFromRect( rect, MONITOR_DEFAULTTOPRIMARY )));
1833 rect = &new_rect;
1837 SERVER_START_REQ( set_cursor )
1839 req->clip_msg = WM_WINE_CLIPCURSOR;
1840 if (rect)
1842 req->flags = SET_CURSOR_CLIP;
1843 req->clip.left = rect->left;
1844 req->clip.top = rect->top;
1845 req->clip.right = rect->right;
1846 req->clip.bottom = rect->bottom;
1848 else req->flags = SET_CURSOR_NOCLIP;
1850 if ((ret = !wine_server_call( req )))
1852 new_rect.left = reply->new_clip.left;
1853 new_rect.top = reply->new_clip.top;
1854 new_rect.right = reply->new_clip.right;
1855 new_rect.bottom = reply->new_clip.bottom;
1858 SERVER_END_REQ;
1859 if (ret) USER_Driver->pClipCursor( &new_rect );
1860 return ret;
1864 /***********************************************************************
1865 * GetClipCursor (USER32.@)
1867 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1869 DPI_AWARENESS_CONTEXT context;
1870 UINT dpi;
1871 BOOL ret;
1873 if (!rect) return FALSE;
1875 SERVER_START_REQ( set_cursor )
1877 req->flags = 0;
1878 if ((ret = !wine_server_call( req )))
1880 rect->left = reply->new_clip.left;
1881 rect->top = reply->new_clip.top;
1882 rect->right = reply->new_clip.right;
1883 rect->bottom = reply->new_clip.bottom;
1886 SERVER_END_REQ;
1888 if (ret && (dpi = get_thread_dpi()))
1890 context = SetThreadDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE );
1891 *rect = map_dpi_rect( *rect, get_monitor_dpi( MonitorFromRect( rect, MONITOR_DEFAULTTOPRIMARY )), dpi );
1892 SetThreadDpiAwarenessContext( context );
1894 return ret;
1898 /***********************************************************************
1899 * SetSystemCursor (USER32.@)
1901 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1903 FIXME("(%p,%08x),stub!\n", hcur, id);
1904 return TRUE;
1908 /**********************************************************************
1909 * LookupIconIdFromDirectoryEx (USER32.@)
1911 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1912 INT width, INT height, UINT cFlag )
1914 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1915 UINT retVal = 0;
1916 if( dir && !dir->idReserved && (dir->idType & 3) )
1918 const CURSORICONDIRENTRY* entry;
1919 int depth = (cFlag & LR_MONOCHROME) ? 1 : get_display_bpp();
1921 if( bIcon )
1922 entry = CURSORICON_FindBestIconRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1923 else
1924 entry = CURSORICON_FindBestCursorRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1926 if( entry ) retVal = entry->wResId;
1928 else WARN_(cursor)("invalid resource directory\n");
1929 return retVal;
1932 /**********************************************************************
1933 * LookupIconIdFromDirectory (USER32.@)
1935 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1937 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1940 /***********************************************************************
1941 * LoadCursorW (USER32.@)
1943 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1945 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1947 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1948 LR_SHARED | LR_DEFAULTSIZE );
1951 /***********************************************************************
1952 * LoadCursorA (USER32.@)
1954 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1956 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1958 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1959 LR_SHARED | LR_DEFAULTSIZE );
1962 /***********************************************************************
1963 * LoadCursorFromFileW (USER32.@)
1965 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1967 TRACE("%s\n", debugstr_w(name));
1969 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1970 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1973 /***********************************************************************
1974 * LoadCursorFromFileA (USER32.@)
1976 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1978 TRACE("%s\n", debugstr_a(name));
1980 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1981 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1984 /***********************************************************************
1985 * LoadIconW (USER32.@)
1987 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1989 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1991 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1992 LR_SHARED | LR_DEFAULTSIZE );
1995 /***********************************************************************
1996 * LoadIconA (USER32.@)
1998 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
2000 TRACE("%p, %s\n", hInstance, debugstr_a(name));
2002 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
2003 LR_SHARED | LR_DEFAULTSIZE );
2006 /**********************************************************************
2007 * GetCursorFrameInfo (USER32.@)
2009 * NOTES
2010 * So far no use has been found for the second parameter, it is currently presumed
2011 * that this parameter is reserved for future use.
2013 * PARAMS
2014 * hCursor [I] Handle to cursor for which to retrieve information
2015 * reserved [I] No purpose has been found for this parameter (may be NULL)
2016 * istep [I] The step of the cursor for which to retrieve information
2017 * rate_jiffies [O] Pointer to DWORD that receives the frame-specific delay (cannot be NULL)
2018 * num_steps [O] Pointer to DWORD that receives the number of steps in the cursor (cannot be NULL)
2020 * RETURNS
2021 * Success: Handle to a frame of the cursor (specified by istep)
2022 * Failure: NULL cursor (0)
2024 HCURSOR WINAPI GetCursorFrameInfo(HCURSOR hCursor, DWORD reserved, DWORD istep, DWORD *rate_jiffies, DWORD *num_steps)
2026 struct cursoricon_object *ptr;
2027 HCURSOR ret = 0;
2028 UINT icon_steps;
2030 if (rate_jiffies == NULL || num_steps == NULL) return 0;
2032 if (!(ptr = get_icon_ptr( hCursor ))) return 0;
2034 TRACE("%p => %d %d %p %p\n", hCursor, reserved, istep, rate_jiffies, num_steps);
2035 if (reserved != 0)
2036 FIXME("Second parameter non-zero (%d), please report this!\n", reserved);
2038 icon_steps = get_icon_steps(ptr);
2039 if (istep < icon_steps || !ptr->is_ani)
2041 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) ptr;
2042 UINT icon_frames = 1;
2044 if (ptr->is_ani)
2045 icon_frames = ani_icon_data->num_frames;
2046 if (ptr->is_ani && icon_frames > 1)
2047 ret = ani_icon_data->frames[istep];
2048 else
2049 ret = hCursor;
2050 if (icon_frames == 1)
2052 *rate_jiffies = 0;
2053 *num_steps = 1;
2055 else if (icon_steps == 1)
2057 *num_steps = ~0;
2058 *rate_jiffies = ptr->delay;
2060 else if (istep < icon_steps)
2062 struct cursoricon_frame *frame;
2064 *num_steps = icon_steps;
2065 frame = get_icon_frame( ptr, istep );
2066 if (get_icon_steps(ptr) == 1)
2067 *num_steps = ~0;
2068 else
2069 *num_steps = get_icon_steps(ptr);
2070 /* If this specific frame does not have a delay then use the global delay */
2071 if (frame->delay == ~0)
2072 *rate_jiffies = ptr->delay;
2073 else
2074 *rate_jiffies = frame->delay;
2075 release_icon_frame( ptr, frame );
2079 release_user_handle_ptr( ptr );
2081 return ret;
2084 /**********************************************************************
2085 * GetIconInfo (USER32.@)
2087 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
2089 ICONINFOEXW infoW;
2091 infoW.cbSize = sizeof(infoW);
2092 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
2093 iconinfo->fIcon = infoW.fIcon;
2094 iconinfo->xHotspot = infoW.xHotspot;
2095 iconinfo->yHotspot = infoW.yHotspot;
2096 iconinfo->hbmColor = infoW.hbmColor;
2097 iconinfo->hbmMask = infoW.hbmMask;
2098 return TRUE;
2101 /**********************************************************************
2102 * GetIconInfoExA (USER32.@)
2104 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
2106 ICONINFOEXW infoW;
2108 if (info->cbSize != sizeof(*info))
2110 SetLastError( ERROR_INVALID_PARAMETER );
2111 return FALSE;
2113 infoW.cbSize = sizeof(infoW);
2114 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
2115 info->fIcon = infoW.fIcon;
2116 info->xHotspot = infoW.xHotspot;
2117 info->yHotspot = infoW.yHotspot;
2118 info->hbmColor = infoW.hbmColor;
2119 info->hbmMask = infoW.hbmMask;
2120 info->wResID = infoW.wResID;
2121 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
2122 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
2123 return TRUE;
2126 /**********************************************************************
2127 * GetIconInfoExW (USER32.@)
2129 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
2131 struct cursoricon_frame *frame;
2132 struct cursoricon_object *ptr;
2133 HMODULE module;
2134 BOOL ret = TRUE;
2136 if (info->cbSize != sizeof(*info))
2138 SetLastError( ERROR_INVALID_PARAMETER );
2139 return FALSE;
2141 if (!(ptr = get_icon_ptr( icon )))
2143 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2144 return FALSE;
2147 frame = get_icon_frame( ptr, 0 );
2148 if (!frame)
2150 release_user_handle_ptr( ptr );
2151 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2152 return FALSE;
2155 TRACE("%p => %dx%d\n", icon, frame->width, frame->height);
2157 info->fIcon = ptr->is_icon;
2158 info->xHotspot = ptr->hotspot.x;
2159 info->yHotspot = ptr->hotspot.y;
2160 info->hbmColor = copy_bitmap( frame->color );
2161 info->hbmMask = copy_bitmap( frame->mask );
2162 info->wResID = 0;
2163 info->szModName[0] = 0;
2164 info->szResName[0] = 0;
2165 if (ptr->module)
2167 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
2168 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
2170 if (!info->hbmMask || (!info->hbmColor && frame->color))
2172 DeleteObject( info->hbmMask );
2173 DeleteObject( info->hbmColor );
2174 ret = FALSE;
2176 module = ptr->module;
2177 release_icon_frame( ptr, frame );
2178 release_user_handle_ptr( ptr );
2179 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
2180 return ret;
2183 /* copy an icon bitmap, even when it can't be selected into a DC */
2184 /* helper for CreateIconIndirect */
2185 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
2186 HBITMAP src, int width, int height )
2188 HDC hdc = CreateCompatibleDC( 0 );
2190 if (!SelectObject( hdc, src )) /* do it the hard way */
2192 BITMAPINFO *info;
2193 void *bits;
2195 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
2196 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2197 info->bmiHeader.biWidth = width;
2198 info->bmiHeader.biHeight = height;
2199 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
2200 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
2201 info->bmiHeader.biCompression = BI_RGB;
2202 info->bmiHeader.biSizeImage = get_dib_image_size( width, height, info->bmiHeader.biBitCount );
2203 info->bmiHeader.biXPelsPerMeter = 0;
2204 info->bmiHeader.biYPelsPerMeter = 0;
2205 info->bmiHeader.biClrUsed = 0;
2206 info->bmiHeader.biClrImportant = 0;
2207 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
2208 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
2209 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
2210 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
2212 HeapFree( GetProcessHeap(), 0, bits );
2213 HeapFree( GetProcessHeap(), 0, info );
2215 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
2217 DeleteDC( hdc );
2220 /**********************************************************************
2221 * CreateIconIndirect (USER32.@)
2223 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2225 BITMAP bmpXor, bmpAnd;
2226 HICON hObj;
2227 HBITMAP color = 0, mask;
2228 int width, height;
2229 HDC hdc;
2231 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2232 iconinfo->hbmColor, iconinfo->hbmMask,
2233 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2235 if (!iconinfo->hbmMask) return 0;
2237 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2238 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2239 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2240 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2242 if (iconinfo->hbmColor)
2244 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2245 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2246 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2247 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2249 width = bmpXor.bmWidth;
2250 height = bmpXor.bmHeight;
2251 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1 || bmpAnd.bmPlanes * bmpAnd.bmBitsPixel != 1)
2253 color = create_color_bitmap( width, height );
2254 mask = CreateBitmap( width, height, 1, 1, NULL );
2256 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2258 else
2260 width = bmpAnd.bmWidth;
2261 height = bmpAnd.bmHeight;
2262 mask = CreateBitmap( width, height, 1, 1, NULL );
2265 hdc = CreateCompatibleDC( 0 );
2266 SelectObject( hdc, mask );
2267 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
2269 if (color)
2271 SelectObject( hdc, color );
2272 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
2274 else if (iconinfo->hbmColor)
2276 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
2278 else height /= 2;
2280 DeleteDC( hdc );
2282 hObj = alloc_icon_handle( FALSE, 0 );
2283 if (hObj)
2285 struct cursoricon_object *info = get_icon_ptr( hObj );
2286 struct cursoricon_frame *frame;
2288 info->is_icon = iconinfo->fIcon;
2289 frame = get_icon_frame( info, 0 );
2290 frame->delay = ~0;
2291 frame->width = width;
2292 frame->height = height;
2293 frame->color = color;
2294 frame->mask = mask;
2295 frame->alpha = create_alpha_bitmap( iconinfo->hbmColor, NULL, NULL );
2296 release_icon_frame( info, frame );
2297 if (info->is_icon)
2299 info->hotspot.x = width / 2;
2300 info->hotspot.y = height / 2;
2302 else
2304 info->hotspot.x = iconinfo->xHotspot;
2305 info->hotspot.y = iconinfo->yHotspot;
2308 release_user_handle_ptr( info );
2310 return hObj;
2313 /******************************************************************************
2314 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2316 * NOTES
2317 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2319 * PARAMS
2320 * hdc [I] Handle to device context
2321 * x0 [I] X coordinate of upper left corner
2322 * y0 [I] Y coordinate of upper left corner
2323 * hIcon [I] Handle to icon to draw
2324 * cxWidth [I] Width of icon
2325 * cyWidth [I] Height of icon
2326 * istep [I] Index of frame in animated cursor
2327 * hbr [I] Handle to background brush
2328 * flags [I] Icon-drawing flags
2330 * RETURNS
2331 * Success: TRUE
2332 * Failure: FALSE
2334 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2335 INT cxWidth, INT cyWidth, UINT istep,
2336 HBRUSH hbr, UINT flags )
2338 struct cursoricon_frame *frame;
2339 struct cursoricon_object *ptr;
2340 HDC hdc_dest, hMemDC;
2341 BOOL result = FALSE, DoOffscreen;
2342 HBITMAP hB_off = 0;
2343 COLORREF oldFg, oldBg;
2344 INT x, y, nStretchMode;
2346 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2347 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2349 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2350 if (istep >= get_icon_steps( ptr ))
2352 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
2353 release_user_handle_ptr( ptr );
2354 return FALSE;
2356 if (!(frame = get_icon_frame( ptr, istep )))
2358 FIXME_(icon)("Error retrieving icon frame %d\n", istep);
2359 release_user_handle_ptr( ptr );
2360 return FALSE;
2362 if (!(hMemDC = CreateCompatibleDC( hdc )))
2364 release_icon_frame( ptr, frame );
2365 release_user_handle_ptr( ptr );
2366 return FALSE;
2369 if (flags & DI_NOMIRROR)
2370 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2372 /* Calculate the size of the destination image. */
2373 if (cxWidth == 0)
2375 if (flags & DI_DEFAULTSIZE)
2376 cxWidth = GetSystemMetrics (SM_CXICON);
2377 else
2378 cxWidth = frame->width;
2380 if (cyWidth == 0)
2382 if (flags & DI_DEFAULTSIZE)
2383 cyWidth = GetSystemMetrics (SM_CYICON);
2384 else
2385 cyWidth = frame->height;
2388 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2390 if (DoOffscreen) {
2391 RECT r;
2393 SetRect(&r, 0, 0, cxWidth, cxWidth);
2395 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2396 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2398 DeleteDC( hdc_dest );
2399 goto failed;
2401 SelectObject(hdc_dest, hB_off);
2402 FillRect(hdc_dest, &r, hbr);
2403 x = y = 0;
2405 else
2407 hdc_dest = hdc;
2408 x = x0;
2409 y = y0;
2412 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2414 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2415 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2417 if (frame->alpha && (flags & DI_IMAGE))
2419 BOOL alpha_blend = TRUE;
2421 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2423 BITMAP bm;
2424 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2425 alpha_blend = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel > 8;
2427 if (alpha_blend)
2429 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2430 SelectObject( hMemDC, frame->alpha );
2431 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2432 0, 0, frame->width, frame->height,
2433 pixelblend )) goto done;
2437 if (flags & DI_MASK)
2439 DWORD rop = (flags & DI_IMAGE) ? SRCAND : SRCCOPY;
2440 SelectObject( hMemDC, frame->mask );
2441 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2442 hMemDC, 0, 0, frame->width, frame->height, rop );
2445 if (flags & DI_IMAGE)
2447 if (frame->color)
2449 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2450 SelectObject( hMemDC, frame->color );
2451 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2452 hMemDC, 0, 0, frame->width, frame->height, rop );
2454 else
2456 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2457 SelectObject( hMemDC, frame->mask );
2458 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2459 hMemDC, 0, frame->height, frame->width,
2460 frame->height, rop );
2464 done:
2465 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2467 SetTextColor( hdc, oldFg );
2468 SetBkColor( hdc, oldBg );
2469 SetStretchBltMode (hdc, nStretchMode);
2470 result = TRUE;
2471 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2472 if (hB_off) DeleteObject(hB_off);
2473 failed:
2474 DeleteDC( hMemDC );
2475 release_icon_frame( ptr, frame );
2476 release_user_handle_ptr( ptr );
2477 return result;
2480 /***********************************************************************
2481 * DIB_FixColorsToLoadflags
2483 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2484 * are in loadflags
2486 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2488 int colors;
2489 COLORREF c_W, c_S, c_F, c_L, c_C;
2490 int incr,i;
2491 RGBQUAD *ptr;
2492 int bitmap_type;
2493 LONG width;
2494 LONG height;
2495 WORD bpp;
2496 DWORD compr;
2498 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2500 WARN_(resource)("Invalid bitmap\n");
2501 return;
2504 if (bpp > 8) return;
2506 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2508 incr = 3;
2509 colors = 1 << bpp;
2511 else
2513 incr = 4;
2514 colors = bmi->bmiHeader.biClrUsed;
2515 if (colors > 256) colors = 256;
2516 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2519 c_W = GetSysColor(COLOR_WINDOW);
2520 c_S = GetSysColor(COLOR_3DSHADOW);
2521 c_F = GetSysColor(COLOR_3DFACE);
2522 c_L = GetSysColor(COLOR_3DLIGHT);
2524 if (loadflags & LR_LOADTRANSPARENT) {
2525 switch (bpp) {
2526 case 1: pix = pix >> 7; break;
2527 case 4: pix = pix >> 4; break;
2528 case 8: break;
2529 default:
2530 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2531 return;
2533 if (pix >= colors) {
2534 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2535 return;
2537 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2538 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2539 ptr->rgbBlue = GetBValue(c_W);
2540 ptr->rgbGreen = GetGValue(c_W);
2541 ptr->rgbRed = GetRValue(c_W);
2543 if (loadflags & LR_LOADMAP3DCOLORS)
2544 for (i=0; i<colors; i++) {
2545 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2546 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2547 if (c_C == RGB(128, 128, 128)) {
2548 ptr->rgbRed = GetRValue(c_S);
2549 ptr->rgbGreen = GetGValue(c_S);
2550 ptr->rgbBlue = GetBValue(c_S);
2551 } else if (c_C == RGB(192, 192, 192)) {
2552 ptr->rgbRed = GetRValue(c_F);
2553 ptr->rgbGreen = GetGValue(c_F);
2554 ptr->rgbBlue = GetBValue(c_F);
2555 } else if (c_C == RGB(223, 223, 223)) {
2556 ptr->rgbRed = GetRValue(c_L);
2557 ptr->rgbGreen = GetGValue(c_L);
2558 ptr->rgbBlue = GetBValue(c_L);
2564 /**********************************************************************
2565 * BITMAP_Load
2567 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2568 INT desiredx, INT desiredy, UINT loadflags )
2570 HBITMAP hbitmap = 0, orig_bm;
2571 HRSRC hRsrc;
2572 HGLOBAL handle;
2573 const char *ptr = NULL;
2574 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2575 int size;
2576 BYTE pix;
2577 char *bits;
2578 LONG width, height, new_width, new_height;
2579 WORD bpp_dummy;
2580 DWORD compr_dummy, offbits = 0;
2581 INT bm_type;
2582 HDC screen_mem_dc = NULL;
2584 if (!(loadflags & LR_LOADFROMFILE))
2586 if (!instance)
2588 /* OEM bitmap: try to load the resource from user32.dll */
2589 instance = user32_module;
2592 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2593 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2595 if ((info = LockResource( handle )) == NULL) return 0;
2597 else
2599 BITMAPFILEHEADER * bmfh;
2601 if (!(ptr = map_fileW( name, NULL ))) return 0;
2602 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2603 bmfh = (BITMAPFILEHEADER *)ptr;
2604 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2606 WARN("Invalid/unsupported bitmap format!\n");
2607 goto end;
2609 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2612 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2613 &bpp_dummy, &compr_dummy);
2614 if (bm_type == -1)
2616 WARN("Invalid bitmap format!\n");
2617 goto end;
2620 size = bitmap_info_size(info, DIB_RGB_COLORS);
2621 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2622 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2624 if (!fix_info || !scaled_info) goto end;
2625 memcpy(fix_info, info, size);
2627 pix = *((LPBYTE)info + size);
2628 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2630 memcpy(scaled_info, fix_info, size);
2632 if(desiredx != 0)
2633 new_width = desiredx;
2634 else
2635 new_width = width;
2637 if(desiredy != 0)
2638 new_height = height > 0 ? desiredy : -desiredy;
2639 else
2640 new_height = height;
2642 if(bm_type == 0)
2644 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2645 core->bcWidth = new_width;
2646 core->bcHeight = new_height;
2648 else
2650 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2651 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2652 WARN("Broken BitmapInfoHeader!\n");
2653 goto end;
2656 scaled_info->bmiHeader.biWidth = new_width;
2657 scaled_info->bmiHeader.biHeight = new_height;
2660 if (new_height < 0) new_height = -new_height;
2662 if (!(screen_mem_dc = CreateCompatibleDC( 0 ))) goto end;
2664 bits = (char *)info + (offbits ? offbits : size);
2666 if (loadflags & LR_CREATEDIBSECTION)
2668 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2669 hbitmap = CreateDIBSection(0, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2671 else
2673 if (is_dib_monochrome(fix_info))
2674 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2675 else
2676 hbitmap = create_color_bitmap(new_width, new_height);
2679 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2680 if (info->bmiHeader.biBitCount > 1)
2681 SetStretchBltMode(screen_mem_dc, HALFTONE);
2682 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2683 SelectObject(screen_mem_dc, orig_bm);
2685 end:
2686 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2687 HeapFree(GetProcessHeap(), 0, scaled_info);
2688 HeapFree(GetProcessHeap(), 0, fix_info);
2689 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2691 return hbitmap;
2694 /**********************************************************************
2695 * LoadImageA (USER32.@)
2697 * See LoadImageW.
2699 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2700 INT desiredx, INT desiredy, UINT loadflags)
2702 HANDLE res;
2703 LPWSTR u_name;
2705 if (IS_INTRESOURCE(name))
2706 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2708 __TRY {
2709 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2710 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2711 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2713 __EXCEPT_PAGE_FAULT {
2714 SetLastError( ERROR_INVALID_PARAMETER );
2715 return 0;
2717 __ENDTRY
2718 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2719 HeapFree(GetProcessHeap(), 0, u_name);
2720 return res;
2724 /******************************************************************************
2725 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2727 * PARAMS
2728 * hinst [I] Handle of instance that contains image
2729 * name [I] Name of image
2730 * type [I] Type of image
2731 * desiredx [I] Desired width
2732 * desiredy [I] Desired height
2733 * loadflags [I] Load flags
2735 * RETURNS
2736 * Success: Handle to newly loaded image
2737 * Failure: NULL
2739 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2741 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2742 INT desiredx, INT desiredy, UINT loadflags )
2744 int depth;
2745 WCHAR path[MAX_PATH];
2747 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2748 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2750 if (loadflags & LR_LOADFROMFILE)
2752 loadflags &= ~LR_SHARED;
2753 /* relative paths are not only relative to the current working directory */
2754 if (SearchPathW(NULL, name, NULL, ARRAY_SIZE(path), path, NULL)) name = path;
2756 switch (type) {
2757 case IMAGE_BITMAP:
2758 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2760 case IMAGE_ICON:
2761 case IMAGE_CURSOR:
2762 depth = 1;
2763 if (!(loadflags & LR_MONOCHROME)) depth = get_display_bpp();
2764 return CURSORICON_Load(hinst, name, desiredx, desiredy, depth, (type == IMAGE_CURSOR), loadflags);
2766 return 0;
2769 /******************************************************************************
2770 * CopyImage (USER32.@) Creates new image and copies attributes to it
2772 * PARAMS
2773 * hnd [I] Handle to image to copy
2774 * type [I] Type of image to copy
2775 * desiredx [I] Desired width of new image
2776 * desiredy [I] Desired height of new image
2777 * flags [I] Copy flags
2779 * RETURNS
2780 * Success: Handle to newly created image
2781 * Failure: NULL
2783 * BUGS
2784 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2785 * all other versions (95/2000/XP have been tested) ignore it.
2787 * NOTES
2788 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2789 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2790 * the copy will have the same depth as the screen.
2791 * The content of the image will only be copied if the bit depth of the
2792 * original image is compatible with the bit depth of the screen, or
2793 * if the source is a DIB section.
2794 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2796 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2797 INT desiredy, UINT flags )
2799 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2800 hnd, type, desiredx, desiredy, flags);
2802 switch (type)
2804 case IMAGE_BITMAP:
2806 HBITMAP res = NULL;
2807 DIBSECTION ds;
2808 int objSize;
2809 BITMAPINFO * bi;
2811 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2812 if (!objSize) return 0;
2813 if ((desiredx < 0) || (desiredy < 0)) return 0;
2815 if (flags & LR_COPYFROMRESOURCE)
2817 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2820 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2821 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2823 /* Allocate memory for a BITMAPINFOHEADER structure and a
2824 color table. The maximum number of colors in a color table
2825 is 256 which corresponds to a bitmap with depth 8.
2826 Bitmaps with higher depths don't have color tables. */
2827 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2828 if (!bi) return 0;
2830 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2831 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2832 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2833 bi->bmiHeader.biCompression = BI_RGB;
2835 if (flags & LR_CREATEDIBSECTION)
2837 /* Create a DIB section. LR_MONOCHROME is ignored */
2838 void * bits;
2839 HDC dc = CreateCompatibleDC(NULL);
2841 if (objSize == sizeof(DIBSECTION))
2843 /* The source bitmap is a DIB.
2844 Get its attributes to create an exact copy */
2845 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2848 bi->bmiHeader.biWidth = desiredx;
2849 bi->bmiHeader.biHeight = desiredy;
2851 /* Get the color table or the color masks */
2852 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2854 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2855 DeleteDC(dc);
2857 else
2859 /* Create a device-dependent bitmap */
2861 BOOL monochrome = (flags & LR_MONOCHROME);
2863 if (objSize == sizeof(DIBSECTION))
2865 /* The source bitmap is a DIB section.
2866 Get its attributes */
2867 HDC dc = CreateCompatibleDC(NULL);
2868 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2869 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2870 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2871 DeleteDC(dc);
2873 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2875 /* Look if the colors of the DIB are black and white */
2877 monochrome =
2878 (bi->bmiColors[0].rgbRed == 0xff
2879 && bi->bmiColors[0].rgbGreen == 0xff
2880 && bi->bmiColors[0].rgbBlue == 0xff
2881 && bi->bmiColors[0].rgbReserved == 0
2882 && bi->bmiColors[1].rgbRed == 0
2883 && bi->bmiColors[1].rgbGreen == 0
2884 && bi->bmiColors[1].rgbBlue == 0
2885 && bi->bmiColors[1].rgbReserved == 0)
2887 (bi->bmiColors[0].rgbRed == 0
2888 && bi->bmiColors[0].rgbGreen == 0
2889 && bi->bmiColors[0].rgbBlue == 0
2890 && bi->bmiColors[0].rgbReserved == 0
2891 && bi->bmiColors[1].rgbRed == 0xff
2892 && bi->bmiColors[1].rgbGreen == 0xff
2893 && bi->bmiColors[1].rgbBlue == 0xff
2894 && bi->bmiColors[1].rgbReserved == 0);
2897 else if (!monochrome)
2899 monochrome = ds.dsBm.bmBitsPixel == 1;
2902 if (monochrome)
2903 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2904 else
2905 res = create_color_bitmap(desiredx, desiredy);
2908 if (res)
2910 /* Only copy the bitmap if it's a DIB section or if it's
2911 compatible to the screen */
2912 if (objSize == sizeof(DIBSECTION) ||
2913 ds.dsBm.bmBitsPixel == 1 ||
2914 ds.dsBm.bmBitsPixel == get_display_bpp())
2916 /* The source bitmap may already be selected in a device context,
2917 use GetDIBits/StretchDIBits and not StretchBlt */
2919 HDC dc;
2920 void * bits;
2922 dc = CreateCompatibleDC(NULL);
2923 if (ds.dsBm.bmBitsPixel > 1)
2924 SetStretchBltMode(dc, HALFTONE);
2926 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2927 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2928 bi->bmiHeader.biSizeImage = 0;
2929 bi->bmiHeader.biClrUsed = 0;
2930 bi->bmiHeader.biClrImportant = 0;
2932 /* Fill in biSizeImage */
2933 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2934 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2936 if (bits)
2938 HBITMAP oldBmp;
2940 /* Get the image bits of the source bitmap */
2941 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2943 /* Copy it to the destination bitmap */
2944 oldBmp = SelectObject(dc, res);
2945 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2946 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2947 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2948 SelectObject(dc, oldBmp);
2950 HeapFree(GetProcessHeap(), 0, bits);
2953 DeleteDC(dc);
2956 if (flags & LR_COPYDELETEORG)
2958 DeleteObject(hnd);
2961 HeapFree(GetProcessHeap(), 0, bi);
2962 return res;
2964 case IMAGE_ICON:
2965 case IMAGE_CURSOR:
2967 struct cursoricon_object *icon;
2968 HICON res = 0;
2969 int depth = (flags & LR_MONOCHROME) ? 1 : get_display_bpp();
2971 if (flags & LR_DEFAULTSIZE)
2973 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2974 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2977 if (!(icon = get_icon_ptr( hnd ))) return 0;
2979 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2980 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2981 !icon->is_icon, flags );
2982 else
2983 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2984 release_user_handle_ptr( icon );
2986 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2987 return res;
2990 return 0;
2994 /******************************************************************************
2995 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2997 * RETURNS
2998 * Success: Handle to specified bitmap
2999 * Failure: NULL
3001 HBITMAP WINAPI LoadBitmapW(
3002 HINSTANCE instance, /* [in] Handle to application instance */
3003 LPCWSTR name) /* [in] Address of bitmap resource name */
3005 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
3008 /**********************************************************************
3009 * LoadBitmapA (USER32.@)
3011 * See LoadBitmapW.
3013 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
3015 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );