2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
7 * 1998 Turchanov Sergey
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/port.h"
38 #include "wine/exception.h"
39 #include "wine/server.h"
42 #include "user_private.h"
43 #include "wine/list.h"
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
48 WINE_DECLARE_DEBUG_CHANNEL(icon);
49 WINE_DECLARE_DEBUG_CHANNEL(resource);
62 } CURSORICONFILEDIRENTRY;
69 CURSORICONFILEDIRENTRY idEntries[1];
74 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
78 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
80 static struct list icon_cache = LIST_INIT( icon_cache );
82 /**********************************************************************
83 * User objects management
86 struct cursoricon_frame
88 HBITMAP color; /* color bitmap */
89 HBITMAP alpha; /* pre-multiplied alpha bitmap for 32-bpp icons */
90 HBITMAP mask; /* mask bitmap (followed by color for 1-bpp icons) */
93 struct cursoricon_object
95 struct user_object obj; /* object header */
96 struct list entry; /* entry in shared icons list */
97 ULONG_PTR param; /* opaque param used by 16-bit code */
98 HMODULE module; /* module for icons loaded from resources */
99 LPWSTR resname; /* resource name for icons loaded from resources */
100 HRSRC rsrc; /* resource for shared icons */
101 BOOL is_icon; /* whether icon or cursor */
105 UINT num_frames; /* number of frames in the icon/cursor */
106 UINT ms_delay; /* delay between frames (in milliseconds) */
107 struct cursoricon_frame frames[1]; /* icon frame information */
110 static HICON alloc_icon_handle( UINT num_frames )
112 struct cursoricon_object *obj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
113 FIELD_OFFSET( struct cursoricon_object, frames[num_frames] ));
116 obj->num_frames = num_frames;
117 return alloc_user_handle( &obj->obj, USER_ICON );
120 static struct cursoricon_object *get_icon_ptr( HICON handle )
122 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
123 if (obj == OBJ_OTHER_PROCESS)
125 WARN( "icon handle %p from other process\n", handle );
131 static void release_icon_ptr( HICON handle, struct cursoricon_object *ptr )
133 release_user_handle_ptr( ptr );
136 static BOOL free_icon_handle( HICON handle )
138 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
140 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
143 ULONG_PTR param = obj->param;
146 assert( !obj->rsrc ); /* shared icons can't be freed */
148 for (i=0; i<obj->num_frames; i++)
150 if (obj->frames[i].alpha) DeleteObject( obj->frames[i].alpha );
151 if (obj->frames[i].color) DeleteObject( obj->frames[i].color );
152 DeleteObject( obj->frames[i].mask );
154 if (!IS_INTRESOURCE( obj->resname )) HeapFree( GetProcessHeap(), 0, obj->resname );
155 HeapFree( GetProcessHeap(), 0, obj );
156 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
157 USER_Driver->pDestroyCursorIcon( handle );
163 ULONG_PTR get_icon_param( HICON handle )
166 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
168 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
172 release_user_handle_ptr( obj );
177 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
180 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
182 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
187 release_user_handle_ptr( obj );
193 /***********************************************************************
196 * Helper function to map a file to memory:
198 * [RETURN] ptr - pointer to mapped file
199 * [RETURN] filesize - pointer size of file to be stored if not NULL
201 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
203 HANDLE hFile, hMapping;
206 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
207 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
208 if (hFile != INVALID_HANDLE_VALUE)
210 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
213 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
214 CloseHandle( hMapping );
216 *filesize = GetFileSize( hFile, NULL );
218 CloseHandle( hFile );
224 /***********************************************************************
225 * get_dib_width_bytes
227 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
229 static int get_dib_width_bytes( int width, int depth )
235 case 1: words = (width + 31) / 32; break;
236 case 4: words = (width + 7) / 8; break;
237 case 8: words = (width + 3) / 4; break;
239 case 16: words = (width + 1) / 2; break;
240 case 24: words = (width * 3 + 3)/4; break;
242 WARN("(%d): Unsupported depth\n", depth );
251 /***********************************************************************
254 * Return the size of the bitmap info structure including color table.
256 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
258 unsigned int colors, size, masks = 0;
260 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
262 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
263 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
264 return sizeof(BITMAPCOREHEADER) + colors *
265 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
267 else /* assume BITMAPINFOHEADER */
269 colors = info->bmiHeader.biClrUsed;
270 if (colors > 256) /* buffer overflow otherwise */
272 if (!colors && (info->bmiHeader.biBitCount <= 8))
273 colors = 1 << info->bmiHeader.biBitCount;
274 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
275 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
276 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
281 /***********************************************************************
284 * Helper function to duplicate a bitmap.
286 static HBITMAP copy_bitmap( HBITMAP bitmap )
289 HBITMAP new_bitmap = 0;
292 if (!bitmap) return 0;
293 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
295 if ((src = CreateCompatibleDC( 0 )) && (dst = CreateCompatibleDC( 0 )))
297 SelectObject( src, bitmap );
298 if ((new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight )))
300 SelectObject( dst, new_bitmap );
301 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
310 /***********************************************************************
313 * Returns whether a DIB can be converted to a monochrome DDB.
315 * A DIB can be converted if its color table contains only black and
316 * white. Black must be the first color in the color table.
318 * Note : If the first color in the color table is white followed by
319 * black, we can't convert it to a monochrome DDB with
320 * SetDIBits, because black and white would be inverted.
322 static BOOL is_dib_monochrome( const BITMAPINFO* info )
324 if (info->bmiHeader.biBitCount != 1) return FALSE;
326 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
328 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
330 /* Check if the first color is black */
331 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
335 /* Check if the second color is white */
336 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
337 && (rgb->rgbtBlue == 0xff));
341 else /* assume BITMAPINFOHEADER */
343 const RGBQUAD *rgb = info->bmiColors;
345 /* Check if the first color is black */
346 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
347 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
351 /* Check if the second color is white */
352 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
353 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
359 /***********************************************************************
362 * Get the info from a bitmap header.
363 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
365 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
366 LONG *height, WORD *bpp, DWORD *compr )
368 if (header->biSize == sizeof(BITMAPCOREHEADER))
370 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
371 *width = core->bcWidth;
372 *height = core->bcHeight;
373 *bpp = core->bcBitCount;
377 else if (header->biSize >= sizeof(BITMAPINFOHEADER))
379 *width = header->biWidth;
380 *height = header->biHeight;
381 *bpp = header->biBitCount;
382 *compr = header->biCompression;
385 ERR("(%d): unknown/wrong size for header\n", header->biSize );
389 /**********************************************************************
392 BOOL get_icon_size( HICON handle, SIZE *size )
394 struct cursoricon_object *info;
396 if (!(info = get_icon_ptr( handle ))) return FALSE;
397 size->cx = info->width;
398 size->cy = info->height;
399 release_icon_ptr( handle, info );
404 * The following macro functions account for the irregularities of
405 * accessing cursor and icon resources in files and resource entries.
407 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
408 int *width, int *height, int *bits );
410 /**********************************************************************
411 * CURSORICON_FindBestIcon
413 * Find the icon closest to the requested size and bit depth.
415 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
416 int width, int height, int depth )
418 int i, cx, cy, bits, bestEntry = -1;
419 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
420 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
423 iTotalDiff = 0xFFFFFFFF;
424 iColorDiff = 0xFFFFFFFF;
425 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
427 iTempXDiff = abs(width - cx);
428 iTempYDiff = abs(height - cy);
430 if(iTotalDiff > (iTempXDiff + iTempYDiff))
434 iTotalDiff = iXDiff + iYDiff;
438 /* Find Best Colors for Best Fit */
439 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
441 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
443 iTempColorDiff = abs(depth - bits);
444 if(iColorDiff > iTempColorDiff)
447 iColorDiff = iTempColorDiff;
455 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
456 int *width, int *height, int *bits )
458 CURSORICONDIR *resdir = dir;
461 if ( resdir->idCount <= n )
463 icon = &resdir->idEntries[n].ResInfo.icon;
464 *width = icon->bWidth;
465 *height = icon->bHeight;
466 *bits = resdir->idEntries[n].wBitCount;
470 /**********************************************************************
471 * CURSORICON_FindBestCursor
473 * Find the cursor closest to the requested size.
475 * FIXME: parameter 'color' ignored.
477 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
478 int width, int height, int depth )
480 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
482 /* Double height to account for AND and XOR masks */
486 /* First find the largest one smaller than or equal to the requested size*/
488 maxwidth = maxheight = 0;
489 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
491 if ((cx <= width) && (cy <= height) &&
492 (cx > maxwidth) && (cy > maxheight))
499 if (bestEntry != -1) return bestEntry;
501 /* Now find the smallest one larger than the requested size */
503 maxwidth = maxheight = 255;
504 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
506 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
517 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
518 int *width, int *height, int *bits )
520 CURSORICONDIR *resdir = dir;
523 if ( resdir->idCount <= n )
525 cursor = &resdir->idEntries[n].ResInfo.cursor;
526 *width = cursor->wWidth;
527 *height = cursor->wHeight;
528 *bits = resdir->idEntries[n].wBitCount;
532 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
533 int width, int height, int depth )
537 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
538 width, height, depth );
541 return &dir->idEntries[n];
544 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
545 int width, int height, int depth )
547 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
548 width, height, depth );
551 return &dir->idEntries[n];
554 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
555 int *width, int *height, int *bits )
557 CURSORICONFILEDIR *filedir = dir;
558 CURSORICONFILEDIRENTRY *entry;
559 BITMAPINFOHEADER *info;
561 if ( filedir->idCount <= n )
563 entry = &filedir->idEntries[n];
564 /* FIXME: check against file size */
565 info = (BITMAPINFOHEADER *)((char *)dir + entry->dwDIBOffset);
566 *width = entry->bWidth;
567 *height = entry->bHeight;
568 *bits = info->biBitCount;
572 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
573 int width, int height, int depth )
575 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
576 width, height, depth );
579 return &dir->idEntries[n];
582 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
583 int width, int height, int depth )
585 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
586 width, height, depth );
589 return &dir->idEntries[n];
592 /***********************************************************************
595 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
598 BOOL has_alpha = FALSE;
599 const unsigned char *ptr = bits;
601 if (info->bmiHeader.biBitCount != 32) return FALSE;
602 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
603 if ((has_alpha = (ptr[3] != 0))) break;
607 /***********************************************************************
608 * create_alpha_bitmap
610 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
612 static HBITMAP create_alpha_bitmap( HBITMAP color, HBITMAP mask,
613 const BITMAPINFO *src_info, const void *color_bits )
616 BITMAPINFO *info = NULL;
623 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
624 if (bm.bmBitsPixel != 32) return 0;
626 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
627 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
628 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
629 info->bmiHeader.biWidth = bm.bmWidth;
630 info->bmiHeader.biHeight = -bm.bmHeight;
631 info->bmiHeader.biPlanes = 1;
632 info->bmiHeader.biBitCount = 32;
633 info->bmiHeader.biCompression = BI_RGB;
634 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
635 info->bmiHeader.biXPelsPerMeter = 0;
636 info->bmiHeader.biYPelsPerMeter = 0;
637 info->bmiHeader.biClrUsed = 0;
638 info->bmiHeader.biClrImportant = 0;
639 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
643 SelectObject( hdc, alpha );
644 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
645 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
646 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
651 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
652 if (!bmi_has_alpha( info, bits ))
654 DeleteObject( alpha );
660 /* pre-multiply by alpha */
661 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
663 unsigned int alpha = ptr[3];
664 ptr[0] = ptr[0] * alpha / 255;
665 ptr[1] = ptr[1] * alpha / 255;
666 ptr[2] = ptr[2] * alpha / 255;
671 HeapFree( GetProcessHeap(), 0, info );
676 /***********************************************************************
677 * create_icon_bitmaps
679 * Create the color, mask and alpha bitmaps from the DIB info.
681 static BOOL create_icon_bitmaps( const BITMAPINFO *bmi, int width, int height,
682 HBITMAP *color, HBITMAP *mask, HBITMAP *alpha )
684 BOOL monochrome = is_dib_monochrome( bmi );
685 unsigned int size = bitmap_info_size( bmi, DIB_RGB_COLORS );
687 void *color_bits, *mask_bits;
691 if (!(info = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
693 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
695 memcpy( info, bmi, size );
696 info->bmiHeader.biHeight /= 2;
698 color_bits = (char *)bmi + size;
699 mask_bits = (char *)color_bits +
700 get_dib_width_bytes( bmi->bmiHeader.biWidth,
701 bmi->bmiHeader.biBitCount ) * abs(info->bmiHeader.biHeight);
706 if (!(*mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
709 /* copy color data into second half of mask bitmap */
710 SelectObject( hdc, *mask );
711 StretchDIBits( hdc, 0, height, width, height,
712 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
713 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
717 if (!(*mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
718 if (!(*color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
719 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
721 DeleteObject( *mask );
724 SelectObject( hdc, *color );
725 StretchDIBits( hdc, 0, 0, width, height,
726 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
727 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
729 if (bmi_has_alpha( info, color_bits ))
730 *alpha = create_alpha_bitmap( *color, *mask, info, color_bits );
732 /* convert info to monochrome to copy the mask */
733 info->bmiHeader.biBitCount = 1;
734 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
736 RGBQUAD *rgb = info->bmiColors;
738 info->bmiHeader.biClrUsed = info->bmiHeader.biClrImportant = 2;
739 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
740 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
741 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
745 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)info) + 1);
747 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
748 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
752 SelectObject( hdc, *mask );
753 StretchDIBits( hdc, 0, 0, width, height,
754 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
755 mask_bits, info, DIB_RGB_COLORS, SRCCOPY );
760 HeapFree( GetProcessHeap(), 0, info );
764 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi, HMODULE module, LPCWSTR resname, HRSRC rsrc,
765 POINT hotspot, BOOL bIcon, INT width, INT height, UINT cFlag )
768 HBITMAP color = 0, mask = 0, alpha = 0;
771 /* Check bitmap header */
773 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
774 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
775 bmi->bmiHeader.biCompression != BI_RGB) )
777 WARN_(cursor)("\tinvalid resource bitmap header.\n");
781 if (!width) width = bmi->bmiHeader.biWidth;
782 if (!height) height = bmi->bmiHeader.biHeight/2;
783 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
784 (bmi->bmiHeader.biWidth != width);
786 /* Scale the hotspot */
789 hotspot.x = width / 2;
790 hotspot.y = height / 2;
794 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
795 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
798 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
799 if (!screen_dc) return 0;
801 if (!create_icon_bitmaps( bmi, width, height, &color, &mask, &alpha )) return 0;
803 hObj = alloc_icon_handle(1);
806 struct cursoricon_object *info = get_icon_ptr( hObj );
808 info->is_icon = bIcon;
809 info->module = module;
810 info->hotspot = hotspot;
812 info->height = height;
813 info->frames[0].color = color;
814 info->frames[0].mask = mask;
815 info->frames[0].alpha = alpha;
816 if (!IS_INTRESOURCE(resname))
818 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
819 if (info->resname) strcpyW( info->resname, resname );
821 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
823 if (module && (cFlag & LR_SHARED))
826 list_add_head( &icon_cache, &info->entry );
828 release_icon_ptr( hObj, info );
829 USER_Driver->pCreateCursorIcon( hObj );
833 DeleteObject( color );
834 DeleteObject( alpha );
835 DeleteObject( mask );
841 /**********************************************************************
842 * .ANI cursor support
844 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
845 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
846 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
848 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
849 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
850 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
851 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
852 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
853 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
855 #define ANI_FLAG_ICON 0x1
856 #define ANI_FLAG_SEQUENCE 0x2
872 const unsigned char *data;
875 static void dump_ani_header( const ani_header *header )
877 TRACE(" header size: %d\n", header->header_size);
878 TRACE(" frames: %d\n", header->num_frames);
879 TRACE(" steps: %d\n", header->num_steps);
880 TRACE(" width: %d\n", header->width);
881 TRACE(" height: %d\n", header->height);
882 TRACE(" bpp: %d\n", header->bpp);
883 TRACE(" planes: %d\n", header->num_planes);
884 TRACE(" display rate: %d\n", header->display_rate);
885 TRACE(" flags: 0x%08x\n", header->flags);
907 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
909 const unsigned char *ptr = parent_chunk->data;
910 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
912 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
916 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
917 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
919 ptr += sizeof(DWORD);
920 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
921 ptr += sizeof(DWORD);
922 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
928 ptr += sizeof(DWORD);
929 ptr += (*(const DWORD *)ptr + 1) & ~1;
930 ptr += sizeof(DWORD);
938 * RIFF:'ACON' RIFF chunk
939 * |- CHUNK:'anih' Header
940 * |- CHUNK:'seq ' Sequence information (optional)
941 * \- LIST:'fram' Frame list
942 * |- CHUNK:icon Cursor frames
947 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
948 INT width, INT height, INT depth )
950 struct cursoricon_object *info;
951 ani_header header = {0};
955 riff_chunk_t root_chunk = { bits_size, bits };
956 riff_chunk_t ACON_chunk = {0};
957 riff_chunk_t anih_chunk = {0};
958 riff_chunk_t fram_chunk = {0};
959 const unsigned char *icon_chunk;
960 const unsigned char *icon_data;
962 TRACE("bits %p, bits_size %d\n", bits, bits_size);
964 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
965 if (!ACON_chunk.data)
967 ERR("Failed to get root chunk.\n");
971 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
972 if (!anih_chunk.data)
974 ERR("Failed to get 'anih' chunk.\n");
977 memcpy( &header, anih_chunk.data, sizeof(header) );
978 dump_ani_header( &header );
980 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
981 if (!fram_chunk.data)
983 ERR("Failed to get icon list.\n");
987 cursor = alloc_icon_handle( header.num_frames );
988 if (!cursor) return 0;
990 info = get_icon_ptr( cursor );
991 info->is_icon = FALSE;
993 /* The .ANI stores the display rate in 1/60s, we store the delay between frames in ms */
994 info->ms_delay = (100 * header.display_rate) / 6;
996 icon_chunk = fram_chunk.data;
997 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
998 for (i=0; i<header.num_frames; i++)
1000 DWORD chunk_size = *(DWORD *)(icon_chunk + sizeof(DWORD));
1001 struct cursoricon_frame *frame = &info->frames[i];
1002 CURSORICONFILEDIRENTRY *entry;
1005 entry = CURSORICON_FindBestIconFile( (CURSORICONFILEDIR *) icon_data,
1006 width, height, depth );
1008 bmi = (BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1009 info->hotspot.x = entry->xHotspot;
1010 info->hotspot.y = entry->yHotspot;
1011 if (!header.width || !header.height)
1013 header.width = entry->bWidth;
1014 header.height = entry->bHeight;
1017 /* Grab a frame from the animation */
1018 if (!create_icon_bitmaps( bmi, header.width, header.height,
1019 &frame->color, &frame->mask, &frame->alpha ))
1021 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1025 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1026 info->num_frames = 0;
1027 release_icon_ptr( cursor, info );
1028 free_icon_handle( cursor );
1034 /* Advance to the next chunk */
1035 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1036 icon_data = icon_chunk + (2 * sizeof(DWORD));
1039 /* There was an error but we at least decoded the first frame, so just use that frame */
1042 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1043 for (i=1; i<info->num_frames; i++)
1045 if (info->frames[i].mask) DeleteObject( info->frames[i].mask );
1046 if (info->frames[i].color) DeleteObject( info->frames[i].color );
1047 if (info->frames[i].alpha) DeleteObject( info->frames[i].alpha );
1049 info->num_frames = 1;
1052 info->width = header.width;
1053 info->height = header.height;
1054 release_icon_ptr( cursor, info );
1060 /**********************************************************************
1061 * CreateIconFromResourceEx (USER32.@)
1063 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1064 * with cbSize parameter as well.
1066 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1067 BOOL bIcon, DWORD dwVersion,
1068 INT width, INT height,
1074 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1075 bits, cbSize, dwVersion, width, height,
1076 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1078 if (!bits) return 0;
1080 if (dwVersion == 0x00020000)
1082 FIXME_(cursor)("\t2.xx resources are not supported\n");
1088 hotspot.x = width / 2;
1089 hotspot.y = height / 2;
1090 bmi = (BITMAPINFO *)bits;
1092 else /* get the hotspot */
1094 SHORT *pt = (SHORT *)bits;
1097 bmi = (BITMAPINFO *)(pt + 2);
1100 return CURSORICON_CreateIconFromBMI( bmi, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1104 /**********************************************************************
1105 * CreateIconFromResource (USER32.@)
1107 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1108 BOOL bIcon, DWORD dwVersion)
1110 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1114 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1115 INT width, INT height, INT depth,
1116 BOOL fCursor, UINT loadflags)
1118 CURSORICONFILEDIRENTRY *entry;
1119 CURSORICONFILEDIR *dir;
1125 TRACE("loading %s\n", debugstr_w( filename ));
1127 bits = map_fileW( filename, &filesize );
1131 /* Check for .ani. */
1132 if (memcmp( bits, "RIFF", 4 ) == 0)
1134 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height,
1139 dir = (CURSORICONFILEDIR*) bits;
1140 if ( filesize < sizeof(*dir) )
1143 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1147 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth );
1149 entry = CURSORICON_FindBestIconFile( dir, width, height, depth );
1154 /* check that we don't run off the end of the file */
1155 if ( entry->dwDIBOffset > filesize )
1157 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1160 hotspot.x = entry->xHotspot;
1161 hotspot.y = entry->yHotspot;
1162 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset], NULL, NULL, NULL,
1163 hotspot, !fCursor, width, height, loadflags );
1165 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1166 UnmapViewOfFile( bits );
1170 /**********************************************************************
1173 * Load a cursor or icon from resource or file.
1175 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1176 INT width, INT height, INT depth,
1177 BOOL fCursor, UINT loadflags)
1183 CURSORICONDIRENTRY *dirEntry;
1188 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1189 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1191 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1192 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1194 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1196 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1197 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1199 /* Get directory resource ID */
1201 if (!(hRsrc = FindResourceW( hInstance, name,
1202 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1205 /* Find the best entry in the directory */
1207 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1208 if (!(dir = LockResource( handle ))) return 0;
1210 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1212 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1213 if (!dirEntry) return 0;
1214 wResId = dirEntry->wResId;
1215 FreeResource( handle );
1217 /* Load the resource */
1219 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1220 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1222 /* If shared icon, check whether it was already loaded */
1223 if (loadflags & LR_SHARED)
1225 struct cursoricon_object *ptr;
1228 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1230 if (ptr->module != hInstance) continue;
1231 if (ptr->rsrc != hRsrc) continue;
1232 hIcon = ptr->obj.handle;
1236 if (hIcon) return hIcon;
1239 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1240 bits = LockResource( handle );
1244 hotspot.x = width / 2;
1245 hotspot.y = height / 2;
1247 else /* get the hotspot */
1249 SHORT *pt = (SHORT *)bits;
1252 bits += 2 * sizeof(SHORT);
1254 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)bits, hInstance, name, hRsrc,
1255 hotspot, !fCursor, width, height, loadflags );
1256 FreeResource( handle );
1261 /***********************************************************************
1262 * CreateCursor (USER32.@)
1264 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1265 INT xHotSpot, INT yHotSpot,
1266 INT nWidth, INT nHeight,
1267 LPCVOID lpANDbits, LPCVOID lpXORbits )
1272 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1273 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1276 info.xHotspot = xHotSpot;
1277 info.yHotspot = yHotSpot;
1278 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1279 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1280 hCursor = CreateIconIndirect( &info );
1281 DeleteObject( info.hbmMask );
1282 DeleteObject( info.hbmColor );
1287 /***********************************************************************
1288 * CreateIcon (USER32.@)
1290 * Creates an icon based on the specified bitmaps. The bitmaps must be
1291 * provided in a device dependent format and will be resized to
1292 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1293 * depth. The provided bitmaps must be top-down bitmaps.
1294 * Although Windows does not support 15bpp(*) this API must support it
1295 * for Winelib applications.
1297 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1301 * Success: handle to an icon
1304 * FIXME: Do we need to resize the bitmaps?
1306 HICON WINAPI CreateIcon(
1307 HINSTANCE hInstance, /* [in] the application's hInstance */
1308 INT nWidth, /* [in] the width of the provided bitmaps */
1309 INT nHeight, /* [in] the height of the provided bitmaps */
1310 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1311 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1312 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1313 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1318 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1319 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1322 iinfo.xHotspot = nWidth / 2;
1323 iinfo.yHotspot = nHeight / 2;
1324 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1325 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1327 hIcon = CreateIconIndirect( &iinfo );
1329 DeleteObject( iinfo.hbmMask );
1330 DeleteObject( iinfo.hbmColor );
1336 /***********************************************************************
1337 * CopyIcon (USER32.@)
1339 HICON WINAPI CopyIcon( HICON hIcon )
1341 struct cursoricon_object *ptrOld, *ptrNew;
1344 if (!(ptrOld = get_icon_ptr( hIcon )))
1346 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1349 if ((hNew = alloc_icon_handle(1)))
1351 ptrNew = get_icon_ptr( hNew );
1352 ptrNew->is_icon = ptrOld->is_icon;
1353 ptrNew->width = ptrOld->width;
1354 ptrNew->height = ptrOld->height;
1355 ptrNew->hotspot = ptrOld->hotspot;
1356 ptrNew->frames[0].mask = copy_bitmap( ptrOld->frames[0].mask );
1357 ptrNew->frames[0].color = copy_bitmap( ptrOld->frames[0].color );
1358 ptrNew->frames[0].alpha = copy_bitmap( ptrOld->frames[0].alpha );
1359 release_icon_ptr( hNew, ptrNew );
1361 release_icon_ptr( hIcon, ptrOld );
1362 if (hNew) USER_Driver->pCreateCursorIcon( hNew );
1367 /***********************************************************************
1368 * DestroyIcon (USER32.@)
1370 BOOL WINAPI DestroyIcon( HICON hIcon )
1373 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1375 TRACE_(icon)("%p\n", hIcon );
1379 BOOL shared = (obj->rsrc != NULL);
1380 release_icon_ptr( hIcon, obj );
1381 ret = (GetCursor() != hIcon);
1382 if (!shared) free_icon_handle( hIcon );
1388 /***********************************************************************
1389 * DestroyCursor (USER32.@)
1391 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1393 return DestroyIcon( hCursor );
1396 /***********************************************************************
1397 * DrawIcon (USER32.@)
1399 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1401 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1404 /***********************************************************************
1405 * SetCursor (USER32.@)
1407 * Set the cursor shape.
1410 * A handle to the previous cursor shape.
1412 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1414 struct cursoricon_object *obj;
1419 TRACE("%p\n", hCursor);
1421 SERVER_START_REQ( set_cursor )
1423 req->flags = SET_CURSOR_HANDLE;
1424 req->handle = wine_server_user_handle( hCursor );
1425 if ((ret = !wine_server_call_err( req )))
1427 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1428 show_count = reply->prev_count;
1435 /* Change the cursor shape only if it is visible */
1436 if (show_count >= 0 && hOldCursor != hCursor) USER_Driver->pSetCursor( hCursor );
1438 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1439 release_icon_ptr( hOldCursor, obj );
1443 /***********************************************************************
1444 * ShowCursor (USER32.@)
1446 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1449 int increment = bShow ? 1 : -1;
1452 SERVER_START_REQ( set_cursor )
1454 req->flags = SET_CURSOR_COUNT;
1455 req->show_count = increment;
1456 wine_server_call( req );
1457 cursor = wine_server_ptr_handle( reply->prev_handle );
1458 count = reply->prev_count + increment;
1462 TRACE("%d, count=%d\n", bShow, count );
1464 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1465 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1470 /***********************************************************************
1471 * GetCursor (USER32.@)
1473 HCURSOR WINAPI GetCursor(void)
1477 SERVER_START_REQ( set_cursor )
1480 wine_server_call( req );
1481 ret = wine_server_ptr_handle( reply->prev_handle );
1488 /***********************************************************************
1489 * ClipCursor (USER32.@)
1491 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1495 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1496 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1497 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1498 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1500 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1501 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1503 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1504 CURSOR_ClipRect = virt;
1506 USER_Driver->pClipCursor( rect );
1511 /***********************************************************************
1512 * GetClipCursor (USER32.@)
1514 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1516 /* If this is first time - initialize the rect */
1517 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1519 return CopyRect( rect, &CURSOR_ClipRect );
1523 /***********************************************************************
1524 * SetSystemCursor (USER32.@)
1526 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1528 FIXME("(%p,%08x),stub!\n", hcur, id);
1533 /**********************************************************************
1534 * LookupIconIdFromDirectoryEx (USER32.@)
1536 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1537 INT width, INT height, UINT cFlag )
1539 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1541 if( dir && !dir->idReserved && (dir->idType & 3) )
1543 CURSORICONDIRENTRY* entry;
1545 const HDC hdc = GetDC(0);
1546 const int depth = (cFlag & LR_MONOCHROME) ?
1547 1 : GetDeviceCaps(hdc, BITSPIXEL);
1551 entry = CURSORICON_FindBestIconRes( dir, width, height, depth );
1553 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth );
1555 if( entry ) retVal = entry->wResId;
1557 else WARN_(cursor)("invalid resource directory\n");
1561 /**********************************************************************
1562 * LookupIconIdFromDirectory (USER32.@)
1564 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1566 return LookupIconIdFromDirectoryEx( dir, bIcon,
1567 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1568 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1571 /***********************************************************************
1572 * LoadCursorW (USER32.@)
1574 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1576 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1578 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1579 LR_SHARED | LR_DEFAULTSIZE );
1582 /***********************************************************************
1583 * LoadCursorA (USER32.@)
1585 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1587 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1589 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1590 LR_SHARED | LR_DEFAULTSIZE );
1593 /***********************************************************************
1594 * LoadCursorFromFileW (USER32.@)
1596 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1598 TRACE("%s\n", debugstr_w(name));
1600 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1601 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1604 /***********************************************************************
1605 * LoadCursorFromFileA (USER32.@)
1607 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1609 TRACE("%s\n", debugstr_a(name));
1611 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1612 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1615 /***********************************************************************
1616 * LoadIconW (USER32.@)
1618 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1620 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1622 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1623 LR_SHARED | LR_DEFAULTSIZE );
1626 /***********************************************************************
1627 * LoadIconA (USER32.@)
1629 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1631 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1633 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1634 LR_SHARED | LR_DEFAULTSIZE );
1637 /**********************************************************************
1638 * GetIconInfo (USER32.@)
1640 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1644 infoW.cbSize = sizeof(infoW);
1645 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
1646 iconinfo->fIcon = infoW.fIcon;
1647 iconinfo->xHotspot = infoW.xHotspot;
1648 iconinfo->yHotspot = infoW.yHotspot;
1649 iconinfo->hbmColor = infoW.hbmColor;
1650 iconinfo->hbmMask = infoW.hbmMask;
1654 /**********************************************************************
1655 * GetIconInfoExA (USER32.@)
1657 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
1661 if (info->cbSize != sizeof(*info))
1663 SetLastError( ERROR_INVALID_PARAMETER );
1666 infoW.cbSize = sizeof(infoW);
1667 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
1668 info->fIcon = infoW.fIcon;
1669 info->xHotspot = infoW.xHotspot;
1670 info->yHotspot = infoW.yHotspot;
1671 info->hbmColor = infoW.hbmColor;
1672 info->hbmMask = infoW.hbmMask;
1673 info->wResID = infoW.wResID;
1674 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
1675 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
1679 /**********************************************************************
1680 * GetIconInfoExW (USER32.@)
1682 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
1684 struct cursoricon_object *ptr;
1687 if (info->cbSize != sizeof(*info))
1689 SetLastError( ERROR_INVALID_PARAMETER );
1692 if (!(ptr = get_icon_ptr( icon )))
1694 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1698 TRACE("%p => %dx%d\n", icon, ptr->width, ptr->height);
1700 info->fIcon = ptr->is_icon;
1701 info->xHotspot = ptr->hotspot.x;
1702 info->yHotspot = ptr->hotspot.y;
1703 info->hbmColor = copy_bitmap( ptr->frames[0].color );
1704 info->hbmMask = copy_bitmap( ptr->frames[0].mask );
1706 info->szModName[0] = 0;
1707 info->szResName[0] = 0;
1710 GetModuleFileNameW( ptr->module, info->szModName, MAX_PATH );
1711 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
1712 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
1714 if (!info->hbmMask || (!info->hbmColor && ptr->frames[0].color))
1716 DeleteObject( info->hbmMask );
1717 DeleteObject( info->hbmColor );
1720 release_icon_ptr( icon, ptr );
1724 /* copy an icon bitmap, even when it can't be selected into a DC */
1725 /* helper for CreateIconIndirect */
1726 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
1727 HBITMAP src, int width, int height )
1729 HDC hdc = CreateCompatibleDC( 0 );
1731 if (!SelectObject( hdc, src )) /* do it the hard way */
1736 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
1737 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1738 info->bmiHeader.biWidth = width;
1739 info->bmiHeader.biHeight = height;
1740 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
1741 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
1742 info->bmiHeader.biCompression = BI_RGB;
1743 info->bmiHeader.biSizeImage = height * get_dib_width_bytes( width, info->bmiHeader.biBitCount );
1744 info->bmiHeader.biXPelsPerMeter = 0;
1745 info->bmiHeader.biYPelsPerMeter = 0;
1746 info->bmiHeader.biClrUsed = 0;
1747 info->bmiHeader.biClrImportant = 0;
1748 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
1749 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
1750 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
1751 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
1753 HeapFree( GetProcessHeap(), 0, bits );
1754 HeapFree( GetProcessHeap(), 0, info );
1756 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
1761 /**********************************************************************
1762 * CreateIconIndirect (USER32.@)
1764 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1766 BITMAP bmpXor, bmpAnd;
1768 HBITMAP color = 0, mask;
1772 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1773 iconinfo->hbmColor, iconinfo->hbmMask,
1774 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1776 if (!iconinfo->hbmMask) return 0;
1778 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1779 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1780 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1781 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1783 if (iconinfo->hbmColor)
1785 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1786 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1787 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
1788 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
1790 width = bmpXor.bmWidth;
1791 height = bmpXor.bmHeight;
1792 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1)
1794 color = CreateCompatibleBitmap( screen_dc, width, height );
1795 mask = CreateBitmap( width, height, 1, 1, NULL );
1797 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
1801 width = bmpAnd.bmWidth;
1802 height = bmpAnd.bmHeight;
1803 mask = CreateBitmap( width, height, 1, 1, NULL );
1806 hdc = CreateCompatibleDC( 0 );
1807 SelectObject( hdc, mask );
1808 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
1812 SelectObject( hdc, color );
1813 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
1815 else if (iconinfo->hbmColor)
1817 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
1823 hObj = alloc_icon_handle(1);
1826 struct cursoricon_object *info = get_icon_ptr( hObj );
1828 info->is_icon = iconinfo->fIcon;
1829 info->width = width;
1830 info->height = height;
1831 info->frames[0].color = color;
1832 info->frames[0].mask = mask;
1833 info->frames[0].alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
1836 info->hotspot.x = width / 2;
1837 info->hotspot.y = height / 2;
1841 info->hotspot.x = iconinfo->xHotspot;
1842 info->hotspot.y = iconinfo->yHotspot;
1845 release_icon_ptr( hObj, info );
1846 USER_Driver->pCreateCursorIcon( hObj );
1851 /******************************************************************************
1852 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1855 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1858 * hdc [I] Handle to device context
1859 * x0 [I] X coordinate of upper left corner
1860 * y0 [I] Y coordinate of upper left corner
1861 * hIcon [I] Handle to icon to draw
1862 * cxWidth [I] Width of icon
1863 * cyWidth [I] Height of icon
1864 * istep [I] Index of frame in animated cursor
1865 * hbr [I] Handle to background brush
1866 * flags [I] Icon-drawing flags
1872 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1873 INT cxWidth, INT cyWidth, UINT istep,
1874 HBRUSH hbr, UINT flags )
1876 struct cursoricon_object *ptr;
1877 HDC hdc_dest, hMemDC;
1878 BOOL result = FALSE, DoOffscreen;
1880 COLORREF oldFg, oldBg;
1881 INT x, y, nStretchMode;
1883 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1884 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1886 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
1887 if (istep >= ptr->num_frames)
1889 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
1890 release_icon_ptr( hIcon, ptr );
1893 if (!(hMemDC = CreateCompatibleDC( hdc )))
1895 release_icon_ptr( hIcon, ptr );
1899 if (flags & DI_NOMIRROR)
1900 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
1902 /* Calculate the size of the destination image. */
1905 if (flags & DI_DEFAULTSIZE)
1906 cxWidth = GetSystemMetrics (SM_CXICON);
1908 cxWidth = ptr->width;
1912 if (flags & DI_DEFAULTSIZE)
1913 cyWidth = GetSystemMetrics (SM_CYICON);
1915 cyWidth = ptr->height;
1918 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1928 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
1929 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
1931 DeleteDC( hdc_dest );
1934 SelectObject(hdc_dest, hB_off);
1935 FillRect(hdc_dest, &r, hbr);
1945 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
1947 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1948 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1950 if (ptr->frames[istep].alpha && (flags & DI_IMAGE))
1952 BOOL is_mono = FALSE;
1954 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
1957 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
1958 is_mono = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel == 1;
1962 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
1963 SelectObject( hMemDC, ptr->frames[istep].alpha );
1964 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
1965 0, 0, ptr->width, ptr->height, pixelblend )) goto done;
1969 if (flags & DI_MASK)
1971 SelectObject( hMemDC, ptr->frames[istep].mask );
1972 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
1973 hMemDC, 0, 0, ptr->width, ptr->height, SRCAND );
1976 if (flags & DI_IMAGE)
1978 if (ptr->frames[istep].color)
1980 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
1981 SelectObject( hMemDC, ptr->frames[istep].color );
1982 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
1983 hMemDC, 0, 0, ptr->width, ptr->height, rop );
1987 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
1988 SelectObject( hMemDC, ptr->frames[istep].mask );
1989 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
1990 hMemDC, 0, ptr->height, ptr->width, ptr->height, rop );
1995 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
1997 SetTextColor( hdc, oldFg );
1998 SetBkColor( hdc, oldBg );
1999 SetStretchBltMode (hdc, nStretchMode);
2001 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2002 if (hB_off) DeleteObject(hB_off);
2005 release_icon_ptr( hIcon, ptr );
2009 /***********************************************************************
2010 * DIB_FixColorsToLoadflags
2012 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2015 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2018 COLORREF c_W, c_S, c_F, c_L, c_C;
2027 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2029 WARN_(resource)("Invalid bitmap\n");
2033 if (bpp > 8) return;
2035 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2043 colors = bmi->bmiHeader.biClrUsed;
2044 if (colors > 256) colors = 256;
2045 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2048 c_W = GetSysColor(COLOR_WINDOW);
2049 c_S = GetSysColor(COLOR_3DSHADOW);
2050 c_F = GetSysColor(COLOR_3DFACE);
2051 c_L = GetSysColor(COLOR_3DLIGHT);
2053 if (loadflags & LR_LOADTRANSPARENT) {
2055 case 1: pix = pix >> 7; break;
2056 case 4: pix = pix >> 4; break;
2059 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2062 if (pix >= colors) {
2063 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2066 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2067 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2068 ptr->rgbBlue = GetBValue(c_W);
2069 ptr->rgbGreen = GetGValue(c_W);
2070 ptr->rgbRed = GetRValue(c_W);
2072 if (loadflags & LR_LOADMAP3DCOLORS)
2073 for (i=0; i<colors; i++) {
2074 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2075 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2076 if (c_C == RGB(128, 128, 128)) {
2077 ptr->rgbRed = GetRValue(c_S);
2078 ptr->rgbGreen = GetGValue(c_S);
2079 ptr->rgbBlue = GetBValue(c_S);
2080 } else if (c_C == RGB(192, 192, 192)) {
2081 ptr->rgbRed = GetRValue(c_F);
2082 ptr->rgbGreen = GetGValue(c_F);
2083 ptr->rgbBlue = GetBValue(c_F);
2084 } else if (c_C == RGB(223, 223, 223)) {
2085 ptr->rgbRed = GetRValue(c_L);
2086 ptr->rgbGreen = GetGValue(c_L);
2087 ptr->rgbBlue = GetBValue(c_L);
2093 /**********************************************************************
2096 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2097 INT desiredx, INT desiredy, UINT loadflags )
2099 HBITMAP hbitmap = 0, orig_bm;
2103 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2107 LONG width, height, new_width, new_height;
2109 DWORD compr_dummy, offbits = 0;
2111 HDC screen_mem_dc = NULL;
2113 if (!(loadflags & LR_LOADFROMFILE))
2117 /* OEM bitmap: try to load the resource from user32.dll */
2118 instance = user32_module;
2121 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2122 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2124 if ((info = LockResource( handle )) == NULL) return 0;
2128 BITMAPFILEHEADER * bmfh;
2130 if (!(ptr = map_fileW( name, NULL ))) return 0;
2131 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2132 bmfh = (BITMAPFILEHEADER *)ptr;
2133 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2135 WARN("Invalid/unsupported bitmap format!\n");
2138 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2141 size = bitmap_info_size(info, DIB_RGB_COLORS);
2142 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2143 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2145 if (!fix_info || !scaled_info) goto end;
2146 memcpy(fix_info, info, size);
2148 pix = *((LPBYTE)info + size);
2149 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2151 memcpy(scaled_info, fix_info, size);
2152 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2153 &bpp_dummy, &compr_dummy);
2156 WARN("Invalid bitmap format!\n");
2161 new_width = desiredx;
2166 new_height = height > 0 ? desiredy : -desiredy;
2168 new_height = height;
2172 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2173 core->bcWidth = new_width;
2174 core->bcHeight = new_height;
2178 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2179 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2180 WARN("Broken BitmapInfoHeader!\n");
2184 scaled_info->bmiHeader.biWidth = new_width;
2185 scaled_info->bmiHeader.biHeight = new_height;
2188 if (new_height < 0) new_height = -new_height;
2190 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2191 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2193 bits = (char *)info + (offbits ? offbits : size);
2195 if (loadflags & LR_CREATEDIBSECTION)
2197 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2198 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2202 if (is_dib_monochrome(fix_info))
2203 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2205 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2208 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2209 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2210 SelectObject(screen_mem_dc, orig_bm);
2213 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2214 HeapFree(GetProcessHeap(), 0, scaled_info);
2215 HeapFree(GetProcessHeap(), 0, fix_info);
2216 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2221 /**********************************************************************
2222 * LoadImageA (USER32.@)
2226 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2227 INT desiredx, INT desiredy, UINT loadflags)
2232 if (IS_INTRESOURCE(name))
2233 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2236 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2237 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2238 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2240 __EXCEPT_PAGE_FAULT {
2241 SetLastError( ERROR_INVALID_PARAMETER );
2245 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2246 HeapFree(GetProcessHeap(), 0, u_name);
2251 /******************************************************************************
2252 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2255 * hinst [I] Handle of instance that contains image
2256 * name [I] Name of image
2257 * type [I] Type of image
2258 * desiredx [I] Desired width
2259 * desiredy [I] Desired height
2260 * loadflags [I] Load flags
2263 * Success: Handle to newly loaded image
2266 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2268 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2269 INT desiredx, INT desiredy, UINT loadflags )
2271 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2272 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2274 if (loadflags & LR_DEFAULTSIZE) {
2275 if (type == IMAGE_ICON) {
2276 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2277 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2278 } else if (type == IMAGE_CURSOR) {
2279 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2280 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2283 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2286 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2289 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2292 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2293 GetDeviceCaps(screen_dc, BITSPIXEL),
2299 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2300 1, TRUE, loadflags);
2305 /******************************************************************************
2306 * CopyImage (USER32.@) Creates new image and copies attributes to it
2309 * hnd [I] Handle to image to copy
2310 * type [I] Type of image to copy
2311 * desiredx [I] Desired width of new image
2312 * desiredy [I] Desired height of new image
2313 * flags [I] Copy flags
2316 * Success: Handle to newly created image
2320 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2321 * all other versions (95/2000/XP have been tested) ignore it.
2324 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2325 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2326 * the copy will have the same depth as the screen.
2327 * The content of the image will only be copied if the bit depth of the
2328 * original image is compatible with the bit depth of the screen, or
2329 * if the source is a DIB section.
2330 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2332 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2333 INT desiredy, UINT flags )
2335 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2336 hnd, type, desiredx, desiredy, flags);
2347 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2348 if (!objSize) return 0;
2349 if ((desiredx < 0) || (desiredy < 0)) return 0;
2351 if (flags & LR_COPYFROMRESOURCE)
2353 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2356 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2357 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2359 /* Allocate memory for a BITMAPINFOHEADER structure and a
2360 color table. The maximum number of colors in a color table
2361 is 256 which corresponds to a bitmap with depth 8.
2362 Bitmaps with higher depths don't have color tables. */
2363 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2366 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2367 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2368 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2369 bi->bmiHeader.biCompression = BI_RGB;
2371 if (flags & LR_CREATEDIBSECTION)
2373 /* Create a DIB section. LR_MONOCHROME is ignored */
2375 HDC dc = CreateCompatibleDC(NULL);
2377 if (objSize == sizeof(DIBSECTION))
2379 /* The source bitmap is a DIB.
2380 Get its attributes to create an exact copy */
2381 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2384 /* Get the color table or the color masks */
2385 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2387 bi->bmiHeader.biWidth = desiredx;
2388 bi->bmiHeader.biHeight = desiredy;
2389 bi->bmiHeader.biSizeImage = 0;
2391 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2396 /* Create a device-dependent bitmap */
2398 BOOL monochrome = (flags & LR_MONOCHROME);
2400 if (objSize == sizeof(DIBSECTION))
2402 /* The source bitmap is a DIB section.
2403 Get its attributes */
2404 HDC dc = CreateCompatibleDC(NULL);
2405 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2406 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2407 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2410 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2412 /* Look if the colors of the DIB are black and white */
2415 (bi->bmiColors[0].rgbRed == 0xff
2416 && bi->bmiColors[0].rgbGreen == 0xff
2417 && bi->bmiColors[0].rgbBlue == 0xff
2418 && bi->bmiColors[0].rgbReserved == 0
2419 && bi->bmiColors[1].rgbRed == 0
2420 && bi->bmiColors[1].rgbGreen == 0
2421 && bi->bmiColors[1].rgbBlue == 0
2422 && bi->bmiColors[1].rgbReserved == 0)
2424 (bi->bmiColors[0].rgbRed == 0
2425 && bi->bmiColors[0].rgbGreen == 0
2426 && bi->bmiColors[0].rgbBlue == 0
2427 && bi->bmiColors[0].rgbReserved == 0
2428 && bi->bmiColors[1].rgbRed == 0xff
2429 && bi->bmiColors[1].rgbGreen == 0xff
2430 && bi->bmiColors[1].rgbBlue == 0xff
2431 && bi->bmiColors[1].rgbReserved == 0);
2434 else if (!monochrome)
2436 monochrome = ds.dsBm.bmBitsPixel == 1;
2441 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2445 HDC screenDC = GetDC(NULL);
2446 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2447 ReleaseDC(NULL, screenDC);
2453 /* Only copy the bitmap if it's a DIB section or if it's
2454 compatible to the screen */
2457 if (objSize == sizeof(DIBSECTION))
2459 copyContents = TRUE;
2463 HDC screenDC = GetDC(NULL);
2464 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2465 ReleaseDC(NULL, screenDC);
2467 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2472 /* The source bitmap may already be selected in a device context,
2473 use GetDIBits/StretchDIBits and not StretchBlt */
2478 dc = CreateCompatibleDC(NULL);
2480 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2481 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2482 bi->bmiHeader.biSizeImage = 0;
2483 bi->bmiHeader.biClrUsed = 0;
2484 bi->bmiHeader.biClrImportant = 0;
2486 /* Fill in biSizeImage */
2487 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2488 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2494 /* Get the image bits of the source bitmap */
2495 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2497 /* Copy it to the destination bitmap */
2498 oldBmp = SelectObject(dc, res);
2499 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2500 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2501 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2502 SelectObject(dc, oldBmp);
2504 HeapFree(GetProcessHeap(), 0, bits);
2510 if (flags & LR_COPYDELETEORG)
2515 HeapFree(GetProcessHeap(), 0, bi);
2521 struct cursoricon_object *icon;
2523 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( screen_dc, BITSPIXEL );
2525 if (flags & LR_DEFAULTSIZE)
2527 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2528 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2531 if (!(icon = get_icon_ptr( hnd ))) return 0;
2533 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2534 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2535 type == IMAGE_CURSOR, flags );
2537 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2538 release_icon_ptr( hnd, icon );
2540 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2548 /******************************************************************************
2549 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2552 * Success: Handle to specified bitmap
2555 HBITMAP WINAPI LoadBitmapW(
2556 HINSTANCE instance, /* [in] Handle to application instance */
2557 LPCWSTR name) /* [in] Address of bitmap resource name */
2559 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2562 /**********************************************************************
2563 * LoadBitmapA (USER32.@)
2567 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2569 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );