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 HBITMAP color; /* color bitmap */
87 HBITMAP alpha; /* pre-multiplied alpha bitmap for 32-bpp icons */
88 HBITMAP mask; /* mask bitmap (followed by color for 1-bpp icons) */
91 struct cursoricon_object
93 struct user_object obj; /* object header */
94 struct list entry; /* entry in shared icons list */
95 ULONG_PTR param; /* opaque param used by 16-bit code */
96 HMODULE module; /* module for icons loaded from resources */
97 LPWSTR resname; /* resource name for icons loaded from resources */
98 HRSRC rsrc; /* resource for shared icons */
99 BOOL is_icon; /* whether icon or cursor */
103 UINT num_frames; /* number of frames in the icon/cursor */
104 UINT num_steps; /* number of sequence steps in the icon/cursor */
105 UINT delay; /* delay between frames (in jiffies) */
106 struct cursoricon_frame frames[1]; /* icon frame information */
109 static HICON alloc_icon_handle( UINT num_frames )
111 struct cursoricon_object *obj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
112 FIELD_OFFSET( struct cursoricon_object, frames[num_frames] ));
116 obj->num_steps = num_frames; /* changed later for some animated cursors */
117 obj->num_frames = num_frames;
118 return alloc_user_handle( &obj->obj, USER_ICON );
121 static struct cursoricon_object *get_icon_ptr( HICON handle )
123 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
124 if (obj == OBJ_OTHER_PROCESS)
126 WARN( "icon handle %p from other process\n", handle );
132 static void release_icon_ptr( HICON handle, struct cursoricon_object *ptr )
134 release_user_handle_ptr( ptr );
137 static BOOL free_icon_handle( HICON handle )
139 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
141 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
144 ULONG_PTR param = obj->param;
147 assert( !obj->rsrc ); /* shared icons can't be freed */
149 for (i=0; i<obj->num_frames; i++)
151 if (obj->frames[i].alpha) DeleteObject( obj->frames[i].alpha );
152 if (obj->frames[i].color) DeleteObject( obj->frames[i].color );
153 DeleteObject( obj->frames[i].mask );
155 if (!IS_INTRESOURCE( obj->resname )) HeapFree( GetProcessHeap(), 0, obj->resname );
156 HeapFree( GetProcessHeap(), 0, obj );
157 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
158 USER_Driver->pDestroyCursorIcon( handle );
164 ULONG_PTR get_icon_param( HICON handle )
167 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
169 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
173 release_user_handle_ptr( obj );
178 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
181 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
183 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
188 release_user_handle_ptr( obj );
194 /***********************************************************************
197 * Helper function to map a file to memory:
199 * [RETURN] ptr - pointer to mapped file
200 * [RETURN] filesize - pointer size of file to be stored if not NULL
202 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
204 HANDLE hFile, hMapping;
207 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
208 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
209 if (hFile != INVALID_HANDLE_VALUE)
211 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
214 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
215 CloseHandle( hMapping );
217 *filesize = GetFileSize( hFile, NULL );
219 CloseHandle( hFile );
225 /***********************************************************************
226 * get_dib_width_bytes
228 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
230 static int get_dib_width_bytes( int width, int depth )
236 case 1: words = (width + 31) / 32; break;
237 case 4: words = (width + 7) / 8; break;
238 case 8: words = (width + 3) / 4; break;
240 case 16: words = (width + 1) / 2; break;
241 case 24: words = (width * 3 + 3)/4; break;
243 WARN("(%d): Unsupported depth\n", depth );
252 /***********************************************************************
255 * Return the size of the bitmap info structure including color table.
257 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
259 unsigned int colors, size, masks = 0;
261 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
263 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
264 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
265 return sizeof(BITMAPCOREHEADER) + colors *
266 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
268 else /* assume BITMAPINFOHEADER */
270 colors = info->bmiHeader.biClrUsed;
271 if (colors > 256) /* buffer overflow otherwise */
273 if (!colors && (info->bmiHeader.biBitCount <= 8))
274 colors = 1 << info->bmiHeader.biBitCount;
275 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
276 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
277 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
282 /***********************************************************************
285 * Helper function to duplicate a bitmap.
287 static HBITMAP copy_bitmap( HBITMAP bitmap )
290 HBITMAP new_bitmap = 0;
293 if (!bitmap) return 0;
294 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
296 if ((src = CreateCompatibleDC( 0 )) && (dst = CreateCompatibleDC( 0 )))
298 SelectObject( src, bitmap );
299 if ((new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight )))
301 SelectObject( dst, new_bitmap );
302 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
311 /***********************************************************************
314 * Returns whether a DIB can be converted to a monochrome DDB.
316 * A DIB can be converted if its color table contains only black and
317 * white. Black must be the first color in the color table.
319 * Note : If the first color in the color table is white followed by
320 * black, we can't convert it to a monochrome DDB with
321 * SetDIBits, because black and white would be inverted.
323 static BOOL is_dib_monochrome( const BITMAPINFO* info )
325 if (info->bmiHeader.biBitCount != 1) return FALSE;
327 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
329 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
331 /* Check if the first color is black */
332 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
336 /* Check if the second color is white */
337 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
338 && (rgb->rgbtBlue == 0xff));
342 else /* assume BITMAPINFOHEADER */
344 const RGBQUAD *rgb = info->bmiColors;
346 /* Check if the first color is black */
347 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
348 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
352 /* Check if the second color is white */
353 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
354 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
360 /***********************************************************************
363 * Get the info from a bitmap header.
364 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
366 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
367 LONG *height, WORD *bpp, DWORD *compr )
369 if (header->biSize == sizeof(BITMAPCOREHEADER))
371 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
372 *width = core->bcWidth;
373 *height = core->bcHeight;
374 *bpp = core->bcBitCount;
378 else if (header->biSize == sizeof(BITMAPINFOHEADER) ||
379 header->biSize == sizeof(BITMAPV4HEADER) ||
380 header->biSize == sizeof(BITMAPV5HEADER))
382 *width = header->biWidth;
383 *height = header->biHeight;
384 *bpp = header->biBitCount;
385 *compr = header->biCompression;
388 WARN("unknown/wrong size (%u) for header\n", header->biSize);
392 /**********************************************************************
395 BOOL get_icon_size( HICON handle, SIZE *size )
397 struct cursoricon_object *info;
399 if (!(info = get_icon_ptr( handle ))) return FALSE;
400 size->cx = info->width;
401 size->cy = info->height;
402 release_icon_ptr( handle, info );
407 * The following macro functions account for the irregularities of
408 * accessing cursor and icon resources in files and resource entries.
410 typedef BOOL (*fnGetCIEntry)( LPCVOID dir, int n,
411 int *width, int *height, int *bits );
413 /**********************************************************************
414 * CURSORICON_FindBestIcon
416 * Find the icon closest to the requested size and bit depth.
418 static int CURSORICON_FindBestIcon( LPCVOID dir, fnGetCIEntry get_entry,
419 int width, int height, int depth, UINT loadflags )
421 int i, cx, cy, bits, bestEntry = -1;
422 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
423 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
426 iTotalDiff = 0xFFFFFFFF;
427 iColorDiff = 0xFFFFFFFF;
429 if (loadflags & LR_DEFAULTSIZE)
431 if (!width) width = GetSystemMetrics( SM_CXICON );
432 if (!height) height = GetSystemMetrics( SM_CYICON );
434 else if (!width && !height)
436 /* use the size of the first entry */
437 if (!get_entry( dir, 0, &width, &height, &bits )) return -1;
441 for ( i = 0; iTotalDiff && get_entry( dir, i, &cx, &cy, &bits ); i++ )
443 iTempXDiff = abs(width - cx);
444 iTempYDiff = abs(height - cy);
446 if(iTotalDiff > (iTempXDiff + iTempYDiff))
450 iTotalDiff = iXDiff + iYDiff;
454 /* Find Best Colors for Best Fit */
455 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
457 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
459 iTempColorDiff = abs(depth - bits);
460 if(iColorDiff > iTempColorDiff)
463 iColorDiff = iTempColorDiff;
471 static BOOL CURSORICON_GetResIconEntry( LPCVOID dir, int n,
472 int *width, int *height, int *bits )
474 const CURSORICONDIR *resdir = dir;
475 const ICONRESDIR *icon;
477 if ( resdir->idCount <= n )
479 icon = &resdir->idEntries[n].ResInfo.icon;
480 *width = icon->bWidth;
481 *height = icon->bHeight;
482 *bits = resdir->idEntries[n].wBitCount;
486 /**********************************************************************
487 * CURSORICON_FindBestCursor
489 * Find the cursor closest to the requested size.
491 * FIXME: parameter 'color' ignored.
493 static int CURSORICON_FindBestCursor( LPCVOID dir, fnGetCIEntry get_entry,
494 int width, int height, int depth, UINT loadflags )
496 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
498 if (loadflags & LR_DEFAULTSIZE)
500 if (!width) width = GetSystemMetrics( SM_CXCURSOR );
501 if (!height) height = GetSystemMetrics( SM_CYCURSOR );
503 else if (!width && !height)
505 /* use the first entry */
506 if (!get_entry( dir, 0, &width, &height, &bits )) return -1;
510 /* Double height to account for AND and XOR masks */
514 /* First find the largest one smaller than or equal to the requested size*/
516 maxwidth = maxheight = 0;
517 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
519 if ((cx <= width) && (cy <= height) &&
520 (cx > maxwidth) && (cy > maxheight))
527 if (bestEntry != -1) return bestEntry;
529 /* Now find the smallest one larger than the requested size */
531 maxwidth = maxheight = 255;
532 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
534 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
545 static BOOL CURSORICON_GetResCursorEntry( LPCVOID dir, int n,
546 int *width, int *height, int *bits )
548 const CURSORICONDIR *resdir = dir;
549 const CURSORDIR *cursor;
551 if ( resdir->idCount <= n )
553 cursor = &resdir->idEntries[n].ResInfo.cursor;
554 *width = cursor->wWidth;
555 *height = cursor->wHeight;
556 *bits = resdir->idEntries[n].wBitCount;
560 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir,
561 int width, int height, int depth,
566 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
567 width, height, depth, loadflags );
570 return &dir->idEntries[n];
573 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir,
574 int width, int height, int depth,
577 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
578 width, height, depth, loadflags );
581 return &dir->idEntries[n];
584 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, int n,
585 int *width, int *height, int *bits )
587 const CURSORICONFILEDIR *filedir = dir;
588 const CURSORICONFILEDIRENTRY *entry;
589 const BITMAPINFOHEADER *info;
591 if ( filedir->idCount <= n )
593 entry = &filedir->idEntries[n];
594 /* FIXME: check against file size */
595 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
596 *width = entry->bWidth;
597 *height = entry->bHeight;
598 *bits = info->biBitCount;
602 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir,
603 int width, int height, int depth,
606 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
607 width, height, depth, loadflags );
610 return &dir->idEntries[n];
613 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir,
614 int width, int height, int depth,
617 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
618 width, height, depth, loadflags );
621 return &dir->idEntries[n];
624 /***********************************************************************
627 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
630 BOOL has_alpha = FALSE;
631 const unsigned char *ptr = bits;
633 if (info->bmiHeader.biBitCount != 32) return FALSE;
634 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
635 if ((has_alpha = (ptr[3] != 0))) break;
639 /***********************************************************************
640 * create_alpha_bitmap
642 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
644 static HBITMAP create_alpha_bitmap( HBITMAP color, HBITMAP mask,
645 const BITMAPINFO *src_info, const void *color_bits )
648 BITMAPINFO *info = NULL;
655 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
656 if (bm.bmBitsPixel != 32) return 0;
658 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
659 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
660 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
661 info->bmiHeader.biWidth = bm.bmWidth;
662 info->bmiHeader.biHeight = -bm.bmHeight;
663 info->bmiHeader.biPlanes = 1;
664 info->bmiHeader.biBitCount = 32;
665 info->bmiHeader.biCompression = BI_RGB;
666 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
667 info->bmiHeader.biXPelsPerMeter = 0;
668 info->bmiHeader.biYPelsPerMeter = 0;
669 info->bmiHeader.biClrUsed = 0;
670 info->bmiHeader.biClrImportant = 0;
671 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
675 SelectObject( hdc, alpha );
676 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
677 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
678 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
683 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
684 if (!bmi_has_alpha( info, bits ))
686 DeleteObject( alpha );
692 /* pre-multiply by alpha */
693 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
695 unsigned int alpha = ptr[3];
696 ptr[0] = ptr[0] * alpha / 255;
697 ptr[1] = ptr[1] * alpha / 255;
698 ptr[2] = ptr[2] * alpha / 255;
703 HeapFree( GetProcessHeap(), 0, info );
708 /***********************************************************************
709 * create_icon_bitmaps
711 * Create the color, mask and alpha bitmaps from the DIB info.
713 static BOOL create_icon_bitmaps( const BITMAPINFO *bmi, int width, int height,
714 HBITMAP *color, HBITMAP *mask, HBITMAP *alpha )
716 BOOL monochrome = is_dib_monochrome( bmi );
717 unsigned int size = bitmap_info_size( bmi, DIB_RGB_COLORS );
719 const void *color_bits, *mask_bits;
723 if (!(info = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
725 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
727 memcpy( info, bmi, size );
728 info->bmiHeader.biHeight /= 2;
730 color_bits = (const char*)bmi + size;
731 mask_bits = (const char*)color_bits +
732 get_dib_width_bytes( bmi->bmiHeader.biWidth,
733 bmi->bmiHeader.biBitCount ) * abs(info->bmiHeader.biHeight);
738 if (!(*mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
741 /* copy color data into second half of mask bitmap */
742 SelectObject( hdc, *mask );
743 StretchDIBits( hdc, 0, height, width, height,
744 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
745 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
749 if (!(*mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
750 if (!(*color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
751 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
753 DeleteObject( *mask );
756 SelectObject( hdc, *color );
757 StretchDIBits( hdc, 0, 0, width, height,
758 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
759 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
761 if (bmi_has_alpha( info, color_bits ))
762 *alpha = create_alpha_bitmap( *color, *mask, info, color_bits );
764 /* convert info to monochrome to copy the mask */
765 info->bmiHeader.biBitCount = 1;
766 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
768 RGBQUAD *rgb = info->bmiColors;
770 info->bmiHeader.biClrUsed = info->bmiHeader.biClrImportant = 2;
771 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
772 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
773 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
777 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)info) + 1);
779 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
780 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
784 SelectObject( hdc, *mask );
785 StretchDIBits( hdc, 0, 0, width, height,
786 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
787 mask_bits, info, DIB_RGB_COLORS, SRCCOPY );
792 HeapFree( GetProcessHeap(), 0, info );
796 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi, HMODULE module, LPCWSTR resname, HRSRC rsrc,
797 POINT hotspot, BOOL bIcon, INT width, INT height, UINT cFlag )
800 HBITMAP color = 0, mask = 0, alpha = 0;
803 /* Check bitmap header */
805 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
806 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
807 bmi->bmiHeader.biCompression != BI_RGB) )
809 WARN_(cursor)("\tinvalid resource bitmap header.\n");
813 if (cFlag & LR_DEFAULTSIZE)
815 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
816 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
820 if (!width) width = bmi->bmiHeader.biWidth;
821 if (!height) height = bmi->bmiHeader.biHeight/2;
823 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
824 (bmi->bmiHeader.biWidth != width);
826 /* Scale the hotspot */
829 hotspot.x = width / 2;
830 hotspot.y = height / 2;
834 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
835 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
838 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
839 if (!screen_dc) return 0;
841 if (!create_icon_bitmaps( bmi, width, height, &color, &mask, &alpha )) return 0;
843 hObj = alloc_icon_handle(1);
846 struct cursoricon_object *info = get_icon_ptr( hObj );
848 info->is_icon = bIcon;
849 info->module = module;
850 info->hotspot = hotspot;
852 info->height = height;
853 info->frames[0].color = color;
854 info->frames[0].mask = mask;
855 info->frames[0].alpha = alpha;
856 if (!IS_INTRESOURCE(resname))
858 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
859 if (info->resname) strcpyW( info->resname, resname );
861 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
863 if (module && (cFlag & LR_SHARED))
866 list_add_head( &icon_cache, &info->entry );
868 release_icon_ptr( hObj, info );
869 USER_Driver->pCreateCursorIcon( hObj );
873 DeleteObject( color );
874 DeleteObject( alpha );
875 DeleteObject( mask );
881 /**********************************************************************
882 * .ANI cursor support
884 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
885 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
886 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
888 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
889 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
890 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
891 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
892 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
893 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
894 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
896 #define ANI_FLAG_ICON 0x1
897 #define ANI_FLAG_SEQUENCE 0x2
913 const unsigned char *data;
916 static void dump_ani_header( const ani_header *header )
918 TRACE(" header size: %d\n", header->header_size);
919 TRACE(" frames: %d\n", header->num_frames);
920 TRACE(" steps: %d\n", header->num_steps);
921 TRACE(" width: %d\n", header->width);
922 TRACE(" height: %d\n", header->height);
923 TRACE(" bpp: %d\n", header->bpp);
924 TRACE(" planes: %d\n", header->num_planes);
925 TRACE(" display rate: %d\n", header->display_rate);
926 TRACE(" flags: 0x%08x\n", header->flags);
948 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
950 const unsigned char *ptr = parent_chunk->data;
951 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
953 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
957 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
958 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
960 ptr += sizeof(DWORD);
961 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
962 ptr += sizeof(DWORD);
963 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
969 ptr += sizeof(DWORD);
970 ptr += (*(const DWORD *)ptr + 1) & ~1;
971 ptr += sizeof(DWORD);
979 * RIFF:'ACON' RIFF chunk
980 * |- CHUNK:'anih' Header
981 * |- CHUNK:'seq ' Sequence information (optional)
982 * \- LIST:'fram' Frame list
983 * |- CHUNK:icon Cursor frames
988 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
989 INT width, INT height, INT depth, UINT loadflags )
991 struct cursoricon_object *info;
992 ani_header header = {0};
996 riff_chunk_t root_chunk = { bits_size, bits };
997 riff_chunk_t ACON_chunk = {0};
998 riff_chunk_t anih_chunk = {0};
999 riff_chunk_t fram_chunk = {0};
1000 riff_chunk_t rate_chunk = {0};
1001 const unsigned char *icon_chunk;
1002 const unsigned char *icon_data;
1004 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1006 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1007 if (!ACON_chunk.data)
1009 ERR("Failed to get root chunk.\n");
1013 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1014 if (!anih_chunk.data)
1016 ERR("Failed to get 'anih' chunk.\n");
1019 memcpy( &header, anih_chunk.data, sizeof(header) );
1020 dump_ani_header( &header );
1022 if (!(header.flags & ANI_FLAG_ICON))
1024 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1028 if (header.flags & ANI_FLAG_SEQUENCE)
1029 FIXME("Animated icon/cursor sequence data is not currently supported, frames may appear out of sequence.\n");
1031 riff_find_chunk( ANI_rate_ID, 0, &ACON_chunk, &rate_chunk );
1032 if (rate_chunk.data)
1033 FIXME("Animated icon/cursor multiple frame-frate data not currently supported.\n");
1035 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1036 if (!fram_chunk.data)
1038 ERR("Failed to get icon list.\n");
1042 cursor = alloc_icon_handle( header.num_frames );
1043 if (!cursor) return 0;
1045 info = get_icon_ptr( cursor );
1046 info->is_icon = FALSE;
1047 if (header.num_steps > header.num_frames)
1049 FIXME("More steps than frames and sequence-based cursors not yet supported.\n");
1050 info->num_steps = header.num_frames;
1053 info->num_steps = header.num_steps;
1055 /* The .ANI stores the display rate in jiffies (1/60s) */
1056 info->delay = header.display_rate;
1058 icon_chunk = fram_chunk.data;
1059 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1060 for (i=0; i<header.num_frames; i++)
1062 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1063 struct cursoricon_frame *frame = &info->frames[i];
1064 const CURSORICONFILEDIRENTRY *entry;
1065 const BITMAPINFO *bmi;
1067 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1068 width, height, depth, loadflags );
1070 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1071 info->hotspot.x = entry->xHotspot;
1072 info->hotspot.y = entry->yHotspot;
1073 if (!header.width || !header.height)
1075 header.width = entry->bWidth;
1076 header.height = entry->bHeight;
1079 /* Grab a frame from the animation */
1080 if (!create_icon_bitmaps( bmi, header.width, header.height,
1081 &frame->color, &frame->mask, &frame->alpha ))
1083 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1087 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1088 info->num_frames = 0;
1089 release_icon_ptr( cursor, info );
1090 free_icon_handle( cursor );
1096 /* Advance to the next chunk */
1097 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1098 icon_data = icon_chunk + (2 * sizeof(DWORD));
1101 /* There was an error but we at least decoded the first frame, so just use that frame */
1104 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1105 for (i=1; i<info->num_frames; i++)
1107 if (info->frames[i].mask) DeleteObject( info->frames[i].mask );
1108 if (info->frames[i].color) DeleteObject( info->frames[i].color );
1109 if (info->frames[i].alpha) DeleteObject( info->frames[i].alpha );
1111 info->num_frames = 1;
1112 info->num_steps = 1;
1115 info->width = header.width;
1116 info->height = header.height;
1117 release_icon_ptr( cursor, info );
1123 /**********************************************************************
1124 * CreateIconFromResourceEx (USER32.@)
1126 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1127 * with cbSize parameter as well.
1129 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1130 BOOL bIcon, DWORD dwVersion,
1131 INT width, INT height,
1137 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1138 bits, cbSize, dwVersion, width, height,
1139 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1141 if (!bits) return 0;
1143 if (dwVersion == 0x00020000)
1145 FIXME_(cursor)("\t2.xx resources are not supported\n");
1149 /* Check if the resource is an animated icon/cursor */
1150 if (!memcmp(bits, "RIFF", 4))
1151 return CURSORICON_CreateIconFromANI( bits, cbSize, width, height, 0 /* default depth */, cFlag );
1155 hotspot.x = width / 2;
1156 hotspot.y = height / 2;
1157 bmi = (BITMAPINFO *)bits;
1159 else /* get the hotspot */
1161 SHORT *pt = (SHORT *)bits;
1164 bmi = (BITMAPINFO *)(pt + 2);
1167 return CURSORICON_CreateIconFromBMI( bmi, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1171 /**********************************************************************
1172 * CreateIconFromResource (USER32.@)
1174 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1175 BOOL bIcon, DWORD dwVersion)
1177 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1181 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1182 INT width, INT height, INT depth,
1183 BOOL fCursor, UINT loadflags)
1185 const CURSORICONFILEDIRENTRY *entry;
1186 const CURSORICONFILEDIR *dir;
1192 TRACE("loading %s\n", debugstr_w( filename ));
1194 bits = map_fileW( filename, &filesize );
1198 /* Check for .ani. */
1199 if (memcmp( bits, "RIFF", 4 ) == 0)
1201 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, loadflags );
1205 dir = (const CURSORICONFILEDIR*) bits;
1206 if ( filesize < sizeof(*dir) )
1209 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1213 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth, loadflags );
1215 entry = CURSORICON_FindBestIconFile( dir, width, height, depth, loadflags );
1220 /* check that we don't run off the end of the file */
1221 if ( entry->dwDIBOffset > filesize )
1223 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1226 hotspot.x = entry->xHotspot;
1227 hotspot.y = entry->yHotspot;
1228 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset], NULL, NULL, NULL,
1229 hotspot, !fCursor, width, height, loadflags );
1231 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1232 UnmapViewOfFile( bits );
1236 /**********************************************************************
1239 * Load a cursor or icon from resource or file.
1241 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1242 INT width, INT height, INT depth,
1243 BOOL fCursor, UINT loadflags)
1248 const CURSORICONDIR *dir;
1249 const CURSORICONDIRENTRY *dirEntry;
1254 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1255 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1257 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1258 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1260 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1262 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1263 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1265 /* Get directory resource ID */
1267 if (!(hRsrc = FindResourceW( hInstance, name,
1268 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1271 /* Find the best entry in the directory */
1273 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1274 if (!(dir = LockResource( handle ))) return 0;
1276 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth, loadflags );
1278 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth, loadflags );
1279 if (!dirEntry) return 0;
1280 wResId = dirEntry->wResId;
1281 FreeResource( handle );
1283 /* Load the resource */
1285 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1286 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1288 /* If shared icon, check whether it was already loaded */
1289 if (loadflags & LR_SHARED)
1291 struct cursoricon_object *ptr;
1294 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1296 if (ptr->module != hInstance) continue;
1297 if (ptr->rsrc != hRsrc) continue;
1298 hIcon = ptr->obj.handle;
1302 if (hIcon) return hIcon;
1305 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1306 bits = LockResource( handle );
1310 hotspot.x = width / 2;
1311 hotspot.y = height / 2;
1313 else /* get the hotspot */
1315 SHORT *pt = (SHORT *)bits;
1318 bits += 2 * sizeof(SHORT);
1320 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)bits, hInstance, name, hRsrc,
1321 hotspot, !fCursor, width, height, loadflags );
1322 FreeResource( handle );
1327 /***********************************************************************
1328 * CreateCursor (USER32.@)
1330 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1331 INT xHotSpot, INT yHotSpot,
1332 INT nWidth, INT nHeight,
1333 LPCVOID lpANDbits, LPCVOID lpXORbits )
1338 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1339 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1342 info.xHotspot = xHotSpot;
1343 info.yHotspot = yHotSpot;
1344 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1345 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1346 hCursor = CreateIconIndirect( &info );
1347 DeleteObject( info.hbmMask );
1348 DeleteObject( info.hbmColor );
1353 /***********************************************************************
1354 * CreateIcon (USER32.@)
1356 * Creates an icon based on the specified bitmaps. The bitmaps must be
1357 * provided in a device dependent format and will be resized to
1358 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1359 * depth. The provided bitmaps must be top-down bitmaps.
1360 * Although Windows does not support 15bpp(*) this API must support it
1361 * for Winelib applications.
1363 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1367 * Success: handle to an icon
1370 * FIXME: Do we need to resize the bitmaps?
1372 HICON WINAPI CreateIcon(
1373 HINSTANCE hInstance, /* [in] the application's hInstance */
1374 INT nWidth, /* [in] the width of the provided bitmaps */
1375 INT nHeight, /* [in] the height of the provided bitmaps */
1376 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1377 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1378 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1379 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1384 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1385 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1388 iinfo.xHotspot = nWidth / 2;
1389 iinfo.yHotspot = nHeight / 2;
1390 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1391 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1393 hIcon = CreateIconIndirect( &iinfo );
1395 DeleteObject( iinfo.hbmMask );
1396 DeleteObject( iinfo.hbmColor );
1402 /***********************************************************************
1403 * CopyIcon (USER32.@)
1405 HICON WINAPI CopyIcon( HICON hIcon )
1407 struct cursoricon_object *ptrOld, *ptrNew;
1410 if (!(ptrOld = get_icon_ptr( hIcon )))
1412 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1415 if ((hNew = alloc_icon_handle(1)))
1417 ptrNew = get_icon_ptr( hNew );
1418 ptrNew->is_icon = ptrOld->is_icon;
1419 ptrNew->width = ptrOld->width;
1420 ptrNew->height = ptrOld->height;
1421 ptrNew->hotspot = ptrOld->hotspot;
1422 ptrNew->frames[0].mask = copy_bitmap( ptrOld->frames[0].mask );
1423 ptrNew->frames[0].color = copy_bitmap( ptrOld->frames[0].color );
1424 ptrNew->frames[0].alpha = copy_bitmap( ptrOld->frames[0].alpha );
1425 release_icon_ptr( hNew, ptrNew );
1427 release_icon_ptr( hIcon, ptrOld );
1428 if (hNew) USER_Driver->pCreateCursorIcon( hNew );
1433 /***********************************************************************
1434 * DestroyIcon (USER32.@)
1436 BOOL WINAPI DestroyIcon( HICON hIcon )
1439 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1441 TRACE_(icon)("%p\n", hIcon );
1445 BOOL shared = (obj->rsrc != NULL);
1446 release_icon_ptr( hIcon, obj );
1447 ret = (GetCursor() != hIcon);
1448 if (!shared) free_icon_handle( hIcon );
1454 /***********************************************************************
1455 * DestroyCursor (USER32.@)
1457 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1459 return DestroyIcon( hCursor );
1462 /***********************************************************************
1463 * DrawIcon (USER32.@)
1465 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1467 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1470 /***********************************************************************
1471 * SetCursor (USER32.@)
1473 * Set the cursor shape.
1476 * A handle to the previous cursor shape.
1478 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1480 struct cursoricon_object *obj;
1485 TRACE("%p\n", hCursor);
1487 SERVER_START_REQ( set_cursor )
1489 req->flags = SET_CURSOR_HANDLE;
1490 req->handle = wine_server_user_handle( hCursor );
1491 if ((ret = !wine_server_call_err( req )))
1493 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1494 show_count = reply->prev_count;
1501 /* Change the cursor shape only if it is visible */
1502 if (show_count >= 0 && hOldCursor != hCursor) USER_Driver->pSetCursor( hCursor );
1504 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1505 release_icon_ptr( hOldCursor, obj );
1509 /***********************************************************************
1510 * ShowCursor (USER32.@)
1512 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1515 int increment = bShow ? 1 : -1;
1518 SERVER_START_REQ( set_cursor )
1520 req->flags = SET_CURSOR_COUNT;
1521 req->show_count = increment;
1522 wine_server_call( req );
1523 cursor = wine_server_ptr_handle( reply->prev_handle );
1524 count = reply->prev_count + increment;
1528 TRACE("%d, count=%d\n", bShow, count );
1530 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1531 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1536 /***********************************************************************
1537 * GetCursor (USER32.@)
1539 HCURSOR WINAPI GetCursor(void)
1543 SERVER_START_REQ( set_cursor )
1546 wine_server_call( req );
1547 ret = wine_server_ptr_handle( reply->prev_handle );
1554 /***********************************************************************
1555 * ClipCursor (USER32.@)
1557 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1562 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1564 SERVER_START_REQ( set_cursor )
1566 req->flags = SET_CURSOR_CLIP;
1569 req->clip.left = rect->left;
1570 req->clip.top = rect->top;
1571 req->clip.right = rect->right;
1572 req->clip.bottom = rect->bottom;
1574 if ((ret = !wine_server_call( req )))
1576 new_rect.left = reply->new_clip.left;
1577 new_rect.top = reply->new_clip.top;
1578 new_rect.right = reply->new_clip.right;
1579 new_rect.bottom = reply->new_clip.bottom;
1583 if (ret) USER_Driver->pClipCursor( &new_rect );
1588 /***********************************************************************
1589 * GetClipCursor (USER32.@)
1591 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1595 if (!rect) return FALSE;
1597 SERVER_START_REQ( set_cursor )
1600 if ((ret = !wine_server_call( req )))
1602 rect->left = reply->new_clip.left;
1603 rect->top = reply->new_clip.top;
1604 rect->right = reply->new_clip.right;
1605 rect->bottom = reply->new_clip.bottom;
1613 /***********************************************************************
1614 * SetSystemCursor (USER32.@)
1616 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1618 FIXME("(%p,%08x),stub!\n", hcur, id);
1623 /**********************************************************************
1624 * LookupIconIdFromDirectoryEx (USER32.@)
1626 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1627 INT width, INT height, UINT cFlag )
1629 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1631 if( dir && !dir->idReserved && (dir->idType & 3) )
1633 const CURSORICONDIRENTRY* entry;
1635 const HDC hdc = GetDC(0);
1636 const int depth = (cFlag & LR_MONOCHROME) ?
1637 1 : GetDeviceCaps(hdc, BITSPIXEL);
1641 entry = CURSORICON_FindBestIconRes( dir, width, height, depth, LR_DEFAULTSIZE );
1643 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth, LR_DEFAULTSIZE );
1645 if( entry ) retVal = entry->wResId;
1647 else WARN_(cursor)("invalid resource directory\n");
1651 /**********************************************************************
1652 * LookupIconIdFromDirectory (USER32.@)
1654 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1656 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1659 /***********************************************************************
1660 * LoadCursorW (USER32.@)
1662 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1664 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1666 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1667 LR_SHARED | LR_DEFAULTSIZE );
1670 /***********************************************************************
1671 * LoadCursorA (USER32.@)
1673 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1675 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1677 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1678 LR_SHARED | LR_DEFAULTSIZE );
1681 /***********************************************************************
1682 * LoadCursorFromFileW (USER32.@)
1684 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1686 TRACE("%s\n", debugstr_w(name));
1688 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1689 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1692 /***********************************************************************
1693 * LoadCursorFromFileA (USER32.@)
1695 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1697 TRACE("%s\n", debugstr_a(name));
1699 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1700 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1703 /***********************************************************************
1704 * LoadIconW (USER32.@)
1706 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1708 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1710 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1711 LR_SHARED | LR_DEFAULTSIZE );
1714 /***********************************************************************
1715 * LoadIconA (USER32.@)
1717 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1719 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1721 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1722 LR_SHARED | LR_DEFAULTSIZE );
1725 /**********************************************************************
1726 * GetCursorFrameInfo (USER32.@)
1728 HCURSOR WINAPI GetCursorFrameInfo(HCURSOR hCursor, DWORD unk1, DWORD istep, DWORD *rate_jiffies, DWORD *num_steps)
1730 struct cursoricon_object *ptr;
1733 if (rate_jiffies == NULL || num_steps == NULL) return 0;
1735 if (!(ptr = get_icon_ptr( hCursor ))) return 0;
1737 FIXME("semi-stub! %p => %d %d %p %p\n", hCursor, unk1, istep, rate_jiffies, num_steps);
1739 /* Important Note: Sequences are not currently supported, so this implementation
1740 * will not properly handle all cases. */
1741 if (istep < ptr->num_steps || ptr->num_frames == 1)
1744 if (ptr->num_frames == 1)
1751 if (ptr->num_steps == 1)
1754 *num_steps = ptr->num_steps;
1755 *rate_jiffies = ptr->delay;
1759 release_icon_ptr( hCursor, ptr );
1764 /**********************************************************************
1765 * GetIconInfo (USER32.@)
1767 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1771 infoW.cbSize = sizeof(infoW);
1772 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
1773 iconinfo->fIcon = infoW.fIcon;
1774 iconinfo->xHotspot = infoW.xHotspot;
1775 iconinfo->yHotspot = infoW.yHotspot;
1776 iconinfo->hbmColor = infoW.hbmColor;
1777 iconinfo->hbmMask = infoW.hbmMask;
1781 /**********************************************************************
1782 * GetIconInfoExA (USER32.@)
1784 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
1788 if (info->cbSize != sizeof(*info))
1790 SetLastError( ERROR_INVALID_PARAMETER );
1793 infoW.cbSize = sizeof(infoW);
1794 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
1795 info->fIcon = infoW.fIcon;
1796 info->xHotspot = infoW.xHotspot;
1797 info->yHotspot = infoW.yHotspot;
1798 info->hbmColor = infoW.hbmColor;
1799 info->hbmMask = infoW.hbmMask;
1800 info->wResID = infoW.wResID;
1801 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
1802 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
1806 /**********************************************************************
1807 * GetIconInfoExW (USER32.@)
1809 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
1811 struct cursoricon_object *ptr;
1815 if (info->cbSize != sizeof(*info))
1817 SetLastError( ERROR_INVALID_PARAMETER );
1820 if (!(ptr = get_icon_ptr( icon )))
1822 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1826 TRACE("%p => %dx%d\n", icon, ptr->width, ptr->height);
1828 info->fIcon = ptr->is_icon;
1829 info->xHotspot = ptr->hotspot.x;
1830 info->yHotspot = ptr->hotspot.y;
1831 info->hbmColor = copy_bitmap( ptr->frames[0].color );
1832 info->hbmMask = copy_bitmap( ptr->frames[0].mask );
1834 info->szModName[0] = 0;
1835 info->szResName[0] = 0;
1838 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
1839 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
1841 if (!info->hbmMask || (!info->hbmColor && ptr->frames[0].color))
1843 DeleteObject( info->hbmMask );
1844 DeleteObject( info->hbmColor );
1847 module = ptr->module;
1848 release_icon_ptr( icon, ptr );
1849 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
1853 /* copy an icon bitmap, even when it can't be selected into a DC */
1854 /* helper for CreateIconIndirect */
1855 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
1856 HBITMAP src, int width, int height )
1858 HDC hdc = CreateCompatibleDC( 0 );
1860 if (!SelectObject( hdc, src )) /* do it the hard way */
1865 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
1866 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1867 info->bmiHeader.biWidth = width;
1868 info->bmiHeader.biHeight = height;
1869 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
1870 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
1871 info->bmiHeader.biCompression = BI_RGB;
1872 info->bmiHeader.biSizeImage = height * get_dib_width_bytes( width, info->bmiHeader.biBitCount );
1873 info->bmiHeader.biXPelsPerMeter = 0;
1874 info->bmiHeader.biYPelsPerMeter = 0;
1875 info->bmiHeader.biClrUsed = 0;
1876 info->bmiHeader.biClrImportant = 0;
1877 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
1878 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
1879 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
1880 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
1882 HeapFree( GetProcessHeap(), 0, bits );
1883 HeapFree( GetProcessHeap(), 0, info );
1885 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
1890 /**********************************************************************
1891 * CreateIconIndirect (USER32.@)
1893 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1895 BITMAP bmpXor, bmpAnd;
1897 HBITMAP color = 0, mask;
1901 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1902 iconinfo->hbmColor, iconinfo->hbmMask,
1903 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1905 if (!iconinfo->hbmMask) return 0;
1907 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1908 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1909 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1910 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1912 if (iconinfo->hbmColor)
1914 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1915 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1916 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
1917 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
1919 width = bmpXor.bmWidth;
1920 height = bmpXor.bmHeight;
1921 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1)
1923 color = CreateCompatibleBitmap( screen_dc, width, height );
1924 mask = CreateBitmap( width, height, 1, 1, NULL );
1926 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
1930 width = bmpAnd.bmWidth;
1931 height = bmpAnd.bmHeight;
1932 mask = CreateBitmap( width, height, 1, 1, NULL );
1935 hdc = CreateCompatibleDC( 0 );
1936 SelectObject( hdc, mask );
1937 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
1941 SelectObject( hdc, color );
1942 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
1944 else if (iconinfo->hbmColor)
1946 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
1952 hObj = alloc_icon_handle(1);
1955 struct cursoricon_object *info = get_icon_ptr( hObj );
1957 info->is_icon = iconinfo->fIcon;
1958 info->width = width;
1959 info->height = height;
1960 info->frames[0].color = color;
1961 info->frames[0].mask = mask;
1962 info->frames[0].alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
1965 info->hotspot.x = width / 2;
1966 info->hotspot.y = height / 2;
1970 info->hotspot.x = iconinfo->xHotspot;
1971 info->hotspot.y = iconinfo->yHotspot;
1974 release_icon_ptr( hObj, info );
1975 USER_Driver->pCreateCursorIcon( hObj );
1980 /******************************************************************************
1981 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1984 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1987 * hdc [I] Handle to device context
1988 * x0 [I] X coordinate of upper left corner
1989 * y0 [I] Y coordinate of upper left corner
1990 * hIcon [I] Handle to icon to draw
1991 * cxWidth [I] Width of icon
1992 * cyWidth [I] Height of icon
1993 * istep [I] Index of frame in animated cursor
1994 * hbr [I] Handle to background brush
1995 * flags [I] Icon-drawing flags
2001 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2002 INT cxWidth, INT cyWidth, UINT istep,
2003 HBRUSH hbr, UINT flags )
2005 struct cursoricon_object *ptr;
2006 HDC hdc_dest, hMemDC;
2007 BOOL result = FALSE, DoOffscreen;
2009 COLORREF oldFg, oldBg;
2010 INT x, y, nStretchMode;
2012 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2013 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2015 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2016 if (istep >= ptr->num_steps)
2018 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
2019 release_icon_ptr( hIcon, ptr );
2022 if (!(hMemDC = CreateCompatibleDC( hdc )))
2024 release_icon_ptr( hIcon, ptr );
2028 if (flags & DI_NOMIRROR)
2029 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2031 /* Calculate the size of the destination image. */
2034 if (flags & DI_DEFAULTSIZE)
2035 cxWidth = GetSystemMetrics (SM_CXICON);
2037 cxWidth = ptr->width;
2041 if (flags & DI_DEFAULTSIZE)
2042 cyWidth = GetSystemMetrics (SM_CYICON);
2044 cyWidth = ptr->height;
2047 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2057 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2058 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2060 DeleteDC( hdc_dest );
2063 SelectObject(hdc_dest, hB_off);
2064 FillRect(hdc_dest, &r, hbr);
2074 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2076 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2077 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2079 if (ptr->frames[istep].alpha && (flags & DI_IMAGE))
2081 BOOL is_mono = FALSE;
2083 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2086 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2087 is_mono = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel == 1;
2091 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2092 SelectObject( hMemDC, ptr->frames[istep].alpha );
2093 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2094 0, 0, ptr->width, ptr->height, pixelblend )) goto done;
2098 if (flags & DI_MASK)
2100 SelectObject( hMemDC, ptr->frames[istep].mask );
2101 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2102 hMemDC, 0, 0, ptr->width, ptr->height, SRCAND );
2105 if (flags & DI_IMAGE)
2107 if (ptr->frames[istep].color)
2109 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2110 SelectObject( hMemDC, ptr->frames[istep].color );
2111 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2112 hMemDC, 0, 0, ptr->width, ptr->height, rop );
2116 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2117 SelectObject( hMemDC, ptr->frames[istep].mask );
2118 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2119 hMemDC, 0, ptr->height, ptr->width, ptr->height, rop );
2124 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2126 SetTextColor( hdc, oldFg );
2127 SetBkColor( hdc, oldBg );
2128 SetStretchBltMode (hdc, nStretchMode);
2130 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2131 if (hB_off) DeleteObject(hB_off);
2134 release_icon_ptr( hIcon, ptr );
2138 /***********************************************************************
2139 * DIB_FixColorsToLoadflags
2141 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2144 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2147 COLORREF c_W, c_S, c_F, c_L, c_C;
2156 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2158 WARN_(resource)("Invalid bitmap\n");
2162 if (bpp > 8) return;
2164 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2172 colors = bmi->bmiHeader.biClrUsed;
2173 if (colors > 256) colors = 256;
2174 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2177 c_W = GetSysColor(COLOR_WINDOW);
2178 c_S = GetSysColor(COLOR_3DSHADOW);
2179 c_F = GetSysColor(COLOR_3DFACE);
2180 c_L = GetSysColor(COLOR_3DLIGHT);
2182 if (loadflags & LR_LOADTRANSPARENT) {
2184 case 1: pix = pix >> 7; break;
2185 case 4: pix = pix >> 4; break;
2188 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2191 if (pix >= colors) {
2192 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2195 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2196 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2197 ptr->rgbBlue = GetBValue(c_W);
2198 ptr->rgbGreen = GetGValue(c_W);
2199 ptr->rgbRed = GetRValue(c_W);
2201 if (loadflags & LR_LOADMAP3DCOLORS)
2202 for (i=0; i<colors; i++) {
2203 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2204 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2205 if (c_C == RGB(128, 128, 128)) {
2206 ptr->rgbRed = GetRValue(c_S);
2207 ptr->rgbGreen = GetGValue(c_S);
2208 ptr->rgbBlue = GetBValue(c_S);
2209 } else if (c_C == RGB(192, 192, 192)) {
2210 ptr->rgbRed = GetRValue(c_F);
2211 ptr->rgbGreen = GetGValue(c_F);
2212 ptr->rgbBlue = GetBValue(c_F);
2213 } else if (c_C == RGB(223, 223, 223)) {
2214 ptr->rgbRed = GetRValue(c_L);
2215 ptr->rgbGreen = GetGValue(c_L);
2216 ptr->rgbBlue = GetBValue(c_L);
2222 /**********************************************************************
2225 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2226 INT desiredx, INT desiredy, UINT loadflags )
2228 HBITMAP hbitmap = 0, orig_bm;
2232 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2236 LONG width, height, new_width, new_height;
2238 DWORD compr_dummy, offbits = 0;
2240 HDC screen_mem_dc = NULL;
2242 if (!(loadflags & LR_LOADFROMFILE))
2246 /* OEM bitmap: try to load the resource from user32.dll */
2247 instance = user32_module;
2250 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2251 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2253 if ((info = LockResource( handle )) == NULL) return 0;
2257 BITMAPFILEHEADER * bmfh;
2259 if (!(ptr = map_fileW( name, NULL ))) return 0;
2260 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2261 bmfh = (BITMAPFILEHEADER *)ptr;
2262 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2264 WARN("Invalid/unsupported bitmap format!\n");
2267 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2270 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2271 &bpp_dummy, &compr_dummy);
2274 WARN("Invalid bitmap format!\n");
2278 size = bitmap_info_size(info, DIB_RGB_COLORS);
2279 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2280 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2282 if (!fix_info || !scaled_info) goto end;
2283 memcpy(fix_info, info, size);
2285 pix = *((LPBYTE)info + size);
2286 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2288 memcpy(scaled_info, fix_info, size);
2291 new_width = desiredx;
2296 new_height = height > 0 ? desiredy : -desiredy;
2298 new_height = height;
2302 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2303 core->bcWidth = new_width;
2304 core->bcHeight = new_height;
2308 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2309 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2310 WARN("Broken BitmapInfoHeader!\n");
2314 scaled_info->bmiHeader.biWidth = new_width;
2315 scaled_info->bmiHeader.biHeight = new_height;
2318 if (new_height < 0) new_height = -new_height;
2320 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2321 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2323 bits = (char *)info + (offbits ? offbits : size);
2325 if (loadflags & LR_CREATEDIBSECTION)
2327 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2328 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2332 if (is_dib_monochrome(fix_info))
2333 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2335 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2338 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2339 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2340 SelectObject(screen_mem_dc, orig_bm);
2343 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2344 HeapFree(GetProcessHeap(), 0, scaled_info);
2345 HeapFree(GetProcessHeap(), 0, fix_info);
2346 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2351 /**********************************************************************
2352 * LoadImageA (USER32.@)
2356 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2357 INT desiredx, INT desiredy, UINT loadflags)
2362 if (IS_INTRESOURCE(name))
2363 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2366 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2367 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2368 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2370 __EXCEPT_PAGE_FAULT {
2371 SetLastError( ERROR_INVALID_PARAMETER );
2375 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2376 HeapFree(GetProcessHeap(), 0, u_name);
2381 /******************************************************************************
2382 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2385 * hinst [I] Handle of instance that contains image
2386 * name [I] Name of image
2387 * type [I] Type of image
2388 * desiredx [I] Desired width
2389 * desiredy [I] Desired height
2390 * loadflags [I] Load flags
2393 * Success: Handle to newly loaded image
2396 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2398 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2399 INT desiredx, INT desiredy, UINT loadflags )
2401 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2402 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2404 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2407 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2410 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2413 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2414 GetDeviceCaps(screen_dc, BITSPIXEL),
2420 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2421 1, TRUE, loadflags);
2426 /******************************************************************************
2427 * CopyImage (USER32.@) Creates new image and copies attributes to it
2430 * hnd [I] Handle to image to copy
2431 * type [I] Type of image to copy
2432 * desiredx [I] Desired width of new image
2433 * desiredy [I] Desired height of new image
2434 * flags [I] Copy flags
2437 * Success: Handle to newly created image
2441 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2442 * all other versions (95/2000/XP have been tested) ignore it.
2445 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2446 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2447 * the copy will have the same depth as the screen.
2448 * The content of the image will only be copied if the bit depth of the
2449 * original image is compatible with the bit depth of the screen, or
2450 * if the source is a DIB section.
2451 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2453 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2454 INT desiredy, UINT flags )
2456 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2457 hnd, type, desiredx, desiredy, flags);
2468 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2469 if (!objSize) return 0;
2470 if ((desiredx < 0) || (desiredy < 0)) return 0;
2472 if (flags & LR_COPYFROMRESOURCE)
2474 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2477 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2478 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2480 /* Allocate memory for a BITMAPINFOHEADER structure and a
2481 color table. The maximum number of colors in a color table
2482 is 256 which corresponds to a bitmap with depth 8.
2483 Bitmaps with higher depths don't have color tables. */
2484 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2487 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2488 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2489 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2490 bi->bmiHeader.biCompression = BI_RGB;
2492 if (flags & LR_CREATEDIBSECTION)
2494 /* Create a DIB section. LR_MONOCHROME is ignored */
2496 HDC dc = CreateCompatibleDC(NULL);
2498 if (objSize == sizeof(DIBSECTION))
2500 /* The source bitmap is a DIB.
2501 Get its attributes to create an exact copy */
2502 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2505 /* Get the color table or the color masks */
2506 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2508 bi->bmiHeader.biWidth = desiredx;
2509 bi->bmiHeader.biHeight = desiredy;
2510 bi->bmiHeader.biSizeImage = 0;
2512 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2517 /* Create a device-dependent bitmap */
2519 BOOL monochrome = (flags & LR_MONOCHROME);
2521 if (objSize == sizeof(DIBSECTION))
2523 /* The source bitmap is a DIB section.
2524 Get its attributes */
2525 HDC dc = CreateCompatibleDC(NULL);
2526 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2527 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2528 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2531 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2533 /* Look if the colors of the DIB are black and white */
2536 (bi->bmiColors[0].rgbRed == 0xff
2537 && bi->bmiColors[0].rgbGreen == 0xff
2538 && bi->bmiColors[0].rgbBlue == 0xff
2539 && bi->bmiColors[0].rgbReserved == 0
2540 && bi->bmiColors[1].rgbRed == 0
2541 && bi->bmiColors[1].rgbGreen == 0
2542 && bi->bmiColors[1].rgbBlue == 0
2543 && bi->bmiColors[1].rgbReserved == 0)
2545 (bi->bmiColors[0].rgbRed == 0
2546 && bi->bmiColors[0].rgbGreen == 0
2547 && bi->bmiColors[0].rgbBlue == 0
2548 && bi->bmiColors[0].rgbReserved == 0
2549 && bi->bmiColors[1].rgbRed == 0xff
2550 && bi->bmiColors[1].rgbGreen == 0xff
2551 && bi->bmiColors[1].rgbBlue == 0xff
2552 && bi->bmiColors[1].rgbReserved == 0);
2555 else if (!monochrome)
2557 monochrome = ds.dsBm.bmBitsPixel == 1;
2562 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2566 HDC screenDC = GetDC(NULL);
2567 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2568 ReleaseDC(NULL, screenDC);
2574 /* Only copy the bitmap if it's a DIB section or if it's
2575 compatible to the screen */
2578 if (objSize == sizeof(DIBSECTION))
2580 copyContents = TRUE;
2584 HDC screenDC = GetDC(NULL);
2585 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2586 ReleaseDC(NULL, screenDC);
2588 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2593 /* The source bitmap may already be selected in a device context,
2594 use GetDIBits/StretchDIBits and not StretchBlt */
2599 dc = CreateCompatibleDC(NULL);
2601 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2602 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2603 bi->bmiHeader.biSizeImage = 0;
2604 bi->bmiHeader.biClrUsed = 0;
2605 bi->bmiHeader.biClrImportant = 0;
2607 /* Fill in biSizeImage */
2608 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2609 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2615 /* Get the image bits of the source bitmap */
2616 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2618 /* Copy it to the destination bitmap */
2619 oldBmp = SelectObject(dc, res);
2620 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2621 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2622 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2623 SelectObject(dc, oldBmp);
2625 HeapFree(GetProcessHeap(), 0, bits);
2631 if (flags & LR_COPYDELETEORG)
2636 HeapFree(GetProcessHeap(), 0, bi);
2642 struct cursoricon_object *icon;
2644 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( screen_dc, BITSPIXEL );
2646 if (flags & LR_DEFAULTSIZE)
2648 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2649 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2652 if (!(icon = get_icon_ptr( hnd ))) return 0;
2654 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2655 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2656 type == IMAGE_CURSOR, flags );
2658 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2659 release_icon_ptr( hnd, icon );
2661 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2669 /******************************************************************************
2670 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2673 * Success: Handle to specified bitmap
2676 HBITMAP WINAPI LoadBitmapW(
2677 HINSTANCE instance, /* [in] Handle to application instance */
2678 LPCWSTR name) /* [in] Address of bitmap resource name */
2680 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2683 /**********************************************************************
2684 * LoadBitmapA (USER32.@)
2688 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2690 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );