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];
76 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
78 static struct list icon_cache = LIST_INIT( icon_cache );
80 /**********************************************************************
81 * User objects management
84 struct cursoricon_frame
86 UINT width; /* frame-specific width */
87 UINT height; /* frame-specific height */
88 UINT delay; /* frame-specific delay between this frame and the next (in jiffies) */
89 HBITMAP color; /* color bitmap */
90 HBITMAP alpha; /* pre-multiplied alpha bitmap for 32-bpp icons */
91 HBITMAP mask; /* mask bitmap (followed by color for 1-bpp icons) */
94 struct cursoricon_object
96 struct user_object obj; /* object header */
97 struct list entry; /* entry in shared icons list */
98 ULONG_PTR param; /* opaque param used by 16-bit code */
99 HMODULE module; /* module for icons loaded from resources */
100 LPWSTR resname; /* resource name for icons loaded from resources */
101 HRSRC rsrc; /* resource for shared icons */
102 BOOL is_icon; /* whether icon or cursor */
103 BOOL is_ani; /* whether this object is a static cursor or an animated cursor */
104 UINT delay; /* delay between this frame and the next (in jiffies) */
108 struct static_cursoricon_object
110 struct cursoricon_object shared;
111 struct cursoricon_frame frame; /* frame-specific icon data */
114 struct animated_cursoricon_object
116 struct cursoricon_object shared;
117 UINT num_frames; /* number of frames in the icon/cursor */
118 UINT num_steps; /* number of sequence steps in the icon/cursor */
119 HICON frames[1]; /* list of animated cursor frames */
122 static HICON alloc_icon_handle( BOOL is_ani, UINT num_steps )
124 struct cursoricon_object *obj;
128 icon_size = FIELD_OFFSET( struct animated_cursoricon_object, frames[num_steps] );
130 icon_size = sizeof( struct static_cursoricon_object );
131 obj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, icon_size );
134 obj->is_ani = is_ani;
137 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
139 ani_icon_data->num_steps = num_steps;
140 ani_icon_data->num_frames = num_steps; /* changed later for some animated cursors */
142 return alloc_user_handle( &obj->obj, USER_ICON );
145 static struct cursoricon_object *get_icon_ptr( HICON handle )
147 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
148 if (obj == OBJ_OTHER_PROCESS)
150 WARN( "icon handle %p from other process\n", handle );
156 static void release_icon_ptr( HICON handle, struct cursoricon_object *ptr )
158 release_user_handle_ptr( ptr );
161 static struct cursoricon_frame *get_icon_frame( struct cursoricon_object *obj, int istep )
163 struct static_cursoricon_object *req_frame;
167 struct animated_cursoricon_object *ani_icon_data;
168 struct cursoricon_object *frameobj;
170 ani_icon_data = (struct animated_cursoricon_object *) obj;
171 if (!(frameobj = get_icon_ptr( ani_icon_data->frames[istep] )))
173 req_frame = (struct static_cursoricon_object *) frameobj;
176 req_frame = (struct static_cursoricon_object *) obj;
178 return &req_frame->frame;
181 static void release_icon_frame( struct cursoricon_object *obj, int istep, struct cursoricon_frame *frame )
185 struct animated_cursoricon_object *ani_icon_data;
186 struct cursoricon_object *frameobj;
188 ani_icon_data = (struct animated_cursoricon_object *) obj;
189 frameobj = (struct cursoricon_object *) (((char *)frame) - FIELD_OFFSET(struct static_cursoricon_object, frame));
190 release_icon_ptr( ani_icon_data->frames[istep], frameobj );
194 static UINT get_icon_steps( struct cursoricon_object *obj )
198 struct animated_cursoricon_object *ani_icon_data;
200 ani_icon_data = (struct animated_cursoricon_object *) obj;
201 return ani_icon_data->num_steps;
206 static BOOL free_icon_handle( HICON handle )
208 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
210 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
213 ULONG_PTR param = obj->param;
216 assert( !obj->rsrc ); /* shared icons can't be freed */
220 struct cursoricon_frame *frame = get_icon_frame( obj, 0 );
222 if (frame->alpha) DeleteObject( frame->alpha );
223 if (frame->color) DeleteObject( frame->color );
224 DeleteObject( frame->mask );
225 release_icon_frame( obj, 0, frame );
229 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
231 for (i=0; i<ani_icon_data->num_steps; i++)
233 HICON hFrame = ani_icon_data->frames[i];
239 free_icon_handle( ani_icon_data->frames[i] );
240 for (j=0; j<ani_icon_data->num_steps; j++)
242 if (ani_icon_data->frames[j] == hFrame)
243 ani_icon_data->frames[j] = 0;
248 if (!IS_INTRESOURCE( obj->resname )) HeapFree( GetProcessHeap(), 0, obj->resname );
249 HeapFree( GetProcessHeap(), 0, obj );
250 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
251 USER_Driver->pDestroyCursorIcon( handle );
257 ULONG_PTR get_icon_param( HICON handle )
260 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
262 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
266 release_user_handle_ptr( obj );
271 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
274 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
276 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
281 release_user_handle_ptr( obj );
287 /***********************************************************************
290 * Helper function to map a file to memory:
292 * [RETURN] ptr - pointer to mapped file
293 * [RETURN] filesize - pointer size of file to be stored if not NULL
295 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
297 HANDLE hFile, hMapping;
300 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
301 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
302 if (hFile != INVALID_HANDLE_VALUE)
304 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
307 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
308 CloseHandle( hMapping );
310 *filesize = GetFileSize( hFile, NULL );
312 CloseHandle( hFile );
318 /***********************************************************************
321 * Return the size of a DIB bitmap in bytes.
323 static int get_dib_image_size( int width, int height, int depth )
325 return (((width * depth + 31) / 8) & ~3) * abs( height );
329 /***********************************************************************
332 * Return the size of the bitmap info structure including color table.
334 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
336 unsigned int colors, size, masks = 0;
338 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
340 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
341 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
342 return sizeof(BITMAPCOREHEADER) + colors *
343 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
345 else /* assume BITMAPINFOHEADER */
347 colors = info->bmiHeader.biClrUsed;
348 if (colors > 256) /* buffer overflow otherwise */
350 if (!colors && (info->bmiHeader.biBitCount <= 8))
351 colors = 1 << info->bmiHeader.biBitCount;
352 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
353 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
354 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
359 /***********************************************************************
362 * Helper function to duplicate a bitmap.
364 static HBITMAP copy_bitmap( HBITMAP bitmap )
367 HBITMAP new_bitmap = 0;
370 if (!bitmap) return 0;
371 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
373 if ((src = CreateCompatibleDC( 0 )) && (dst = CreateCompatibleDC( 0 )))
375 SelectObject( src, bitmap );
376 if ((new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight )))
378 SelectObject( dst, new_bitmap );
379 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
388 /***********************************************************************
391 * Returns whether a DIB can be converted to a monochrome DDB.
393 * A DIB can be converted if its color table contains only black and
394 * white. Black must be the first color in the color table.
396 * Note : If the first color in the color table is white followed by
397 * black, we can't convert it to a monochrome DDB with
398 * SetDIBits, because black and white would be inverted.
400 static BOOL is_dib_monochrome( const BITMAPINFO* info )
402 if (info->bmiHeader.biBitCount != 1) return FALSE;
404 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
406 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
408 /* Check if the first color is black */
409 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
413 /* Check if the second color is white */
414 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
415 && (rgb->rgbtBlue == 0xff));
419 else /* assume BITMAPINFOHEADER */
421 const RGBQUAD *rgb = info->bmiColors;
423 /* Check if the first color is black */
424 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
425 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
429 /* Check if the second color is white */
430 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
431 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
437 /***********************************************************************
440 * Get the info from a bitmap header.
441 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
443 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
444 LONG *height, WORD *bpp, DWORD *compr )
446 if (header->biSize == sizeof(BITMAPCOREHEADER))
448 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
449 *width = core->bcWidth;
450 *height = core->bcHeight;
451 *bpp = core->bcBitCount;
455 else if (header->biSize == sizeof(BITMAPINFOHEADER) ||
456 header->biSize == sizeof(BITMAPV4HEADER) ||
457 header->biSize == sizeof(BITMAPV5HEADER))
459 *width = header->biWidth;
460 *height = header->biHeight;
461 *bpp = header->biBitCount;
462 *compr = header->biCompression;
465 WARN("unknown/wrong size (%u) for header\n", header->biSize);
469 /**********************************************************************
472 BOOL get_icon_size( HICON handle, SIZE *size )
474 struct cursoricon_object *info;
475 struct cursoricon_frame *frame;
477 if (!(info = get_icon_ptr( handle ))) return FALSE;
478 frame = get_icon_frame( info, 0 );
479 size->cx = frame->width;
480 size->cy = frame->height;
481 release_icon_frame( info, 0, frame);
482 release_icon_ptr( handle, info );
487 * The following macro functions account for the irregularities of
488 * accessing cursor and icon resources in files and resource entries.
490 typedef BOOL (*fnGetCIEntry)( LPCVOID dir, DWORD size, int n,
491 int *width, int *height, int *bits );
493 /**********************************************************************
494 * CURSORICON_FindBestIcon
496 * Find the icon closest to the requested size and bit depth.
498 static int CURSORICON_FindBestIcon( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
499 int width, int height, int depth, UINT loadflags )
501 int i, cx, cy, bits, bestEntry = -1;
502 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
503 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
506 iTotalDiff = 0xFFFFFFFF;
507 iColorDiff = 0xFFFFFFFF;
509 if (loadflags & LR_DEFAULTSIZE)
511 if (!width) width = GetSystemMetrics( SM_CXICON );
512 if (!height) height = GetSystemMetrics( SM_CYICON );
514 else if (!width && !height)
516 /* use the size of the first entry */
517 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
521 for ( i = 0; iTotalDiff && get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
523 iTempXDiff = abs(width - cx);
524 iTempYDiff = abs(height - cy);
526 if(iTotalDiff > (iTempXDiff + iTempYDiff))
530 iTotalDiff = iXDiff + iYDiff;
534 /* Find Best Colors for Best Fit */
535 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
537 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
539 iTempColorDiff = abs(depth - bits);
540 if(iColorDiff > iTempColorDiff)
543 iColorDiff = iTempColorDiff;
551 static BOOL CURSORICON_GetResIconEntry( LPCVOID dir, DWORD size, int n,
552 int *width, int *height, int *bits )
554 const CURSORICONDIR *resdir = dir;
555 const ICONRESDIR *icon;
557 if ( resdir->idCount <= n )
559 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
561 icon = &resdir->idEntries[n].ResInfo.icon;
562 *width = icon->bWidth;
563 *height = icon->bHeight;
564 *bits = resdir->idEntries[n].wBitCount;
568 /**********************************************************************
569 * CURSORICON_FindBestCursor
571 * Find the cursor closest to the requested size.
573 * FIXME: parameter 'color' ignored.
575 static int CURSORICON_FindBestCursor( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
576 int width, int height, int depth, UINT loadflags )
578 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
580 if (loadflags & LR_DEFAULTSIZE)
582 if (!width) width = GetSystemMetrics( SM_CXCURSOR );
583 if (!height) height = GetSystemMetrics( SM_CYCURSOR );
585 else if (!width && !height)
587 /* use the first entry */
588 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
592 /* Double height to account for AND and XOR masks */
596 /* First find the largest one smaller than or equal to the requested size*/
598 maxwidth = maxheight = 0;
599 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
601 if ((cx <= width) && (cy <= height) &&
602 (cx > maxwidth) && (cy > maxheight))
609 if (bestEntry != -1) return bestEntry;
611 /* Now find the smallest one larger than the requested size */
613 maxwidth = maxheight = 255;
614 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
616 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
627 static BOOL CURSORICON_GetResCursorEntry( LPCVOID dir, DWORD size, int n,
628 int *width, int *height, int *bits )
630 const CURSORICONDIR *resdir = dir;
631 const CURSORDIR *cursor;
633 if ( resdir->idCount <= n )
635 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
637 cursor = &resdir->idEntries[n].ResInfo.cursor;
638 *width = cursor->wWidth;
639 *height = cursor->wHeight;
640 *bits = resdir->idEntries[n].wBitCount;
644 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir, DWORD size,
645 int width, int height, int depth,
650 n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetResIconEntry,
651 width, height, depth, loadflags );
654 return &dir->idEntries[n];
657 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir, DWORD size,
658 int width, int height, int depth,
661 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetResCursorEntry,
662 width, height, depth, loadflags );
665 return &dir->idEntries[n];
668 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, DWORD size, int n,
669 int *width, int *height, int *bits )
671 const CURSORICONFILEDIR *filedir = dir;
672 const CURSORICONFILEDIRENTRY *entry;
673 const BITMAPINFOHEADER *info;
675 if ( filedir->idCount <= n )
677 if ((const char *)&filedir->idEntries[n + 1] - (const char *)dir > size)
679 entry = &filedir->idEntries[n];
680 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
681 if ((const char *)(info + 1) - (const char *)dir > size) return FALSE;
682 *width = entry->bWidth;
683 *height = entry->bHeight;
684 *bits = info->biBitCount;
688 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir, DWORD size,
689 int width, int height, int depth,
692 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetFileEntry,
693 width, height, depth, loadflags );
696 return &dir->idEntries[n];
699 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir, DWORD size,
700 int width, int height, int depth,
703 int n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetFileEntry,
704 width, height, depth, loadflags );
707 return &dir->idEntries[n];
710 /***********************************************************************
713 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
716 BOOL has_alpha = FALSE;
717 const unsigned char *ptr = bits;
719 if (info->bmiHeader.biBitCount != 32) return FALSE;
720 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
721 if ((has_alpha = (ptr[3] != 0))) break;
725 /***********************************************************************
726 * create_alpha_bitmap
728 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
730 static HBITMAP create_alpha_bitmap( HBITMAP color, HBITMAP mask,
731 const BITMAPINFO *src_info, const void *color_bits )
734 BITMAPINFO *info = NULL;
741 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
742 if (bm.bmBitsPixel != 32) return 0;
744 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
745 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
746 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
747 info->bmiHeader.biWidth = bm.bmWidth;
748 info->bmiHeader.biHeight = -bm.bmHeight;
749 info->bmiHeader.biPlanes = 1;
750 info->bmiHeader.biBitCount = 32;
751 info->bmiHeader.biCompression = BI_RGB;
752 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
753 info->bmiHeader.biXPelsPerMeter = 0;
754 info->bmiHeader.biYPelsPerMeter = 0;
755 info->bmiHeader.biClrUsed = 0;
756 info->bmiHeader.biClrImportant = 0;
757 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
761 SelectObject( hdc, alpha );
762 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
763 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
764 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
769 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
770 if (!bmi_has_alpha( info, bits ))
772 DeleteObject( alpha );
778 /* pre-multiply by alpha */
779 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
781 unsigned int alpha = ptr[3];
782 ptr[0] = ptr[0] * alpha / 255;
783 ptr[1] = ptr[1] * alpha / 255;
784 ptr[2] = ptr[2] * alpha / 255;
789 HeapFree( GetProcessHeap(), 0, info );
794 /***********************************************************************
795 * create_icon_from_bmi
797 * Create an icon from its BITMAPINFO.
799 static HICON create_icon_from_bmi( BITMAPINFO *bmi, HMODULE module, LPCWSTR resname, HRSRC rsrc,
800 POINT hotspot, BOOL bIcon, INT width, INT height, UINT cFlag )
802 unsigned int size = bitmap_info_size( bmi, DIB_RGB_COLORS );
803 BOOL monochrome = is_dib_monochrome( bmi );
804 HBITMAP color = 0, mask = 0, alpha = 0;
805 const void *color_bits, *mask_bits;
806 BITMAPINFO *bmi_copy;
812 /* Check bitmap header */
814 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
815 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
816 bmi->bmiHeader.biCompression != BI_RGB) )
818 WARN_(cursor)("\tinvalid resource bitmap header.\n");
822 if (cFlag & LR_DEFAULTSIZE)
824 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
825 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
829 if (!width) width = bmi->bmiHeader.biWidth;
830 if (!height) height = bmi->bmiHeader.biHeight/2;
832 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
833 (bmi->bmiHeader.biWidth != width);
835 /* Scale the hotspot */
838 hotspot.x = width / 2;
839 hotspot.y = height / 2;
843 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
844 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
847 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
848 if (!screen_dc) return 0;
850 if (!(bmi_copy = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
852 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
854 memcpy( bmi_copy, bmi, size );
855 bmi_copy->bmiHeader.biHeight /= 2;
857 color_bits = (const char*)bmi + size;
858 mask_bits = (const char*)color_bits +
859 get_dib_image_size( bmi->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight, bmi->bmiHeader.biBitCount );
864 if (!(mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
867 /* copy color data into second half of mask bitmap */
868 SelectObject( hdc, mask );
869 StretchDIBits( hdc, 0, height, width, height,
870 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
871 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
875 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
876 if (!(color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
877 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
879 DeleteObject( mask );
882 SelectObject( hdc, color );
883 StretchDIBits( hdc, 0, 0, width, height,
884 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
885 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
887 if (bmi_has_alpha( bmi_copy, color_bits ))
888 alpha = create_alpha_bitmap( color, mask, bmi_copy, color_bits );
890 /* convert info to monochrome to copy the mask */
891 bmi_copy->bmiHeader.biBitCount = 1;
892 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
894 RGBQUAD *rgb = bmi_copy->bmiColors;
896 bmi_copy->bmiHeader.biClrUsed = bmi_copy->bmiHeader.biClrImportant = 2;
897 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
898 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
899 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
903 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)bmi_copy) + 1);
905 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
906 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
910 SelectObject( hdc, mask );
911 StretchDIBits( hdc, 0, 0, width, height,
912 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
913 mask_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
918 HeapFree( GetProcessHeap(), 0, bmi_copy );
921 hObj = alloc_icon_handle( FALSE, 1 );
924 struct cursoricon_object *info = get_icon_ptr( hObj );
925 struct cursoricon_frame *frame;
927 info->is_icon = bIcon;
928 info->module = module;
929 info->hotspot = hotspot;
930 frame = get_icon_frame( info, 0 );
932 frame->width = width;
933 frame->height = height;
934 frame->color = color;
936 frame->alpha = alpha;
937 release_icon_frame( info, 0, frame );
938 if (!IS_INTRESOURCE(resname))
940 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
941 if (info->resname) strcpyW( info->resname, resname );
943 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
945 if (module && (cFlag & LR_SHARED))
948 list_add_head( &icon_cache, &info->entry );
950 release_icon_ptr( hObj, info );
951 USER_Driver->pCreateCursorIcon( hObj );
955 DeleteObject( color );
956 DeleteObject( alpha );
957 DeleteObject( mask );
963 /**********************************************************************
964 * .ANI cursor support
966 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
967 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
968 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
970 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
971 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
972 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
973 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
974 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
975 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
976 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
978 #define ANI_FLAG_ICON 0x1
979 #define ANI_FLAG_SEQUENCE 0x2
995 const unsigned char *data;
998 static void dump_ani_header( const ani_header *header )
1000 TRACE(" header size: %d\n", header->header_size);
1001 TRACE(" frames: %d\n", header->num_frames);
1002 TRACE(" steps: %d\n", header->num_steps);
1003 TRACE(" width: %d\n", header->width);
1004 TRACE(" height: %d\n", header->height);
1005 TRACE(" bpp: %d\n", header->bpp);
1006 TRACE(" planes: %d\n", header->num_planes);
1007 TRACE(" display rate: %d\n", header->display_rate);
1008 TRACE(" flags: 0x%08x\n", header->flags);
1030 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1032 const unsigned char *ptr = parent_chunk->data;
1033 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1035 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1039 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1040 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1042 ptr += sizeof(DWORD);
1043 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1044 ptr += sizeof(DWORD);
1045 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1051 ptr += sizeof(DWORD);
1052 ptr += (*(const DWORD *)ptr + 1) & ~1;
1053 ptr += sizeof(DWORD);
1061 * RIFF:'ACON' RIFF chunk
1062 * |- CHUNK:'anih' Header
1063 * |- CHUNK:'seq ' Sequence information (optional)
1064 * \- LIST:'fram' Frame list
1065 * |- CHUNK:icon Cursor frames
1070 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size, INT width, INT height,
1071 INT depth, BOOL is_icon, UINT loadflags )
1073 struct animated_cursoricon_object *ani_icon_data;
1074 struct cursoricon_object *info;
1075 DWORD *frame_rates = NULL;
1076 DWORD *frame_seq = NULL;
1077 ani_header header = {0};
1078 BOOL use_seq = FALSE;
1083 riff_chunk_t root_chunk = { bits_size, bits };
1084 riff_chunk_t ACON_chunk = {0};
1085 riff_chunk_t anih_chunk = {0};
1086 riff_chunk_t fram_chunk = {0};
1087 riff_chunk_t rate_chunk = {0};
1088 riff_chunk_t seq_chunk = {0};
1089 const unsigned char *icon_chunk;
1090 const unsigned char *icon_data;
1092 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1094 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1095 if (!ACON_chunk.data)
1097 ERR("Failed to get root chunk.\n");
1101 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1102 if (!anih_chunk.data)
1104 ERR("Failed to get 'anih' chunk.\n");
1107 memcpy( &header, anih_chunk.data, sizeof(header) );
1108 dump_ani_header( &header );
1110 if (!(header.flags & ANI_FLAG_ICON))
1112 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1116 if (header.flags & ANI_FLAG_SEQUENCE)
1118 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1121 frame_seq = (DWORD *) seq_chunk.data;
1126 FIXME("Sequence data expected but not found, assuming steps == frames.\n");
1127 header.num_steps = header.num_frames;
1131 riff_find_chunk( ANI_rate_ID, 0, &ACON_chunk, &rate_chunk );
1132 if (rate_chunk.data)
1133 frame_rates = (DWORD *) rate_chunk.data;
1135 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1136 if (!fram_chunk.data)
1138 ERR("Failed to get icon list.\n");
1142 cursor = alloc_icon_handle( TRUE, header.num_steps );
1143 if (!cursor) return 0;
1144 frames = HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD)*header.num_frames );
1147 free_icon_handle( cursor );
1151 info = get_icon_ptr( cursor );
1152 ani_icon_data = (struct animated_cursoricon_object *) info;
1153 info->is_icon = is_icon;
1154 ani_icon_data->num_frames = header.num_frames;
1156 /* The .ANI stores the display rate in jiffies (1/60s) */
1157 info->delay = header.display_rate;
1159 icon_chunk = fram_chunk.data;
1160 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1161 for (i=0; i<header.num_frames; i++)
1163 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1164 const CURSORICONFILEDIRENTRY *entry;
1165 INT frameWidth, frameHeight;
1166 const BITMAPINFO *bmi;
1168 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1169 bits + bits_size - icon_data,
1170 width, height, depth, loadflags );
1172 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1173 info->hotspot.x = entry->xHotspot;
1174 info->hotspot.y = entry->yHotspot;
1175 if (!header.width || !header.height)
1177 frameWidth = entry->bWidth;
1178 frameHeight = entry->bHeight;
1182 frameWidth = header.width;
1183 frameHeight = header.height;
1186 /* Grab a frame from the animation */
1187 frames[i] = create_icon_from_bmi( (BITMAPINFO *)bmi, NULL, NULL, NULL, info->hotspot,
1188 is_icon, frameWidth, frameHeight, loadflags );
1191 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1195 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1196 ani_icon_data->num_frames = 0;
1197 release_icon_ptr( cursor, info );
1198 free_icon_handle( cursor );
1199 HeapFree( GetProcessHeap(), 0, frames );
1205 /* Advance to the next chunk */
1206 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1207 icon_data = icon_chunk + (2 * sizeof(DWORD));
1210 /* There was an error but we at least decoded the first frame, so just use that frame */
1213 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1214 for (i=1; i<ani_icon_data->num_frames; i++)
1215 free_icon_handle( ani_icon_data->frames[i] );
1218 ani_icon_data->num_steps = 1;
1219 ani_icon_data->num_frames = 1;
1222 /* Setup the animated frames in the correct sequence */
1223 for (i=0; i<ani_icon_data->num_steps; i++)
1225 DWORD frame_id = use_seq ? frame_seq[i] : i;
1226 struct cursoricon_frame *frame;
1228 if (frame_id >= ani_icon_data->num_frames)
1230 frame_id = ani_icon_data->num_frames-1;
1231 ERR_(cursor)("Sequence indicates frame past end of list, corrupt?\n");
1233 ani_icon_data->frames[i] = frames[frame_id];
1234 frame = get_icon_frame( info, i );
1236 frame->delay = frame_rates[i];
1239 release_icon_frame( info, i, frame );
1242 HeapFree( GetProcessHeap(), 0, frames );
1243 release_icon_ptr( cursor, info );
1249 /**********************************************************************
1250 * CreateIconFromResourceEx (USER32.@)
1252 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1253 * with cbSize parameter as well.
1255 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1256 BOOL bIcon, DWORD dwVersion,
1257 INT width, INT height,
1263 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1264 bits, cbSize, dwVersion, width, height,
1265 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1267 if (!bits) return 0;
1269 if (dwVersion == 0x00020000)
1271 FIXME_(cursor)("\t2.xx resources are not supported\n");
1275 /* Check if the resource is an animated icon/cursor */
1276 if (!memcmp(bits, "RIFF", 4))
1277 return CURSORICON_CreateIconFromANI( bits, cbSize, width, height,
1278 0 /* default depth */, bIcon, cFlag );
1282 hotspot.x = width / 2;
1283 hotspot.y = height / 2;
1284 bmi = (BITMAPINFO *)bits;
1286 else /* get the hotspot */
1288 SHORT *pt = (SHORT *)bits;
1291 bmi = (BITMAPINFO *)(pt + 2);
1294 return create_icon_from_bmi( bmi, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1298 /**********************************************************************
1299 * CreateIconFromResource (USER32.@)
1301 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1302 BOOL bIcon, DWORD dwVersion)
1304 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1308 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1309 INT width, INT height, INT depth,
1310 BOOL fCursor, UINT loadflags)
1312 const CURSORICONFILEDIRENTRY *entry;
1313 const CURSORICONFILEDIR *dir;
1319 TRACE("loading %s\n", debugstr_w( filename ));
1321 bits = map_fileW( filename, &filesize );
1325 /* Check for .ani. */
1326 if (memcmp( bits, "RIFF", 4 ) == 0)
1328 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, !fCursor, loadflags );
1332 dir = (const CURSORICONFILEDIR*) bits;
1333 if ( filesize < sizeof(*dir) )
1336 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1340 entry = CURSORICON_FindBestCursorFile( dir, filesize, width, height, depth, loadflags );
1342 entry = CURSORICON_FindBestIconFile( dir, filesize, width, height, depth, loadflags );
1347 /* check that we don't run off the end of the file */
1348 if ( entry->dwDIBOffset > filesize )
1350 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1353 hotspot.x = entry->xHotspot;
1354 hotspot.y = entry->yHotspot;
1355 hIcon = create_icon_from_bmi( (BITMAPINFO *)&bits[entry->dwDIBOffset], NULL, NULL, NULL,
1356 hotspot, !fCursor, width, height, loadflags );
1358 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1359 UnmapViewOfFile( bits );
1363 /**********************************************************************
1366 * Load a cursor or icon from resource or file.
1368 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1369 INT width, INT height, INT depth,
1370 BOOL fCursor, UINT loadflags)
1376 const CURSORICONDIR *dir;
1377 const CURSORICONDIRENTRY *dirEntry;
1382 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1383 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1385 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1386 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1388 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1390 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1391 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1393 /* Get directory resource ID */
1395 if (!(hRsrc = FindResourceW( hInstance, name,
1396 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1398 /* try animated resource */
1399 if (!(hRsrc = FindResourceW( hInstance, name,
1400 (LPWSTR)(fCursor ? RT_ANICURSOR : RT_ANIICON) ))) return 0;
1401 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1402 bits = LockResource( handle );
1403 return CURSORICON_CreateIconFromANI( bits, SizeofResource( hInstance, handle ),
1404 width, height, depth, !fCursor, loadflags );
1407 /* Find the best entry in the directory */
1409 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1410 if (!(dir = LockResource( handle ))) return 0;
1411 size = SizeofResource( hInstance, hRsrc );
1413 dirEntry = CURSORICON_FindBestCursorRes( dir, size, width, height, depth, loadflags );
1415 dirEntry = CURSORICON_FindBestIconRes( dir, size, width, height, depth, loadflags );
1416 if (!dirEntry) return 0;
1417 wResId = dirEntry->wResId;
1418 FreeResource( handle );
1420 /* Load the resource */
1422 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1423 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1425 /* If shared icon, check whether it was already loaded */
1426 if (loadflags & LR_SHARED)
1428 struct cursoricon_object *ptr;
1431 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1433 if (ptr->module != hInstance) continue;
1434 if (ptr->rsrc != hRsrc) continue;
1435 hIcon = ptr->obj.handle;
1439 if (hIcon) return hIcon;
1442 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1443 bits = LockResource( handle );
1447 hotspot.x = width / 2;
1448 hotspot.y = height / 2;
1450 else /* get the hotspot */
1452 SHORT *pt = (SHORT *)bits;
1455 bits += 2 * sizeof(SHORT);
1457 hIcon = create_icon_from_bmi( (BITMAPINFO *)bits, hInstance, name, hRsrc,
1458 hotspot, !fCursor, width, height, loadflags );
1459 FreeResource( handle );
1464 /***********************************************************************
1465 * CreateCursor (USER32.@)
1467 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1468 INT xHotSpot, INT yHotSpot,
1469 INT nWidth, INT nHeight,
1470 LPCVOID lpANDbits, LPCVOID lpXORbits )
1475 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1476 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1479 info.xHotspot = xHotSpot;
1480 info.yHotspot = yHotSpot;
1481 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1482 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1483 hCursor = CreateIconIndirect( &info );
1484 DeleteObject( info.hbmMask );
1485 DeleteObject( info.hbmColor );
1490 /***********************************************************************
1491 * CreateIcon (USER32.@)
1493 * Creates an icon based on the specified bitmaps. The bitmaps must be
1494 * provided in a device dependent format and will be resized to
1495 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1496 * depth. The provided bitmaps must be top-down bitmaps.
1497 * Although Windows does not support 15bpp(*) this API must support it
1498 * for Winelib applications.
1500 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1504 * Success: handle to an icon
1507 * FIXME: Do we need to resize the bitmaps?
1509 HICON WINAPI CreateIcon(
1510 HINSTANCE hInstance, /* [in] the application's hInstance */
1511 INT nWidth, /* [in] the width of the provided bitmaps */
1512 INT nHeight, /* [in] the height of the provided bitmaps */
1513 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1514 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1515 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1516 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1521 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1522 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1525 iinfo.xHotspot = nWidth / 2;
1526 iinfo.yHotspot = nHeight / 2;
1527 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1528 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1530 hIcon = CreateIconIndirect( &iinfo );
1532 DeleteObject( iinfo.hbmMask );
1533 DeleteObject( iinfo.hbmColor );
1539 /***********************************************************************
1540 * CopyIcon (USER32.@)
1542 HICON WINAPI CopyIcon( HICON hIcon )
1544 struct cursoricon_object *ptrOld, *ptrNew;
1547 if (!(ptrOld = get_icon_ptr( hIcon )))
1549 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1552 if ((hNew = alloc_icon_handle( FALSE, 1 )))
1554 struct cursoricon_frame *frameOld, *frameNew;
1556 ptrNew = get_icon_ptr( hNew );
1557 ptrNew->is_icon = ptrOld->is_icon;
1558 ptrNew->hotspot = ptrOld->hotspot;
1559 if (!(frameOld = get_icon_frame( ptrOld, 0 )))
1561 release_icon_ptr( hIcon, ptrOld );
1562 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1565 if (!(frameNew = get_icon_frame( ptrNew, 0 )))
1567 release_icon_frame( ptrOld, 0, frameOld );
1568 release_icon_ptr( hIcon, ptrOld );
1569 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1572 frameNew->delay = 0;
1573 frameNew->width = frameOld->width;
1574 frameNew->height = frameOld->height;
1575 frameNew->mask = copy_bitmap( frameOld->mask );
1576 frameNew->color = copy_bitmap( frameOld->color );
1577 frameNew->alpha = copy_bitmap( frameOld->alpha );
1578 release_icon_frame( ptrOld, 0, frameOld );
1579 release_icon_frame( ptrNew, 0, frameNew );
1580 release_icon_ptr( hNew, ptrNew );
1582 release_icon_ptr( hIcon, ptrOld );
1583 if (hNew) USER_Driver->pCreateCursorIcon( hNew );
1588 /***********************************************************************
1589 * DestroyIcon (USER32.@)
1591 BOOL WINAPI DestroyIcon( HICON hIcon )
1594 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1596 TRACE_(icon)("%p\n", hIcon );
1600 BOOL shared = (obj->rsrc != NULL);
1601 release_icon_ptr( hIcon, obj );
1602 ret = (GetCursor() != hIcon);
1603 if (!shared) free_icon_handle( hIcon );
1609 /***********************************************************************
1610 * DestroyCursor (USER32.@)
1612 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1614 return DestroyIcon( hCursor );
1617 /***********************************************************************
1618 * DrawIcon (USER32.@)
1620 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1622 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1625 /***********************************************************************
1626 * SetCursor (USER32.@)
1628 * Set the cursor shape.
1631 * A handle to the previous cursor shape.
1633 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1635 struct cursoricon_object *obj;
1640 TRACE("%p\n", hCursor);
1642 SERVER_START_REQ( set_cursor )
1644 req->flags = SET_CURSOR_HANDLE;
1645 req->handle = wine_server_user_handle( hCursor );
1646 if ((ret = !wine_server_call_err( req )))
1648 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1649 show_count = reply->prev_count;
1655 USER_Driver->pSetCursor( show_count >= 0 ? hCursor : 0 );
1657 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1658 release_icon_ptr( hOldCursor, obj );
1662 /***********************************************************************
1663 * ShowCursor (USER32.@)
1665 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1668 int increment = bShow ? 1 : -1;
1671 SERVER_START_REQ( set_cursor )
1673 req->flags = SET_CURSOR_COUNT;
1674 req->show_count = increment;
1675 wine_server_call( req );
1676 cursor = wine_server_ptr_handle( reply->prev_handle );
1677 count = reply->prev_count + increment;
1681 TRACE("%d, count=%d\n", bShow, count );
1683 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1684 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1689 /***********************************************************************
1690 * GetCursor (USER32.@)
1692 HCURSOR WINAPI GetCursor(void)
1696 SERVER_START_REQ( set_cursor )
1699 wine_server_call( req );
1700 ret = wine_server_ptr_handle( reply->prev_handle );
1707 /***********************************************************************
1708 * ClipCursor (USER32.@)
1710 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1715 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1717 if (rect && (rect->left > rect->right || rect->top > rect->bottom)) return FALSE;
1719 SERVER_START_REQ( set_cursor )
1721 req->clip_msg = WM_WINE_CLIPCURSOR;
1724 req->flags = SET_CURSOR_CLIP;
1725 req->clip.left = rect->left;
1726 req->clip.top = rect->top;
1727 req->clip.right = rect->right;
1728 req->clip.bottom = rect->bottom;
1730 else req->flags = SET_CURSOR_NOCLIP;
1732 if ((ret = !wine_server_call( req )))
1734 new_rect.left = reply->new_clip.left;
1735 new_rect.top = reply->new_clip.top;
1736 new_rect.right = reply->new_clip.right;
1737 new_rect.bottom = reply->new_clip.bottom;
1741 if (ret) USER_Driver->pClipCursor( &new_rect );
1746 /***********************************************************************
1747 * GetClipCursor (USER32.@)
1749 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1753 if (!rect) return FALSE;
1755 SERVER_START_REQ( set_cursor )
1758 if ((ret = !wine_server_call( req )))
1760 rect->left = reply->new_clip.left;
1761 rect->top = reply->new_clip.top;
1762 rect->right = reply->new_clip.right;
1763 rect->bottom = reply->new_clip.bottom;
1771 /***********************************************************************
1772 * SetSystemCursor (USER32.@)
1774 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1776 FIXME("(%p,%08x),stub!\n", hcur, id);
1781 /**********************************************************************
1782 * LookupIconIdFromDirectoryEx (USER32.@)
1784 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1785 INT width, INT height, UINT cFlag )
1787 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1789 if( dir && !dir->idReserved && (dir->idType & 3) )
1791 const CURSORICONDIRENTRY* entry;
1793 const HDC hdc = GetDC(0);
1794 const int depth = (cFlag & LR_MONOCHROME) ?
1795 1 : GetDeviceCaps(hdc, BITSPIXEL);
1799 entry = CURSORICON_FindBestIconRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1801 entry = CURSORICON_FindBestCursorRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1803 if( entry ) retVal = entry->wResId;
1805 else WARN_(cursor)("invalid resource directory\n");
1809 /**********************************************************************
1810 * LookupIconIdFromDirectory (USER32.@)
1812 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1814 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1817 /***********************************************************************
1818 * LoadCursorW (USER32.@)
1820 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1822 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1824 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1825 LR_SHARED | LR_DEFAULTSIZE );
1828 /***********************************************************************
1829 * LoadCursorA (USER32.@)
1831 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1833 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1835 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1836 LR_SHARED | LR_DEFAULTSIZE );
1839 /***********************************************************************
1840 * LoadCursorFromFileW (USER32.@)
1842 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1844 TRACE("%s\n", debugstr_w(name));
1846 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1847 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1850 /***********************************************************************
1851 * LoadCursorFromFileA (USER32.@)
1853 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1855 TRACE("%s\n", debugstr_a(name));
1857 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1858 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1861 /***********************************************************************
1862 * LoadIconW (USER32.@)
1864 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1866 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1868 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1869 LR_SHARED | LR_DEFAULTSIZE );
1872 /***********************************************************************
1873 * LoadIconA (USER32.@)
1875 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1877 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1879 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1880 LR_SHARED | LR_DEFAULTSIZE );
1883 /**********************************************************************
1884 * GetCursorFrameInfo (USER32.@)
1887 * So far no use has been found for the second parameter, it is currently presumed
1888 * that this parameter is reserved for future use.
1891 * hCursor [I] Handle to cursor for which to retrieve information
1892 * reserved [I] No purpose has been found for this parameter (may be NULL)
1893 * istep [I] The step of the cursor for which to retrieve information
1894 * rate_jiffies [O] Pointer to DWORD that receives the frame-specific delay (cannot be NULL)
1895 * num_steps [O] Pointer to DWORD that receives the number of steps in the cursor (cannot be NULL)
1898 * Success: Handle to a frame of the cursor (specified by istep)
1899 * Failure: NULL cursor (0)
1901 HCURSOR WINAPI GetCursorFrameInfo(HCURSOR hCursor, DWORD reserved, DWORD istep, DWORD *rate_jiffies, DWORD *num_steps)
1903 struct cursoricon_object *ptr;
1907 if (rate_jiffies == NULL || num_steps == NULL) return 0;
1909 if (!(ptr = get_icon_ptr( hCursor ))) return 0;
1911 TRACE("%p => %d %d %p %p\n", hCursor, reserved, istep, rate_jiffies, num_steps);
1913 FIXME("Second parameter non-zero (%d), please report this!\n", reserved);
1915 icon_steps = get_icon_steps(ptr);
1916 if (istep < icon_steps || !ptr->is_ani)
1918 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) ptr;
1919 UINT icon_frames = 1;
1922 icon_frames = ani_icon_data->num_frames;
1923 if (ptr->is_ani && icon_frames > 1)
1924 ret = ani_icon_data->frames[istep];
1927 if (icon_frames == 1)
1932 else if (icon_steps == 1)
1935 *rate_jiffies = ptr->delay;
1937 else if (istep < icon_steps)
1939 struct cursoricon_frame *frame;
1941 *num_steps = icon_steps;
1942 frame = get_icon_frame( ptr, istep );
1943 if (get_icon_steps(ptr) == 1)
1946 *num_steps = get_icon_steps(ptr);
1947 /* If this specific frame does not have a delay then use the global delay */
1948 if (frame->delay == ~0)
1949 *rate_jiffies = ptr->delay;
1951 *rate_jiffies = frame->delay;
1952 release_icon_frame( ptr, istep, frame );
1956 release_icon_ptr( hCursor, ptr );
1961 /**********************************************************************
1962 * GetIconInfo (USER32.@)
1964 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1968 infoW.cbSize = sizeof(infoW);
1969 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
1970 iconinfo->fIcon = infoW.fIcon;
1971 iconinfo->xHotspot = infoW.xHotspot;
1972 iconinfo->yHotspot = infoW.yHotspot;
1973 iconinfo->hbmColor = infoW.hbmColor;
1974 iconinfo->hbmMask = infoW.hbmMask;
1978 /**********************************************************************
1979 * GetIconInfoExA (USER32.@)
1981 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
1985 if (info->cbSize != sizeof(*info))
1987 SetLastError( ERROR_INVALID_PARAMETER );
1990 infoW.cbSize = sizeof(infoW);
1991 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
1992 info->fIcon = infoW.fIcon;
1993 info->xHotspot = infoW.xHotspot;
1994 info->yHotspot = infoW.yHotspot;
1995 info->hbmColor = infoW.hbmColor;
1996 info->hbmMask = infoW.hbmMask;
1997 info->wResID = infoW.wResID;
1998 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
1999 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
2003 /**********************************************************************
2004 * GetIconInfoExW (USER32.@)
2006 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
2008 struct cursoricon_frame *frame;
2009 struct cursoricon_object *ptr;
2013 if (info->cbSize != sizeof(*info))
2015 SetLastError( ERROR_INVALID_PARAMETER );
2018 if (!(ptr = get_icon_ptr( icon )))
2020 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2024 frame = get_icon_frame( ptr, 0 );
2027 release_icon_ptr( icon, ptr );
2028 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2032 TRACE("%p => %dx%d\n", icon, frame->width, frame->height);
2034 info->fIcon = ptr->is_icon;
2035 info->xHotspot = ptr->hotspot.x;
2036 info->yHotspot = ptr->hotspot.y;
2037 info->hbmColor = copy_bitmap( frame->color );
2038 info->hbmMask = copy_bitmap( frame->mask );
2040 info->szModName[0] = 0;
2041 info->szResName[0] = 0;
2044 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
2045 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
2047 if (!info->hbmMask || (!info->hbmColor && frame->color))
2049 DeleteObject( info->hbmMask );
2050 DeleteObject( info->hbmColor );
2053 module = ptr->module;
2054 release_icon_frame( ptr, 0, frame );
2055 release_icon_ptr( icon, ptr );
2056 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
2060 /* copy an icon bitmap, even when it can't be selected into a DC */
2061 /* helper for CreateIconIndirect */
2062 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
2063 HBITMAP src, int width, int height )
2065 HDC hdc = CreateCompatibleDC( 0 );
2067 if (!SelectObject( hdc, src )) /* do it the hard way */
2072 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
2073 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2074 info->bmiHeader.biWidth = width;
2075 info->bmiHeader.biHeight = height;
2076 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
2077 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
2078 info->bmiHeader.biCompression = BI_RGB;
2079 info->bmiHeader.biSizeImage = get_dib_image_size( width, height, info->bmiHeader.biBitCount );
2080 info->bmiHeader.biXPelsPerMeter = 0;
2081 info->bmiHeader.biYPelsPerMeter = 0;
2082 info->bmiHeader.biClrUsed = 0;
2083 info->bmiHeader.biClrImportant = 0;
2084 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
2085 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
2086 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
2087 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
2089 HeapFree( GetProcessHeap(), 0, bits );
2090 HeapFree( GetProcessHeap(), 0, info );
2092 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
2097 /**********************************************************************
2098 * CreateIconIndirect (USER32.@)
2100 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2102 BITMAP bmpXor, bmpAnd;
2104 HBITMAP color = 0, mask;
2108 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2109 iconinfo->hbmColor, iconinfo->hbmMask,
2110 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2112 if (!iconinfo->hbmMask) return 0;
2114 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2115 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2116 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2117 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2119 if (iconinfo->hbmColor)
2121 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2122 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2123 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2124 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2126 width = bmpXor.bmWidth;
2127 height = bmpXor.bmHeight;
2128 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1 || bmpAnd.bmPlanes * bmpAnd.bmBitsPixel != 1)
2130 color = CreateCompatibleBitmap( screen_dc, width, height );
2131 mask = CreateBitmap( width, height, 1, 1, NULL );
2133 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2137 width = bmpAnd.bmWidth;
2138 height = bmpAnd.bmHeight;
2139 mask = CreateBitmap( width, height, 1, 1, NULL );
2142 hdc = CreateCompatibleDC( 0 );
2143 SelectObject( hdc, mask );
2144 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
2148 SelectObject( hdc, color );
2149 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
2151 else if (iconinfo->hbmColor)
2153 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
2159 hObj = alloc_icon_handle( FALSE, 1 );
2162 struct cursoricon_object *info = get_icon_ptr( hObj );
2163 struct cursoricon_frame *frame;
2165 info->is_icon = iconinfo->fIcon;
2166 frame = get_icon_frame( info, 0 );
2168 frame->width = width;
2169 frame->height = height;
2170 frame->color = color;
2172 frame->alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
2173 release_icon_frame( info, 0, frame );
2176 info->hotspot.x = width / 2;
2177 info->hotspot.y = height / 2;
2181 info->hotspot.x = iconinfo->xHotspot;
2182 info->hotspot.y = iconinfo->yHotspot;
2185 release_icon_ptr( hObj, info );
2186 USER_Driver->pCreateCursorIcon( hObj );
2191 /******************************************************************************
2192 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2195 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2198 * hdc [I] Handle to device context
2199 * x0 [I] X coordinate of upper left corner
2200 * y0 [I] Y coordinate of upper left corner
2201 * hIcon [I] Handle to icon to draw
2202 * cxWidth [I] Width of icon
2203 * cyWidth [I] Height of icon
2204 * istep [I] Index of frame in animated cursor
2205 * hbr [I] Handle to background brush
2206 * flags [I] Icon-drawing flags
2212 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2213 INT cxWidth, INT cyWidth, UINT istep,
2214 HBRUSH hbr, UINT flags )
2216 struct cursoricon_frame *frame;
2217 struct cursoricon_object *ptr;
2218 HDC hdc_dest, hMemDC;
2219 BOOL result = FALSE, DoOffscreen;
2221 COLORREF oldFg, oldBg;
2222 INT x, y, nStretchMode;
2224 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2225 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2227 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2228 if (istep >= get_icon_steps( ptr ))
2230 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
2231 release_icon_ptr( hIcon, ptr );
2234 if (!(frame = get_icon_frame( ptr, istep )))
2236 FIXME_(icon)("Error retrieving icon frame %d\n", istep);
2237 release_icon_ptr( hIcon, ptr );
2240 if (!(hMemDC = CreateCompatibleDC( hdc )))
2242 release_icon_frame( ptr, istep, frame );
2243 release_icon_ptr( hIcon, ptr );
2247 if (flags & DI_NOMIRROR)
2248 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2250 /* Calculate the size of the destination image. */
2253 if (flags & DI_DEFAULTSIZE)
2254 cxWidth = GetSystemMetrics (SM_CXICON);
2256 cxWidth = frame->width;
2260 if (flags & DI_DEFAULTSIZE)
2261 cyWidth = GetSystemMetrics (SM_CYICON);
2263 cyWidth = frame->height;
2266 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2276 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2277 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2279 DeleteDC( hdc_dest );
2282 SelectObject(hdc_dest, hB_off);
2283 FillRect(hdc_dest, &r, hbr);
2293 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2295 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2296 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2298 if (frame->alpha && (flags & DI_IMAGE))
2300 BOOL alpha_blend = TRUE;
2302 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2305 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2306 alpha_blend = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel > 8;
2310 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2311 SelectObject( hMemDC, frame->alpha );
2312 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2313 0, 0, frame->width, frame->height,
2314 pixelblend )) goto done;
2318 if (flags & DI_MASK)
2320 DWORD rop = (flags & DI_IMAGE) ? SRCAND : SRCCOPY;
2321 SelectObject( hMemDC, frame->mask );
2322 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2323 hMemDC, 0, 0, frame->width, frame->height, rop );
2326 if (flags & DI_IMAGE)
2330 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2331 SelectObject( hMemDC, frame->color );
2332 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2333 hMemDC, 0, 0, frame->width, frame->height, rop );
2337 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2338 SelectObject( hMemDC, frame->mask );
2339 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2340 hMemDC, 0, frame->height, frame->width,
2341 frame->height, rop );
2346 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2348 SetTextColor( hdc, oldFg );
2349 SetBkColor( hdc, oldBg );
2350 SetStretchBltMode (hdc, nStretchMode);
2352 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2353 if (hB_off) DeleteObject(hB_off);
2356 release_icon_frame( ptr, istep, frame );
2357 release_icon_ptr( hIcon, ptr );
2361 /***********************************************************************
2362 * DIB_FixColorsToLoadflags
2364 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2367 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2370 COLORREF c_W, c_S, c_F, c_L, c_C;
2379 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2381 WARN_(resource)("Invalid bitmap\n");
2385 if (bpp > 8) return;
2387 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2395 colors = bmi->bmiHeader.biClrUsed;
2396 if (colors > 256) colors = 256;
2397 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2400 c_W = GetSysColor(COLOR_WINDOW);
2401 c_S = GetSysColor(COLOR_3DSHADOW);
2402 c_F = GetSysColor(COLOR_3DFACE);
2403 c_L = GetSysColor(COLOR_3DLIGHT);
2405 if (loadflags & LR_LOADTRANSPARENT) {
2407 case 1: pix = pix >> 7; break;
2408 case 4: pix = pix >> 4; break;
2411 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2414 if (pix >= colors) {
2415 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2418 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2419 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2420 ptr->rgbBlue = GetBValue(c_W);
2421 ptr->rgbGreen = GetGValue(c_W);
2422 ptr->rgbRed = GetRValue(c_W);
2424 if (loadflags & LR_LOADMAP3DCOLORS)
2425 for (i=0; i<colors; i++) {
2426 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2427 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2428 if (c_C == RGB(128, 128, 128)) {
2429 ptr->rgbRed = GetRValue(c_S);
2430 ptr->rgbGreen = GetGValue(c_S);
2431 ptr->rgbBlue = GetBValue(c_S);
2432 } else if (c_C == RGB(192, 192, 192)) {
2433 ptr->rgbRed = GetRValue(c_F);
2434 ptr->rgbGreen = GetGValue(c_F);
2435 ptr->rgbBlue = GetBValue(c_F);
2436 } else if (c_C == RGB(223, 223, 223)) {
2437 ptr->rgbRed = GetRValue(c_L);
2438 ptr->rgbGreen = GetGValue(c_L);
2439 ptr->rgbBlue = GetBValue(c_L);
2445 /**********************************************************************
2448 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2449 INT desiredx, INT desiredy, UINT loadflags )
2451 HBITMAP hbitmap = 0, orig_bm;
2455 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2459 LONG width, height, new_width, new_height;
2461 DWORD compr_dummy, offbits = 0;
2463 HDC screen_mem_dc = NULL;
2465 if (!(loadflags & LR_LOADFROMFILE))
2469 /* OEM bitmap: try to load the resource from user32.dll */
2470 instance = user32_module;
2473 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2474 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2476 if ((info = LockResource( handle )) == NULL) return 0;
2480 BITMAPFILEHEADER * bmfh;
2482 if (!(ptr = map_fileW( name, NULL ))) return 0;
2483 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2484 bmfh = (BITMAPFILEHEADER *)ptr;
2485 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2487 WARN("Invalid/unsupported bitmap format!\n");
2490 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2493 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2494 &bpp_dummy, &compr_dummy);
2497 WARN("Invalid bitmap format!\n");
2501 size = bitmap_info_size(info, DIB_RGB_COLORS);
2502 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2503 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2505 if (!fix_info || !scaled_info) goto end;
2506 memcpy(fix_info, info, size);
2508 pix = *((LPBYTE)info + size);
2509 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2511 memcpy(scaled_info, fix_info, size);
2514 new_width = desiredx;
2519 new_height = height > 0 ? desiredy : -desiredy;
2521 new_height = height;
2525 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2526 core->bcWidth = new_width;
2527 core->bcHeight = new_height;
2531 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2532 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2533 WARN("Broken BitmapInfoHeader!\n");
2537 scaled_info->bmiHeader.biWidth = new_width;
2538 scaled_info->bmiHeader.biHeight = new_height;
2541 if (new_height < 0) new_height = -new_height;
2543 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2544 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2546 bits = (char *)info + (offbits ? offbits : size);
2548 if (loadflags & LR_CREATEDIBSECTION)
2550 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2551 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2555 if (is_dib_monochrome(fix_info))
2556 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2558 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2561 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2562 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2563 SelectObject(screen_mem_dc, orig_bm);
2566 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2567 HeapFree(GetProcessHeap(), 0, scaled_info);
2568 HeapFree(GetProcessHeap(), 0, fix_info);
2569 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2574 /**********************************************************************
2575 * LoadImageA (USER32.@)
2579 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2580 INT desiredx, INT desiredy, UINT loadflags)
2585 if (IS_INTRESOURCE(name))
2586 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2589 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2590 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2591 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2593 __EXCEPT_PAGE_FAULT {
2594 SetLastError( ERROR_INVALID_PARAMETER );
2598 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2599 HeapFree(GetProcessHeap(), 0, u_name);
2604 /******************************************************************************
2605 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2608 * hinst [I] Handle of instance that contains image
2609 * name [I] Name of image
2610 * type [I] Type of image
2611 * desiredx [I] Desired width
2612 * desiredy [I] Desired height
2613 * loadflags [I] Load flags
2616 * Success: Handle to newly loaded image
2619 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2621 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2622 INT desiredx, INT desiredy, UINT loadflags )
2624 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2625 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2627 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2630 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2633 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2636 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2637 GetDeviceCaps(screen_dc, BITSPIXEL),
2643 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2644 1, TRUE, loadflags);
2649 /******************************************************************************
2650 * CopyImage (USER32.@) Creates new image and copies attributes to it
2653 * hnd [I] Handle to image to copy
2654 * type [I] Type of image to copy
2655 * desiredx [I] Desired width of new image
2656 * desiredy [I] Desired height of new image
2657 * flags [I] Copy flags
2660 * Success: Handle to newly created image
2664 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2665 * all other versions (95/2000/XP have been tested) ignore it.
2668 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2669 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2670 * the copy will have the same depth as the screen.
2671 * The content of the image will only be copied if the bit depth of the
2672 * original image is compatible with the bit depth of the screen, or
2673 * if the source is a DIB section.
2674 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2676 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2677 INT desiredy, UINT flags )
2679 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2680 hnd, type, desiredx, desiredy, flags);
2691 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2692 if (!objSize) return 0;
2693 if ((desiredx < 0) || (desiredy < 0)) return 0;
2695 if (flags & LR_COPYFROMRESOURCE)
2697 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2700 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2701 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2703 /* Allocate memory for a BITMAPINFOHEADER structure and a
2704 color table. The maximum number of colors in a color table
2705 is 256 which corresponds to a bitmap with depth 8.
2706 Bitmaps with higher depths don't have color tables. */
2707 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2710 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2711 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2712 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2713 bi->bmiHeader.biCompression = BI_RGB;
2715 if (flags & LR_CREATEDIBSECTION)
2717 /* Create a DIB section. LR_MONOCHROME is ignored */
2719 HDC dc = CreateCompatibleDC(NULL);
2721 if (objSize == sizeof(DIBSECTION))
2723 /* The source bitmap is a DIB.
2724 Get its attributes to create an exact copy */
2725 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2728 bi->bmiHeader.biWidth = desiredx;
2729 bi->bmiHeader.biHeight = desiredy;
2731 /* Get the color table or the color masks */
2732 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2734 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2739 /* Create a device-dependent bitmap */
2741 BOOL monochrome = (flags & LR_MONOCHROME);
2743 if (objSize == sizeof(DIBSECTION))
2745 /* The source bitmap is a DIB section.
2746 Get its attributes */
2747 HDC dc = CreateCompatibleDC(NULL);
2748 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2749 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2750 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2753 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2755 /* Look if the colors of the DIB are black and white */
2758 (bi->bmiColors[0].rgbRed == 0xff
2759 && bi->bmiColors[0].rgbGreen == 0xff
2760 && bi->bmiColors[0].rgbBlue == 0xff
2761 && bi->bmiColors[0].rgbReserved == 0
2762 && bi->bmiColors[1].rgbRed == 0
2763 && bi->bmiColors[1].rgbGreen == 0
2764 && bi->bmiColors[1].rgbBlue == 0
2765 && bi->bmiColors[1].rgbReserved == 0)
2767 (bi->bmiColors[0].rgbRed == 0
2768 && bi->bmiColors[0].rgbGreen == 0
2769 && bi->bmiColors[0].rgbBlue == 0
2770 && bi->bmiColors[0].rgbReserved == 0
2771 && bi->bmiColors[1].rgbRed == 0xff
2772 && bi->bmiColors[1].rgbGreen == 0xff
2773 && bi->bmiColors[1].rgbBlue == 0xff
2774 && bi->bmiColors[1].rgbReserved == 0);
2777 else if (!monochrome)
2779 monochrome = ds.dsBm.bmBitsPixel == 1;
2784 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2788 HDC screenDC = GetDC(NULL);
2789 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2790 ReleaseDC(NULL, screenDC);
2796 /* Only copy the bitmap if it's a DIB section or if it's
2797 compatible to the screen */
2800 if (objSize == sizeof(DIBSECTION))
2802 copyContents = TRUE;
2806 HDC screenDC = GetDC(NULL);
2807 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2808 ReleaseDC(NULL, screenDC);
2810 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2815 /* The source bitmap may already be selected in a device context,
2816 use GetDIBits/StretchDIBits and not StretchBlt */
2821 dc = CreateCompatibleDC(NULL);
2823 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2824 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2825 bi->bmiHeader.biSizeImage = 0;
2826 bi->bmiHeader.biClrUsed = 0;
2827 bi->bmiHeader.biClrImportant = 0;
2829 /* Fill in biSizeImage */
2830 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2831 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2837 /* Get the image bits of the source bitmap */
2838 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2840 /* Copy it to the destination bitmap */
2841 oldBmp = SelectObject(dc, res);
2842 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2843 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2844 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2845 SelectObject(dc, oldBmp);
2847 HeapFree(GetProcessHeap(), 0, bits);
2853 if (flags & LR_COPYDELETEORG)
2858 HeapFree(GetProcessHeap(), 0, bi);
2864 struct cursoricon_object *icon;
2866 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( screen_dc, BITSPIXEL );
2868 if (flags & LR_DEFAULTSIZE)
2870 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2871 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2874 if (!(icon = get_icon_ptr( hnd ))) return 0;
2876 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2877 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2878 type == IMAGE_CURSOR, flags );
2880 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2881 release_icon_ptr( hnd, icon );
2883 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2891 /******************************************************************************
2892 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2895 * Success: Handle to specified bitmap
2898 HBITMAP WINAPI LoadBitmapW(
2899 HINSTANCE instance, /* [in] Handle to application instance */
2900 LPCWSTR name) /* [in] Address of bitmap resource name */
2902 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2905 /**********************************************************************
2906 * LoadBitmapA (USER32.@)
2910 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2912 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );