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 /***********************************************************************
319 * get_dib_width_bytes
321 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
323 static int get_dib_width_bytes( int width, int depth )
329 case 1: words = (width + 31) / 32; break;
330 case 4: words = (width + 7) / 8; break;
331 case 8: words = (width + 3) / 4; break;
333 case 16: words = (width + 1) / 2; break;
334 case 24: words = (width * 3 + 3)/4; break;
336 WARN("(%d): Unsupported depth\n", depth );
345 /***********************************************************************
348 * Return the size of the bitmap info structure including color table.
350 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
352 unsigned int colors, size, masks = 0;
354 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
356 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
357 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
358 return sizeof(BITMAPCOREHEADER) + colors *
359 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
361 else /* assume BITMAPINFOHEADER */
363 colors = info->bmiHeader.biClrUsed;
364 if (colors > 256) /* buffer overflow otherwise */
366 if (!colors && (info->bmiHeader.biBitCount <= 8))
367 colors = 1 << info->bmiHeader.biBitCount;
368 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
369 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
370 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
375 /***********************************************************************
378 * Helper function to duplicate a bitmap.
380 static HBITMAP copy_bitmap( HBITMAP bitmap )
383 HBITMAP new_bitmap = 0;
386 if (!bitmap) return 0;
387 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
389 if ((src = CreateCompatibleDC( 0 )) && (dst = CreateCompatibleDC( 0 )))
391 SelectObject( src, bitmap );
392 if ((new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight )))
394 SelectObject( dst, new_bitmap );
395 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
404 /***********************************************************************
407 * Returns whether a DIB can be converted to a monochrome DDB.
409 * A DIB can be converted if its color table contains only black and
410 * white. Black must be the first color in the color table.
412 * Note : If the first color in the color table is white followed by
413 * black, we can't convert it to a monochrome DDB with
414 * SetDIBits, because black and white would be inverted.
416 static BOOL is_dib_monochrome( const BITMAPINFO* info )
418 if (info->bmiHeader.biBitCount != 1) return FALSE;
420 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
422 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
424 /* Check if the first color is black */
425 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
429 /* Check if the second color is white */
430 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
431 && (rgb->rgbtBlue == 0xff));
435 else /* assume BITMAPINFOHEADER */
437 const RGBQUAD *rgb = info->bmiColors;
439 /* Check if the first color is black */
440 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
441 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
445 /* Check if the second color is white */
446 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
447 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
453 /***********************************************************************
456 * Get the info from a bitmap header.
457 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
459 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
460 LONG *height, WORD *bpp, DWORD *compr )
462 if (header->biSize == sizeof(BITMAPCOREHEADER))
464 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
465 *width = core->bcWidth;
466 *height = core->bcHeight;
467 *bpp = core->bcBitCount;
471 else if (header->biSize == sizeof(BITMAPINFOHEADER) ||
472 header->biSize == sizeof(BITMAPV4HEADER) ||
473 header->biSize == sizeof(BITMAPV5HEADER))
475 *width = header->biWidth;
476 *height = header->biHeight;
477 *bpp = header->biBitCount;
478 *compr = header->biCompression;
481 WARN("unknown/wrong size (%u) for header\n", header->biSize);
485 /**********************************************************************
488 BOOL get_icon_size( HICON handle, SIZE *size )
490 struct cursoricon_object *info;
491 struct cursoricon_frame *frame;
493 if (!(info = get_icon_ptr( handle ))) return FALSE;
494 frame = get_icon_frame( info, 0 );
495 size->cx = frame->width;
496 size->cy = frame->height;
497 release_icon_frame( info, 0, frame);
498 release_icon_ptr( handle, info );
503 * The following macro functions account for the irregularities of
504 * accessing cursor and icon resources in files and resource entries.
506 typedef BOOL (*fnGetCIEntry)( LPCVOID dir, int n,
507 int *width, int *height, int *bits );
509 /**********************************************************************
510 * CURSORICON_FindBestIcon
512 * Find the icon closest to the requested size and bit depth.
514 static int CURSORICON_FindBestIcon( LPCVOID dir, fnGetCIEntry get_entry,
515 int width, int height, int depth, UINT loadflags )
517 int i, cx, cy, bits, bestEntry = -1;
518 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
519 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
522 iTotalDiff = 0xFFFFFFFF;
523 iColorDiff = 0xFFFFFFFF;
525 if (loadflags & LR_DEFAULTSIZE)
527 if (!width) width = GetSystemMetrics( SM_CXICON );
528 if (!height) height = GetSystemMetrics( SM_CYICON );
530 else if (!width && !height)
532 /* use the size of the first entry */
533 if (!get_entry( dir, 0, &width, &height, &bits )) return -1;
537 for ( i = 0; iTotalDiff && get_entry( dir, i, &cx, &cy, &bits ); i++ )
539 iTempXDiff = abs(width - cx);
540 iTempYDiff = abs(height - cy);
542 if(iTotalDiff > (iTempXDiff + iTempYDiff))
546 iTotalDiff = iXDiff + iYDiff;
550 /* Find Best Colors for Best Fit */
551 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
553 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
555 iTempColorDiff = abs(depth - bits);
556 if(iColorDiff > iTempColorDiff)
559 iColorDiff = iTempColorDiff;
567 static BOOL CURSORICON_GetResIconEntry( LPCVOID dir, int n,
568 int *width, int *height, int *bits )
570 const CURSORICONDIR *resdir = dir;
571 const ICONRESDIR *icon;
573 if ( resdir->idCount <= n )
575 icon = &resdir->idEntries[n].ResInfo.icon;
576 *width = icon->bWidth;
577 *height = icon->bHeight;
578 *bits = resdir->idEntries[n].wBitCount;
582 /**********************************************************************
583 * CURSORICON_FindBestCursor
585 * Find the cursor closest to the requested size.
587 * FIXME: parameter 'color' ignored.
589 static int CURSORICON_FindBestCursor( LPCVOID dir, fnGetCIEntry get_entry,
590 int width, int height, int depth, UINT loadflags )
592 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
594 if (loadflags & LR_DEFAULTSIZE)
596 if (!width) width = GetSystemMetrics( SM_CXCURSOR );
597 if (!height) height = GetSystemMetrics( SM_CYCURSOR );
599 else if (!width && !height)
601 /* use the first entry */
602 if (!get_entry( dir, 0, &width, &height, &bits )) return -1;
606 /* Double height to account for AND and XOR masks */
610 /* First find the largest one smaller than or equal to the requested size*/
612 maxwidth = maxheight = 0;
613 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
615 if ((cx <= width) && (cy <= height) &&
616 (cx > maxwidth) && (cy > maxheight))
623 if (bestEntry != -1) return bestEntry;
625 /* Now find the smallest one larger than the requested size */
627 maxwidth = maxheight = 255;
628 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
630 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
641 static BOOL CURSORICON_GetResCursorEntry( LPCVOID dir, int n,
642 int *width, int *height, int *bits )
644 const CURSORICONDIR *resdir = dir;
645 const CURSORDIR *cursor;
647 if ( resdir->idCount <= n )
649 cursor = &resdir->idEntries[n].ResInfo.cursor;
650 *width = cursor->wWidth;
651 *height = cursor->wHeight;
652 *bits = resdir->idEntries[n].wBitCount;
656 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir,
657 int width, int height, int depth,
662 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
663 width, height, depth, loadflags );
666 return &dir->idEntries[n];
669 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir,
670 int width, int height, int depth,
673 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
674 width, height, depth, loadflags );
677 return &dir->idEntries[n];
680 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, int n,
681 int *width, int *height, int *bits )
683 const CURSORICONFILEDIR *filedir = dir;
684 const CURSORICONFILEDIRENTRY *entry;
685 const BITMAPINFOHEADER *info;
687 if ( filedir->idCount <= n )
689 entry = &filedir->idEntries[n];
690 /* FIXME: check against file size */
691 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
692 *width = entry->bWidth;
693 *height = entry->bHeight;
694 *bits = info->biBitCount;
698 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir,
699 int width, int height, int depth,
702 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
703 width, height, depth, loadflags );
706 return &dir->idEntries[n];
709 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir,
710 int width, int height, int depth,
713 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
714 width, height, depth, loadflags );
717 return &dir->idEntries[n];
720 /***********************************************************************
723 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
726 BOOL has_alpha = FALSE;
727 const unsigned char *ptr = bits;
729 if (info->bmiHeader.biBitCount != 32) return FALSE;
730 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
731 if ((has_alpha = (ptr[3] != 0))) break;
735 /***********************************************************************
736 * create_alpha_bitmap
738 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
740 static HBITMAP create_alpha_bitmap( HBITMAP color, HBITMAP mask,
741 const BITMAPINFO *src_info, const void *color_bits )
744 BITMAPINFO *info = NULL;
751 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
752 if (bm.bmBitsPixel != 32) return 0;
754 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
755 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
756 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
757 info->bmiHeader.biWidth = bm.bmWidth;
758 info->bmiHeader.biHeight = -bm.bmHeight;
759 info->bmiHeader.biPlanes = 1;
760 info->bmiHeader.biBitCount = 32;
761 info->bmiHeader.biCompression = BI_RGB;
762 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
763 info->bmiHeader.biXPelsPerMeter = 0;
764 info->bmiHeader.biYPelsPerMeter = 0;
765 info->bmiHeader.biClrUsed = 0;
766 info->bmiHeader.biClrImportant = 0;
767 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
771 SelectObject( hdc, alpha );
772 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
773 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
774 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
779 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
780 if (!bmi_has_alpha( info, bits ))
782 DeleteObject( alpha );
788 /* pre-multiply by alpha */
789 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
791 unsigned int alpha = ptr[3];
792 ptr[0] = ptr[0] * alpha / 255;
793 ptr[1] = ptr[1] * alpha / 255;
794 ptr[2] = ptr[2] * alpha / 255;
799 HeapFree( GetProcessHeap(), 0, info );
804 /***********************************************************************
805 * create_icon_from_bmi
807 * Create an icon from its BITMAPINFO.
809 static HICON create_icon_from_bmi( BITMAPINFO *bmi, HMODULE module, LPCWSTR resname, HRSRC rsrc,
810 POINT hotspot, BOOL bIcon, INT width, INT height, UINT cFlag )
812 unsigned int size = bitmap_info_size( bmi, DIB_RGB_COLORS );
813 BOOL monochrome = is_dib_monochrome( bmi );
814 HBITMAP color = 0, mask = 0, alpha = 0;
815 const void *color_bits, *mask_bits;
816 BITMAPINFO *bmi_copy;
822 /* Check bitmap header */
824 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
825 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
826 bmi->bmiHeader.biCompression != BI_RGB) )
828 WARN_(cursor)("\tinvalid resource bitmap header.\n");
832 if (cFlag & LR_DEFAULTSIZE)
834 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
835 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
839 if (!width) width = bmi->bmiHeader.biWidth;
840 if (!height) height = bmi->bmiHeader.biHeight/2;
842 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
843 (bmi->bmiHeader.biWidth != width);
845 /* Scale the hotspot */
848 hotspot.x = width / 2;
849 hotspot.y = height / 2;
853 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
854 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
857 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
858 if (!screen_dc) return 0;
860 if (!(bmi_copy = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
862 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
864 memcpy( bmi_copy, bmi, size );
865 bmi_copy->bmiHeader.biHeight /= 2;
867 color_bits = (const char*)bmi + size;
868 mask_bits = (const char*)color_bits +
869 get_dib_width_bytes( bmi->bmiHeader.biWidth,
870 bmi->bmiHeader.biBitCount ) * abs(bmi_copy->bmiHeader.biHeight);
875 if (!(mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
878 /* copy color data into second half of mask bitmap */
879 SelectObject( hdc, mask );
880 StretchDIBits( hdc, 0, height, width, height,
881 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
882 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
886 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
887 if (!(color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
888 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
890 DeleteObject( mask );
893 SelectObject( hdc, color );
894 StretchDIBits( hdc, 0, 0, width, height,
895 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
896 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
898 if (bmi_has_alpha( bmi_copy, color_bits ))
899 alpha = create_alpha_bitmap( color, mask, bmi_copy, color_bits );
901 /* convert info to monochrome to copy the mask */
902 bmi_copy->bmiHeader.biBitCount = 1;
903 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
905 RGBQUAD *rgb = bmi_copy->bmiColors;
907 bmi_copy->bmiHeader.biClrUsed = bmi_copy->bmiHeader.biClrImportant = 2;
908 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
909 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
910 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
914 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)bmi_copy) + 1);
916 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
917 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
921 SelectObject( hdc, mask );
922 StretchDIBits( hdc, 0, 0, width, height,
923 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
924 mask_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
929 HeapFree( GetProcessHeap(), 0, bmi_copy );
932 hObj = alloc_icon_handle( FALSE, 1 );
935 struct cursoricon_object *info = get_icon_ptr( hObj );
936 struct cursoricon_frame *frame;
938 info->is_icon = bIcon;
939 info->module = module;
940 info->hotspot = hotspot;
941 frame = get_icon_frame( info, 0 );
943 frame->width = width;
944 frame->height = height;
945 frame->color = color;
947 frame->alpha = alpha;
948 release_icon_frame( info, 0, frame );
949 if (!IS_INTRESOURCE(resname))
951 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
952 if (info->resname) strcpyW( info->resname, resname );
954 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
956 if (module && (cFlag & LR_SHARED))
959 list_add_head( &icon_cache, &info->entry );
961 release_icon_ptr( hObj, info );
962 USER_Driver->pCreateCursorIcon( hObj );
966 DeleteObject( color );
967 DeleteObject( alpha );
968 DeleteObject( mask );
974 /**********************************************************************
975 * .ANI cursor support
977 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
978 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
979 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
981 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
982 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
983 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
984 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
985 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
986 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
987 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
989 #define ANI_FLAG_ICON 0x1
990 #define ANI_FLAG_SEQUENCE 0x2
1006 const unsigned char *data;
1009 static void dump_ani_header( const ani_header *header )
1011 TRACE(" header size: %d\n", header->header_size);
1012 TRACE(" frames: %d\n", header->num_frames);
1013 TRACE(" steps: %d\n", header->num_steps);
1014 TRACE(" width: %d\n", header->width);
1015 TRACE(" height: %d\n", header->height);
1016 TRACE(" bpp: %d\n", header->bpp);
1017 TRACE(" planes: %d\n", header->num_planes);
1018 TRACE(" display rate: %d\n", header->display_rate);
1019 TRACE(" flags: 0x%08x\n", header->flags);
1041 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1043 const unsigned char *ptr = parent_chunk->data;
1044 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1046 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1050 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1051 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1053 ptr += sizeof(DWORD);
1054 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1055 ptr += sizeof(DWORD);
1056 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1062 ptr += sizeof(DWORD);
1063 ptr += (*(const DWORD *)ptr + 1) & ~1;
1064 ptr += sizeof(DWORD);
1072 * RIFF:'ACON' RIFF chunk
1073 * |- CHUNK:'anih' Header
1074 * |- CHUNK:'seq ' Sequence information (optional)
1075 * \- LIST:'fram' Frame list
1076 * |- CHUNK:icon Cursor frames
1081 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
1082 INT width, INT height, INT depth, UINT loadflags )
1084 struct animated_cursoricon_object *ani_icon_data;
1085 struct cursoricon_object *info;
1086 DWORD *frame_rates = NULL;
1087 DWORD *frame_seq = NULL;
1088 ani_header header = {0};
1089 BOOL use_seq = FALSE;
1094 riff_chunk_t root_chunk = { bits_size, bits };
1095 riff_chunk_t ACON_chunk = {0};
1096 riff_chunk_t anih_chunk = {0};
1097 riff_chunk_t fram_chunk = {0};
1098 riff_chunk_t rate_chunk = {0};
1099 riff_chunk_t seq_chunk = {0};
1100 const unsigned char *icon_chunk;
1101 const unsigned char *icon_data;
1103 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1105 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1106 if (!ACON_chunk.data)
1108 ERR("Failed to get root chunk.\n");
1112 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1113 if (!anih_chunk.data)
1115 ERR("Failed to get 'anih' chunk.\n");
1118 memcpy( &header, anih_chunk.data, sizeof(header) );
1119 dump_ani_header( &header );
1121 if (!(header.flags & ANI_FLAG_ICON))
1123 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1127 if (header.flags & ANI_FLAG_SEQUENCE)
1129 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1132 frame_seq = (DWORD *) seq_chunk.data;
1137 FIXME("Sequence data expected but not found, assuming steps == frames.\n");
1138 header.num_steps = header.num_frames;
1142 riff_find_chunk( ANI_rate_ID, 0, &ACON_chunk, &rate_chunk );
1143 if (rate_chunk.data)
1144 frame_rates = (DWORD *) rate_chunk.data;
1146 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1147 if (!fram_chunk.data)
1149 ERR("Failed to get icon list.\n");
1153 cursor = alloc_icon_handle( TRUE, header.num_steps );
1154 if (!cursor) return 0;
1155 frames = HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD)*header.num_frames );
1158 free_icon_handle( cursor );
1162 info = get_icon_ptr( cursor );
1163 ani_icon_data = (struct animated_cursoricon_object *) info;
1164 info->is_icon = FALSE;
1165 ani_icon_data->num_frames = header.num_frames;
1167 /* The .ANI stores the display rate in jiffies (1/60s) */
1168 info->delay = header.display_rate;
1170 icon_chunk = fram_chunk.data;
1171 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1172 for (i=0; i<header.num_frames; i++)
1174 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1175 const CURSORICONFILEDIRENTRY *entry;
1176 INT frameWidth, frameHeight;
1177 const BITMAPINFO *bmi;
1179 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1180 width, height, depth, loadflags );
1182 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1183 info->hotspot.x = entry->xHotspot;
1184 info->hotspot.y = entry->yHotspot;
1185 if (!header.width || !header.height)
1187 frameWidth = entry->bWidth;
1188 frameHeight = entry->bHeight;
1192 frameWidth = header.width;
1193 frameHeight = header.height;
1196 /* Grab a frame from the animation */
1197 frames[i] = create_icon_from_bmi( (BITMAPINFO *)bmi, NULL, NULL, NULL, info->hotspot,
1198 FALSE, frameWidth, frameHeight, loadflags );
1201 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1205 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1206 ani_icon_data->num_frames = 0;
1207 release_icon_ptr( cursor, info );
1208 free_icon_handle( cursor );
1209 HeapFree( GetProcessHeap(), 0, frames );
1215 /* Advance to the next chunk */
1216 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1217 icon_data = icon_chunk + (2 * sizeof(DWORD));
1220 /* There was an error but we at least decoded the first frame, so just use that frame */
1223 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1224 for (i=1; i<ani_icon_data->num_frames; i++)
1225 free_icon_handle( ani_icon_data->frames[i] );
1228 ani_icon_data->num_steps = 1;
1229 ani_icon_data->num_frames = 1;
1232 /* Setup the animated frames in the correct sequence */
1233 for (i=0; i<ani_icon_data->num_steps; i++)
1235 DWORD frame_id = use_seq ? frame_seq[i] : i;
1236 struct cursoricon_frame *frame;
1238 if (frame_id >= ani_icon_data->num_frames)
1240 frame_id = ani_icon_data->num_frames-1;
1241 ERR_(cursor)("Sequence indicates frame past end of list, corrupt?\n");
1243 ani_icon_data->frames[i] = frames[frame_id];
1244 frame = get_icon_frame( info, i );
1246 frame->delay = frame_rates[i];
1249 release_icon_frame( info, i, frame );
1252 HeapFree( GetProcessHeap(), 0, frames );
1253 release_icon_ptr( cursor, info );
1259 /**********************************************************************
1260 * CreateIconFromResourceEx (USER32.@)
1262 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1263 * with cbSize parameter as well.
1265 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1266 BOOL bIcon, DWORD dwVersion,
1267 INT width, INT height,
1273 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1274 bits, cbSize, dwVersion, width, height,
1275 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1277 if (!bits) return 0;
1279 if (dwVersion == 0x00020000)
1281 FIXME_(cursor)("\t2.xx resources are not supported\n");
1285 /* Check if the resource is an animated icon/cursor */
1286 if (!memcmp(bits, "RIFF", 4))
1287 return CURSORICON_CreateIconFromANI( bits, cbSize, width, height, 0 /* default depth */, cFlag );
1291 hotspot.x = width / 2;
1292 hotspot.y = height / 2;
1293 bmi = (BITMAPINFO *)bits;
1295 else /* get the hotspot */
1297 SHORT *pt = (SHORT *)bits;
1300 bmi = (BITMAPINFO *)(pt + 2);
1303 return create_icon_from_bmi( bmi, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1307 /**********************************************************************
1308 * CreateIconFromResource (USER32.@)
1310 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1311 BOOL bIcon, DWORD dwVersion)
1313 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1317 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1318 INT width, INT height, INT depth,
1319 BOOL fCursor, UINT loadflags)
1321 const CURSORICONFILEDIRENTRY *entry;
1322 const CURSORICONFILEDIR *dir;
1328 TRACE("loading %s\n", debugstr_w( filename ));
1330 bits = map_fileW( filename, &filesize );
1334 /* Check for .ani. */
1335 if (memcmp( bits, "RIFF", 4 ) == 0)
1337 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, loadflags );
1341 dir = (const CURSORICONFILEDIR*) bits;
1342 if ( filesize < sizeof(*dir) )
1345 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1349 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth, loadflags );
1351 entry = CURSORICON_FindBestIconFile( dir, width, height, depth, loadflags );
1356 /* check that we don't run off the end of the file */
1357 if ( entry->dwDIBOffset > filesize )
1359 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1362 hotspot.x = entry->xHotspot;
1363 hotspot.y = entry->yHotspot;
1364 hIcon = create_icon_from_bmi( (BITMAPINFO *)&bits[entry->dwDIBOffset], NULL, NULL, NULL,
1365 hotspot, !fCursor, width, height, loadflags );
1367 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1368 UnmapViewOfFile( bits );
1372 /**********************************************************************
1375 * Load a cursor or icon from resource or file.
1377 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1378 INT width, INT height, INT depth,
1379 BOOL fCursor, UINT loadflags)
1384 const CURSORICONDIR *dir;
1385 const CURSORICONDIRENTRY *dirEntry;
1390 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1391 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1393 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1394 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1396 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1398 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1399 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1401 /* Get directory resource ID */
1403 if (!(hRsrc = FindResourceW( hInstance, name,
1404 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1407 /* Find the best entry in the directory */
1409 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1410 if (!(dir = LockResource( handle ))) return 0;
1412 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth, loadflags );
1414 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth, loadflags );
1415 if (!dirEntry) return 0;
1416 wResId = dirEntry->wResId;
1417 FreeResource( handle );
1419 /* Load the resource */
1421 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1422 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1424 /* If shared icon, check whether it was already loaded */
1425 if (loadflags & LR_SHARED)
1427 struct cursoricon_object *ptr;
1430 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1432 if (ptr->module != hInstance) continue;
1433 if (ptr->rsrc != hRsrc) continue;
1434 hIcon = ptr->obj.handle;
1438 if (hIcon) return hIcon;
1441 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1442 bits = LockResource( handle );
1446 hotspot.x = width / 2;
1447 hotspot.y = height / 2;
1449 else /* get the hotspot */
1451 SHORT *pt = (SHORT *)bits;
1454 bits += 2 * sizeof(SHORT);
1456 hIcon = create_icon_from_bmi( (BITMAPINFO *)bits, hInstance, name, hRsrc,
1457 hotspot, !fCursor, width, height, loadflags );
1458 FreeResource( handle );
1463 /***********************************************************************
1464 * CreateCursor (USER32.@)
1466 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1467 INT xHotSpot, INT yHotSpot,
1468 INT nWidth, INT nHeight,
1469 LPCVOID lpANDbits, LPCVOID lpXORbits )
1474 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1475 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1478 info.xHotspot = xHotSpot;
1479 info.yHotspot = yHotSpot;
1480 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1481 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1482 hCursor = CreateIconIndirect( &info );
1483 DeleteObject( info.hbmMask );
1484 DeleteObject( info.hbmColor );
1489 /***********************************************************************
1490 * CreateIcon (USER32.@)
1492 * Creates an icon based on the specified bitmaps. The bitmaps must be
1493 * provided in a device dependent format and will be resized to
1494 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1495 * depth. The provided bitmaps must be top-down bitmaps.
1496 * Although Windows does not support 15bpp(*) this API must support it
1497 * for Winelib applications.
1499 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1503 * Success: handle to an icon
1506 * FIXME: Do we need to resize the bitmaps?
1508 HICON WINAPI CreateIcon(
1509 HINSTANCE hInstance, /* [in] the application's hInstance */
1510 INT nWidth, /* [in] the width of the provided bitmaps */
1511 INT nHeight, /* [in] the height of the provided bitmaps */
1512 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1513 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1514 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1515 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1520 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1521 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1524 iinfo.xHotspot = nWidth / 2;
1525 iinfo.yHotspot = nHeight / 2;
1526 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1527 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1529 hIcon = CreateIconIndirect( &iinfo );
1531 DeleteObject( iinfo.hbmMask );
1532 DeleteObject( iinfo.hbmColor );
1538 /***********************************************************************
1539 * CopyIcon (USER32.@)
1541 HICON WINAPI CopyIcon( HICON hIcon )
1543 struct cursoricon_object *ptrOld, *ptrNew;
1546 if (!(ptrOld = get_icon_ptr( hIcon )))
1548 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1551 if ((hNew = alloc_icon_handle( FALSE, 1 )))
1553 struct cursoricon_frame *frameOld, *frameNew;
1555 ptrNew = get_icon_ptr( hNew );
1556 ptrNew->is_icon = ptrOld->is_icon;
1557 ptrNew->hotspot = ptrOld->hotspot;
1558 if (!(frameOld = get_icon_frame( ptrOld, 0 )))
1560 release_icon_ptr( hIcon, ptrOld );
1561 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1564 if (!(frameNew = get_icon_frame( ptrNew, 0 )))
1566 release_icon_frame( ptrOld, 0, frameOld );
1567 release_icon_ptr( hIcon, ptrOld );
1568 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1571 frameNew->delay = 0;
1572 frameNew->width = frameOld->width;
1573 frameNew->height = frameOld->height;
1574 frameNew->mask = copy_bitmap( frameOld->mask );
1575 frameNew->color = copy_bitmap( frameOld->color );
1576 frameNew->alpha = copy_bitmap( frameOld->alpha );
1577 release_icon_frame( ptrOld, 0, frameOld );
1578 release_icon_frame( ptrNew, 0, frameNew );
1579 release_icon_ptr( hNew, ptrNew );
1581 release_icon_ptr( hIcon, ptrOld );
1582 if (hNew) USER_Driver->pCreateCursorIcon( hNew );
1587 /***********************************************************************
1588 * DestroyIcon (USER32.@)
1590 BOOL WINAPI DestroyIcon( HICON hIcon )
1593 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1595 TRACE_(icon)("%p\n", hIcon );
1599 BOOL shared = (obj->rsrc != NULL);
1600 release_icon_ptr( hIcon, obj );
1601 ret = (GetCursor() != hIcon);
1602 if (!shared) free_icon_handle( hIcon );
1608 /***********************************************************************
1609 * DestroyCursor (USER32.@)
1611 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1613 return DestroyIcon( hCursor );
1616 /***********************************************************************
1617 * DrawIcon (USER32.@)
1619 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1621 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1624 /***********************************************************************
1625 * SetCursor (USER32.@)
1627 * Set the cursor shape.
1630 * A handle to the previous cursor shape.
1632 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1634 struct cursoricon_object *obj;
1639 TRACE("%p\n", hCursor);
1641 SERVER_START_REQ( set_cursor )
1643 req->flags = SET_CURSOR_HANDLE;
1644 req->handle = wine_server_user_handle( hCursor );
1645 if ((ret = !wine_server_call_err( req )))
1647 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1648 show_count = reply->prev_count;
1654 USER_Driver->pSetCursor( show_count >= 0 ? hCursor : 0 );
1656 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1657 release_icon_ptr( hOldCursor, obj );
1661 /***********************************************************************
1662 * ShowCursor (USER32.@)
1664 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1667 int increment = bShow ? 1 : -1;
1670 SERVER_START_REQ( set_cursor )
1672 req->flags = SET_CURSOR_COUNT;
1673 req->show_count = increment;
1674 wine_server_call( req );
1675 cursor = wine_server_ptr_handle( reply->prev_handle );
1676 count = reply->prev_count + increment;
1680 TRACE("%d, count=%d\n", bShow, count );
1682 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1683 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1688 /***********************************************************************
1689 * GetCursor (USER32.@)
1691 HCURSOR WINAPI GetCursor(void)
1695 SERVER_START_REQ( set_cursor )
1698 wine_server_call( req );
1699 ret = wine_server_ptr_handle( reply->prev_handle );
1706 /***********************************************************************
1707 * ClipCursor (USER32.@)
1709 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1714 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1716 SERVER_START_REQ( set_cursor )
1718 req->flags = SET_CURSOR_CLIP;
1719 req->clip_msg = WM_WINE_CLIPCURSOR;
1722 req->clip.left = rect->left;
1723 req->clip.top = rect->top;
1724 req->clip.right = rect->right;
1725 req->clip.bottom = rect->bottom;
1727 if ((ret = !wine_server_call( req )))
1729 new_rect.left = reply->new_clip.left;
1730 new_rect.top = reply->new_clip.top;
1731 new_rect.right = reply->new_clip.right;
1732 new_rect.bottom = reply->new_clip.bottom;
1736 if (ret) USER_Driver->pClipCursor( &new_rect );
1741 /***********************************************************************
1742 * GetClipCursor (USER32.@)
1744 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1748 if (!rect) return FALSE;
1750 SERVER_START_REQ( set_cursor )
1753 if ((ret = !wine_server_call( req )))
1755 rect->left = reply->new_clip.left;
1756 rect->top = reply->new_clip.top;
1757 rect->right = reply->new_clip.right;
1758 rect->bottom = reply->new_clip.bottom;
1766 /***********************************************************************
1767 * SetSystemCursor (USER32.@)
1769 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1771 FIXME("(%p,%08x),stub!\n", hcur, id);
1776 /**********************************************************************
1777 * LookupIconIdFromDirectoryEx (USER32.@)
1779 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1780 INT width, INT height, UINT cFlag )
1782 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1784 if( dir && !dir->idReserved && (dir->idType & 3) )
1786 const CURSORICONDIRENTRY* entry;
1788 const HDC hdc = GetDC(0);
1789 const int depth = (cFlag & LR_MONOCHROME) ?
1790 1 : GetDeviceCaps(hdc, BITSPIXEL);
1794 entry = CURSORICON_FindBestIconRes( dir, width, height, depth, LR_DEFAULTSIZE );
1796 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth, LR_DEFAULTSIZE );
1798 if( entry ) retVal = entry->wResId;
1800 else WARN_(cursor)("invalid resource directory\n");
1804 /**********************************************************************
1805 * LookupIconIdFromDirectory (USER32.@)
1807 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1809 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1812 /***********************************************************************
1813 * LoadCursorW (USER32.@)
1815 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1817 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1819 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1820 LR_SHARED | LR_DEFAULTSIZE );
1823 /***********************************************************************
1824 * LoadCursorA (USER32.@)
1826 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1828 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1830 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1831 LR_SHARED | LR_DEFAULTSIZE );
1834 /***********************************************************************
1835 * LoadCursorFromFileW (USER32.@)
1837 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1839 TRACE("%s\n", debugstr_w(name));
1841 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1842 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1845 /***********************************************************************
1846 * LoadCursorFromFileA (USER32.@)
1848 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1850 TRACE("%s\n", debugstr_a(name));
1852 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1853 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1856 /***********************************************************************
1857 * LoadIconW (USER32.@)
1859 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1861 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1863 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1864 LR_SHARED | LR_DEFAULTSIZE );
1867 /***********************************************************************
1868 * LoadIconA (USER32.@)
1870 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1872 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1874 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1875 LR_SHARED | LR_DEFAULTSIZE );
1878 /**********************************************************************
1879 * GetCursorFrameInfo (USER32.@)
1882 * So far no use has been found for the second parameter, it is currently presumed
1883 * that this parameter is reserved for future use.
1886 * hCursor [I] Handle to cursor for which to retrieve information
1887 * reserved [I] No purpose has been found for this parameter (may be NULL)
1888 * istep [I] The step of the cursor for which to retrieve information
1889 * rate_jiffies [O] Pointer to DWORD that receives the frame-specific delay (cannot be NULL)
1890 * num_steps [O] Pointer to DWORD that receives the number of steps in the cursor (cannot be NULL)
1893 * Success: Handle to a frame of the cursor (specified by istep)
1894 * Failure: NULL cursor (0)
1896 HCURSOR WINAPI GetCursorFrameInfo(HCURSOR hCursor, DWORD reserved, DWORD istep, DWORD *rate_jiffies, DWORD *num_steps)
1898 struct cursoricon_object *ptr;
1902 if (rate_jiffies == NULL || num_steps == NULL) return 0;
1904 if (!(ptr = get_icon_ptr( hCursor ))) return 0;
1906 TRACE("%p => %d %d %p %p\n", hCursor, reserved, istep, rate_jiffies, num_steps);
1908 FIXME("Second parameter non-zero (%d), please report this!\n", reserved);
1910 icon_steps = get_icon_steps(ptr);
1911 if (istep < icon_steps || !ptr->is_ani)
1913 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) ptr;
1914 UINT icon_frames = 1;
1917 icon_frames = ani_icon_data->num_frames;
1918 if (ptr->is_ani && icon_frames > 1)
1919 ret = ani_icon_data->frames[istep];
1922 if (icon_frames == 1)
1927 else if (icon_steps == 1)
1930 *rate_jiffies = ptr->delay;
1932 else if (istep < icon_steps)
1934 struct cursoricon_frame *frame;
1936 *num_steps = icon_steps;
1937 frame = get_icon_frame( ptr, istep );
1938 if (get_icon_steps(ptr) == 1)
1941 *num_steps = get_icon_steps(ptr);
1942 /* If this specific frame does not have a delay then use the global delay */
1943 if (frame->delay == ~0)
1944 *rate_jiffies = ptr->delay;
1946 *rate_jiffies = frame->delay;
1947 release_icon_frame( ptr, istep, frame );
1951 release_icon_ptr( hCursor, ptr );
1956 /**********************************************************************
1957 * GetIconInfo (USER32.@)
1959 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1963 infoW.cbSize = sizeof(infoW);
1964 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
1965 iconinfo->fIcon = infoW.fIcon;
1966 iconinfo->xHotspot = infoW.xHotspot;
1967 iconinfo->yHotspot = infoW.yHotspot;
1968 iconinfo->hbmColor = infoW.hbmColor;
1969 iconinfo->hbmMask = infoW.hbmMask;
1973 /**********************************************************************
1974 * GetIconInfoExA (USER32.@)
1976 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
1980 if (info->cbSize != sizeof(*info))
1982 SetLastError( ERROR_INVALID_PARAMETER );
1985 infoW.cbSize = sizeof(infoW);
1986 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
1987 info->fIcon = infoW.fIcon;
1988 info->xHotspot = infoW.xHotspot;
1989 info->yHotspot = infoW.yHotspot;
1990 info->hbmColor = infoW.hbmColor;
1991 info->hbmMask = infoW.hbmMask;
1992 info->wResID = infoW.wResID;
1993 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
1994 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
1998 /**********************************************************************
1999 * GetIconInfoExW (USER32.@)
2001 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
2003 struct cursoricon_frame *frame;
2004 struct cursoricon_object *ptr;
2008 if (info->cbSize != sizeof(*info))
2010 SetLastError( ERROR_INVALID_PARAMETER );
2013 if (!(ptr = get_icon_ptr( icon )))
2015 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2019 frame = get_icon_frame( ptr, 0 );
2022 release_icon_ptr( icon, ptr );
2023 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2027 TRACE("%p => %dx%d\n", icon, frame->width, frame->height);
2029 info->fIcon = ptr->is_icon;
2030 info->xHotspot = ptr->hotspot.x;
2031 info->yHotspot = ptr->hotspot.y;
2032 info->hbmColor = copy_bitmap( frame->color );
2033 info->hbmMask = copy_bitmap( frame->mask );
2035 info->szModName[0] = 0;
2036 info->szResName[0] = 0;
2039 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
2040 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
2042 if (!info->hbmMask || (!info->hbmColor && frame->color))
2044 DeleteObject( info->hbmMask );
2045 DeleteObject( info->hbmColor );
2048 module = ptr->module;
2049 release_icon_frame( ptr, 0, frame );
2050 release_icon_ptr( icon, ptr );
2051 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
2055 /* copy an icon bitmap, even when it can't be selected into a DC */
2056 /* helper for CreateIconIndirect */
2057 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
2058 HBITMAP src, int width, int height )
2060 HDC hdc = CreateCompatibleDC( 0 );
2062 if (!SelectObject( hdc, src )) /* do it the hard way */
2067 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
2068 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2069 info->bmiHeader.biWidth = width;
2070 info->bmiHeader.biHeight = height;
2071 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
2072 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
2073 info->bmiHeader.biCompression = BI_RGB;
2074 info->bmiHeader.biSizeImage = height * get_dib_width_bytes( width, info->bmiHeader.biBitCount );
2075 info->bmiHeader.biXPelsPerMeter = 0;
2076 info->bmiHeader.biYPelsPerMeter = 0;
2077 info->bmiHeader.biClrUsed = 0;
2078 info->bmiHeader.biClrImportant = 0;
2079 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
2080 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
2081 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
2082 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
2084 HeapFree( GetProcessHeap(), 0, bits );
2085 HeapFree( GetProcessHeap(), 0, info );
2087 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
2092 /**********************************************************************
2093 * CreateIconIndirect (USER32.@)
2095 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2097 BITMAP bmpXor, bmpAnd;
2099 HBITMAP color = 0, mask;
2103 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2104 iconinfo->hbmColor, iconinfo->hbmMask,
2105 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2107 if (!iconinfo->hbmMask) return 0;
2109 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2110 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2111 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2112 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2114 if (iconinfo->hbmColor)
2116 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2117 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2118 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2119 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2121 width = bmpXor.bmWidth;
2122 height = bmpXor.bmHeight;
2123 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1)
2125 color = CreateCompatibleBitmap( screen_dc, width, height );
2126 mask = CreateBitmap( width, height, 1, 1, NULL );
2128 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2132 width = bmpAnd.bmWidth;
2133 height = bmpAnd.bmHeight;
2134 mask = CreateBitmap( width, height, 1, 1, NULL );
2137 hdc = CreateCompatibleDC( 0 );
2138 SelectObject( hdc, mask );
2139 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
2143 SelectObject( hdc, color );
2144 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
2146 else if (iconinfo->hbmColor)
2148 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
2154 hObj = alloc_icon_handle( FALSE, 1 );
2157 struct cursoricon_object *info = get_icon_ptr( hObj );
2158 struct cursoricon_frame *frame;
2160 info->is_icon = iconinfo->fIcon;
2161 frame = get_icon_frame( info, 0 );
2163 frame->width = width;
2164 frame->height = height;
2165 frame->color = color;
2167 frame->alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
2168 release_icon_frame( info, 0, frame );
2171 info->hotspot.x = width / 2;
2172 info->hotspot.y = height / 2;
2176 info->hotspot.x = iconinfo->xHotspot;
2177 info->hotspot.y = iconinfo->yHotspot;
2180 release_icon_ptr( hObj, info );
2181 USER_Driver->pCreateCursorIcon( hObj );
2186 /******************************************************************************
2187 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2190 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2193 * hdc [I] Handle to device context
2194 * x0 [I] X coordinate of upper left corner
2195 * y0 [I] Y coordinate of upper left corner
2196 * hIcon [I] Handle to icon to draw
2197 * cxWidth [I] Width of icon
2198 * cyWidth [I] Height of icon
2199 * istep [I] Index of frame in animated cursor
2200 * hbr [I] Handle to background brush
2201 * flags [I] Icon-drawing flags
2207 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2208 INT cxWidth, INT cyWidth, UINT istep,
2209 HBRUSH hbr, UINT flags )
2211 struct cursoricon_frame *frame;
2212 struct cursoricon_object *ptr;
2213 HDC hdc_dest, hMemDC;
2214 BOOL result = FALSE, DoOffscreen;
2216 COLORREF oldFg, oldBg;
2217 INT x, y, nStretchMode;
2219 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2220 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2222 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2223 if (istep >= get_icon_steps( ptr ))
2225 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
2226 release_icon_ptr( hIcon, ptr );
2229 if (!(frame = get_icon_frame( ptr, istep )))
2231 FIXME_(icon)("Error retrieving icon frame %d\n", istep);
2232 release_icon_ptr( hIcon, ptr );
2235 if (!(hMemDC = CreateCompatibleDC( hdc )))
2237 release_icon_frame( ptr, istep, frame );
2238 release_icon_ptr( hIcon, ptr );
2242 if (flags & DI_NOMIRROR)
2243 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2245 /* Calculate the size of the destination image. */
2248 if (flags & DI_DEFAULTSIZE)
2249 cxWidth = GetSystemMetrics (SM_CXICON);
2251 cxWidth = frame->width;
2255 if (flags & DI_DEFAULTSIZE)
2256 cyWidth = GetSystemMetrics (SM_CYICON);
2258 cyWidth = frame->height;
2261 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2271 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2272 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2274 DeleteDC( hdc_dest );
2277 SelectObject(hdc_dest, hB_off);
2278 FillRect(hdc_dest, &r, hbr);
2288 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2290 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2291 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2293 if (frame->alpha && (flags & DI_IMAGE))
2295 BOOL is_mono = FALSE;
2297 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2300 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2301 is_mono = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel == 1;
2305 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2306 SelectObject( hMemDC, frame->alpha );
2307 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2308 0, 0, frame->width, frame->height,
2309 pixelblend )) goto done;
2313 if (flags & DI_MASK)
2315 SelectObject( hMemDC, frame->mask );
2316 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2317 hMemDC, 0, 0, frame->width, frame->height, SRCAND );
2320 if (flags & DI_IMAGE)
2324 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2325 SelectObject( hMemDC, frame->color );
2326 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2327 hMemDC, 0, 0, frame->width, frame->height, rop );
2331 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2332 SelectObject( hMemDC, frame->mask );
2333 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2334 hMemDC, 0, frame->height, frame->width,
2335 frame->height, rop );
2340 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2342 SetTextColor( hdc, oldFg );
2343 SetBkColor( hdc, oldBg );
2344 SetStretchBltMode (hdc, nStretchMode);
2346 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2347 if (hB_off) DeleteObject(hB_off);
2350 release_icon_frame( ptr, istep, frame );
2351 release_icon_ptr( hIcon, ptr );
2355 /***********************************************************************
2356 * DIB_FixColorsToLoadflags
2358 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2361 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2364 COLORREF c_W, c_S, c_F, c_L, c_C;
2373 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2375 WARN_(resource)("Invalid bitmap\n");
2379 if (bpp > 8) return;
2381 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2389 colors = bmi->bmiHeader.biClrUsed;
2390 if (colors > 256) colors = 256;
2391 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2394 c_W = GetSysColor(COLOR_WINDOW);
2395 c_S = GetSysColor(COLOR_3DSHADOW);
2396 c_F = GetSysColor(COLOR_3DFACE);
2397 c_L = GetSysColor(COLOR_3DLIGHT);
2399 if (loadflags & LR_LOADTRANSPARENT) {
2401 case 1: pix = pix >> 7; break;
2402 case 4: pix = pix >> 4; break;
2405 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2408 if (pix >= colors) {
2409 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2412 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2413 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2414 ptr->rgbBlue = GetBValue(c_W);
2415 ptr->rgbGreen = GetGValue(c_W);
2416 ptr->rgbRed = GetRValue(c_W);
2418 if (loadflags & LR_LOADMAP3DCOLORS)
2419 for (i=0; i<colors; i++) {
2420 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2421 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2422 if (c_C == RGB(128, 128, 128)) {
2423 ptr->rgbRed = GetRValue(c_S);
2424 ptr->rgbGreen = GetGValue(c_S);
2425 ptr->rgbBlue = GetBValue(c_S);
2426 } else if (c_C == RGB(192, 192, 192)) {
2427 ptr->rgbRed = GetRValue(c_F);
2428 ptr->rgbGreen = GetGValue(c_F);
2429 ptr->rgbBlue = GetBValue(c_F);
2430 } else if (c_C == RGB(223, 223, 223)) {
2431 ptr->rgbRed = GetRValue(c_L);
2432 ptr->rgbGreen = GetGValue(c_L);
2433 ptr->rgbBlue = GetBValue(c_L);
2439 /**********************************************************************
2442 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2443 INT desiredx, INT desiredy, UINT loadflags )
2445 HBITMAP hbitmap = 0, orig_bm;
2449 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2453 LONG width, height, new_width, new_height;
2455 DWORD compr_dummy, offbits = 0;
2457 HDC screen_mem_dc = NULL;
2459 if (!(loadflags & LR_LOADFROMFILE))
2463 /* OEM bitmap: try to load the resource from user32.dll */
2464 instance = user32_module;
2467 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2468 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2470 if ((info = LockResource( handle )) == NULL) return 0;
2474 BITMAPFILEHEADER * bmfh;
2476 if (!(ptr = map_fileW( name, NULL ))) return 0;
2477 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2478 bmfh = (BITMAPFILEHEADER *)ptr;
2479 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2481 WARN("Invalid/unsupported bitmap format!\n");
2484 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2487 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2488 &bpp_dummy, &compr_dummy);
2491 WARN("Invalid bitmap format!\n");
2495 size = bitmap_info_size(info, DIB_RGB_COLORS);
2496 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2497 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2499 if (!fix_info || !scaled_info) goto end;
2500 memcpy(fix_info, info, size);
2502 pix = *((LPBYTE)info + size);
2503 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2505 memcpy(scaled_info, fix_info, size);
2508 new_width = desiredx;
2513 new_height = height > 0 ? desiredy : -desiredy;
2515 new_height = height;
2519 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2520 core->bcWidth = new_width;
2521 core->bcHeight = new_height;
2525 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2526 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2527 WARN("Broken BitmapInfoHeader!\n");
2531 scaled_info->bmiHeader.biWidth = new_width;
2532 scaled_info->bmiHeader.biHeight = new_height;
2535 if (new_height < 0) new_height = -new_height;
2537 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2538 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2540 bits = (char *)info + (offbits ? offbits : size);
2542 if (loadflags & LR_CREATEDIBSECTION)
2544 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2545 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2549 if (is_dib_monochrome(fix_info))
2550 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2552 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2555 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2556 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2557 SelectObject(screen_mem_dc, orig_bm);
2560 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2561 HeapFree(GetProcessHeap(), 0, scaled_info);
2562 HeapFree(GetProcessHeap(), 0, fix_info);
2563 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2568 /**********************************************************************
2569 * LoadImageA (USER32.@)
2573 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2574 INT desiredx, INT desiredy, UINT loadflags)
2579 if (IS_INTRESOURCE(name))
2580 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2583 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2584 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2585 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2587 __EXCEPT_PAGE_FAULT {
2588 SetLastError( ERROR_INVALID_PARAMETER );
2592 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2593 HeapFree(GetProcessHeap(), 0, u_name);
2598 /******************************************************************************
2599 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2602 * hinst [I] Handle of instance that contains image
2603 * name [I] Name of image
2604 * type [I] Type of image
2605 * desiredx [I] Desired width
2606 * desiredy [I] Desired height
2607 * loadflags [I] Load flags
2610 * Success: Handle to newly loaded image
2613 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2615 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2616 INT desiredx, INT desiredy, UINT loadflags )
2618 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2619 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2621 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2624 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2627 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2630 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2631 GetDeviceCaps(screen_dc, BITSPIXEL),
2637 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2638 1, TRUE, loadflags);
2643 /******************************************************************************
2644 * CopyImage (USER32.@) Creates new image and copies attributes to it
2647 * hnd [I] Handle to image to copy
2648 * type [I] Type of image to copy
2649 * desiredx [I] Desired width of new image
2650 * desiredy [I] Desired height of new image
2651 * flags [I] Copy flags
2654 * Success: Handle to newly created image
2658 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2659 * all other versions (95/2000/XP have been tested) ignore it.
2662 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2663 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2664 * the copy will have the same depth as the screen.
2665 * The content of the image will only be copied if the bit depth of the
2666 * original image is compatible with the bit depth of the screen, or
2667 * if the source is a DIB section.
2668 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2670 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2671 INT desiredy, UINT flags )
2673 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2674 hnd, type, desiredx, desiredy, flags);
2685 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2686 if (!objSize) return 0;
2687 if ((desiredx < 0) || (desiredy < 0)) return 0;
2689 if (flags & LR_COPYFROMRESOURCE)
2691 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2694 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2695 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2697 /* Allocate memory for a BITMAPINFOHEADER structure and a
2698 color table. The maximum number of colors in a color table
2699 is 256 which corresponds to a bitmap with depth 8.
2700 Bitmaps with higher depths don't have color tables. */
2701 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2704 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2705 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2706 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2707 bi->bmiHeader.biCompression = BI_RGB;
2709 if (flags & LR_CREATEDIBSECTION)
2711 /* Create a DIB section. LR_MONOCHROME is ignored */
2713 HDC dc = CreateCompatibleDC(NULL);
2715 if (objSize == sizeof(DIBSECTION))
2717 /* The source bitmap is a DIB.
2718 Get its attributes to create an exact copy */
2719 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2722 /* Get the color table or the color masks */
2723 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2725 bi->bmiHeader.biWidth = desiredx;
2726 bi->bmiHeader.biHeight = desiredy;
2727 bi->bmiHeader.biSizeImage = 0;
2729 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2734 /* Create a device-dependent bitmap */
2736 BOOL monochrome = (flags & LR_MONOCHROME);
2738 if (objSize == sizeof(DIBSECTION))
2740 /* The source bitmap is a DIB section.
2741 Get its attributes */
2742 HDC dc = CreateCompatibleDC(NULL);
2743 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2744 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2745 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2748 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2750 /* Look if the colors of the DIB are black and white */
2753 (bi->bmiColors[0].rgbRed == 0xff
2754 && bi->bmiColors[0].rgbGreen == 0xff
2755 && bi->bmiColors[0].rgbBlue == 0xff
2756 && bi->bmiColors[0].rgbReserved == 0
2757 && bi->bmiColors[1].rgbRed == 0
2758 && bi->bmiColors[1].rgbGreen == 0
2759 && bi->bmiColors[1].rgbBlue == 0
2760 && bi->bmiColors[1].rgbReserved == 0)
2762 (bi->bmiColors[0].rgbRed == 0
2763 && bi->bmiColors[0].rgbGreen == 0
2764 && bi->bmiColors[0].rgbBlue == 0
2765 && bi->bmiColors[0].rgbReserved == 0
2766 && bi->bmiColors[1].rgbRed == 0xff
2767 && bi->bmiColors[1].rgbGreen == 0xff
2768 && bi->bmiColors[1].rgbBlue == 0xff
2769 && bi->bmiColors[1].rgbReserved == 0);
2772 else if (!monochrome)
2774 monochrome = ds.dsBm.bmBitsPixel == 1;
2779 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2783 HDC screenDC = GetDC(NULL);
2784 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2785 ReleaseDC(NULL, screenDC);
2791 /* Only copy the bitmap if it's a DIB section or if it's
2792 compatible to the screen */
2795 if (objSize == sizeof(DIBSECTION))
2797 copyContents = TRUE;
2801 HDC screenDC = GetDC(NULL);
2802 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2803 ReleaseDC(NULL, screenDC);
2805 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2810 /* The source bitmap may already be selected in a device context,
2811 use GetDIBits/StretchDIBits and not StretchBlt */
2816 dc = CreateCompatibleDC(NULL);
2818 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2819 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2820 bi->bmiHeader.biSizeImage = 0;
2821 bi->bmiHeader.biClrUsed = 0;
2822 bi->bmiHeader.biClrImportant = 0;
2824 /* Fill in biSizeImage */
2825 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2826 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2832 /* Get the image bits of the source bitmap */
2833 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2835 /* Copy it to the destination bitmap */
2836 oldBmp = SelectObject(dc, res);
2837 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2838 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2839 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2840 SelectObject(dc, oldBmp);
2842 HeapFree(GetProcessHeap(), 0, bits);
2848 if (flags & LR_COPYDELETEORG)
2853 HeapFree(GetProcessHeap(), 0, bi);
2859 struct cursoricon_object *icon;
2861 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( screen_dc, BITSPIXEL );
2863 if (flags & LR_DEFAULTSIZE)
2865 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2866 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2869 if (!(icon = get_icon_ptr( hnd ))) return 0;
2871 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2872 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2873 type == IMAGE_CURSOR, flags );
2875 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2876 release_icon_ptr( hnd, icon );
2878 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2886 /******************************************************************************
2887 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2890 * Success: Handle to specified bitmap
2893 HBITMAP WINAPI LoadBitmapW(
2894 HINSTANCE instance, /* [in] Handle to application instance */
2895 LPCWSTR name) /* [in] Address of bitmap resource name */
2897 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2900 /**********************************************************************
2901 * LoadBitmapA (USER32.@)
2905 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2907 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );