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;
1688 if (info->cbSize != sizeof(*info))
1690 SetLastError( ERROR_INVALID_PARAMETER );
1693 if (!(ptr = get_icon_ptr( icon )))
1695 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1699 TRACE("%p => %dx%d\n", icon, ptr->width, ptr->height);
1701 info->fIcon = ptr->is_icon;
1702 info->xHotspot = ptr->hotspot.x;
1703 info->yHotspot = ptr->hotspot.y;
1704 info->hbmColor = copy_bitmap( ptr->frames[0].color );
1705 info->hbmMask = copy_bitmap( ptr->frames[0].mask );
1707 info->szModName[0] = 0;
1708 info->szResName[0] = 0;
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 module = ptr->module;
1721 release_icon_ptr( icon, ptr );
1722 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
1726 /* copy an icon bitmap, even when it can't be selected into a DC */
1727 /* helper for CreateIconIndirect */
1728 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
1729 HBITMAP src, int width, int height )
1731 HDC hdc = CreateCompatibleDC( 0 );
1733 if (!SelectObject( hdc, src )) /* do it the hard way */
1738 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
1739 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1740 info->bmiHeader.biWidth = width;
1741 info->bmiHeader.biHeight = height;
1742 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
1743 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
1744 info->bmiHeader.biCompression = BI_RGB;
1745 info->bmiHeader.biSizeImage = height * get_dib_width_bytes( width, info->bmiHeader.biBitCount );
1746 info->bmiHeader.biXPelsPerMeter = 0;
1747 info->bmiHeader.biYPelsPerMeter = 0;
1748 info->bmiHeader.biClrUsed = 0;
1749 info->bmiHeader.biClrImportant = 0;
1750 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
1751 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
1752 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
1753 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
1755 HeapFree( GetProcessHeap(), 0, bits );
1756 HeapFree( GetProcessHeap(), 0, info );
1758 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
1763 /**********************************************************************
1764 * CreateIconIndirect (USER32.@)
1766 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1768 BITMAP bmpXor, bmpAnd;
1770 HBITMAP color = 0, mask;
1774 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1775 iconinfo->hbmColor, iconinfo->hbmMask,
1776 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1778 if (!iconinfo->hbmMask) return 0;
1780 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1781 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1782 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1783 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1785 if (iconinfo->hbmColor)
1787 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1788 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1789 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
1790 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
1792 width = bmpXor.bmWidth;
1793 height = bmpXor.bmHeight;
1794 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1)
1796 color = CreateCompatibleBitmap( screen_dc, width, height );
1797 mask = CreateBitmap( width, height, 1, 1, NULL );
1799 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
1803 width = bmpAnd.bmWidth;
1804 height = bmpAnd.bmHeight;
1805 mask = CreateBitmap( width, height, 1, 1, NULL );
1808 hdc = CreateCompatibleDC( 0 );
1809 SelectObject( hdc, mask );
1810 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
1814 SelectObject( hdc, color );
1815 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
1817 else if (iconinfo->hbmColor)
1819 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
1825 hObj = alloc_icon_handle(1);
1828 struct cursoricon_object *info = get_icon_ptr( hObj );
1830 info->is_icon = iconinfo->fIcon;
1831 info->width = width;
1832 info->height = height;
1833 info->frames[0].color = color;
1834 info->frames[0].mask = mask;
1835 info->frames[0].alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
1838 info->hotspot.x = width / 2;
1839 info->hotspot.y = height / 2;
1843 info->hotspot.x = iconinfo->xHotspot;
1844 info->hotspot.y = iconinfo->yHotspot;
1847 release_icon_ptr( hObj, info );
1848 USER_Driver->pCreateCursorIcon( hObj );
1853 /******************************************************************************
1854 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1857 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1860 * hdc [I] Handle to device context
1861 * x0 [I] X coordinate of upper left corner
1862 * y0 [I] Y coordinate of upper left corner
1863 * hIcon [I] Handle to icon to draw
1864 * cxWidth [I] Width of icon
1865 * cyWidth [I] Height of icon
1866 * istep [I] Index of frame in animated cursor
1867 * hbr [I] Handle to background brush
1868 * flags [I] Icon-drawing flags
1874 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1875 INT cxWidth, INT cyWidth, UINT istep,
1876 HBRUSH hbr, UINT flags )
1878 struct cursoricon_object *ptr;
1879 HDC hdc_dest, hMemDC;
1880 BOOL result = FALSE, DoOffscreen;
1882 COLORREF oldFg, oldBg;
1883 INT x, y, nStretchMode;
1885 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1886 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1888 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
1889 if (istep >= ptr->num_frames)
1891 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
1892 release_icon_ptr( hIcon, ptr );
1895 if (!(hMemDC = CreateCompatibleDC( hdc )))
1897 release_icon_ptr( hIcon, ptr );
1901 if (flags & DI_NOMIRROR)
1902 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
1904 /* Calculate the size of the destination image. */
1907 if (flags & DI_DEFAULTSIZE)
1908 cxWidth = GetSystemMetrics (SM_CXICON);
1910 cxWidth = ptr->width;
1914 if (flags & DI_DEFAULTSIZE)
1915 cyWidth = GetSystemMetrics (SM_CYICON);
1917 cyWidth = ptr->height;
1920 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1930 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
1931 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
1933 DeleteDC( hdc_dest );
1936 SelectObject(hdc_dest, hB_off);
1937 FillRect(hdc_dest, &r, hbr);
1947 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
1949 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1950 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1952 if (ptr->frames[istep].alpha && (flags & DI_IMAGE))
1954 BOOL is_mono = FALSE;
1956 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
1959 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
1960 is_mono = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel == 1;
1964 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
1965 SelectObject( hMemDC, ptr->frames[istep].alpha );
1966 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
1967 0, 0, ptr->width, ptr->height, pixelblend )) goto done;
1971 if (flags & DI_MASK)
1973 SelectObject( hMemDC, ptr->frames[istep].mask );
1974 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
1975 hMemDC, 0, 0, ptr->width, ptr->height, SRCAND );
1978 if (flags & DI_IMAGE)
1980 if (ptr->frames[istep].color)
1982 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
1983 SelectObject( hMemDC, ptr->frames[istep].color );
1984 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
1985 hMemDC, 0, 0, ptr->width, ptr->height, rop );
1989 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
1990 SelectObject( hMemDC, ptr->frames[istep].mask );
1991 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
1992 hMemDC, 0, ptr->height, ptr->width, ptr->height, rop );
1997 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
1999 SetTextColor( hdc, oldFg );
2000 SetBkColor( hdc, oldBg );
2001 SetStretchBltMode (hdc, nStretchMode);
2003 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2004 if (hB_off) DeleteObject(hB_off);
2007 release_icon_ptr( hIcon, ptr );
2011 /***********************************************************************
2012 * DIB_FixColorsToLoadflags
2014 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2017 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2020 COLORREF c_W, c_S, c_F, c_L, c_C;
2029 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2031 WARN_(resource)("Invalid bitmap\n");
2035 if (bpp > 8) return;
2037 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2045 colors = bmi->bmiHeader.biClrUsed;
2046 if (colors > 256) colors = 256;
2047 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2050 c_W = GetSysColor(COLOR_WINDOW);
2051 c_S = GetSysColor(COLOR_3DSHADOW);
2052 c_F = GetSysColor(COLOR_3DFACE);
2053 c_L = GetSysColor(COLOR_3DLIGHT);
2055 if (loadflags & LR_LOADTRANSPARENT) {
2057 case 1: pix = pix >> 7; break;
2058 case 4: pix = pix >> 4; break;
2061 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2064 if (pix >= colors) {
2065 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2068 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2069 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2070 ptr->rgbBlue = GetBValue(c_W);
2071 ptr->rgbGreen = GetGValue(c_W);
2072 ptr->rgbRed = GetRValue(c_W);
2074 if (loadflags & LR_LOADMAP3DCOLORS)
2075 for (i=0; i<colors; i++) {
2076 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2077 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2078 if (c_C == RGB(128, 128, 128)) {
2079 ptr->rgbRed = GetRValue(c_S);
2080 ptr->rgbGreen = GetGValue(c_S);
2081 ptr->rgbBlue = GetBValue(c_S);
2082 } else if (c_C == RGB(192, 192, 192)) {
2083 ptr->rgbRed = GetRValue(c_F);
2084 ptr->rgbGreen = GetGValue(c_F);
2085 ptr->rgbBlue = GetBValue(c_F);
2086 } else if (c_C == RGB(223, 223, 223)) {
2087 ptr->rgbRed = GetRValue(c_L);
2088 ptr->rgbGreen = GetGValue(c_L);
2089 ptr->rgbBlue = GetBValue(c_L);
2095 /**********************************************************************
2098 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2099 INT desiredx, INT desiredy, UINT loadflags )
2101 HBITMAP hbitmap = 0, orig_bm;
2105 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2109 LONG width, height, new_width, new_height;
2111 DWORD compr_dummy, offbits = 0;
2113 HDC screen_mem_dc = NULL;
2115 if (!(loadflags & LR_LOADFROMFILE))
2119 /* OEM bitmap: try to load the resource from user32.dll */
2120 instance = user32_module;
2123 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2124 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2126 if ((info = LockResource( handle )) == NULL) return 0;
2130 BITMAPFILEHEADER * bmfh;
2132 if (!(ptr = map_fileW( name, NULL ))) return 0;
2133 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2134 bmfh = (BITMAPFILEHEADER *)ptr;
2135 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2137 WARN("Invalid/unsupported bitmap format!\n");
2140 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2143 size = bitmap_info_size(info, DIB_RGB_COLORS);
2144 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2145 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2147 if (!fix_info || !scaled_info) goto end;
2148 memcpy(fix_info, info, size);
2150 pix = *((LPBYTE)info + size);
2151 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2153 memcpy(scaled_info, fix_info, size);
2154 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2155 &bpp_dummy, &compr_dummy);
2158 WARN("Invalid bitmap format!\n");
2163 new_width = desiredx;
2168 new_height = height > 0 ? desiredy : -desiredy;
2170 new_height = height;
2174 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2175 core->bcWidth = new_width;
2176 core->bcHeight = new_height;
2180 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2181 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2182 WARN("Broken BitmapInfoHeader!\n");
2186 scaled_info->bmiHeader.biWidth = new_width;
2187 scaled_info->bmiHeader.biHeight = new_height;
2190 if (new_height < 0) new_height = -new_height;
2192 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2193 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2195 bits = (char *)info + (offbits ? offbits : size);
2197 if (loadflags & LR_CREATEDIBSECTION)
2199 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2200 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2204 if (is_dib_monochrome(fix_info))
2205 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2207 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2210 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2211 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2212 SelectObject(screen_mem_dc, orig_bm);
2215 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2216 HeapFree(GetProcessHeap(), 0, scaled_info);
2217 HeapFree(GetProcessHeap(), 0, fix_info);
2218 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2223 /**********************************************************************
2224 * LoadImageA (USER32.@)
2228 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2229 INT desiredx, INT desiredy, UINT loadflags)
2234 if (IS_INTRESOURCE(name))
2235 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2238 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2239 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2240 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2242 __EXCEPT_PAGE_FAULT {
2243 SetLastError( ERROR_INVALID_PARAMETER );
2247 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2248 HeapFree(GetProcessHeap(), 0, u_name);
2253 /******************************************************************************
2254 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2257 * hinst [I] Handle of instance that contains image
2258 * name [I] Name of image
2259 * type [I] Type of image
2260 * desiredx [I] Desired width
2261 * desiredy [I] Desired height
2262 * loadflags [I] Load flags
2265 * Success: Handle to newly loaded image
2268 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2270 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2271 INT desiredx, INT desiredy, UINT loadflags )
2273 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2274 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2276 if (loadflags & LR_DEFAULTSIZE) {
2277 if (type == IMAGE_ICON) {
2278 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2279 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2280 } else if (type == IMAGE_CURSOR) {
2281 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2282 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2285 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2288 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2291 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2294 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2295 GetDeviceCaps(screen_dc, BITSPIXEL),
2301 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2302 1, TRUE, loadflags);
2307 /******************************************************************************
2308 * CopyImage (USER32.@) Creates new image and copies attributes to it
2311 * hnd [I] Handle to image to copy
2312 * type [I] Type of image to copy
2313 * desiredx [I] Desired width of new image
2314 * desiredy [I] Desired height of new image
2315 * flags [I] Copy flags
2318 * Success: Handle to newly created image
2322 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2323 * all other versions (95/2000/XP have been tested) ignore it.
2326 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2327 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2328 * the copy will have the same depth as the screen.
2329 * The content of the image will only be copied if the bit depth of the
2330 * original image is compatible with the bit depth of the screen, or
2331 * if the source is a DIB section.
2332 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2334 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2335 INT desiredy, UINT flags )
2337 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2338 hnd, type, desiredx, desiredy, flags);
2349 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2350 if (!objSize) return 0;
2351 if ((desiredx < 0) || (desiredy < 0)) return 0;
2353 if (flags & LR_COPYFROMRESOURCE)
2355 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2358 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2359 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2361 /* Allocate memory for a BITMAPINFOHEADER structure and a
2362 color table. The maximum number of colors in a color table
2363 is 256 which corresponds to a bitmap with depth 8.
2364 Bitmaps with higher depths don't have color tables. */
2365 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2368 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2369 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2370 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2371 bi->bmiHeader.biCompression = BI_RGB;
2373 if (flags & LR_CREATEDIBSECTION)
2375 /* Create a DIB section. LR_MONOCHROME is ignored */
2377 HDC dc = CreateCompatibleDC(NULL);
2379 if (objSize == sizeof(DIBSECTION))
2381 /* The source bitmap is a DIB.
2382 Get its attributes to create an exact copy */
2383 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2386 /* Get the color table or the color masks */
2387 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2389 bi->bmiHeader.biWidth = desiredx;
2390 bi->bmiHeader.biHeight = desiredy;
2391 bi->bmiHeader.biSizeImage = 0;
2393 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2398 /* Create a device-dependent bitmap */
2400 BOOL monochrome = (flags & LR_MONOCHROME);
2402 if (objSize == sizeof(DIBSECTION))
2404 /* The source bitmap is a DIB section.
2405 Get its attributes */
2406 HDC dc = CreateCompatibleDC(NULL);
2407 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2408 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2409 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2412 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2414 /* Look if the colors of the DIB are black and white */
2417 (bi->bmiColors[0].rgbRed == 0xff
2418 && bi->bmiColors[0].rgbGreen == 0xff
2419 && bi->bmiColors[0].rgbBlue == 0xff
2420 && bi->bmiColors[0].rgbReserved == 0
2421 && bi->bmiColors[1].rgbRed == 0
2422 && bi->bmiColors[1].rgbGreen == 0
2423 && bi->bmiColors[1].rgbBlue == 0
2424 && bi->bmiColors[1].rgbReserved == 0)
2426 (bi->bmiColors[0].rgbRed == 0
2427 && bi->bmiColors[0].rgbGreen == 0
2428 && bi->bmiColors[0].rgbBlue == 0
2429 && bi->bmiColors[0].rgbReserved == 0
2430 && bi->bmiColors[1].rgbRed == 0xff
2431 && bi->bmiColors[1].rgbGreen == 0xff
2432 && bi->bmiColors[1].rgbBlue == 0xff
2433 && bi->bmiColors[1].rgbReserved == 0);
2436 else if (!monochrome)
2438 monochrome = ds.dsBm.bmBitsPixel == 1;
2443 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2447 HDC screenDC = GetDC(NULL);
2448 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2449 ReleaseDC(NULL, screenDC);
2455 /* Only copy the bitmap if it's a DIB section or if it's
2456 compatible to the screen */
2459 if (objSize == sizeof(DIBSECTION))
2461 copyContents = TRUE;
2465 HDC screenDC = GetDC(NULL);
2466 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2467 ReleaseDC(NULL, screenDC);
2469 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2474 /* The source bitmap may already be selected in a device context,
2475 use GetDIBits/StretchDIBits and not StretchBlt */
2480 dc = CreateCompatibleDC(NULL);
2482 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2483 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2484 bi->bmiHeader.biSizeImage = 0;
2485 bi->bmiHeader.biClrUsed = 0;
2486 bi->bmiHeader.biClrImportant = 0;
2488 /* Fill in biSizeImage */
2489 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2490 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2496 /* Get the image bits of the source bitmap */
2497 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2499 /* Copy it to the destination bitmap */
2500 oldBmp = SelectObject(dc, res);
2501 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2502 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2503 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2504 SelectObject(dc, oldBmp);
2506 HeapFree(GetProcessHeap(), 0, bits);
2512 if (flags & LR_COPYDELETEORG)
2517 HeapFree(GetProcessHeap(), 0, bi);
2523 struct cursoricon_object *icon;
2525 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( screen_dc, BITSPIXEL );
2527 if (flags & LR_DEFAULTSIZE)
2529 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2530 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2533 if (!(icon = get_icon_ptr( hnd ))) return 0;
2535 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2536 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2537 type == IMAGE_CURSOR, flags );
2539 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2540 release_icon_ptr( hnd, icon );
2542 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2550 /******************************************************************************
2551 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2554 * Success: Handle to specified bitmap
2557 HBITMAP WINAPI LoadBitmapW(
2558 HINSTANCE instance, /* [in] Handle to application instance */
2559 LPCWSTR name) /* [in] Address of bitmap resource name */
2561 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2564 /**********************************************************************
2565 * LoadBitmapA (USER32.@)
2569 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2571 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );