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)( LPCVOID 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( LPCVOID dir, fnGetCIEntry get_entry,
416 int width, int height, int depth, UINT loadflags )
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;
426 if (loadflags & LR_DEFAULTSIZE)
428 if (!width) width = GetSystemMetrics( SM_CXICON );
429 if (!height) height = GetSystemMetrics( SM_CYICON );
431 else if (!width && !height)
433 /* use the size of the first entry */
434 if (!get_entry( dir, 0, &width, &height, &bits )) return -1;
438 for ( i = 0; iTotalDiff && get_entry( dir, i, &cx, &cy, &bits ); i++ )
440 iTempXDiff = abs(width - cx);
441 iTempYDiff = abs(height - cy);
443 if(iTotalDiff > (iTempXDiff + iTempYDiff))
447 iTotalDiff = iXDiff + iYDiff;
451 /* Find Best Colors for Best Fit */
452 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
454 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
456 iTempColorDiff = abs(depth - bits);
457 if(iColorDiff > iTempColorDiff)
460 iColorDiff = iTempColorDiff;
468 static BOOL CURSORICON_GetResIconEntry( LPCVOID dir, int n,
469 int *width, int *height, int *bits )
471 const CURSORICONDIR *resdir = dir;
472 const ICONRESDIR *icon;
474 if ( resdir->idCount <= n )
476 icon = &resdir->idEntries[n].ResInfo.icon;
477 *width = icon->bWidth;
478 *height = icon->bHeight;
479 *bits = resdir->idEntries[n].wBitCount;
483 /**********************************************************************
484 * CURSORICON_FindBestCursor
486 * Find the cursor closest to the requested size.
488 * FIXME: parameter 'color' ignored.
490 static int CURSORICON_FindBestCursor( LPCVOID dir, fnGetCIEntry get_entry,
491 int width, int height, int depth, UINT loadflags )
493 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
495 if (loadflags & LR_DEFAULTSIZE)
497 if (!width) width = GetSystemMetrics( SM_CXCURSOR );
498 if (!height) height = GetSystemMetrics( SM_CYCURSOR );
500 else if (!width && !height)
502 /* use the first entry */
503 if (!get_entry( dir, 0, &width, &height, &bits )) return -1;
507 /* Double height to account for AND and XOR masks */
511 /* First find the largest one smaller than or equal to the requested size*/
513 maxwidth = maxheight = 0;
514 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
516 if ((cx <= width) && (cy <= height) &&
517 (cx > maxwidth) && (cy > maxheight))
524 if (bestEntry != -1) return bestEntry;
526 /* Now find the smallest one larger than the requested size */
528 maxwidth = maxheight = 255;
529 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
531 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
542 static BOOL CURSORICON_GetResCursorEntry( LPCVOID dir, int n,
543 int *width, int *height, int *bits )
545 const CURSORICONDIR *resdir = dir;
546 const CURSORDIR *cursor;
548 if ( resdir->idCount <= n )
550 cursor = &resdir->idEntries[n].ResInfo.cursor;
551 *width = cursor->wWidth;
552 *height = cursor->wHeight;
553 *bits = resdir->idEntries[n].wBitCount;
557 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir,
558 int width, int height, int depth,
563 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
564 width, height, depth, loadflags );
567 return &dir->idEntries[n];
570 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir,
571 int width, int height, int depth,
574 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
575 width, height, depth, loadflags );
578 return &dir->idEntries[n];
581 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, int n,
582 int *width, int *height, int *bits )
584 const CURSORICONFILEDIR *filedir = dir;
585 const CURSORICONFILEDIRENTRY *entry;
586 const BITMAPINFOHEADER *info;
588 if ( filedir->idCount <= n )
590 entry = &filedir->idEntries[n];
591 /* FIXME: check against file size */
592 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
593 *width = entry->bWidth;
594 *height = entry->bHeight;
595 *bits = info->biBitCount;
599 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir,
600 int width, int height, int depth,
603 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
604 width, height, depth, loadflags );
607 return &dir->idEntries[n];
610 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir,
611 int width, int height, int depth,
614 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
615 width, height, depth, loadflags );
618 return &dir->idEntries[n];
621 /***********************************************************************
624 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
627 BOOL has_alpha = FALSE;
628 const unsigned char *ptr = bits;
630 if (info->bmiHeader.biBitCount != 32) return FALSE;
631 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
632 if ((has_alpha = (ptr[3] != 0))) break;
636 /***********************************************************************
637 * create_alpha_bitmap
639 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
641 static HBITMAP create_alpha_bitmap( HBITMAP color, HBITMAP mask,
642 const BITMAPINFO *src_info, const void *color_bits )
645 BITMAPINFO *info = NULL;
652 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
653 if (bm.bmBitsPixel != 32) return 0;
655 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
656 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
657 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
658 info->bmiHeader.biWidth = bm.bmWidth;
659 info->bmiHeader.biHeight = -bm.bmHeight;
660 info->bmiHeader.biPlanes = 1;
661 info->bmiHeader.biBitCount = 32;
662 info->bmiHeader.biCompression = BI_RGB;
663 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
664 info->bmiHeader.biXPelsPerMeter = 0;
665 info->bmiHeader.biYPelsPerMeter = 0;
666 info->bmiHeader.biClrUsed = 0;
667 info->bmiHeader.biClrImportant = 0;
668 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
672 SelectObject( hdc, alpha );
673 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
674 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
675 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
680 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
681 if (!bmi_has_alpha( info, bits ))
683 DeleteObject( alpha );
689 /* pre-multiply by alpha */
690 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
692 unsigned int alpha = ptr[3];
693 ptr[0] = ptr[0] * alpha / 255;
694 ptr[1] = ptr[1] * alpha / 255;
695 ptr[2] = ptr[2] * alpha / 255;
700 HeapFree( GetProcessHeap(), 0, info );
705 /***********************************************************************
706 * create_icon_bitmaps
708 * Create the color, mask and alpha bitmaps from the DIB info.
710 static BOOL create_icon_bitmaps( const BITMAPINFO *bmi, int width, int height,
711 HBITMAP *color, HBITMAP *mask, HBITMAP *alpha )
713 BOOL monochrome = is_dib_monochrome( bmi );
714 unsigned int size = bitmap_info_size( bmi, DIB_RGB_COLORS );
716 const void *color_bits, *mask_bits;
720 if (!(info = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
722 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
724 memcpy( info, bmi, size );
725 info->bmiHeader.biHeight /= 2;
727 color_bits = (const char*)bmi + size;
728 mask_bits = (const char*)color_bits +
729 get_dib_width_bytes( bmi->bmiHeader.biWidth,
730 bmi->bmiHeader.biBitCount ) * abs(info->bmiHeader.biHeight);
735 if (!(*mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
738 /* copy color data into second half of mask bitmap */
739 SelectObject( hdc, *mask );
740 StretchDIBits( hdc, 0, height, width, height,
741 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
742 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
746 if (!(*mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
747 if (!(*color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
748 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
750 DeleteObject( *mask );
753 SelectObject( hdc, *color );
754 StretchDIBits( hdc, 0, 0, width, height,
755 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
756 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
758 if (bmi_has_alpha( info, color_bits ))
759 *alpha = create_alpha_bitmap( *color, *mask, info, color_bits );
761 /* convert info to monochrome to copy the mask */
762 info->bmiHeader.biBitCount = 1;
763 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
765 RGBQUAD *rgb = info->bmiColors;
767 info->bmiHeader.biClrUsed = info->bmiHeader.biClrImportant = 2;
768 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
769 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
770 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
774 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)info) + 1);
776 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
777 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
781 SelectObject( hdc, *mask );
782 StretchDIBits( hdc, 0, 0, width, height,
783 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
784 mask_bits, info, DIB_RGB_COLORS, SRCCOPY );
789 HeapFree( GetProcessHeap(), 0, info );
793 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi, HMODULE module, LPCWSTR resname, HRSRC rsrc,
794 POINT hotspot, BOOL bIcon, INT width, INT height, UINT cFlag )
797 HBITMAP color = 0, mask = 0, alpha = 0;
800 /* Check bitmap header */
802 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
803 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
804 bmi->bmiHeader.biCompression != BI_RGB) )
806 WARN_(cursor)("\tinvalid resource bitmap header.\n");
810 if (cFlag & LR_DEFAULTSIZE)
812 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
813 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
817 if (!width) width = bmi->bmiHeader.biWidth;
818 if (!height) height = bmi->bmiHeader.biHeight/2;
820 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
821 (bmi->bmiHeader.biWidth != width);
823 /* Scale the hotspot */
826 hotspot.x = width / 2;
827 hotspot.y = height / 2;
831 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
832 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
835 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
836 if (!screen_dc) return 0;
838 if (!create_icon_bitmaps( bmi, width, height, &color, &mask, &alpha )) return 0;
840 hObj = alloc_icon_handle(1);
843 struct cursoricon_object *info = get_icon_ptr( hObj );
845 info->is_icon = bIcon;
846 info->module = module;
847 info->hotspot = hotspot;
849 info->height = height;
850 info->frames[0].color = color;
851 info->frames[0].mask = mask;
852 info->frames[0].alpha = alpha;
853 if (!IS_INTRESOURCE(resname))
855 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
856 if (info->resname) strcpyW( info->resname, resname );
858 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
860 if (module && (cFlag & LR_SHARED))
863 list_add_head( &icon_cache, &info->entry );
865 release_icon_ptr( hObj, info );
866 USER_Driver->pCreateCursorIcon( hObj );
870 DeleteObject( color );
871 DeleteObject( alpha );
872 DeleteObject( mask );
878 /**********************************************************************
879 * .ANI cursor support
881 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
882 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
883 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
885 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
886 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
887 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
888 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
889 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
890 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
892 #define ANI_FLAG_ICON 0x1
893 #define ANI_FLAG_SEQUENCE 0x2
909 const unsigned char *data;
912 static void dump_ani_header( const ani_header *header )
914 TRACE(" header size: %d\n", header->header_size);
915 TRACE(" frames: %d\n", header->num_frames);
916 TRACE(" steps: %d\n", header->num_steps);
917 TRACE(" width: %d\n", header->width);
918 TRACE(" height: %d\n", header->height);
919 TRACE(" bpp: %d\n", header->bpp);
920 TRACE(" planes: %d\n", header->num_planes);
921 TRACE(" display rate: %d\n", header->display_rate);
922 TRACE(" flags: 0x%08x\n", header->flags);
944 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
946 const unsigned char *ptr = parent_chunk->data;
947 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
949 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
953 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
954 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
956 ptr += sizeof(DWORD);
957 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
958 ptr += sizeof(DWORD);
959 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
965 ptr += sizeof(DWORD);
966 ptr += (*(const DWORD *)ptr + 1) & ~1;
967 ptr += sizeof(DWORD);
975 * RIFF:'ACON' RIFF chunk
976 * |- CHUNK:'anih' Header
977 * |- CHUNK:'seq ' Sequence information (optional)
978 * \- LIST:'fram' Frame list
979 * |- CHUNK:icon Cursor frames
984 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
985 INT width, INT height, INT depth, UINT loadflags )
987 struct cursoricon_object *info;
988 ani_header header = {0};
992 riff_chunk_t root_chunk = { bits_size, bits };
993 riff_chunk_t ACON_chunk = {0};
994 riff_chunk_t anih_chunk = {0};
995 riff_chunk_t fram_chunk = {0};
996 const unsigned char *icon_chunk;
997 const unsigned char *icon_data;
999 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1001 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1002 if (!ACON_chunk.data)
1004 ERR("Failed to get root chunk.\n");
1008 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1009 if (!anih_chunk.data)
1011 ERR("Failed to get 'anih' chunk.\n");
1014 memcpy( &header, anih_chunk.data, sizeof(header) );
1015 dump_ani_header( &header );
1017 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1018 if (!fram_chunk.data)
1020 ERR("Failed to get icon list.\n");
1024 cursor = alloc_icon_handle( header.num_frames );
1025 if (!cursor) return 0;
1027 info = get_icon_ptr( cursor );
1028 info->is_icon = FALSE;
1030 /* The .ANI stores the display rate in 1/60s, we store the delay between frames in ms */
1031 info->ms_delay = (100 * header.display_rate) / 6;
1033 icon_chunk = fram_chunk.data;
1034 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1035 for (i=0; i<header.num_frames; i++)
1037 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1038 struct cursoricon_frame *frame = &info->frames[i];
1039 const CURSORICONFILEDIRENTRY *entry;
1040 const BITMAPINFO *bmi;
1042 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1043 width, height, depth, loadflags );
1045 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1046 info->hotspot.x = entry->xHotspot;
1047 info->hotspot.y = entry->yHotspot;
1048 if (!header.width || !header.height)
1050 header.width = entry->bWidth;
1051 header.height = entry->bHeight;
1054 /* Grab a frame from the animation */
1055 if (!create_icon_bitmaps( bmi, header.width, header.height,
1056 &frame->color, &frame->mask, &frame->alpha ))
1058 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1062 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1063 info->num_frames = 0;
1064 release_icon_ptr( cursor, info );
1065 free_icon_handle( cursor );
1071 /* Advance to the next chunk */
1072 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1073 icon_data = icon_chunk + (2 * sizeof(DWORD));
1076 /* There was an error but we at least decoded the first frame, so just use that frame */
1079 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1080 for (i=1; i<info->num_frames; i++)
1082 if (info->frames[i].mask) DeleteObject( info->frames[i].mask );
1083 if (info->frames[i].color) DeleteObject( info->frames[i].color );
1084 if (info->frames[i].alpha) DeleteObject( info->frames[i].alpha );
1086 info->num_frames = 1;
1089 info->width = header.width;
1090 info->height = header.height;
1091 release_icon_ptr( cursor, info );
1097 /**********************************************************************
1098 * CreateIconFromResourceEx (USER32.@)
1100 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1101 * with cbSize parameter as well.
1103 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1104 BOOL bIcon, DWORD dwVersion,
1105 INT width, INT height,
1111 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1112 bits, cbSize, dwVersion, width, height,
1113 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1115 if (!bits) return 0;
1117 if (dwVersion == 0x00020000)
1119 FIXME_(cursor)("\t2.xx resources are not supported\n");
1125 hotspot.x = width / 2;
1126 hotspot.y = height / 2;
1127 bmi = (BITMAPINFO *)bits;
1129 else /* get the hotspot */
1131 SHORT *pt = (SHORT *)bits;
1134 bmi = (BITMAPINFO *)(pt + 2);
1137 return CURSORICON_CreateIconFromBMI( bmi, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1141 /**********************************************************************
1142 * CreateIconFromResource (USER32.@)
1144 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1145 BOOL bIcon, DWORD dwVersion)
1147 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1151 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1152 INT width, INT height, INT depth,
1153 BOOL fCursor, UINT loadflags)
1155 const CURSORICONFILEDIRENTRY *entry;
1156 const CURSORICONFILEDIR *dir;
1162 TRACE("loading %s\n", debugstr_w( filename ));
1164 bits = map_fileW( filename, &filesize );
1168 /* Check for .ani. */
1169 if (memcmp( bits, "RIFF", 4 ) == 0)
1171 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, loadflags );
1175 dir = (const CURSORICONFILEDIR*) bits;
1176 if ( filesize < sizeof(*dir) )
1179 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1183 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth, loadflags );
1185 entry = CURSORICON_FindBestIconFile( dir, width, height, depth, loadflags );
1190 /* check that we don't run off the end of the file */
1191 if ( entry->dwDIBOffset > filesize )
1193 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1196 hotspot.x = entry->xHotspot;
1197 hotspot.y = entry->yHotspot;
1198 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset], NULL, NULL, NULL,
1199 hotspot, !fCursor, width, height, loadflags );
1201 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1202 UnmapViewOfFile( bits );
1206 /**********************************************************************
1209 * Load a cursor or icon from resource or file.
1211 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1212 INT width, INT height, INT depth,
1213 BOOL fCursor, UINT loadflags)
1218 const CURSORICONDIR *dir;
1219 const CURSORICONDIRENTRY *dirEntry;
1224 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1225 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1227 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1228 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1230 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1232 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1233 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1235 /* Get directory resource ID */
1237 if (!(hRsrc = FindResourceW( hInstance, name,
1238 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1241 /* Find the best entry in the directory */
1243 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1244 if (!(dir = LockResource( handle ))) return 0;
1246 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth, loadflags );
1248 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth, loadflags );
1249 if (!dirEntry) return 0;
1250 wResId = dirEntry->wResId;
1251 FreeResource( handle );
1253 /* Load the resource */
1255 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1256 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1258 /* If shared icon, check whether it was already loaded */
1259 if (loadflags & LR_SHARED)
1261 struct cursoricon_object *ptr;
1264 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1266 if (ptr->module != hInstance) continue;
1267 if (ptr->rsrc != hRsrc) continue;
1268 hIcon = ptr->obj.handle;
1272 if (hIcon) return hIcon;
1275 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1276 bits = LockResource( handle );
1280 hotspot.x = width / 2;
1281 hotspot.y = height / 2;
1283 else /* get the hotspot */
1285 SHORT *pt = (SHORT *)bits;
1288 bits += 2 * sizeof(SHORT);
1290 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)bits, hInstance, name, hRsrc,
1291 hotspot, !fCursor, width, height, loadflags );
1292 FreeResource( handle );
1297 /***********************************************************************
1298 * CreateCursor (USER32.@)
1300 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1301 INT xHotSpot, INT yHotSpot,
1302 INT nWidth, INT nHeight,
1303 LPCVOID lpANDbits, LPCVOID lpXORbits )
1308 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1309 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1312 info.xHotspot = xHotSpot;
1313 info.yHotspot = yHotSpot;
1314 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1315 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1316 hCursor = CreateIconIndirect( &info );
1317 DeleteObject( info.hbmMask );
1318 DeleteObject( info.hbmColor );
1323 /***********************************************************************
1324 * CreateIcon (USER32.@)
1326 * Creates an icon based on the specified bitmaps. The bitmaps must be
1327 * provided in a device dependent format and will be resized to
1328 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1329 * depth. The provided bitmaps must be top-down bitmaps.
1330 * Although Windows does not support 15bpp(*) this API must support it
1331 * for Winelib applications.
1333 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1337 * Success: handle to an icon
1340 * FIXME: Do we need to resize the bitmaps?
1342 HICON WINAPI CreateIcon(
1343 HINSTANCE hInstance, /* [in] the application's hInstance */
1344 INT nWidth, /* [in] the width of the provided bitmaps */
1345 INT nHeight, /* [in] the height of the provided bitmaps */
1346 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1347 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1348 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1349 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1354 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1355 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1358 iinfo.xHotspot = nWidth / 2;
1359 iinfo.yHotspot = nHeight / 2;
1360 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1361 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1363 hIcon = CreateIconIndirect( &iinfo );
1365 DeleteObject( iinfo.hbmMask );
1366 DeleteObject( iinfo.hbmColor );
1372 /***********************************************************************
1373 * CopyIcon (USER32.@)
1375 HICON WINAPI CopyIcon( HICON hIcon )
1377 struct cursoricon_object *ptrOld, *ptrNew;
1380 if (!(ptrOld = get_icon_ptr( hIcon )))
1382 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1385 if ((hNew = alloc_icon_handle(1)))
1387 ptrNew = get_icon_ptr( hNew );
1388 ptrNew->is_icon = ptrOld->is_icon;
1389 ptrNew->width = ptrOld->width;
1390 ptrNew->height = ptrOld->height;
1391 ptrNew->hotspot = ptrOld->hotspot;
1392 ptrNew->frames[0].mask = copy_bitmap( ptrOld->frames[0].mask );
1393 ptrNew->frames[0].color = copy_bitmap( ptrOld->frames[0].color );
1394 ptrNew->frames[0].alpha = copy_bitmap( ptrOld->frames[0].alpha );
1395 release_icon_ptr( hNew, ptrNew );
1397 release_icon_ptr( hIcon, ptrOld );
1398 if (hNew) USER_Driver->pCreateCursorIcon( hNew );
1403 /***********************************************************************
1404 * DestroyIcon (USER32.@)
1406 BOOL WINAPI DestroyIcon( HICON hIcon )
1409 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1411 TRACE_(icon)("%p\n", hIcon );
1415 BOOL shared = (obj->rsrc != NULL);
1416 release_icon_ptr( hIcon, obj );
1417 ret = (GetCursor() != hIcon);
1418 if (!shared) free_icon_handle( hIcon );
1424 /***********************************************************************
1425 * DestroyCursor (USER32.@)
1427 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1429 return DestroyIcon( hCursor );
1432 /***********************************************************************
1433 * DrawIcon (USER32.@)
1435 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1437 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1440 /***********************************************************************
1441 * SetCursor (USER32.@)
1443 * Set the cursor shape.
1446 * A handle to the previous cursor shape.
1448 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1450 struct cursoricon_object *obj;
1455 TRACE("%p\n", hCursor);
1457 SERVER_START_REQ( set_cursor )
1459 req->flags = SET_CURSOR_HANDLE;
1460 req->handle = wine_server_user_handle( hCursor );
1461 if ((ret = !wine_server_call_err( req )))
1463 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1464 show_count = reply->prev_count;
1471 /* Change the cursor shape only if it is visible */
1472 if (show_count >= 0 && hOldCursor != hCursor) USER_Driver->pSetCursor( hCursor );
1474 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1475 release_icon_ptr( hOldCursor, obj );
1479 /***********************************************************************
1480 * ShowCursor (USER32.@)
1482 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1485 int increment = bShow ? 1 : -1;
1488 SERVER_START_REQ( set_cursor )
1490 req->flags = SET_CURSOR_COUNT;
1491 req->show_count = increment;
1492 wine_server_call( req );
1493 cursor = wine_server_ptr_handle( reply->prev_handle );
1494 count = reply->prev_count + increment;
1498 TRACE("%d, count=%d\n", bShow, count );
1500 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1501 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1506 /***********************************************************************
1507 * GetCursor (USER32.@)
1509 HCURSOR WINAPI GetCursor(void)
1513 SERVER_START_REQ( set_cursor )
1516 wine_server_call( req );
1517 ret = wine_server_ptr_handle( reply->prev_handle );
1524 /***********************************************************************
1525 * ClipCursor (USER32.@)
1527 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1531 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1532 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1533 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1534 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1536 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1537 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1539 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1540 CURSOR_ClipRect = virt;
1542 USER_Driver->pClipCursor( rect );
1547 /***********************************************************************
1548 * GetClipCursor (USER32.@)
1550 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1552 /* If this is first time - initialize the rect */
1553 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1555 return CopyRect( rect, &CURSOR_ClipRect );
1559 /***********************************************************************
1560 * SetSystemCursor (USER32.@)
1562 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1564 FIXME("(%p,%08x),stub!\n", hcur, id);
1569 /**********************************************************************
1570 * LookupIconIdFromDirectoryEx (USER32.@)
1572 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1573 INT width, INT height, UINT cFlag )
1575 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1577 if( dir && !dir->idReserved && (dir->idType & 3) )
1579 const CURSORICONDIRENTRY* entry;
1581 const HDC hdc = GetDC(0);
1582 const int depth = (cFlag & LR_MONOCHROME) ?
1583 1 : GetDeviceCaps(hdc, BITSPIXEL);
1587 entry = CURSORICON_FindBestIconRes( dir, width, height, depth, LR_DEFAULTSIZE );
1589 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth, LR_DEFAULTSIZE );
1591 if( entry ) retVal = entry->wResId;
1593 else WARN_(cursor)("invalid resource directory\n");
1597 /**********************************************************************
1598 * LookupIconIdFromDirectory (USER32.@)
1600 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1602 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1605 /***********************************************************************
1606 * LoadCursorW (USER32.@)
1608 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1610 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1612 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1613 LR_SHARED | LR_DEFAULTSIZE );
1616 /***********************************************************************
1617 * LoadCursorA (USER32.@)
1619 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1621 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1623 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1624 LR_SHARED | LR_DEFAULTSIZE );
1627 /***********************************************************************
1628 * LoadCursorFromFileW (USER32.@)
1630 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1632 TRACE("%s\n", debugstr_w(name));
1634 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1635 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1638 /***********************************************************************
1639 * LoadCursorFromFileA (USER32.@)
1641 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1643 TRACE("%s\n", debugstr_a(name));
1645 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1646 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1649 /***********************************************************************
1650 * LoadIconW (USER32.@)
1652 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1654 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1656 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1657 LR_SHARED | LR_DEFAULTSIZE );
1660 /***********************************************************************
1661 * LoadIconA (USER32.@)
1663 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1665 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1667 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1668 LR_SHARED | LR_DEFAULTSIZE );
1671 /**********************************************************************
1672 * GetIconInfo (USER32.@)
1674 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1678 infoW.cbSize = sizeof(infoW);
1679 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
1680 iconinfo->fIcon = infoW.fIcon;
1681 iconinfo->xHotspot = infoW.xHotspot;
1682 iconinfo->yHotspot = infoW.yHotspot;
1683 iconinfo->hbmColor = infoW.hbmColor;
1684 iconinfo->hbmMask = infoW.hbmMask;
1688 /**********************************************************************
1689 * GetIconInfoExA (USER32.@)
1691 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
1695 if (info->cbSize != sizeof(*info))
1697 SetLastError( ERROR_INVALID_PARAMETER );
1700 infoW.cbSize = sizeof(infoW);
1701 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
1702 info->fIcon = infoW.fIcon;
1703 info->xHotspot = infoW.xHotspot;
1704 info->yHotspot = infoW.yHotspot;
1705 info->hbmColor = infoW.hbmColor;
1706 info->hbmMask = infoW.hbmMask;
1707 info->wResID = infoW.wResID;
1708 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
1709 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
1713 /**********************************************************************
1714 * GetIconInfoExW (USER32.@)
1716 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
1718 struct cursoricon_object *ptr;
1722 if (info->cbSize != sizeof(*info))
1724 SetLastError( ERROR_INVALID_PARAMETER );
1727 if (!(ptr = get_icon_ptr( icon )))
1729 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1733 TRACE("%p => %dx%d\n", icon, ptr->width, ptr->height);
1735 info->fIcon = ptr->is_icon;
1736 info->xHotspot = ptr->hotspot.x;
1737 info->yHotspot = ptr->hotspot.y;
1738 info->hbmColor = copy_bitmap( ptr->frames[0].color );
1739 info->hbmMask = copy_bitmap( ptr->frames[0].mask );
1741 info->szModName[0] = 0;
1742 info->szResName[0] = 0;
1745 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
1746 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
1748 if (!info->hbmMask || (!info->hbmColor && ptr->frames[0].color))
1750 DeleteObject( info->hbmMask );
1751 DeleteObject( info->hbmColor );
1754 module = ptr->module;
1755 release_icon_ptr( icon, ptr );
1756 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
1760 /* copy an icon bitmap, even when it can't be selected into a DC */
1761 /* helper for CreateIconIndirect */
1762 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
1763 HBITMAP src, int width, int height )
1765 HDC hdc = CreateCompatibleDC( 0 );
1767 if (!SelectObject( hdc, src )) /* do it the hard way */
1772 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
1773 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1774 info->bmiHeader.biWidth = width;
1775 info->bmiHeader.biHeight = height;
1776 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
1777 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
1778 info->bmiHeader.biCompression = BI_RGB;
1779 info->bmiHeader.biSizeImage = height * get_dib_width_bytes( width, info->bmiHeader.biBitCount );
1780 info->bmiHeader.biXPelsPerMeter = 0;
1781 info->bmiHeader.biYPelsPerMeter = 0;
1782 info->bmiHeader.biClrUsed = 0;
1783 info->bmiHeader.biClrImportant = 0;
1784 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
1785 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
1786 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
1787 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
1789 HeapFree( GetProcessHeap(), 0, bits );
1790 HeapFree( GetProcessHeap(), 0, info );
1792 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
1797 /**********************************************************************
1798 * CreateIconIndirect (USER32.@)
1800 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1802 BITMAP bmpXor, bmpAnd;
1804 HBITMAP color = 0, mask;
1808 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1809 iconinfo->hbmColor, iconinfo->hbmMask,
1810 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1812 if (!iconinfo->hbmMask) return 0;
1814 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1815 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1816 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1817 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1819 if (iconinfo->hbmColor)
1821 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1822 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1823 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
1824 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
1826 width = bmpXor.bmWidth;
1827 height = bmpXor.bmHeight;
1828 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1)
1830 color = CreateCompatibleBitmap( screen_dc, width, height );
1831 mask = CreateBitmap( width, height, 1, 1, NULL );
1833 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
1837 width = bmpAnd.bmWidth;
1838 height = bmpAnd.bmHeight;
1839 mask = CreateBitmap( width, height, 1, 1, NULL );
1842 hdc = CreateCompatibleDC( 0 );
1843 SelectObject( hdc, mask );
1844 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
1848 SelectObject( hdc, color );
1849 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
1851 else if (iconinfo->hbmColor)
1853 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
1859 hObj = alloc_icon_handle(1);
1862 struct cursoricon_object *info = get_icon_ptr( hObj );
1864 info->is_icon = iconinfo->fIcon;
1865 info->width = width;
1866 info->height = height;
1867 info->frames[0].color = color;
1868 info->frames[0].mask = mask;
1869 info->frames[0].alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
1872 info->hotspot.x = width / 2;
1873 info->hotspot.y = height / 2;
1877 info->hotspot.x = iconinfo->xHotspot;
1878 info->hotspot.y = iconinfo->yHotspot;
1881 release_icon_ptr( hObj, info );
1882 USER_Driver->pCreateCursorIcon( hObj );
1887 /******************************************************************************
1888 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1891 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1894 * hdc [I] Handle to device context
1895 * x0 [I] X coordinate of upper left corner
1896 * y0 [I] Y coordinate of upper left corner
1897 * hIcon [I] Handle to icon to draw
1898 * cxWidth [I] Width of icon
1899 * cyWidth [I] Height of icon
1900 * istep [I] Index of frame in animated cursor
1901 * hbr [I] Handle to background brush
1902 * flags [I] Icon-drawing flags
1908 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1909 INT cxWidth, INT cyWidth, UINT istep,
1910 HBRUSH hbr, UINT flags )
1912 struct cursoricon_object *ptr;
1913 HDC hdc_dest, hMemDC;
1914 BOOL result = FALSE, DoOffscreen;
1916 COLORREF oldFg, oldBg;
1917 INT x, y, nStretchMode;
1919 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1920 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1922 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
1923 if (istep >= ptr->num_frames)
1925 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
1926 release_icon_ptr( hIcon, ptr );
1929 if (!(hMemDC = CreateCompatibleDC( hdc )))
1931 release_icon_ptr( hIcon, ptr );
1935 if (flags & DI_NOMIRROR)
1936 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
1938 /* Calculate the size of the destination image. */
1941 if (flags & DI_DEFAULTSIZE)
1942 cxWidth = GetSystemMetrics (SM_CXICON);
1944 cxWidth = ptr->width;
1948 if (flags & DI_DEFAULTSIZE)
1949 cyWidth = GetSystemMetrics (SM_CYICON);
1951 cyWidth = ptr->height;
1954 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1964 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
1965 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
1967 DeleteDC( hdc_dest );
1970 SelectObject(hdc_dest, hB_off);
1971 FillRect(hdc_dest, &r, hbr);
1981 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
1983 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1984 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1986 if (ptr->frames[istep].alpha && (flags & DI_IMAGE))
1988 BOOL is_mono = FALSE;
1990 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
1993 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
1994 is_mono = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel == 1;
1998 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
1999 SelectObject( hMemDC, ptr->frames[istep].alpha );
2000 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2001 0, 0, ptr->width, ptr->height, pixelblend )) goto done;
2005 if (flags & DI_MASK)
2007 SelectObject( hMemDC, ptr->frames[istep].mask );
2008 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2009 hMemDC, 0, 0, ptr->width, ptr->height, SRCAND );
2012 if (flags & DI_IMAGE)
2014 if (ptr->frames[istep].color)
2016 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2017 SelectObject( hMemDC, ptr->frames[istep].color );
2018 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2019 hMemDC, 0, 0, ptr->width, ptr->height, rop );
2023 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2024 SelectObject( hMemDC, ptr->frames[istep].mask );
2025 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2026 hMemDC, 0, ptr->height, ptr->width, ptr->height, rop );
2031 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2033 SetTextColor( hdc, oldFg );
2034 SetBkColor( hdc, oldBg );
2035 SetStretchBltMode (hdc, nStretchMode);
2037 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2038 if (hB_off) DeleteObject(hB_off);
2041 release_icon_ptr( hIcon, ptr );
2045 /***********************************************************************
2046 * DIB_FixColorsToLoadflags
2048 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2051 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2054 COLORREF c_W, c_S, c_F, c_L, c_C;
2063 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2065 WARN_(resource)("Invalid bitmap\n");
2069 if (bpp > 8) return;
2071 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2079 colors = bmi->bmiHeader.biClrUsed;
2080 if (colors > 256) colors = 256;
2081 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2084 c_W = GetSysColor(COLOR_WINDOW);
2085 c_S = GetSysColor(COLOR_3DSHADOW);
2086 c_F = GetSysColor(COLOR_3DFACE);
2087 c_L = GetSysColor(COLOR_3DLIGHT);
2089 if (loadflags & LR_LOADTRANSPARENT) {
2091 case 1: pix = pix >> 7; break;
2092 case 4: pix = pix >> 4; break;
2095 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2098 if (pix >= colors) {
2099 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2102 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2103 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2104 ptr->rgbBlue = GetBValue(c_W);
2105 ptr->rgbGreen = GetGValue(c_W);
2106 ptr->rgbRed = GetRValue(c_W);
2108 if (loadflags & LR_LOADMAP3DCOLORS)
2109 for (i=0; i<colors; i++) {
2110 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2111 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2112 if (c_C == RGB(128, 128, 128)) {
2113 ptr->rgbRed = GetRValue(c_S);
2114 ptr->rgbGreen = GetGValue(c_S);
2115 ptr->rgbBlue = GetBValue(c_S);
2116 } else if (c_C == RGB(192, 192, 192)) {
2117 ptr->rgbRed = GetRValue(c_F);
2118 ptr->rgbGreen = GetGValue(c_F);
2119 ptr->rgbBlue = GetBValue(c_F);
2120 } else if (c_C == RGB(223, 223, 223)) {
2121 ptr->rgbRed = GetRValue(c_L);
2122 ptr->rgbGreen = GetGValue(c_L);
2123 ptr->rgbBlue = GetBValue(c_L);
2129 /**********************************************************************
2132 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2133 INT desiredx, INT desiredy, UINT loadflags )
2135 HBITMAP hbitmap = 0, orig_bm;
2139 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2143 LONG width, height, new_width, new_height;
2145 DWORD compr_dummy, offbits = 0;
2147 HDC screen_mem_dc = NULL;
2149 if (!(loadflags & LR_LOADFROMFILE))
2153 /* OEM bitmap: try to load the resource from user32.dll */
2154 instance = user32_module;
2157 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2158 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2160 if ((info = LockResource( handle )) == NULL) return 0;
2164 BITMAPFILEHEADER * bmfh;
2166 if (!(ptr = map_fileW( name, NULL ))) return 0;
2167 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2168 bmfh = (BITMAPFILEHEADER *)ptr;
2169 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2171 WARN("Invalid/unsupported bitmap format!\n");
2174 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2177 size = bitmap_info_size(info, DIB_RGB_COLORS);
2178 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2179 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2181 if (!fix_info || !scaled_info) goto end;
2182 memcpy(fix_info, info, size);
2184 pix = *((LPBYTE)info + size);
2185 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2187 memcpy(scaled_info, fix_info, size);
2188 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2189 &bpp_dummy, &compr_dummy);
2192 WARN("Invalid bitmap format!\n");
2197 new_width = desiredx;
2202 new_height = height > 0 ? desiredy : -desiredy;
2204 new_height = height;
2208 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2209 core->bcWidth = new_width;
2210 core->bcHeight = new_height;
2214 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2215 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2216 WARN("Broken BitmapInfoHeader!\n");
2220 scaled_info->bmiHeader.biWidth = new_width;
2221 scaled_info->bmiHeader.biHeight = new_height;
2224 if (new_height < 0) new_height = -new_height;
2226 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2227 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2229 bits = (char *)info + (offbits ? offbits : size);
2231 if (loadflags & LR_CREATEDIBSECTION)
2233 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2234 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2238 if (is_dib_monochrome(fix_info))
2239 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2241 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2244 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2245 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2246 SelectObject(screen_mem_dc, orig_bm);
2249 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2250 HeapFree(GetProcessHeap(), 0, scaled_info);
2251 HeapFree(GetProcessHeap(), 0, fix_info);
2252 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2257 /**********************************************************************
2258 * LoadImageA (USER32.@)
2262 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2263 INT desiredx, INT desiredy, UINT loadflags)
2268 if (IS_INTRESOURCE(name))
2269 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2272 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2273 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2274 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2276 __EXCEPT_PAGE_FAULT {
2277 SetLastError( ERROR_INVALID_PARAMETER );
2281 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2282 HeapFree(GetProcessHeap(), 0, u_name);
2287 /******************************************************************************
2288 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2291 * hinst [I] Handle of instance that contains image
2292 * name [I] Name of image
2293 * type [I] Type of image
2294 * desiredx [I] Desired width
2295 * desiredy [I] Desired height
2296 * loadflags [I] Load flags
2299 * Success: Handle to newly loaded image
2302 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2304 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2305 INT desiredx, INT desiredy, UINT loadflags )
2307 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2308 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2310 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2313 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2316 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2319 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2320 GetDeviceCaps(screen_dc, BITSPIXEL),
2326 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2327 1, TRUE, loadflags);
2332 /******************************************************************************
2333 * CopyImage (USER32.@) Creates new image and copies attributes to it
2336 * hnd [I] Handle to image to copy
2337 * type [I] Type of image to copy
2338 * desiredx [I] Desired width of new image
2339 * desiredy [I] Desired height of new image
2340 * flags [I] Copy flags
2343 * Success: Handle to newly created image
2347 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2348 * all other versions (95/2000/XP have been tested) ignore it.
2351 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2352 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2353 * the copy will have the same depth as the screen.
2354 * The content of the image will only be copied if the bit depth of the
2355 * original image is compatible with the bit depth of the screen, or
2356 * if the source is a DIB section.
2357 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2359 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2360 INT desiredy, UINT flags )
2362 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2363 hnd, type, desiredx, desiredy, flags);
2374 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2375 if (!objSize) return 0;
2376 if ((desiredx < 0) || (desiredy < 0)) return 0;
2378 if (flags & LR_COPYFROMRESOURCE)
2380 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2383 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2384 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2386 /* Allocate memory for a BITMAPINFOHEADER structure and a
2387 color table. The maximum number of colors in a color table
2388 is 256 which corresponds to a bitmap with depth 8.
2389 Bitmaps with higher depths don't have color tables. */
2390 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2393 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2394 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2395 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2396 bi->bmiHeader.biCompression = BI_RGB;
2398 if (flags & LR_CREATEDIBSECTION)
2400 /* Create a DIB section. LR_MONOCHROME is ignored */
2402 HDC dc = CreateCompatibleDC(NULL);
2404 if (objSize == sizeof(DIBSECTION))
2406 /* The source bitmap is a DIB.
2407 Get its attributes to create an exact copy */
2408 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2411 /* Get the color table or the color masks */
2412 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2414 bi->bmiHeader.biWidth = desiredx;
2415 bi->bmiHeader.biHeight = desiredy;
2416 bi->bmiHeader.biSizeImage = 0;
2418 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2423 /* Create a device-dependent bitmap */
2425 BOOL monochrome = (flags & LR_MONOCHROME);
2427 if (objSize == sizeof(DIBSECTION))
2429 /* The source bitmap is a DIB section.
2430 Get its attributes */
2431 HDC dc = CreateCompatibleDC(NULL);
2432 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2433 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2434 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2437 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2439 /* Look if the colors of the DIB are black and white */
2442 (bi->bmiColors[0].rgbRed == 0xff
2443 && bi->bmiColors[0].rgbGreen == 0xff
2444 && bi->bmiColors[0].rgbBlue == 0xff
2445 && bi->bmiColors[0].rgbReserved == 0
2446 && bi->bmiColors[1].rgbRed == 0
2447 && bi->bmiColors[1].rgbGreen == 0
2448 && bi->bmiColors[1].rgbBlue == 0
2449 && bi->bmiColors[1].rgbReserved == 0)
2451 (bi->bmiColors[0].rgbRed == 0
2452 && bi->bmiColors[0].rgbGreen == 0
2453 && bi->bmiColors[0].rgbBlue == 0
2454 && bi->bmiColors[0].rgbReserved == 0
2455 && bi->bmiColors[1].rgbRed == 0xff
2456 && bi->bmiColors[1].rgbGreen == 0xff
2457 && bi->bmiColors[1].rgbBlue == 0xff
2458 && bi->bmiColors[1].rgbReserved == 0);
2461 else if (!monochrome)
2463 monochrome = ds.dsBm.bmBitsPixel == 1;
2468 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2472 HDC screenDC = GetDC(NULL);
2473 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2474 ReleaseDC(NULL, screenDC);
2480 /* Only copy the bitmap if it's a DIB section or if it's
2481 compatible to the screen */
2484 if (objSize == sizeof(DIBSECTION))
2486 copyContents = TRUE;
2490 HDC screenDC = GetDC(NULL);
2491 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2492 ReleaseDC(NULL, screenDC);
2494 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2499 /* The source bitmap may already be selected in a device context,
2500 use GetDIBits/StretchDIBits and not StretchBlt */
2505 dc = CreateCompatibleDC(NULL);
2507 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2508 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2509 bi->bmiHeader.biSizeImage = 0;
2510 bi->bmiHeader.biClrUsed = 0;
2511 bi->bmiHeader.biClrImportant = 0;
2513 /* Fill in biSizeImage */
2514 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2515 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2521 /* Get the image bits of the source bitmap */
2522 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2524 /* Copy it to the destination bitmap */
2525 oldBmp = SelectObject(dc, res);
2526 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2527 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2528 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2529 SelectObject(dc, oldBmp);
2531 HeapFree(GetProcessHeap(), 0, bits);
2537 if (flags & LR_COPYDELETEORG)
2542 HeapFree(GetProcessHeap(), 0, bi);
2548 struct cursoricon_object *icon;
2550 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( screen_dc, BITSPIXEL );
2552 if (flags & LR_DEFAULTSIZE)
2554 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2555 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2558 if (!(icon = get_icon_ptr( hnd ))) return 0;
2560 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2561 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2562 type == IMAGE_CURSOR, flags );
2564 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2565 release_icon_ptr( hnd, icon );
2567 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2575 /******************************************************************************
2576 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2579 * Success: Handle to specified bitmap
2582 HBITMAP WINAPI LoadBitmapW(
2583 HINSTANCE instance, /* [in] Handle to application instance */
2584 LPCWSTR name) /* [in] Address of bitmap resource name */
2586 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2589 /**********************************************************************
2590 * LoadBitmapA (USER32.@)
2594 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2596 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );