windowscodecs: Add support for 8bpp grayscale TIFF with 8bpp alpha.
[wine.git] / dlls / user32 / cursoricon.c
blobf7b6bcace90d4d3c180ab98ce1fb91eb17656c8c
1 /*
2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
6 * 1997 Alex Korobka
7 * 1998 Turchanov Sergey
8 * 2007 Henri Verbeet
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <stdlib.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "winerror.h"
37 #include "winnls.h"
38 #include "wine/exception.h"
39 #include "wine/server.h"
40 #include "controls.h"
41 #include "win.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);
51 #include "pshpack1.h"
53 typedef struct {
54 BYTE bWidth;
55 BYTE bHeight;
56 BYTE bColorCount;
57 BYTE bReserved;
58 WORD xHotspot;
59 WORD yHotspot;
60 DWORD dwDIBSize;
61 DWORD dwDIBOffset;
62 } CURSORICONFILEDIRENTRY;
64 typedef struct
66 WORD idReserved;
67 WORD idType;
68 WORD idCount;
69 CURSORICONFILEDIRENTRY idEntries[1];
70 } CURSORICONFILEDIR;
72 #include "poppack.h"
74 static HDC screen_dc;
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) */
105 POINT hotspot;
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;
125 int icon_size;
126 HICON handle;
128 if (is_ani)
129 icon_size = FIELD_OFFSET( struct animated_cursoricon_object, frames[num_steps] );
130 else
131 icon_size = sizeof( struct static_cursoricon_object );
132 obj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, icon_size );
133 if (!obj) return NULL;
135 obj->delay = 0;
136 obj->is_ani = is_ani;
137 if (is_ani)
139 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
141 ani_icon_data->num_steps = num_steps;
142 ani_icon_data->num_frames = num_steps; /* changed later for some animated cursors */
145 if (!(handle = alloc_user_handle( &obj->obj, USER_ICON )))
146 HeapFree( GetProcessHeap(), 0, obj );
147 return handle;
150 static struct cursoricon_object *get_icon_ptr( HICON handle )
152 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
153 if (obj == OBJ_OTHER_PROCESS)
155 WARN( "icon handle %p from other process\n", handle );
156 obj = NULL;
158 return obj;
161 static void release_icon_ptr( HICON handle, struct cursoricon_object *ptr )
163 release_user_handle_ptr( ptr );
166 static struct cursoricon_frame *get_icon_frame( struct cursoricon_object *obj, int istep )
168 struct static_cursoricon_object *req_frame;
170 if (obj->is_ani)
172 struct animated_cursoricon_object *ani_icon_data;
173 struct cursoricon_object *frameobj;
175 ani_icon_data = (struct animated_cursoricon_object *) obj;
176 if (!(frameobj = get_icon_ptr( ani_icon_data->frames[istep] )))
177 return 0;
178 req_frame = (struct static_cursoricon_object *) frameobj;
180 else
181 req_frame = (struct static_cursoricon_object *) obj;
183 return &req_frame->frame;
186 static void release_icon_frame( struct cursoricon_object *obj, int istep, struct cursoricon_frame *frame )
188 if (obj->is_ani)
190 struct animated_cursoricon_object *ani_icon_data;
191 struct cursoricon_object *frameobj;
193 ani_icon_data = (struct animated_cursoricon_object *) obj;
194 frameobj = (struct cursoricon_object *) (((char *)frame) - FIELD_OFFSET(struct static_cursoricon_object, frame));
195 release_icon_ptr( ani_icon_data->frames[istep], frameobj );
199 static UINT get_icon_steps( struct cursoricon_object *obj )
201 if (obj->is_ani)
203 struct animated_cursoricon_object *ani_icon_data;
205 ani_icon_data = (struct animated_cursoricon_object *) obj;
206 return ani_icon_data->num_steps;
208 return 1;
211 static BOOL free_icon_handle( HICON handle )
213 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
215 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
216 else if (obj)
218 ULONG_PTR param = obj->param;
219 UINT i;
221 assert( !obj->rsrc ); /* shared icons can't be freed */
223 if (!obj->is_ani)
225 struct cursoricon_frame *frame = get_icon_frame( obj, 0 );
227 if (frame->alpha) DeleteObject( frame->alpha );
228 if (frame->color) DeleteObject( frame->color );
229 DeleteObject( frame->mask );
230 release_icon_frame( obj, 0, frame );
232 else
234 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
236 for (i=0; i<ani_icon_data->num_steps; i++)
238 HICON hFrame = ani_icon_data->frames[i];
240 if (hFrame)
242 UINT j;
244 free_icon_handle( ani_icon_data->frames[i] );
245 for (j=0; j<ani_icon_data->num_steps; j++)
247 if (ani_icon_data->frames[j] == hFrame)
248 ani_icon_data->frames[j] = 0;
253 if (!IS_INTRESOURCE( obj->resname )) HeapFree( GetProcessHeap(), 0, obj->resname );
254 HeapFree( GetProcessHeap(), 0, obj );
255 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
256 USER_Driver->pDestroyCursorIcon( handle );
257 return TRUE;
259 return FALSE;
262 ULONG_PTR get_icon_param( HICON handle )
264 ULONG_PTR ret = 0;
265 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
267 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
268 else if (obj)
270 ret = obj->param;
271 release_user_handle_ptr( obj );
273 return ret;
276 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
278 ULONG_PTR ret = 0;
279 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
281 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
282 else if (obj)
284 ret = obj->param;
285 obj->param = param;
286 release_user_handle_ptr( obj );
288 return ret;
292 /***********************************************************************
293 * map_fileW
295 * Helper function to map a file to memory:
296 * name - file name
297 * [RETURN] ptr - pointer to mapped file
298 * [RETURN] filesize - pointer size of file to be stored if not NULL
300 static const void *map_fileW( LPCWSTR name, LPDWORD filesize )
302 HANDLE hFile, hMapping;
303 LPVOID ptr = NULL;
305 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
306 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
307 if (hFile != INVALID_HANDLE_VALUE)
309 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
310 if (hMapping)
312 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
313 CloseHandle( hMapping );
314 if (filesize)
315 *filesize = GetFileSize( hFile, NULL );
317 CloseHandle( hFile );
319 return ptr;
323 /***********************************************************************
324 * get_dib_image_size
326 * Return the size of a DIB bitmap in bytes.
328 static int get_dib_image_size( int width, int height, int depth )
330 return (((width * depth + 31) / 8) & ~3) * abs( height );
334 /***********************************************************************
335 * bitmap_info_size
337 * Return the size of the bitmap info structure including color table.
339 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
341 unsigned int colors, size, masks = 0;
343 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
345 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
346 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
347 return sizeof(BITMAPCOREHEADER) + colors *
348 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
350 else /* assume BITMAPINFOHEADER */
352 colors = info->bmiHeader.biClrUsed;
353 if (colors > 256) /* buffer overflow otherwise */
354 colors = 256;
355 if (!colors && (info->bmiHeader.biBitCount <= 8))
356 colors = 1 << info->bmiHeader.biBitCount;
357 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
358 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
359 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
364 /***********************************************************************
365 * copy_bitmap
367 * Helper function to duplicate a bitmap.
369 static HBITMAP copy_bitmap( HBITMAP bitmap )
371 HDC src, dst = 0;
372 HBITMAP new_bitmap = 0;
373 BITMAP bmp;
375 if (!bitmap) return 0;
376 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
378 if ((src = CreateCompatibleDC( 0 )) && (dst = CreateCompatibleDC( 0 )))
380 SelectObject( src, bitmap );
381 if ((new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight )))
383 SelectObject( dst, new_bitmap );
384 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
387 DeleteDC( dst );
388 DeleteDC( src );
389 return new_bitmap;
393 /***********************************************************************
394 * is_dib_monochrome
396 * Returns whether a DIB can be converted to a monochrome DDB.
398 * A DIB can be converted if its color table contains only black and
399 * white. Black must be the first color in the color table.
401 * Note : If the first color in the color table is white followed by
402 * black, we can't convert it to a monochrome DDB with
403 * SetDIBits, because black and white would be inverted.
405 static BOOL is_dib_monochrome( const BITMAPINFO* info )
407 if (info->bmiHeader.biBitCount != 1) return FALSE;
409 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
411 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
413 /* Check if the first color is black */
414 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
416 rgb++;
418 /* Check if the second color is white */
419 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
420 && (rgb->rgbtBlue == 0xff));
422 else return FALSE;
424 else /* assume BITMAPINFOHEADER */
426 const RGBQUAD *rgb = info->bmiColors;
428 /* Check if the first color is black */
429 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
430 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
432 rgb++;
434 /* Check if the second color is white */
435 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
436 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
438 else return FALSE;
442 /***********************************************************************
443 * DIB_GetBitmapInfo
445 * Get the info from a bitmap header.
446 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
448 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
449 LONG *height, WORD *bpp, DWORD *compr )
451 if (header->biSize == sizeof(BITMAPCOREHEADER))
453 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
454 *width = core->bcWidth;
455 *height = core->bcHeight;
456 *bpp = core->bcBitCount;
457 *compr = 0;
458 return 0;
460 else if (header->biSize == sizeof(BITMAPINFOHEADER) ||
461 header->biSize == sizeof(BITMAPV4HEADER) ||
462 header->biSize == sizeof(BITMAPV5HEADER))
464 *width = header->biWidth;
465 *height = header->biHeight;
466 *bpp = header->biBitCount;
467 *compr = header->biCompression;
468 return 1;
470 WARN("unknown/wrong size (%u) for header\n", header->biSize);
471 return -1;
474 /**********************************************************************
475 * get_icon_size
477 BOOL get_icon_size( HICON handle, SIZE *size )
479 struct cursoricon_object *info;
480 struct cursoricon_frame *frame;
482 if (!(info = get_icon_ptr( handle ))) return FALSE;
483 frame = get_icon_frame( info, 0 );
484 size->cx = frame->width;
485 size->cy = frame->height;
486 release_icon_frame( info, 0, frame);
487 release_icon_ptr( handle, info );
488 return TRUE;
492 * The following macro functions account for the irregularities of
493 * accessing cursor and icon resources in files and resource entries.
495 typedef BOOL (*fnGetCIEntry)( LPCVOID dir, DWORD size, int n,
496 int *width, int *height, int *bits );
498 /**********************************************************************
499 * CURSORICON_FindBestIcon
501 * Find the icon closest to the requested size and bit depth.
503 static int CURSORICON_FindBestIcon( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
504 int width, int height, int depth, UINT loadflags )
506 int i, cx, cy, bits, bestEntry = -1;
507 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
508 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
510 /* Find Best Fit */
511 iTotalDiff = 0xFFFFFFFF;
512 iColorDiff = 0xFFFFFFFF;
514 if (loadflags & LR_DEFAULTSIZE)
516 if (!width) width = GetSystemMetrics( SM_CXICON );
517 if (!height) height = GetSystemMetrics( SM_CYICON );
519 else if (!width && !height)
521 /* use the size of the first entry */
522 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
523 iTotalDiff = 0;
526 for ( i = 0; iTotalDiff && get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
528 iTempXDiff = abs(width - cx);
529 iTempYDiff = abs(height - cy);
531 if(iTotalDiff > (iTempXDiff + iTempYDiff))
533 iXDiff = iTempXDiff;
534 iYDiff = iTempYDiff;
535 iTotalDiff = iXDiff + iYDiff;
539 /* Find Best Colors for Best Fit */
540 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
542 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
544 iTempColorDiff = abs(depth - bits);
545 if(iColorDiff > iTempColorDiff)
547 bestEntry = i;
548 iColorDiff = iTempColorDiff;
553 return bestEntry;
556 static BOOL CURSORICON_GetResIconEntry( LPCVOID dir, DWORD size, int n,
557 int *width, int *height, int *bits )
559 const CURSORICONDIR *resdir = dir;
560 const ICONRESDIR *icon;
562 if ( resdir->idCount <= n )
563 return FALSE;
564 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
565 return FALSE;
566 icon = &resdir->idEntries[n].ResInfo.icon;
567 *width = icon->bWidth;
568 *height = icon->bHeight;
569 *bits = resdir->idEntries[n].wBitCount;
570 return TRUE;
573 /**********************************************************************
574 * CURSORICON_FindBestCursor
576 * Find the cursor closest to the requested size.
578 * FIXME: parameter 'color' ignored.
580 static int CURSORICON_FindBestCursor( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
581 int width, int height, int depth, UINT loadflags )
583 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
585 if (loadflags & LR_DEFAULTSIZE)
587 if (!width) width = GetSystemMetrics( SM_CXCURSOR );
588 if (!height) height = GetSystemMetrics( SM_CYCURSOR );
590 else if (!width && !height)
592 /* use the first entry */
593 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
594 return 0;
597 /* Double height to account for AND and XOR masks */
599 height *= 2;
601 /* First find the largest one smaller than or equal to the requested size*/
603 maxwidth = maxheight = 0;
604 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
606 if ((cx <= width) && (cy <= height) &&
607 (cx > maxwidth) && (cy > maxheight))
609 bestEntry = i;
610 maxwidth = cx;
611 maxheight = cy;
614 if (bestEntry != -1) return bestEntry;
616 /* Now find the smallest one larger than the requested size */
618 maxwidth = maxheight = 255;
619 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
621 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
623 bestEntry = i;
624 maxwidth = cx;
625 maxheight = cy;
629 return bestEntry;
632 static BOOL CURSORICON_GetResCursorEntry( LPCVOID dir, DWORD size, int n,
633 int *width, int *height, int *bits )
635 const CURSORICONDIR *resdir = dir;
636 const CURSORDIR *cursor;
638 if ( resdir->idCount <= n )
639 return FALSE;
640 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
641 return FALSE;
642 cursor = &resdir->idEntries[n].ResInfo.cursor;
643 *width = cursor->wWidth;
644 *height = cursor->wHeight;
645 *bits = resdir->idEntries[n].wBitCount;
646 return TRUE;
649 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir, DWORD size,
650 int width, int height, int depth,
651 UINT loadflags )
653 int n;
655 n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetResIconEntry,
656 width, height, depth, loadflags );
657 if ( n < 0 )
658 return NULL;
659 return &dir->idEntries[n];
662 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir, DWORD size,
663 int width, int height, int depth,
664 UINT loadflags )
666 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetResCursorEntry,
667 width, height, depth, loadflags );
668 if ( n < 0 )
669 return NULL;
670 return &dir->idEntries[n];
673 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, DWORD size, int n,
674 int *width, int *height, int *bits )
676 const CURSORICONFILEDIR *filedir = dir;
677 const CURSORICONFILEDIRENTRY *entry;
678 const BITMAPINFOHEADER *info;
680 if ( filedir->idCount <= n )
681 return FALSE;
682 if ((const char *)&filedir->idEntries[n + 1] - (const char *)dir > size)
683 return FALSE;
684 entry = &filedir->idEntries[n];
685 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
686 if ((const char *)(info + 1) - (const char *)dir > size) return FALSE;
687 *width = entry->bWidth;
688 *height = entry->bHeight;
689 *bits = info->biBitCount;
690 return TRUE;
693 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir, DWORD size,
694 int width, int height, int depth,
695 UINT loadflags )
697 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetFileEntry,
698 width, height, depth, loadflags );
699 if ( n < 0 )
700 return NULL;
701 return &dir->idEntries[n];
704 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir, DWORD size,
705 int width, int height, int depth,
706 UINT loadflags )
708 int n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetFileEntry,
709 width, height, depth, loadflags );
710 if ( n < 0 )
711 return NULL;
712 return &dir->idEntries[n];
715 /***********************************************************************
716 * bmi_has_alpha
718 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
720 int i;
721 BOOL has_alpha = FALSE;
722 const unsigned char *ptr = bits;
724 if (info->bmiHeader.biBitCount != 32) return FALSE;
725 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
726 if ((has_alpha = (ptr[3] != 0))) break;
727 return has_alpha;
730 /***********************************************************************
731 * create_alpha_bitmap
733 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
735 static HBITMAP create_alpha_bitmap( HBITMAP color, HBITMAP mask,
736 const BITMAPINFO *src_info, const void *color_bits )
738 HBITMAP alpha = 0;
739 BITMAPINFO *info = NULL;
740 BITMAP bm;
741 HDC hdc;
742 void *bits;
743 unsigned char *ptr;
744 int i;
746 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
747 if (bm.bmBitsPixel != 32) return 0;
749 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
750 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
751 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
752 info->bmiHeader.biWidth = bm.bmWidth;
753 info->bmiHeader.biHeight = -bm.bmHeight;
754 info->bmiHeader.biPlanes = 1;
755 info->bmiHeader.biBitCount = 32;
756 info->bmiHeader.biCompression = BI_RGB;
757 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
758 info->bmiHeader.biXPelsPerMeter = 0;
759 info->bmiHeader.biYPelsPerMeter = 0;
760 info->bmiHeader.biClrUsed = 0;
761 info->bmiHeader.biClrImportant = 0;
762 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
764 if (src_info)
766 SelectObject( hdc, alpha );
767 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
768 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
769 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
772 else
774 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
775 if (!bmi_has_alpha( info, bits ))
777 DeleteObject( alpha );
778 alpha = 0;
779 goto done;
783 /* pre-multiply by alpha */
784 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
786 unsigned int alpha = ptr[3];
787 ptr[0] = ptr[0] * alpha / 255;
788 ptr[1] = ptr[1] * alpha / 255;
789 ptr[2] = ptr[2] * alpha / 255;
792 done:
793 DeleteDC( hdc );
794 HeapFree( GetProcessHeap(), 0, info );
795 return alpha;
799 /***********************************************************************
800 * create_icon_from_bmi
802 * Create an icon from its BITMAPINFO.
804 static HICON create_icon_from_bmi( const BITMAPINFO *bmi, DWORD maxsize, HMODULE module, LPCWSTR resname,
805 HRSRC rsrc, POINT hotspot, BOOL bIcon, INT width, INT height,
806 UINT cFlag )
808 DWORD size, color_size, mask_size;
809 HBITMAP color = 0, mask = 0, alpha = 0;
810 const void *color_bits, *mask_bits;
811 BITMAPINFO *bmi_copy;
812 BOOL ret = FALSE;
813 BOOL do_stretch;
814 HICON hObj = 0;
815 HDC hdc = 0;
817 /* Check bitmap header */
819 if (maxsize < sizeof(BITMAPCOREHEADER))
821 WARN( "invalid size %u\n", maxsize );
822 return 0;
824 if (maxsize < bmi->bmiHeader.biSize)
826 WARN( "invalid header size %u\n", bmi->bmiHeader.biSize );
827 return 0;
829 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
830 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
831 (bmi->bmiHeader.biCompression != BI_RGB &&
832 bmi->bmiHeader.biCompression != BI_BITFIELDS)) )
834 WARN( "invalid bitmap header %u\n", bmi->bmiHeader.biSize );
835 return 0;
838 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
839 color_size = get_dib_image_size( bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight / 2,
840 bmi->bmiHeader.biBitCount );
841 mask_size = get_dib_image_size( bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight / 2, 1 );
842 if (size > maxsize || color_size > maxsize - size)
844 WARN( "truncated file %u < %u+%u+%u\n", maxsize, size, color_size, mask_size );
845 return 0;
847 if (mask_size > maxsize - size - color_size) mask_size = 0; /* no mask */
849 if (cFlag & LR_DEFAULTSIZE)
851 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
852 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
854 else
856 if (!width) width = bmi->bmiHeader.biWidth;
857 if (!height) height = bmi->bmiHeader.biHeight/2;
859 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
860 (bmi->bmiHeader.biWidth != width);
862 /* Scale the hotspot */
863 if (bIcon)
865 hotspot.x = width / 2;
866 hotspot.y = height / 2;
868 else if (do_stretch)
870 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
871 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
874 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
875 if (!screen_dc) return 0;
877 if (!(bmi_copy = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
878 return 0;
879 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
881 memcpy( bmi_copy, bmi, size );
882 bmi_copy->bmiHeader.biHeight /= 2;
884 color_bits = (const char*)bmi + size;
885 mask_bits = (const char*)color_bits + color_size;
887 alpha = 0;
888 if (is_dib_monochrome( bmi ))
890 if (!(mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
891 color = 0;
893 /* copy color data into second half of mask bitmap */
894 SelectObject( hdc, mask );
895 StretchDIBits( hdc, 0, height, width, height,
896 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
897 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
899 else
901 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
902 if (!(color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
903 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
905 DeleteObject( mask );
906 goto done;
908 SelectObject( hdc, color );
909 StretchDIBits( hdc, 0, 0, width, height,
910 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
911 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
913 if (bmi_has_alpha( bmi_copy, color_bits ))
914 alpha = create_alpha_bitmap( color, mask, bmi_copy, color_bits );
916 /* convert info to monochrome to copy the mask */
917 bmi_copy->bmiHeader.biBitCount = 1;
918 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
920 RGBQUAD *rgb = bmi_copy->bmiColors;
922 bmi_copy->bmiHeader.biClrUsed = bmi_copy->bmiHeader.biClrImportant = 2;
923 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
924 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
925 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
927 else
929 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)bmi_copy) + 1);
931 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
932 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
936 if (mask_size)
938 SelectObject( hdc, mask );
939 StretchDIBits( hdc, 0, 0, width, height,
940 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
941 mask_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
943 ret = TRUE;
945 done:
946 DeleteDC( hdc );
947 HeapFree( GetProcessHeap(), 0, bmi_copy );
949 if (ret)
950 hObj = alloc_icon_handle( FALSE, 1 );
951 if (hObj)
953 struct cursoricon_object *info = get_icon_ptr( hObj );
954 struct cursoricon_frame *frame;
956 info->is_icon = bIcon;
957 info->module = module;
958 info->hotspot = hotspot;
959 frame = get_icon_frame( info, 0 );
960 frame->delay = ~0;
961 frame->width = width;
962 frame->height = height;
963 frame->color = color;
964 frame->mask = mask;
965 frame->alpha = alpha;
966 release_icon_frame( info, 0, frame );
967 if (!IS_INTRESOURCE(resname))
969 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
970 if (info->resname) strcpyW( info->resname, resname );
972 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
974 if (module && (cFlag & LR_SHARED))
976 info->rsrc = rsrc;
977 list_add_head( &icon_cache, &info->entry );
979 release_icon_ptr( hObj, info );
981 else
983 DeleteObject( color );
984 DeleteObject( alpha );
985 DeleteObject( mask );
987 return hObj;
991 /**********************************************************************
992 * .ANI cursor support
994 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
995 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
996 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
998 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
999 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
1000 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
1001 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
1002 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
1003 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
1004 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
1006 #define ANI_FLAG_ICON 0x1
1007 #define ANI_FLAG_SEQUENCE 0x2
1009 typedef struct {
1010 DWORD header_size;
1011 DWORD num_frames;
1012 DWORD num_steps;
1013 DWORD width;
1014 DWORD height;
1015 DWORD bpp;
1016 DWORD num_planes;
1017 DWORD display_rate;
1018 DWORD flags;
1019 } ani_header;
1021 typedef struct {
1022 DWORD data_size;
1023 const unsigned char *data;
1024 } riff_chunk_t;
1026 static void dump_ani_header( const ani_header *header )
1028 TRACE(" header size: %d\n", header->header_size);
1029 TRACE(" frames: %d\n", header->num_frames);
1030 TRACE(" steps: %d\n", header->num_steps);
1031 TRACE(" width: %d\n", header->width);
1032 TRACE(" height: %d\n", header->height);
1033 TRACE(" bpp: %d\n", header->bpp);
1034 TRACE(" planes: %d\n", header->num_planes);
1035 TRACE(" display rate: %d\n", header->display_rate);
1036 TRACE(" flags: 0x%08x\n", header->flags);
1041 * RIFF:
1042 * DWORD "RIFF"
1043 * DWORD size
1044 * DWORD riff_id
1045 * BYTE[] data
1047 * LIST:
1048 * DWORD "LIST"
1049 * DWORD size
1050 * DWORD list_id
1051 * BYTE[] data
1053 * CHUNK:
1054 * DWORD chunk_id
1055 * DWORD size
1056 * BYTE[] data
1058 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1060 const unsigned char *ptr = parent_chunk->data;
1061 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1063 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1065 while (ptr < end)
1067 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1068 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1070 ptr += sizeof(DWORD);
1071 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1072 ptr += sizeof(DWORD);
1073 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1074 chunk->data = ptr;
1076 return;
1079 ptr += sizeof(DWORD);
1080 ptr += (*(const DWORD *)ptr + 1) & ~1;
1081 ptr += sizeof(DWORD);
1087 * .ANI layout:
1089 * RIFF:'ACON' RIFF chunk
1090 * |- CHUNK:'anih' Header
1091 * |- CHUNK:'seq ' Sequence information (optional)
1092 * \- LIST:'fram' Frame list
1093 * |- CHUNK:icon Cursor frames
1094 * |- CHUNK:icon
1095 * |- ...
1096 * \- CHUNK:icon
1098 static HCURSOR CURSORICON_CreateIconFromANI( const BYTE *bits, DWORD bits_size, INT width, INT height,
1099 INT depth, BOOL is_icon, UINT loadflags )
1101 struct animated_cursoricon_object *ani_icon_data;
1102 struct cursoricon_object *info;
1103 DWORD *frame_rates = NULL;
1104 DWORD *frame_seq = NULL;
1105 ani_header header = {0};
1106 BOOL use_seq = FALSE;
1107 HCURSOR cursor = 0;
1108 UINT i;
1109 BOOL error = FALSE;
1110 HICON *frames;
1112 riff_chunk_t root_chunk = { bits_size, bits };
1113 riff_chunk_t ACON_chunk = {0};
1114 riff_chunk_t anih_chunk = {0};
1115 riff_chunk_t fram_chunk = {0};
1116 riff_chunk_t rate_chunk = {0};
1117 riff_chunk_t seq_chunk = {0};
1118 const unsigned char *icon_chunk;
1119 const unsigned char *icon_data;
1121 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1123 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1124 if (!ACON_chunk.data)
1126 ERR("Failed to get root chunk.\n");
1127 return 0;
1130 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1131 if (!anih_chunk.data)
1133 ERR("Failed to get 'anih' chunk.\n");
1134 return 0;
1136 memcpy( &header, anih_chunk.data, sizeof(header) );
1137 dump_ani_header( &header );
1139 if (!(header.flags & ANI_FLAG_ICON))
1141 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1142 return 0;
1145 if (header.flags & ANI_FLAG_SEQUENCE)
1147 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1148 if (seq_chunk.data)
1150 frame_seq = (DWORD *) seq_chunk.data;
1151 use_seq = TRUE;
1153 else
1155 FIXME("Sequence data expected but not found, assuming steps == frames.\n");
1156 header.num_steps = header.num_frames;
1160 riff_find_chunk( ANI_rate_ID, 0, &ACON_chunk, &rate_chunk );
1161 if (rate_chunk.data)
1162 frame_rates = (DWORD *) rate_chunk.data;
1164 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1165 if (!fram_chunk.data)
1167 ERR("Failed to get icon list.\n");
1168 return 0;
1171 cursor = alloc_icon_handle( TRUE, header.num_steps );
1172 if (!cursor) return 0;
1173 frames = HeapAlloc( GetProcessHeap(), 0, sizeof(*frames) * header.num_frames );
1174 if (!frames)
1176 free_icon_handle( cursor );
1177 return 0;
1180 info = get_icon_ptr( cursor );
1181 ani_icon_data = (struct animated_cursoricon_object *) info;
1182 info->is_icon = is_icon;
1183 ani_icon_data->num_frames = header.num_frames;
1185 /* The .ANI stores the display rate in jiffies (1/60s) */
1186 info->delay = header.display_rate;
1188 icon_chunk = fram_chunk.data;
1189 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1190 for (i=0; i<header.num_frames; i++)
1192 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1193 const CURSORICONFILEDIRENTRY *entry;
1194 INT frameWidth, frameHeight;
1195 const BITMAPINFO *bmi;
1197 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1198 bits + bits_size - icon_data,
1199 width, height, depth, loadflags );
1201 info->hotspot.x = entry->xHotspot;
1202 info->hotspot.y = entry->yHotspot;
1203 if (!header.width || !header.height)
1205 frameWidth = entry->bWidth;
1206 frameHeight = entry->bHeight;
1208 else
1210 frameWidth = header.width;
1211 frameHeight = header.height;
1214 frames[i] = NULL;
1215 if (entry->dwDIBOffset < bits + bits_size - icon_data)
1217 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1218 /* Grab a frame from the animation */
1219 frames[i] = create_icon_from_bmi( bmi, bits + bits_size - (const BYTE *)bmi,
1220 NULL, NULL, NULL, info->hotspot,
1221 is_icon, frameWidth, frameHeight, loadflags );
1224 if (!frames[i])
1226 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1227 error = TRUE;
1228 if (i == 0)
1230 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1231 ani_icon_data->num_frames = 0;
1232 release_icon_ptr( cursor, info );
1233 free_icon_handle( cursor );
1234 HeapFree( GetProcessHeap(), 0, frames );
1235 return 0;
1237 break;
1240 /* Advance to the next chunk */
1241 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1242 icon_data = icon_chunk + (2 * sizeof(DWORD));
1245 /* There was an error but we at least decoded the first frame, so just use that frame */
1246 if (error)
1248 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1249 for (i=1; i<ani_icon_data->num_frames; i++)
1250 free_icon_handle( ani_icon_data->frames[i] );
1251 use_seq = FALSE;
1252 info->delay = 0;
1253 ani_icon_data->num_steps = 1;
1254 ani_icon_data->num_frames = 1;
1257 /* Setup the animated frames in the correct sequence */
1258 for (i=0; i<ani_icon_data->num_steps; i++)
1260 DWORD frame_id = use_seq ? frame_seq[i] : i;
1261 struct cursoricon_frame *frame;
1263 if (frame_id >= ani_icon_data->num_frames)
1265 frame_id = ani_icon_data->num_frames-1;
1266 ERR_(cursor)("Sequence indicates frame past end of list, corrupt?\n");
1268 ani_icon_data->frames[i] = frames[frame_id];
1269 frame = get_icon_frame( info, i );
1270 if (frame_rates)
1271 frame->delay = frame_rates[i];
1272 else
1273 frame->delay = ~0;
1274 release_icon_frame( info, i, frame );
1277 HeapFree( GetProcessHeap(), 0, frames );
1278 release_icon_ptr( cursor, info );
1280 return cursor;
1284 /**********************************************************************
1285 * CreateIconFromResourceEx (USER32.@)
1287 * FIXME: Convert to mono when cFlag is LR_MONOCHROME.
1289 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1290 BOOL bIcon, DWORD dwVersion,
1291 INT width, INT height,
1292 UINT cFlag )
1294 POINT hotspot;
1295 const BITMAPINFO *bmi;
1297 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1298 bits, cbSize, dwVersion, width, height,
1299 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1301 if (!bits) return 0;
1303 if (dwVersion == 0x00020000)
1305 FIXME_(cursor)("\t2.xx resources are not supported\n");
1306 return 0;
1309 /* Check if the resource is an animated icon/cursor */
1310 if (!memcmp(bits, "RIFF", 4))
1311 return CURSORICON_CreateIconFromANI( bits, cbSize, width, height,
1312 0 /* default depth */, bIcon, cFlag );
1314 if (bIcon)
1316 hotspot.x = width / 2;
1317 hotspot.y = height / 2;
1318 bmi = (BITMAPINFO *)bits;
1320 else /* get the hotspot */
1322 const SHORT *pt = (const SHORT *)bits;
1323 hotspot.x = pt[0];
1324 hotspot.y = pt[1];
1325 bmi = (const BITMAPINFO *)(pt + 2);
1326 cbSize -= 2 * sizeof(*pt);
1329 return create_icon_from_bmi( bmi, cbSize, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1333 /**********************************************************************
1334 * CreateIconFromResource (USER32.@)
1336 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1337 BOOL bIcon, DWORD dwVersion)
1339 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1343 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1344 INT width, INT height, INT depth,
1345 BOOL fCursor, UINT loadflags)
1347 const CURSORICONFILEDIRENTRY *entry;
1348 const CURSORICONFILEDIR *dir;
1349 DWORD filesize = 0;
1350 HICON hIcon = 0;
1351 const BYTE *bits;
1352 POINT hotspot;
1354 TRACE("loading %s\n", debugstr_w( filename ));
1356 bits = map_fileW( filename, &filesize );
1357 if (!bits)
1358 return hIcon;
1360 /* Check for .ani. */
1361 if (memcmp( bits, "RIFF", 4 ) == 0)
1363 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, !fCursor, loadflags );
1364 goto end;
1367 dir = (const CURSORICONFILEDIR*) bits;
1368 if ( filesize < FIELD_OFFSET( CURSORICONFILEDIR, idEntries[dir->idCount] ))
1369 goto end;
1371 if ( fCursor )
1372 entry = CURSORICON_FindBestCursorFile( dir, filesize, width, height, depth, loadflags );
1373 else
1374 entry = CURSORICON_FindBestIconFile( dir, filesize, width, height, depth, loadflags );
1376 if ( !entry )
1377 goto end;
1379 /* check that we don't run off the end of the file */
1380 if ( entry->dwDIBOffset > filesize )
1381 goto end;
1382 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1383 goto end;
1385 hotspot.x = entry->xHotspot;
1386 hotspot.y = entry->yHotspot;
1387 hIcon = create_icon_from_bmi( (const BITMAPINFO *)&bits[entry->dwDIBOffset], filesize - entry->dwDIBOffset,
1388 NULL, NULL, NULL, hotspot, !fCursor, width, height, loadflags );
1389 end:
1390 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1391 UnmapViewOfFile( bits );
1392 return hIcon;
1395 /**********************************************************************
1396 * CURSORICON_Load
1398 * Load a cursor or icon from resource or file.
1400 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1401 INT width, INT height, INT depth,
1402 BOOL fCursor, UINT loadflags)
1404 HANDLE handle = 0;
1405 HICON hIcon = 0;
1406 HRSRC hRsrc;
1407 DWORD size;
1408 const CURSORICONDIR *dir;
1409 const CURSORICONDIRENTRY *dirEntry;
1410 const BYTE *bits;
1411 WORD wResId;
1412 POINT hotspot;
1414 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1415 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1417 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1418 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1420 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1422 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1423 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1425 /* Get directory resource ID */
1427 if (!(hRsrc = FindResourceW( hInstance, name,
1428 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1430 /* try animated resource */
1431 if (!(hRsrc = FindResourceW( hInstance, name,
1432 (LPWSTR)(fCursor ? RT_ANICURSOR : RT_ANIICON) ))) return 0;
1433 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1434 bits = LockResource( handle );
1435 return CURSORICON_CreateIconFromANI( bits, SizeofResource( hInstance, handle ),
1436 width, height, depth, !fCursor, loadflags );
1439 /* Find the best entry in the directory */
1441 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1442 if (!(dir = LockResource( handle ))) return 0;
1443 size = SizeofResource( hInstance, hRsrc );
1444 if (fCursor)
1445 dirEntry = CURSORICON_FindBestCursorRes( dir, size, width, height, depth, loadflags );
1446 else
1447 dirEntry = CURSORICON_FindBestIconRes( dir, size, width, height, depth, loadflags );
1448 if (!dirEntry) return 0;
1449 wResId = dirEntry->wResId;
1450 FreeResource( handle );
1452 /* Load the resource */
1454 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1455 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1457 /* If shared icon, check whether it was already loaded */
1458 if (loadflags & LR_SHARED)
1460 struct cursoricon_object *ptr;
1462 USER_Lock();
1463 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1465 if (ptr->module != hInstance) continue;
1466 if (ptr->rsrc != hRsrc) continue;
1467 hIcon = ptr->obj.handle;
1468 break;
1470 USER_Unlock();
1471 if (hIcon) return hIcon;
1474 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1475 size = SizeofResource( hInstance, hRsrc );
1476 bits = LockResource( handle );
1478 if (!fCursor)
1480 hotspot.x = width / 2;
1481 hotspot.y = height / 2;
1483 else /* get the hotspot */
1485 const SHORT *pt = (const SHORT *)bits;
1486 hotspot.x = pt[0];
1487 hotspot.y = pt[1];
1488 bits += 2 * sizeof(SHORT);
1489 size -= 2 * sizeof(SHORT);
1491 hIcon = create_icon_from_bmi( (const BITMAPINFO *)bits, size, hInstance, name, hRsrc,
1492 hotspot, !fCursor, width, height, loadflags );
1493 FreeResource( handle );
1494 return hIcon;
1498 /***********************************************************************
1499 * CreateCursor (USER32.@)
1501 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1502 INT xHotSpot, INT yHotSpot,
1503 INT nWidth, INT nHeight,
1504 LPCVOID lpANDbits, LPCVOID lpXORbits )
1506 ICONINFO info;
1507 HCURSOR hCursor;
1509 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1510 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1512 info.fIcon = FALSE;
1513 info.xHotspot = xHotSpot;
1514 info.yHotspot = yHotSpot;
1515 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1516 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1517 hCursor = CreateIconIndirect( &info );
1518 DeleteObject( info.hbmMask );
1519 DeleteObject( info.hbmColor );
1520 return hCursor;
1524 /***********************************************************************
1525 * CreateIcon (USER32.@)
1527 * Creates an icon based on the specified bitmaps. The bitmaps must be
1528 * provided in a device dependent format and will be resized to
1529 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1530 * depth. The provided bitmaps must be top-down bitmaps.
1531 * Although Windows does not support 15bpp(*) this API must support it
1532 * for Winelib applications.
1534 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1535 * format!
1537 * RETURNS
1538 * Success: handle to an icon
1539 * Failure: NULL
1541 * FIXME: Do we need to resize the bitmaps?
1543 HICON WINAPI CreateIcon(
1544 HINSTANCE hInstance, /* [in] the application's hInstance */
1545 INT nWidth, /* [in] the width of the provided bitmaps */
1546 INT nHeight, /* [in] the height of the provided bitmaps */
1547 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1548 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1549 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1550 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1552 ICONINFO iinfo;
1553 HICON hIcon;
1555 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1556 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1558 iinfo.fIcon = TRUE;
1559 iinfo.xHotspot = nWidth / 2;
1560 iinfo.yHotspot = nHeight / 2;
1561 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1562 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1564 hIcon = CreateIconIndirect( &iinfo );
1566 DeleteObject( iinfo.hbmMask );
1567 DeleteObject( iinfo.hbmColor );
1569 return hIcon;
1573 /***********************************************************************
1574 * CopyIcon (USER32.@)
1576 HICON WINAPI CopyIcon( HICON hIcon )
1578 struct cursoricon_object *ptrOld, *ptrNew;
1579 HICON hNew;
1581 if (!(ptrOld = get_icon_ptr( hIcon )))
1583 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1584 return 0;
1586 if ((hNew = alloc_icon_handle( FALSE, 1 )))
1588 struct cursoricon_frame *frameOld, *frameNew;
1590 ptrNew = get_icon_ptr( hNew );
1591 ptrNew->is_icon = ptrOld->is_icon;
1592 ptrNew->hotspot = ptrOld->hotspot;
1593 if (!(frameOld = get_icon_frame( ptrOld, 0 )))
1595 release_icon_ptr( hIcon, ptrOld );
1596 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1597 return 0;
1599 if (!(frameNew = get_icon_frame( ptrNew, 0 )))
1601 release_icon_frame( ptrOld, 0, frameOld );
1602 release_icon_ptr( hIcon, ptrOld );
1603 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1604 return 0;
1606 frameNew->delay = 0;
1607 frameNew->width = frameOld->width;
1608 frameNew->height = frameOld->height;
1609 frameNew->mask = copy_bitmap( frameOld->mask );
1610 frameNew->color = copy_bitmap( frameOld->color );
1611 frameNew->alpha = copy_bitmap( frameOld->alpha );
1612 release_icon_frame( ptrOld, 0, frameOld );
1613 release_icon_frame( ptrNew, 0, frameNew );
1614 release_icon_ptr( hNew, ptrNew );
1616 release_icon_ptr( hIcon, ptrOld );
1617 return hNew;
1621 /***********************************************************************
1622 * DestroyIcon (USER32.@)
1624 BOOL WINAPI DestroyIcon( HICON hIcon )
1626 BOOL ret = FALSE;
1627 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1629 TRACE_(icon)("%p\n", hIcon );
1631 if (obj)
1633 BOOL shared = (obj->rsrc != NULL);
1634 release_icon_ptr( hIcon, obj );
1635 ret = (GetCursor() != hIcon);
1636 if (!shared) free_icon_handle( hIcon );
1638 return ret;
1642 /***********************************************************************
1643 * DestroyCursor (USER32.@)
1645 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1647 return DestroyIcon( hCursor );
1650 /***********************************************************************
1651 * DrawIcon (USER32.@)
1653 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1655 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1658 /***********************************************************************
1659 * SetCursor (USER32.@)
1661 * Set the cursor shape.
1663 * RETURNS
1664 * A handle to the previous cursor shape.
1666 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1668 struct cursoricon_object *obj;
1669 HCURSOR hOldCursor;
1670 int show_count;
1671 BOOL ret;
1673 TRACE("%p\n", hCursor);
1675 SERVER_START_REQ( set_cursor )
1677 req->flags = SET_CURSOR_HANDLE;
1678 req->handle = wine_server_user_handle( hCursor );
1679 if ((ret = !wine_server_call_err( req )))
1681 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1682 show_count = reply->prev_count;
1685 SERVER_END_REQ;
1687 if (!ret) return 0;
1688 USER_Driver->pSetCursor( show_count >= 0 ? hCursor : 0 );
1690 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1691 release_icon_ptr( hOldCursor, obj );
1692 return hOldCursor;
1695 /***********************************************************************
1696 * ShowCursor (USER32.@)
1698 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1700 HCURSOR cursor;
1701 int increment = bShow ? 1 : -1;
1702 int count;
1704 SERVER_START_REQ( set_cursor )
1706 req->flags = SET_CURSOR_COUNT;
1707 req->show_count = increment;
1708 wine_server_call( req );
1709 cursor = wine_server_ptr_handle( reply->prev_handle );
1710 count = reply->prev_count + increment;
1712 SERVER_END_REQ;
1714 TRACE("%d, count=%d\n", bShow, count );
1716 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1717 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1719 return count;
1722 /***********************************************************************
1723 * GetCursor (USER32.@)
1725 HCURSOR WINAPI GetCursor(void)
1727 HCURSOR ret;
1729 SERVER_START_REQ( set_cursor )
1731 req->flags = 0;
1732 wine_server_call( req );
1733 ret = wine_server_ptr_handle( reply->prev_handle );
1735 SERVER_END_REQ;
1736 return ret;
1740 /***********************************************************************
1741 * ClipCursor (USER32.@)
1743 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1745 BOOL ret;
1746 RECT new_rect;
1748 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1750 if (rect && (rect->left > rect->right || rect->top > rect->bottom)) return FALSE;
1752 SERVER_START_REQ( set_cursor )
1754 req->clip_msg = WM_WINE_CLIPCURSOR;
1755 if (rect)
1757 req->flags = SET_CURSOR_CLIP;
1758 req->clip.left = rect->left;
1759 req->clip.top = rect->top;
1760 req->clip.right = rect->right;
1761 req->clip.bottom = rect->bottom;
1763 else req->flags = SET_CURSOR_NOCLIP;
1765 if ((ret = !wine_server_call( req )))
1767 new_rect.left = reply->new_clip.left;
1768 new_rect.top = reply->new_clip.top;
1769 new_rect.right = reply->new_clip.right;
1770 new_rect.bottom = reply->new_clip.bottom;
1773 SERVER_END_REQ;
1774 if (ret) USER_Driver->pClipCursor( &new_rect );
1775 return ret;
1779 /***********************************************************************
1780 * GetClipCursor (USER32.@)
1782 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1784 BOOL ret;
1786 if (!rect) return FALSE;
1788 SERVER_START_REQ( set_cursor )
1790 req->flags = 0;
1791 if ((ret = !wine_server_call( req )))
1793 rect->left = reply->new_clip.left;
1794 rect->top = reply->new_clip.top;
1795 rect->right = reply->new_clip.right;
1796 rect->bottom = reply->new_clip.bottom;
1799 SERVER_END_REQ;
1800 return ret;
1804 /***********************************************************************
1805 * SetSystemCursor (USER32.@)
1807 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1809 FIXME("(%p,%08x),stub!\n", hcur, id);
1810 return TRUE;
1814 /**********************************************************************
1815 * LookupIconIdFromDirectoryEx (USER32.@)
1817 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1818 INT width, INT height, UINT cFlag )
1820 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1821 UINT retVal = 0;
1822 if( dir && !dir->idReserved && (dir->idType & 3) )
1824 const CURSORICONDIRENTRY* entry;
1826 const HDC hdc = GetDC(0);
1827 const int depth = (cFlag & LR_MONOCHROME) ?
1828 1 : GetDeviceCaps(hdc, BITSPIXEL);
1829 ReleaseDC(0, hdc);
1831 if( bIcon )
1832 entry = CURSORICON_FindBestIconRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1833 else
1834 entry = CURSORICON_FindBestCursorRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1836 if( entry ) retVal = entry->wResId;
1838 else WARN_(cursor)("invalid resource directory\n");
1839 return retVal;
1842 /**********************************************************************
1843 * LookupIconIdFromDirectory (USER32.@)
1845 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1847 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1850 /***********************************************************************
1851 * LoadCursorW (USER32.@)
1853 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1855 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1857 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1858 LR_SHARED | LR_DEFAULTSIZE );
1861 /***********************************************************************
1862 * LoadCursorA (USER32.@)
1864 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1866 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1868 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1869 LR_SHARED | LR_DEFAULTSIZE );
1872 /***********************************************************************
1873 * LoadCursorFromFileW (USER32.@)
1875 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1877 TRACE("%s\n", debugstr_w(name));
1879 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1880 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1883 /***********************************************************************
1884 * LoadCursorFromFileA (USER32.@)
1886 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1888 TRACE("%s\n", debugstr_a(name));
1890 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1891 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1894 /***********************************************************************
1895 * LoadIconW (USER32.@)
1897 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1899 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1901 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1902 LR_SHARED | LR_DEFAULTSIZE );
1905 /***********************************************************************
1906 * LoadIconA (USER32.@)
1908 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1910 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1912 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1913 LR_SHARED | LR_DEFAULTSIZE );
1916 /**********************************************************************
1917 * GetCursorFrameInfo (USER32.@)
1919 * NOTES
1920 * So far no use has been found for the second parameter, it is currently presumed
1921 * that this parameter is reserved for future use.
1923 * PARAMS
1924 * hCursor [I] Handle to cursor for which to retrieve information
1925 * reserved [I] No purpose has been found for this parameter (may be NULL)
1926 * istep [I] The step of the cursor for which to retrieve information
1927 * rate_jiffies [O] Pointer to DWORD that receives the frame-specific delay (cannot be NULL)
1928 * num_steps [O] Pointer to DWORD that receives the number of steps in the cursor (cannot be NULL)
1930 * RETURNS
1931 * Success: Handle to a frame of the cursor (specified by istep)
1932 * Failure: NULL cursor (0)
1934 HCURSOR WINAPI GetCursorFrameInfo(HCURSOR hCursor, DWORD reserved, DWORD istep, DWORD *rate_jiffies, DWORD *num_steps)
1936 struct cursoricon_object *ptr;
1937 HCURSOR ret = 0;
1938 UINT icon_steps;
1940 if (rate_jiffies == NULL || num_steps == NULL) return 0;
1942 if (!(ptr = get_icon_ptr( hCursor ))) return 0;
1944 TRACE("%p => %d %d %p %p\n", hCursor, reserved, istep, rate_jiffies, num_steps);
1945 if (reserved != 0)
1946 FIXME("Second parameter non-zero (%d), please report this!\n", reserved);
1948 icon_steps = get_icon_steps(ptr);
1949 if (istep < icon_steps || !ptr->is_ani)
1951 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) ptr;
1952 UINT icon_frames = 1;
1954 if (ptr->is_ani)
1955 icon_frames = ani_icon_data->num_frames;
1956 if (ptr->is_ani && icon_frames > 1)
1957 ret = ani_icon_data->frames[istep];
1958 else
1959 ret = hCursor;
1960 if (icon_frames == 1)
1962 *rate_jiffies = 0;
1963 *num_steps = 1;
1965 else if (icon_steps == 1)
1967 *num_steps = ~0;
1968 *rate_jiffies = ptr->delay;
1970 else if (istep < icon_steps)
1972 struct cursoricon_frame *frame;
1974 *num_steps = icon_steps;
1975 frame = get_icon_frame( ptr, istep );
1976 if (get_icon_steps(ptr) == 1)
1977 *num_steps = ~0;
1978 else
1979 *num_steps = get_icon_steps(ptr);
1980 /* If this specific frame does not have a delay then use the global delay */
1981 if (frame->delay == ~0)
1982 *rate_jiffies = ptr->delay;
1983 else
1984 *rate_jiffies = frame->delay;
1985 release_icon_frame( ptr, istep, frame );
1989 release_icon_ptr( hCursor, ptr );
1991 return ret;
1994 /**********************************************************************
1995 * GetIconInfo (USER32.@)
1997 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1999 ICONINFOEXW infoW;
2001 infoW.cbSize = sizeof(infoW);
2002 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
2003 iconinfo->fIcon = infoW.fIcon;
2004 iconinfo->xHotspot = infoW.xHotspot;
2005 iconinfo->yHotspot = infoW.yHotspot;
2006 iconinfo->hbmColor = infoW.hbmColor;
2007 iconinfo->hbmMask = infoW.hbmMask;
2008 return TRUE;
2011 /**********************************************************************
2012 * GetIconInfoExA (USER32.@)
2014 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
2016 ICONINFOEXW infoW;
2018 if (info->cbSize != sizeof(*info))
2020 SetLastError( ERROR_INVALID_PARAMETER );
2021 return FALSE;
2023 infoW.cbSize = sizeof(infoW);
2024 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
2025 info->fIcon = infoW.fIcon;
2026 info->xHotspot = infoW.xHotspot;
2027 info->yHotspot = infoW.yHotspot;
2028 info->hbmColor = infoW.hbmColor;
2029 info->hbmMask = infoW.hbmMask;
2030 info->wResID = infoW.wResID;
2031 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
2032 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
2033 return TRUE;
2036 /**********************************************************************
2037 * GetIconInfoExW (USER32.@)
2039 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
2041 struct cursoricon_frame *frame;
2042 struct cursoricon_object *ptr;
2043 HMODULE module;
2044 BOOL ret = TRUE;
2046 if (info->cbSize != sizeof(*info))
2048 SetLastError( ERROR_INVALID_PARAMETER );
2049 return FALSE;
2051 if (!(ptr = get_icon_ptr( icon )))
2053 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2054 return FALSE;
2057 frame = get_icon_frame( ptr, 0 );
2058 if (!frame)
2060 release_icon_ptr( icon, ptr );
2061 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2062 return FALSE;
2065 TRACE("%p => %dx%d\n", icon, frame->width, frame->height);
2067 info->fIcon = ptr->is_icon;
2068 info->xHotspot = ptr->hotspot.x;
2069 info->yHotspot = ptr->hotspot.y;
2070 info->hbmColor = copy_bitmap( frame->color );
2071 info->hbmMask = copy_bitmap( frame->mask );
2072 info->wResID = 0;
2073 info->szModName[0] = 0;
2074 info->szResName[0] = 0;
2075 if (ptr->module)
2077 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
2078 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
2080 if (!info->hbmMask || (!info->hbmColor && frame->color))
2082 DeleteObject( info->hbmMask );
2083 DeleteObject( info->hbmColor );
2084 ret = FALSE;
2086 module = ptr->module;
2087 release_icon_frame( ptr, 0, frame );
2088 release_icon_ptr( icon, ptr );
2089 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
2090 return ret;
2093 /* copy an icon bitmap, even when it can't be selected into a DC */
2094 /* helper for CreateIconIndirect */
2095 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
2096 HBITMAP src, int width, int height )
2098 HDC hdc = CreateCompatibleDC( 0 );
2100 if (!SelectObject( hdc, src )) /* do it the hard way */
2102 BITMAPINFO *info;
2103 void *bits;
2105 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
2106 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2107 info->bmiHeader.biWidth = width;
2108 info->bmiHeader.biHeight = height;
2109 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
2110 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
2111 info->bmiHeader.biCompression = BI_RGB;
2112 info->bmiHeader.biSizeImage = get_dib_image_size( width, height, info->bmiHeader.biBitCount );
2113 info->bmiHeader.biXPelsPerMeter = 0;
2114 info->bmiHeader.biYPelsPerMeter = 0;
2115 info->bmiHeader.biClrUsed = 0;
2116 info->bmiHeader.biClrImportant = 0;
2117 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
2118 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
2119 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
2120 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
2122 HeapFree( GetProcessHeap(), 0, bits );
2123 HeapFree( GetProcessHeap(), 0, info );
2125 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
2127 DeleteDC( hdc );
2130 /**********************************************************************
2131 * CreateIconIndirect (USER32.@)
2133 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2135 BITMAP bmpXor, bmpAnd;
2136 HICON hObj;
2137 HBITMAP color = 0, mask;
2138 int width, height;
2139 HDC hdc;
2141 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2142 iconinfo->hbmColor, iconinfo->hbmMask,
2143 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2145 if (!iconinfo->hbmMask) return 0;
2147 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2148 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2149 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2150 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2152 if (iconinfo->hbmColor)
2154 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2155 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2156 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2157 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2159 width = bmpXor.bmWidth;
2160 height = bmpXor.bmHeight;
2161 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1 || bmpAnd.bmPlanes * bmpAnd.bmBitsPixel != 1)
2163 color = CreateCompatibleBitmap( screen_dc, width, height );
2164 mask = CreateBitmap( width, height, 1, 1, NULL );
2166 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2168 else
2170 width = bmpAnd.bmWidth;
2171 height = bmpAnd.bmHeight;
2172 mask = CreateBitmap( width, height, 1, 1, NULL );
2175 hdc = CreateCompatibleDC( 0 );
2176 SelectObject( hdc, mask );
2177 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
2179 if (color)
2181 SelectObject( hdc, color );
2182 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
2184 else if (iconinfo->hbmColor)
2186 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
2188 else height /= 2;
2190 DeleteDC( hdc );
2192 hObj = alloc_icon_handle( FALSE, 1 );
2193 if (hObj)
2195 struct cursoricon_object *info = get_icon_ptr( hObj );
2196 struct cursoricon_frame *frame;
2198 info->is_icon = iconinfo->fIcon;
2199 frame = get_icon_frame( info, 0 );
2200 frame->delay = ~0;
2201 frame->width = width;
2202 frame->height = height;
2203 frame->color = color;
2204 frame->mask = mask;
2205 frame->alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
2206 release_icon_frame( info, 0, frame );
2207 if (info->is_icon)
2209 info->hotspot.x = width / 2;
2210 info->hotspot.y = height / 2;
2212 else
2214 info->hotspot.x = iconinfo->xHotspot;
2215 info->hotspot.y = iconinfo->yHotspot;
2218 release_icon_ptr( hObj, info );
2220 return hObj;
2223 /******************************************************************************
2224 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2226 * NOTES
2227 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2229 * PARAMS
2230 * hdc [I] Handle to device context
2231 * x0 [I] X coordinate of upper left corner
2232 * y0 [I] Y coordinate of upper left corner
2233 * hIcon [I] Handle to icon to draw
2234 * cxWidth [I] Width of icon
2235 * cyWidth [I] Height of icon
2236 * istep [I] Index of frame in animated cursor
2237 * hbr [I] Handle to background brush
2238 * flags [I] Icon-drawing flags
2240 * RETURNS
2241 * Success: TRUE
2242 * Failure: FALSE
2244 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2245 INT cxWidth, INT cyWidth, UINT istep,
2246 HBRUSH hbr, UINT flags )
2248 struct cursoricon_frame *frame;
2249 struct cursoricon_object *ptr;
2250 HDC hdc_dest, hMemDC;
2251 BOOL result = FALSE, DoOffscreen;
2252 HBITMAP hB_off = 0;
2253 COLORREF oldFg, oldBg;
2254 INT x, y, nStretchMode;
2256 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2257 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2259 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2260 if (istep >= get_icon_steps( ptr ))
2262 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
2263 release_icon_ptr( hIcon, ptr );
2264 return FALSE;
2266 if (!(frame = get_icon_frame( ptr, istep )))
2268 FIXME_(icon)("Error retrieving icon frame %d\n", istep);
2269 release_icon_ptr( hIcon, ptr );
2270 return FALSE;
2272 if (!(hMemDC = CreateCompatibleDC( hdc )))
2274 release_icon_frame( ptr, istep, frame );
2275 release_icon_ptr( hIcon, ptr );
2276 return FALSE;
2279 if (flags & DI_NOMIRROR)
2280 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2282 /* Calculate the size of the destination image. */
2283 if (cxWidth == 0)
2285 if (flags & DI_DEFAULTSIZE)
2286 cxWidth = GetSystemMetrics (SM_CXICON);
2287 else
2288 cxWidth = frame->width;
2290 if (cyWidth == 0)
2292 if (flags & DI_DEFAULTSIZE)
2293 cyWidth = GetSystemMetrics (SM_CYICON);
2294 else
2295 cyWidth = frame->height;
2298 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2300 if (DoOffscreen) {
2301 RECT r;
2303 r.left = 0;
2304 r.top = 0;
2305 r.right = cxWidth;
2306 r.bottom = cxWidth;
2308 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2309 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2311 DeleteDC( hdc_dest );
2312 goto failed;
2314 SelectObject(hdc_dest, hB_off);
2315 FillRect(hdc_dest, &r, hbr);
2316 x = y = 0;
2318 else
2320 hdc_dest = hdc;
2321 x = x0;
2322 y = y0;
2325 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2327 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2328 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2330 if (frame->alpha && (flags & DI_IMAGE))
2332 BOOL alpha_blend = TRUE;
2334 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2336 BITMAP bm;
2337 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2338 alpha_blend = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel > 8;
2340 if (alpha_blend)
2342 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2343 SelectObject( hMemDC, frame->alpha );
2344 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2345 0, 0, frame->width, frame->height,
2346 pixelblend )) goto done;
2350 if (flags & DI_MASK)
2352 DWORD rop = (flags & DI_IMAGE) ? SRCAND : SRCCOPY;
2353 SelectObject( hMemDC, frame->mask );
2354 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2355 hMemDC, 0, 0, frame->width, frame->height, rop );
2358 if (flags & DI_IMAGE)
2360 if (frame->color)
2362 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2363 SelectObject( hMemDC, frame->color );
2364 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2365 hMemDC, 0, 0, frame->width, frame->height, rop );
2367 else
2369 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2370 SelectObject( hMemDC, frame->mask );
2371 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2372 hMemDC, 0, frame->height, frame->width,
2373 frame->height, rop );
2377 done:
2378 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2380 SetTextColor( hdc, oldFg );
2381 SetBkColor( hdc, oldBg );
2382 SetStretchBltMode (hdc, nStretchMode);
2383 result = TRUE;
2384 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2385 if (hB_off) DeleteObject(hB_off);
2386 failed:
2387 DeleteDC( hMemDC );
2388 release_icon_frame( ptr, istep, frame );
2389 release_icon_ptr( hIcon, ptr );
2390 return result;
2393 /***********************************************************************
2394 * DIB_FixColorsToLoadflags
2396 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2397 * are in loadflags
2399 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2401 int colors;
2402 COLORREF c_W, c_S, c_F, c_L, c_C;
2403 int incr,i;
2404 RGBQUAD *ptr;
2405 int bitmap_type;
2406 LONG width;
2407 LONG height;
2408 WORD bpp;
2409 DWORD compr;
2411 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2413 WARN_(resource)("Invalid bitmap\n");
2414 return;
2417 if (bpp > 8) return;
2419 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2421 incr = 3;
2422 colors = 1 << bpp;
2424 else
2426 incr = 4;
2427 colors = bmi->bmiHeader.biClrUsed;
2428 if (colors > 256) colors = 256;
2429 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2432 c_W = GetSysColor(COLOR_WINDOW);
2433 c_S = GetSysColor(COLOR_3DSHADOW);
2434 c_F = GetSysColor(COLOR_3DFACE);
2435 c_L = GetSysColor(COLOR_3DLIGHT);
2437 if (loadflags & LR_LOADTRANSPARENT) {
2438 switch (bpp) {
2439 case 1: pix = pix >> 7; break;
2440 case 4: pix = pix >> 4; break;
2441 case 8: break;
2442 default:
2443 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2444 return;
2446 if (pix >= colors) {
2447 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2448 return;
2450 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2451 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2452 ptr->rgbBlue = GetBValue(c_W);
2453 ptr->rgbGreen = GetGValue(c_W);
2454 ptr->rgbRed = GetRValue(c_W);
2456 if (loadflags & LR_LOADMAP3DCOLORS)
2457 for (i=0; i<colors; i++) {
2458 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2459 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2460 if (c_C == RGB(128, 128, 128)) {
2461 ptr->rgbRed = GetRValue(c_S);
2462 ptr->rgbGreen = GetGValue(c_S);
2463 ptr->rgbBlue = GetBValue(c_S);
2464 } else if (c_C == RGB(192, 192, 192)) {
2465 ptr->rgbRed = GetRValue(c_F);
2466 ptr->rgbGreen = GetGValue(c_F);
2467 ptr->rgbBlue = GetBValue(c_F);
2468 } else if (c_C == RGB(223, 223, 223)) {
2469 ptr->rgbRed = GetRValue(c_L);
2470 ptr->rgbGreen = GetGValue(c_L);
2471 ptr->rgbBlue = GetBValue(c_L);
2477 /**********************************************************************
2478 * BITMAP_Load
2480 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2481 INT desiredx, INT desiredy, UINT loadflags )
2483 HBITMAP hbitmap = 0, orig_bm;
2484 HRSRC hRsrc;
2485 HGLOBAL handle;
2486 const char *ptr = NULL;
2487 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2488 int size;
2489 BYTE pix;
2490 char *bits;
2491 LONG width, height, new_width, new_height;
2492 WORD bpp_dummy;
2493 DWORD compr_dummy, offbits = 0;
2494 INT bm_type;
2495 HDC screen_mem_dc = NULL;
2497 if (!(loadflags & LR_LOADFROMFILE))
2499 if (!instance)
2501 /* OEM bitmap: try to load the resource from user32.dll */
2502 instance = user32_module;
2505 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2506 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2508 if ((info = LockResource( handle )) == NULL) return 0;
2510 else
2512 BITMAPFILEHEADER * bmfh;
2514 if (!(ptr = map_fileW( name, NULL ))) return 0;
2515 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2516 bmfh = (BITMAPFILEHEADER *)ptr;
2517 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2519 WARN("Invalid/unsupported bitmap format!\n");
2520 goto end;
2522 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2525 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2526 &bpp_dummy, &compr_dummy);
2527 if (bm_type == -1)
2529 WARN("Invalid bitmap format!\n");
2530 goto end;
2533 size = bitmap_info_size(info, DIB_RGB_COLORS);
2534 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2535 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2537 if (!fix_info || !scaled_info) goto end;
2538 memcpy(fix_info, info, size);
2540 pix = *((LPBYTE)info + size);
2541 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2543 memcpy(scaled_info, fix_info, size);
2545 if(desiredx != 0)
2546 new_width = desiredx;
2547 else
2548 new_width = width;
2550 if(desiredy != 0)
2551 new_height = height > 0 ? desiredy : -desiredy;
2552 else
2553 new_height = height;
2555 if(bm_type == 0)
2557 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2558 core->bcWidth = new_width;
2559 core->bcHeight = new_height;
2561 else
2563 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2564 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2565 WARN("Broken BitmapInfoHeader!\n");
2566 goto end;
2569 scaled_info->bmiHeader.biWidth = new_width;
2570 scaled_info->bmiHeader.biHeight = new_height;
2573 if (new_height < 0) new_height = -new_height;
2575 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2576 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2578 bits = (char *)info + (offbits ? offbits : size);
2580 if (loadflags & LR_CREATEDIBSECTION)
2582 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2583 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2585 else
2587 if (is_dib_monochrome(fix_info))
2588 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2589 else
2590 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2593 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2594 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2595 SelectObject(screen_mem_dc, orig_bm);
2597 end:
2598 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2599 HeapFree(GetProcessHeap(), 0, scaled_info);
2600 HeapFree(GetProcessHeap(), 0, fix_info);
2601 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2603 return hbitmap;
2606 /**********************************************************************
2607 * LoadImageA (USER32.@)
2609 * See LoadImageW.
2611 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2612 INT desiredx, INT desiredy, UINT loadflags)
2614 HANDLE res;
2615 LPWSTR u_name;
2617 if (IS_INTRESOURCE(name))
2618 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2620 __TRY {
2621 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2622 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2623 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2625 __EXCEPT_PAGE_FAULT {
2626 SetLastError( ERROR_INVALID_PARAMETER );
2627 return 0;
2629 __ENDTRY
2630 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2631 HeapFree(GetProcessHeap(), 0, u_name);
2632 return res;
2636 /******************************************************************************
2637 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2639 * PARAMS
2640 * hinst [I] Handle of instance that contains image
2641 * name [I] Name of image
2642 * type [I] Type of image
2643 * desiredx [I] Desired width
2644 * desiredy [I] Desired height
2645 * loadflags [I] Load flags
2647 * RETURNS
2648 * Success: Handle to newly loaded image
2649 * Failure: NULL
2651 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2653 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2654 INT desiredx, INT desiredy, UINT loadflags )
2656 int depth;
2658 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2659 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2661 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2662 switch (type) {
2663 case IMAGE_BITMAP:
2664 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2666 case IMAGE_ICON:
2667 case IMAGE_CURSOR:
2668 depth = 1;
2669 if (!(loadflags & LR_MONOCHROME))
2671 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2672 if (screen_dc) depth = GetDeviceCaps( screen_dc, BITSPIXEL );
2674 return CURSORICON_Load(hinst, name, desiredx, desiredy, depth, (type == IMAGE_CURSOR), loadflags);
2676 return 0;
2679 /******************************************************************************
2680 * CopyImage (USER32.@) Creates new image and copies attributes to it
2682 * PARAMS
2683 * hnd [I] Handle to image to copy
2684 * type [I] Type of image to copy
2685 * desiredx [I] Desired width of new image
2686 * desiredy [I] Desired height of new image
2687 * flags [I] Copy flags
2689 * RETURNS
2690 * Success: Handle to newly created image
2691 * Failure: NULL
2693 * BUGS
2694 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2695 * all other versions (95/2000/XP have been tested) ignore it.
2697 * NOTES
2698 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2699 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2700 * the copy will have the same depth as the screen.
2701 * The content of the image will only be copied if the bit depth of the
2702 * original image is compatible with the bit depth of the screen, or
2703 * if the source is a DIB section.
2704 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2706 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2707 INT desiredy, UINT flags )
2709 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2710 hnd, type, desiredx, desiredy, flags);
2712 switch (type)
2714 case IMAGE_BITMAP:
2716 HBITMAP res = NULL;
2717 DIBSECTION ds;
2718 int objSize;
2719 BITMAPINFO * bi;
2721 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2722 if (!objSize) return 0;
2723 if ((desiredx < 0) || (desiredy < 0)) return 0;
2725 if (flags & LR_COPYFROMRESOURCE)
2727 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2730 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2731 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2733 /* Allocate memory for a BITMAPINFOHEADER structure and a
2734 color table. The maximum number of colors in a color table
2735 is 256 which corresponds to a bitmap with depth 8.
2736 Bitmaps with higher depths don't have color tables. */
2737 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2738 if (!bi) return 0;
2740 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2741 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2742 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2743 bi->bmiHeader.biCompression = BI_RGB;
2745 if (flags & LR_CREATEDIBSECTION)
2747 /* Create a DIB section. LR_MONOCHROME is ignored */
2748 void * bits;
2749 HDC dc = CreateCompatibleDC(NULL);
2751 if (objSize == sizeof(DIBSECTION))
2753 /* The source bitmap is a DIB.
2754 Get its attributes to create an exact copy */
2755 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2758 bi->bmiHeader.biWidth = desiredx;
2759 bi->bmiHeader.biHeight = desiredy;
2761 /* Get the color table or the color masks */
2762 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2764 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2765 DeleteDC(dc);
2767 else
2769 /* Create a device-dependent bitmap */
2771 BOOL monochrome = (flags & LR_MONOCHROME);
2773 if (objSize == sizeof(DIBSECTION))
2775 /* The source bitmap is a DIB section.
2776 Get its attributes */
2777 HDC dc = CreateCompatibleDC(NULL);
2778 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2779 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2780 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2781 DeleteDC(dc);
2783 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2785 /* Look if the colors of the DIB are black and white */
2787 monochrome =
2788 (bi->bmiColors[0].rgbRed == 0xff
2789 && bi->bmiColors[0].rgbGreen == 0xff
2790 && bi->bmiColors[0].rgbBlue == 0xff
2791 && bi->bmiColors[0].rgbReserved == 0
2792 && bi->bmiColors[1].rgbRed == 0
2793 && bi->bmiColors[1].rgbGreen == 0
2794 && bi->bmiColors[1].rgbBlue == 0
2795 && bi->bmiColors[1].rgbReserved == 0)
2797 (bi->bmiColors[0].rgbRed == 0
2798 && bi->bmiColors[0].rgbGreen == 0
2799 && bi->bmiColors[0].rgbBlue == 0
2800 && bi->bmiColors[0].rgbReserved == 0
2801 && bi->bmiColors[1].rgbRed == 0xff
2802 && bi->bmiColors[1].rgbGreen == 0xff
2803 && bi->bmiColors[1].rgbBlue == 0xff
2804 && bi->bmiColors[1].rgbReserved == 0);
2807 else if (!monochrome)
2809 monochrome = ds.dsBm.bmBitsPixel == 1;
2812 if (monochrome)
2814 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2816 else
2818 HDC screenDC = GetDC(NULL);
2819 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2820 ReleaseDC(NULL, screenDC);
2824 if (res)
2826 /* Only copy the bitmap if it's a DIB section or if it's
2827 compatible to the screen */
2828 BOOL copyContents;
2830 if (objSize == sizeof(DIBSECTION))
2832 copyContents = TRUE;
2834 else
2836 HDC screenDC = GetDC(NULL);
2837 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2838 ReleaseDC(NULL, screenDC);
2840 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2843 if (copyContents)
2845 /* The source bitmap may already be selected in a device context,
2846 use GetDIBits/StretchDIBits and not StretchBlt */
2848 HDC dc;
2849 void * bits;
2851 dc = CreateCompatibleDC(NULL);
2853 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2854 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2855 bi->bmiHeader.biSizeImage = 0;
2856 bi->bmiHeader.biClrUsed = 0;
2857 bi->bmiHeader.biClrImportant = 0;
2859 /* Fill in biSizeImage */
2860 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2861 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2863 if (bits)
2865 HBITMAP oldBmp;
2867 /* Get the image bits of the source bitmap */
2868 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2870 /* Copy it to the destination bitmap */
2871 oldBmp = SelectObject(dc, res);
2872 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2873 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2874 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2875 SelectObject(dc, oldBmp);
2877 HeapFree(GetProcessHeap(), 0, bits);
2880 DeleteDC(dc);
2883 if (flags & LR_COPYDELETEORG)
2885 DeleteObject(hnd);
2888 HeapFree(GetProcessHeap(), 0, bi);
2889 return res;
2891 case IMAGE_ICON:
2892 case IMAGE_CURSOR:
2894 struct cursoricon_object *icon;
2895 HICON res = 0;
2896 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( screen_dc, BITSPIXEL );
2898 if (flags & LR_DEFAULTSIZE)
2900 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2901 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2904 if (!(icon = get_icon_ptr( hnd ))) return 0;
2906 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2907 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2908 !icon->is_icon, flags );
2909 else
2910 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2911 release_icon_ptr( hnd, icon );
2913 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2914 return res;
2917 return 0;
2921 /******************************************************************************
2922 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2924 * RETURNS
2925 * Success: Handle to specified bitmap
2926 * Failure: NULL
2928 HBITMAP WINAPI LoadBitmapW(
2929 HINSTANCE instance, /* [in] Handle to application instance */
2930 LPCWSTR name) /* [in] Address of bitmap resource name */
2932 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2935 /**********************************************************************
2936 * LoadBitmapA (USER32.@)
2938 * See LoadBitmapW.
2940 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2942 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );