kernel32: Add a stub for GetCurrentProcessorNumberEx.
[wine.git] / dlls / user32 / cursoricon.c
blobea3f2ef4b45ca56b0da3bd3dd02c9543d85eb6f0
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 struct cursoricon_frame *get_icon_frame( struct cursoricon_object *obj, int istep )
163 struct static_cursoricon_object *req_frame;
165 if (obj->is_ani)
167 struct animated_cursoricon_object *ani_icon_data;
168 struct cursoricon_object *frameobj;
170 ani_icon_data = (struct animated_cursoricon_object *) obj;
171 if (!(frameobj = get_icon_ptr( ani_icon_data->frames[istep] )))
172 return 0;
173 req_frame = (struct static_cursoricon_object *) frameobj;
175 else
176 req_frame = (struct static_cursoricon_object *) obj;
178 return &req_frame->frame;
181 static void release_icon_frame( struct cursoricon_object *obj, struct cursoricon_frame *frame )
183 if (obj->is_ani)
185 struct cursoricon_object *frameobj;
187 frameobj = (struct cursoricon_object *) (((char *)frame) - FIELD_OFFSET(struct static_cursoricon_object, frame));
188 release_user_handle_ptr( frameobj );
192 static UINT get_icon_steps( struct cursoricon_object *obj )
194 if (obj->is_ani)
196 struct animated_cursoricon_object *ani_icon_data;
198 ani_icon_data = (struct animated_cursoricon_object *) obj;
199 return ani_icon_data->num_steps;
201 return 1;
204 static BOOL free_icon_handle( HICON handle )
206 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
208 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
209 else if (obj)
211 ULONG_PTR param = obj->param;
212 UINT i;
214 assert( !obj->rsrc ); /* shared icons can't be freed */
216 if (!obj->is_ani)
218 struct cursoricon_frame *frame = get_icon_frame( obj, 0 );
220 if (frame->alpha) DeleteObject( frame->alpha );
221 if (frame->color) DeleteObject( frame->color );
222 DeleteObject( frame->mask );
223 release_icon_frame( obj, frame );
225 else
227 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
229 for (i=0; i<ani_icon_data->num_steps; i++)
231 HICON hFrame = ani_icon_data->frames[i];
233 if (hFrame)
235 UINT j;
237 free_icon_handle( ani_icon_data->frames[i] );
238 for (j=0; j<ani_icon_data->num_steps; j++)
240 if (ani_icon_data->frames[j] == hFrame)
241 ani_icon_data->frames[j] = 0;
246 if (!IS_INTRESOURCE( obj->resname )) HeapFree( GetProcessHeap(), 0, obj->resname );
247 HeapFree( GetProcessHeap(), 0, obj );
248 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
249 USER_Driver->pDestroyCursorIcon( handle );
250 return TRUE;
252 return FALSE;
255 ULONG_PTR get_icon_param( HICON handle )
257 ULONG_PTR ret = 0;
258 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
260 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
261 else if (obj)
263 ret = obj->param;
264 release_user_handle_ptr( obj );
266 return ret;
269 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
271 ULONG_PTR ret = 0;
272 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
274 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
275 else if (obj)
277 ret = obj->param;
278 obj->param = param;
279 release_user_handle_ptr( obj );
281 return ret;
285 /***********************************************************************
286 * map_fileW
288 * Helper function to map a file to memory:
289 * name - file name
290 * [RETURN] ptr - pointer to mapped file
291 * [RETURN] filesize - pointer size of file to be stored if not NULL
293 static const void *map_fileW( LPCWSTR name, LPDWORD filesize )
295 HANDLE hFile, hMapping;
296 LPVOID ptr = NULL;
298 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
299 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
300 if (hFile != INVALID_HANDLE_VALUE)
302 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
303 if (hMapping)
305 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
306 CloseHandle( hMapping );
307 if (filesize)
308 *filesize = GetFileSize( hFile, NULL );
310 CloseHandle( hFile );
312 return ptr;
316 /***********************************************************************
317 * get_dib_image_size
319 * Return the size of a DIB bitmap in bytes.
321 static int get_dib_image_size( int width, int height, int depth )
323 return (((width * depth + 31) / 8) & ~3) * abs( height );
327 /***********************************************************************
328 * bitmap_info_size
330 * Return the size of the bitmap info structure including color table.
332 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
334 unsigned int colors, size, masks = 0;
336 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
338 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
339 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
340 return sizeof(BITMAPCOREHEADER) + colors *
341 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
343 else /* assume BITMAPINFOHEADER */
345 colors = info->bmiHeader.biClrUsed;
346 if (colors > 256) /* buffer overflow otherwise */
347 colors = 256;
348 if (!colors && (info->bmiHeader.biBitCount <= 8))
349 colors = 1 << info->bmiHeader.biBitCount;
350 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
351 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
352 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
357 /***********************************************************************
358 * copy_bitmap
360 * Helper function to duplicate a bitmap.
362 static HBITMAP copy_bitmap( HBITMAP bitmap )
364 HDC src, dst = 0;
365 HBITMAP new_bitmap = 0;
366 BITMAP bmp;
368 if (!bitmap) return 0;
369 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
371 if ((src = CreateCompatibleDC( 0 )) && (dst = CreateCompatibleDC( 0 )))
373 SelectObject( src, bitmap );
374 if ((new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight )))
376 SelectObject( dst, new_bitmap );
377 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
380 DeleteDC( dst );
381 DeleteDC( src );
382 return new_bitmap;
386 /***********************************************************************
387 * is_dib_monochrome
389 * Returns whether a DIB can be converted to a monochrome DDB.
391 * A DIB can be converted if its color table contains only black and
392 * white. Black must be the first color in the color table.
394 * Note : If the first color in the color table is white followed by
395 * black, we can't convert it to a monochrome DDB with
396 * SetDIBits, because black and white would be inverted.
398 static BOOL is_dib_monochrome( const BITMAPINFO* info )
400 if (info->bmiHeader.biBitCount != 1) return FALSE;
402 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
404 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
406 /* Check if the first color is black */
407 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
409 rgb++;
411 /* Check if the second color is white */
412 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
413 && (rgb->rgbtBlue == 0xff));
415 else return FALSE;
417 else /* assume BITMAPINFOHEADER */
419 const RGBQUAD *rgb = info->bmiColors;
421 /* Check if the first color is black */
422 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
423 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
425 rgb++;
427 /* Check if the second color is white */
428 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
429 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
431 else return FALSE;
435 /***********************************************************************
436 * DIB_GetBitmapInfo
438 * Get the info from a bitmap header.
439 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
441 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
442 LONG *height, WORD *bpp, DWORD *compr )
444 if (header->biSize == sizeof(BITMAPCOREHEADER))
446 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
447 *width = core->bcWidth;
448 *height = core->bcHeight;
449 *bpp = core->bcBitCount;
450 *compr = 0;
451 return 0;
453 else if (header->biSize == sizeof(BITMAPINFOHEADER) ||
454 header->biSize == sizeof(BITMAPV4HEADER) ||
455 header->biSize == sizeof(BITMAPV5HEADER))
457 *width = header->biWidth;
458 *height = header->biHeight;
459 *bpp = header->biBitCount;
460 *compr = header->biCompression;
461 return 1;
463 WARN("unknown/wrong size (%u) for header\n", header->biSize);
464 return -1;
467 /**********************************************************************
468 * get_icon_size
470 BOOL get_icon_size( HICON handle, SIZE *size )
472 struct cursoricon_object *info;
473 struct cursoricon_frame *frame;
475 if (!(info = get_icon_ptr( handle ))) return FALSE;
476 frame = get_icon_frame( info, 0 );
477 size->cx = frame->width;
478 size->cy = frame->height;
479 release_icon_frame( info, frame);
480 release_user_handle_ptr( info );
481 return TRUE;
485 * The following macro functions account for the irregularities of
486 * accessing cursor and icon resources in files and resource entries.
488 typedef BOOL (*fnGetCIEntry)( LPCVOID dir, DWORD size, int n,
489 int *width, int *height, int *bits );
491 /**********************************************************************
492 * CURSORICON_FindBestIcon
494 * Find the icon closest to the requested size and bit depth.
496 static int CURSORICON_FindBestIcon( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
497 int width, int height, int depth, UINT loadflags )
499 int i, cx, cy, bits, bestEntry = -1;
500 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
501 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
503 /* Find Best Fit */
504 iTotalDiff = 0xFFFFFFFF;
505 iColorDiff = 0xFFFFFFFF;
507 if (loadflags & LR_DEFAULTSIZE)
509 if (!width) width = GetSystemMetrics( SM_CXICON );
510 if (!height) height = GetSystemMetrics( SM_CYICON );
512 else if (!width && !height)
514 /* use the size of the first entry */
515 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
516 iTotalDiff = 0;
519 for ( i = 0; iTotalDiff && get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
521 iTempXDiff = abs(width - cx);
522 iTempYDiff = abs(height - cy);
524 if(iTotalDiff > (iTempXDiff + iTempYDiff))
526 iXDiff = iTempXDiff;
527 iYDiff = iTempYDiff;
528 iTotalDiff = iXDiff + iYDiff;
532 /* Find Best Colors for Best Fit */
533 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
535 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
537 iTempColorDiff = abs(depth - bits);
538 if(iColorDiff > iTempColorDiff)
540 bestEntry = i;
541 iColorDiff = iTempColorDiff;
546 return bestEntry;
549 static BOOL CURSORICON_GetResIconEntry( LPCVOID dir, DWORD size, int n,
550 int *width, int *height, int *bits )
552 const CURSORICONDIR *resdir = dir;
553 const ICONRESDIR *icon;
555 if ( resdir->idCount <= n )
556 return FALSE;
557 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
558 return FALSE;
559 icon = &resdir->idEntries[n].ResInfo.icon;
560 *width = icon->bWidth;
561 *height = icon->bHeight;
562 *bits = resdir->idEntries[n].wBitCount;
563 return TRUE;
566 /**********************************************************************
567 * CURSORICON_FindBestCursor
569 * Find the cursor closest to the requested size.
571 * FIXME: parameter 'color' ignored.
573 static int CURSORICON_FindBestCursor( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
574 int width, int height, int depth, UINT loadflags )
576 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
578 if (loadflags & LR_DEFAULTSIZE)
580 if (!width) width = GetSystemMetrics( SM_CXCURSOR );
581 if (!height) height = GetSystemMetrics( SM_CYCURSOR );
583 else if (!width && !height)
585 /* use the first entry */
586 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
587 return 0;
590 /* Double height to account for AND and XOR masks */
592 height *= 2;
594 /* First find the largest one smaller than or equal to the requested size*/
596 maxwidth = maxheight = 0;
597 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
599 if ((cx <= width) && (cy <= height) &&
600 (cx > maxwidth) && (cy > maxheight))
602 bestEntry = i;
603 maxwidth = cx;
604 maxheight = cy;
607 if (bestEntry != -1) return bestEntry;
609 /* Now find the smallest one larger than the requested size */
611 maxwidth = maxheight = 255;
612 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
614 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
616 bestEntry = i;
617 maxwidth = cx;
618 maxheight = cy;
622 return bestEntry;
625 static BOOL CURSORICON_GetResCursorEntry( LPCVOID dir, DWORD size, int n,
626 int *width, int *height, int *bits )
628 const CURSORICONDIR *resdir = dir;
629 const CURSORDIR *cursor;
631 if ( resdir->idCount <= n )
632 return FALSE;
633 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
634 return FALSE;
635 cursor = &resdir->idEntries[n].ResInfo.cursor;
636 *width = cursor->wWidth;
637 *height = cursor->wHeight;
638 *bits = resdir->idEntries[n].wBitCount;
639 return TRUE;
642 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir, DWORD size,
643 int width, int height, int depth,
644 UINT loadflags )
646 int n;
648 n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetResIconEntry,
649 width, height, depth, loadflags );
650 if ( n < 0 )
651 return NULL;
652 return &dir->idEntries[n];
655 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir, DWORD size,
656 int width, int height, int depth,
657 UINT loadflags )
659 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetResCursorEntry,
660 width, height, depth, loadflags );
661 if ( n < 0 )
662 return NULL;
663 return &dir->idEntries[n];
666 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, DWORD size, int n,
667 int *width, int *height, int *bits )
669 const CURSORICONFILEDIR *filedir = dir;
670 const CURSORICONFILEDIRENTRY *entry;
671 const BITMAPINFOHEADER *info;
673 if ( filedir->idCount <= n )
674 return FALSE;
675 if ((const char *)&filedir->idEntries[n + 1] - (const char *)dir > size)
676 return FALSE;
677 entry = &filedir->idEntries[n];
678 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
679 if ((const char *)(info + 1) - (const char *)dir > size) return FALSE;
680 *width = entry->bWidth;
681 *height = entry->bHeight;
682 *bits = info->biBitCount;
683 return TRUE;
686 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir, DWORD size,
687 int width, int height, int depth,
688 UINT loadflags )
690 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetFileEntry,
691 width, height, depth, loadflags );
692 if ( n < 0 )
693 return NULL;
694 return &dir->idEntries[n];
697 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir, DWORD size,
698 int width, int height, int depth,
699 UINT loadflags )
701 int n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetFileEntry,
702 width, height, depth, loadflags );
703 if ( n < 0 )
704 return NULL;
705 return &dir->idEntries[n];
708 /***********************************************************************
709 * bmi_has_alpha
711 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
713 int i;
714 BOOL has_alpha = FALSE;
715 const unsigned char *ptr = bits;
717 if (info->bmiHeader.biBitCount != 32) return FALSE;
718 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
719 if ((has_alpha = (ptr[3] != 0))) break;
720 return has_alpha;
723 /***********************************************************************
724 * create_alpha_bitmap
726 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
728 static HBITMAP create_alpha_bitmap( HBITMAP color, const BITMAPINFO *src_info, const void *color_bits )
730 HBITMAP alpha = 0;
731 BITMAPINFO *info = NULL;
732 BITMAP bm;
733 HDC hdc;
734 void *bits;
735 unsigned char *ptr;
736 int i;
738 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
739 if (bm.bmBitsPixel != 32) return 0;
741 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
742 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
743 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
744 info->bmiHeader.biWidth = bm.bmWidth;
745 info->bmiHeader.biHeight = -bm.bmHeight;
746 info->bmiHeader.biPlanes = 1;
747 info->bmiHeader.biBitCount = 32;
748 info->bmiHeader.biCompression = BI_RGB;
749 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
750 info->bmiHeader.biXPelsPerMeter = 0;
751 info->bmiHeader.biYPelsPerMeter = 0;
752 info->bmiHeader.biClrUsed = 0;
753 info->bmiHeader.biClrImportant = 0;
754 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
756 if (src_info)
758 SelectObject( hdc, alpha );
759 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
760 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
761 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
764 else
766 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
767 if (!bmi_has_alpha( info, bits ))
769 DeleteObject( alpha );
770 alpha = 0;
771 goto done;
775 /* pre-multiply by alpha */
776 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
778 unsigned int alpha = ptr[3];
779 ptr[0] = ptr[0] * alpha / 255;
780 ptr[1] = ptr[1] * alpha / 255;
781 ptr[2] = ptr[2] * alpha / 255;
784 done:
785 DeleteDC( hdc );
786 HeapFree( GetProcessHeap(), 0, info );
787 return alpha;
791 /***********************************************************************
792 * create_icon_from_bmi
794 * Create an icon from its BITMAPINFO.
796 static HICON create_icon_from_bmi( const BITMAPINFO *bmi, DWORD maxsize, HMODULE module, LPCWSTR resname,
797 HRSRC rsrc, POINT hotspot, BOOL bIcon, INT width, INT height,
798 UINT cFlag )
800 DWORD size, color_size, mask_size;
801 HBITMAP color = 0, mask = 0, alpha = 0;
802 const void *color_bits, *mask_bits;
803 BITMAPINFO *bmi_copy;
804 BOOL ret = FALSE;
805 BOOL do_stretch;
806 HICON hObj = 0;
807 HDC hdc = 0;
809 /* Check bitmap header */
811 if (maxsize < sizeof(BITMAPCOREHEADER))
813 WARN( "invalid size %u\n", maxsize );
814 return 0;
816 if (maxsize < bmi->bmiHeader.biSize)
818 WARN( "invalid header size %u\n", bmi->bmiHeader.biSize );
819 return 0;
821 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
822 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
823 (bmi->bmiHeader.biCompression != BI_RGB &&
824 bmi->bmiHeader.biCompression != BI_BITFIELDS)) )
826 WARN( "invalid bitmap header %u\n", bmi->bmiHeader.biSize );
827 return 0;
830 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
831 color_size = get_dib_image_size( bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight / 2,
832 bmi->bmiHeader.biBitCount );
833 mask_size = get_dib_image_size( bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight / 2, 1 );
834 if (size > maxsize || color_size > maxsize - size)
836 WARN( "truncated file %u < %u+%u+%u\n", maxsize, size, color_size, mask_size );
837 return 0;
839 if (mask_size > maxsize - size - color_size) mask_size = 0; /* no mask */
841 if (cFlag & LR_DEFAULTSIZE)
843 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
844 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
846 else
848 if (!width) width = bmi->bmiHeader.biWidth;
849 if (!height) height = bmi->bmiHeader.biHeight/2;
851 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
852 (bmi->bmiHeader.biWidth != width);
854 /* Scale the hotspot */
855 if (bIcon)
857 hotspot.x = width / 2;
858 hotspot.y = height / 2;
860 else if (do_stretch)
862 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
863 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
866 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
867 if (!screen_dc) return 0;
869 if (!(bmi_copy = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
870 return 0;
871 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
873 memcpy( bmi_copy, bmi, size );
874 bmi_copy->bmiHeader.biHeight /= 2;
876 color_bits = (const char*)bmi + size;
877 mask_bits = (const char*)color_bits + color_size;
879 alpha = 0;
880 if (is_dib_monochrome( bmi ))
882 if (!(mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
883 color = 0;
885 /* copy color data into second half of mask bitmap */
886 SelectObject( hdc, mask );
887 StretchDIBits( hdc, 0, height, width, height,
888 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
889 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
891 else
893 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
894 if (!(color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
895 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
897 DeleteObject( mask );
898 goto done;
900 SelectObject( hdc, color );
901 StretchDIBits( hdc, 0, 0, width, height,
902 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
903 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
905 if (bmi_has_alpha( bmi_copy, color_bits ))
906 alpha = create_alpha_bitmap( color, bmi_copy, color_bits );
908 /* convert info to monochrome to copy the mask */
909 bmi_copy->bmiHeader.biBitCount = 1;
910 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
912 RGBQUAD *rgb = bmi_copy->bmiColors;
914 bmi_copy->bmiHeader.biClrUsed = bmi_copy->bmiHeader.biClrImportant = 2;
915 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
916 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
917 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
919 else
921 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)bmi_copy) + 1);
923 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
924 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
928 if (mask_size)
930 SelectObject( hdc, mask );
931 StretchDIBits( hdc, 0, 0, width, height,
932 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
933 mask_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
935 ret = TRUE;
937 done:
938 DeleteDC( hdc );
939 HeapFree( GetProcessHeap(), 0, bmi_copy );
941 if (ret)
942 hObj = alloc_icon_handle( FALSE, 0 );
943 if (hObj)
945 struct cursoricon_object *info = get_icon_ptr( hObj );
946 struct cursoricon_frame *frame;
948 info->is_icon = bIcon;
949 info->module = module;
950 info->hotspot = hotspot;
951 frame = get_icon_frame( info, 0 );
952 frame->delay = ~0;
953 frame->width = width;
954 frame->height = height;
955 frame->color = color;
956 frame->mask = mask;
957 frame->alpha = alpha;
958 release_icon_frame( info, frame );
959 if (!IS_INTRESOURCE(resname))
961 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
962 if (info->resname) strcpyW( info->resname, resname );
964 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
966 if (module && (cFlag & LR_SHARED))
968 info->rsrc = rsrc;
969 list_add_head( &icon_cache, &info->entry );
971 release_user_handle_ptr( info );
973 else
975 DeleteObject( color );
976 DeleteObject( alpha );
977 DeleteObject( mask );
979 return hObj;
983 /**********************************************************************
984 * .ANI cursor support
986 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
987 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
988 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
990 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
991 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
992 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
993 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
994 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
995 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
996 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
998 #define ANI_FLAG_ICON 0x1
999 #define ANI_FLAG_SEQUENCE 0x2
1001 typedef struct {
1002 DWORD header_size;
1003 DWORD num_frames;
1004 DWORD num_steps;
1005 DWORD width;
1006 DWORD height;
1007 DWORD bpp;
1008 DWORD num_planes;
1009 DWORD display_rate;
1010 DWORD flags;
1011 } ani_header;
1013 typedef struct {
1014 DWORD data_size;
1015 const unsigned char *data;
1016 } riff_chunk_t;
1018 static void dump_ani_header( const ani_header *header )
1020 TRACE(" header size: %d\n", header->header_size);
1021 TRACE(" frames: %d\n", header->num_frames);
1022 TRACE(" steps: %d\n", header->num_steps);
1023 TRACE(" width: %d\n", header->width);
1024 TRACE(" height: %d\n", header->height);
1025 TRACE(" bpp: %d\n", header->bpp);
1026 TRACE(" planes: %d\n", header->num_planes);
1027 TRACE(" display rate: %d\n", header->display_rate);
1028 TRACE(" flags: 0x%08x\n", header->flags);
1033 * RIFF:
1034 * DWORD "RIFF"
1035 * DWORD size
1036 * DWORD riff_id
1037 * BYTE[] data
1039 * LIST:
1040 * DWORD "LIST"
1041 * DWORD size
1042 * DWORD list_id
1043 * BYTE[] data
1045 * CHUNK:
1046 * DWORD chunk_id
1047 * DWORD size
1048 * BYTE[] data
1050 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1052 const unsigned char *ptr = parent_chunk->data;
1053 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1055 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1057 while (ptr < end)
1059 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1060 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1062 ptr += sizeof(DWORD);
1063 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1064 ptr += sizeof(DWORD);
1065 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1066 chunk->data = ptr;
1068 return;
1071 ptr += sizeof(DWORD);
1072 ptr += (*(const DWORD *)ptr + 1) & ~1;
1073 ptr += sizeof(DWORD);
1079 * .ANI layout:
1081 * RIFF:'ACON' RIFF chunk
1082 * |- CHUNK:'anih' Header
1083 * |- CHUNK:'seq ' Sequence information (optional)
1084 * \- LIST:'fram' Frame list
1085 * |- CHUNK:icon Cursor frames
1086 * |- CHUNK:icon
1087 * |- ...
1088 * \- CHUNK:icon
1090 static HCURSOR CURSORICON_CreateIconFromANI( const BYTE *bits, DWORD bits_size, INT width, INT height,
1091 INT depth, BOOL is_icon, UINT loadflags )
1093 struct animated_cursoricon_object *ani_icon_data;
1094 struct cursoricon_object *info;
1095 DWORD *frame_rates = NULL;
1096 DWORD *frame_seq = NULL;
1097 ani_header header = {0};
1098 BOOL use_seq = FALSE;
1099 HCURSOR cursor = 0;
1100 UINT i;
1101 BOOL error = FALSE;
1102 HICON *frames;
1104 riff_chunk_t root_chunk = { bits_size, bits };
1105 riff_chunk_t ACON_chunk = {0};
1106 riff_chunk_t anih_chunk = {0};
1107 riff_chunk_t fram_chunk = {0};
1108 riff_chunk_t rate_chunk = {0};
1109 riff_chunk_t seq_chunk = {0};
1110 const unsigned char *icon_chunk;
1111 const unsigned char *icon_data;
1113 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1115 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1116 if (!ACON_chunk.data)
1118 ERR("Failed to get root chunk.\n");
1119 return 0;
1122 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1123 if (!anih_chunk.data)
1125 ERR("Failed to get 'anih' chunk.\n");
1126 return 0;
1128 memcpy( &header, anih_chunk.data, sizeof(header) );
1129 dump_ani_header( &header );
1131 if (!(header.flags & ANI_FLAG_ICON))
1133 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1134 return 0;
1137 if (header.flags & ANI_FLAG_SEQUENCE)
1139 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1140 if (seq_chunk.data)
1142 frame_seq = (DWORD *) seq_chunk.data;
1143 use_seq = TRUE;
1145 else
1147 FIXME("Sequence data expected but not found, assuming steps == frames.\n");
1148 header.num_steps = header.num_frames;
1152 riff_find_chunk( ANI_rate_ID, 0, &ACON_chunk, &rate_chunk );
1153 if (rate_chunk.data)
1154 frame_rates = (DWORD *) rate_chunk.data;
1156 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1157 if (!fram_chunk.data)
1159 ERR("Failed to get icon list.\n");
1160 return 0;
1163 cursor = alloc_icon_handle( TRUE, header.num_steps );
1164 if (!cursor) return 0;
1165 frames = HeapAlloc( GetProcessHeap(), 0, sizeof(*frames) * header.num_frames );
1166 if (!frames)
1168 free_icon_handle( cursor );
1169 return 0;
1172 info = get_icon_ptr( cursor );
1173 ani_icon_data = (struct animated_cursoricon_object *) info;
1174 info->is_icon = is_icon;
1175 ani_icon_data->num_frames = header.num_frames;
1177 /* The .ANI stores the display rate in jiffies (1/60s) */
1178 info->delay = header.display_rate;
1180 icon_chunk = fram_chunk.data;
1181 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1182 for (i=0; i<header.num_frames; i++)
1184 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1185 const CURSORICONFILEDIRENTRY *entry;
1186 INT frameWidth, frameHeight;
1187 const BITMAPINFO *bmi;
1189 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1190 bits + bits_size - icon_data,
1191 width, height, depth, loadflags );
1193 info->hotspot.x = entry->xHotspot;
1194 info->hotspot.y = entry->yHotspot;
1195 if (!header.width || !header.height)
1197 frameWidth = entry->bWidth;
1198 frameHeight = entry->bHeight;
1200 else
1202 frameWidth = header.width;
1203 frameHeight = header.height;
1206 frames[i] = NULL;
1207 if (entry->dwDIBOffset < bits + bits_size - icon_data)
1209 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1210 /* Grab a frame from the animation */
1211 frames[i] = create_icon_from_bmi( bmi, bits + bits_size - (const BYTE *)bmi,
1212 NULL, NULL, NULL, info->hotspot,
1213 is_icon, frameWidth, frameHeight, loadflags );
1216 if (!frames[i])
1218 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1219 error = TRUE;
1220 if (i == 0)
1222 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1223 ani_icon_data->num_frames = 0;
1224 release_user_handle_ptr( info );
1225 free_icon_handle( cursor );
1226 HeapFree( GetProcessHeap(), 0, frames );
1227 return 0;
1229 break;
1232 /* Advance to the next chunk */
1233 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1234 icon_data = icon_chunk + (2 * sizeof(DWORD));
1237 /* There was an error but we at least decoded the first frame, so just use that frame */
1238 if (error)
1240 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1241 for (i=1; i<ani_icon_data->num_frames; i++)
1242 free_icon_handle( ani_icon_data->frames[i] );
1243 use_seq = FALSE;
1244 info->delay = 0;
1245 ani_icon_data->num_steps = 1;
1246 ani_icon_data->num_frames = 1;
1249 /* Setup the animated frames in the correct sequence */
1250 for (i=0; i<ani_icon_data->num_steps; i++)
1252 DWORD frame_id = use_seq ? frame_seq[i] : i;
1253 struct cursoricon_frame *frame;
1255 if (frame_id >= ani_icon_data->num_frames)
1257 frame_id = ani_icon_data->num_frames-1;
1258 ERR_(cursor)("Sequence indicates frame past end of list, corrupt?\n");
1260 ani_icon_data->frames[i] = frames[frame_id];
1261 frame = get_icon_frame( info, i );
1262 if (frame_rates)
1263 frame->delay = frame_rates[i];
1264 else
1265 frame->delay = ~0;
1266 release_icon_frame( info, frame );
1269 HeapFree( GetProcessHeap(), 0, frames );
1270 release_user_handle_ptr( info );
1272 return cursor;
1276 /**********************************************************************
1277 * CreateIconFromResourceEx (USER32.@)
1279 * FIXME: Convert to mono when cFlag is LR_MONOCHROME.
1281 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1282 BOOL bIcon, DWORD dwVersion,
1283 INT width, INT height,
1284 UINT cFlag )
1286 POINT hotspot;
1287 const BITMAPINFO *bmi;
1289 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1290 bits, cbSize, dwVersion, width, height,
1291 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1293 if (!bits) return 0;
1295 if (dwVersion == 0x00020000)
1297 FIXME_(cursor)("\t2.xx resources are not supported\n");
1298 return 0;
1301 /* Check if the resource is an animated icon/cursor */
1302 if (!memcmp(bits, "RIFF", 4))
1303 return CURSORICON_CreateIconFromANI( bits, cbSize, width, height,
1304 0 /* default depth */, bIcon, cFlag );
1306 if (bIcon)
1308 hotspot.x = width / 2;
1309 hotspot.y = height / 2;
1310 bmi = (BITMAPINFO *)bits;
1312 else /* get the hotspot */
1314 const SHORT *pt = (const SHORT *)bits;
1315 hotspot.x = pt[0];
1316 hotspot.y = pt[1];
1317 bmi = (const BITMAPINFO *)(pt + 2);
1318 cbSize -= 2 * sizeof(*pt);
1321 return create_icon_from_bmi( bmi, cbSize, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1325 /**********************************************************************
1326 * CreateIconFromResource (USER32.@)
1328 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1329 BOOL bIcon, DWORD dwVersion)
1331 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1335 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1336 INT width, INT height, INT depth,
1337 BOOL fCursor, UINT loadflags)
1339 const CURSORICONFILEDIRENTRY *entry;
1340 const CURSORICONFILEDIR *dir;
1341 DWORD filesize = 0;
1342 HICON hIcon = 0;
1343 const BYTE *bits;
1344 POINT hotspot;
1346 TRACE("loading %s\n", debugstr_w( filename ));
1348 bits = map_fileW( filename, &filesize );
1349 if (!bits)
1350 return hIcon;
1352 /* Check for .ani. */
1353 if (memcmp( bits, "RIFF", 4 ) == 0)
1355 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, !fCursor, loadflags );
1356 goto end;
1359 dir = (const CURSORICONFILEDIR*) bits;
1360 if ( filesize < FIELD_OFFSET( CURSORICONFILEDIR, idEntries[dir->idCount] ))
1361 goto end;
1363 if ( fCursor )
1364 entry = CURSORICON_FindBestCursorFile( dir, filesize, width, height, depth, loadflags );
1365 else
1366 entry = CURSORICON_FindBestIconFile( dir, filesize, width, height, depth, loadflags );
1368 if ( !entry )
1369 goto end;
1371 /* check that we don't run off the end of the file */
1372 if ( entry->dwDIBOffset > filesize )
1373 goto end;
1374 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1375 goto end;
1377 hotspot.x = entry->xHotspot;
1378 hotspot.y = entry->yHotspot;
1379 hIcon = create_icon_from_bmi( (const BITMAPINFO *)&bits[entry->dwDIBOffset], filesize - entry->dwDIBOffset,
1380 NULL, NULL, NULL, hotspot, !fCursor, width, height, loadflags );
1381 end:
1382 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1383 UnmapViewOfFile( bits );
1384 return hIcon;
1387 /**********************************************************************
1388 * CURSORICON_Load
1390 * Load a cursor or icon from resource or file.
1392 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1393 INT width, INT height, INT depth,
1394 BOOL fCursor, UINT loadflags)
1396 HANDLE handle = 0;
1397 HICON hIcon = 0;
1398 HRSRC hRsrc;
1399 DWORD size;
1400 const CURSORICONDIR *dir;
1401 const CURSORICONDIRENTRY *dirEntry;
1402 const BYTE *bits;
1403 WORD wResId;
1404 POINT hotspot;
1406 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1407 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1409 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1410 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1412 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1414 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1415 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1417 /* Get directory resource ID */
1419 if (!(hRsrc = FindResourceW( hInstance, name,
1420 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1422 /* try animated resource */
1423 if (!(hRsrc = FindResourceW( hInstance, name,
1424 (LPWSTR)(fCursor ? RT_ANICURSOR : RT_ANIICON) ))) return 0;
1425 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1426 bits = LockResource( handle );
1427 return CURSORICON_CreateIconFromANI( bits, SizeofResource( hInstance, handle ),
1428 width, height, depth, !fCursor, loadflags );
1431 /* Find the best entry in the directory */
1433 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1434 if (!(dir = LockResource( handle ))) return 0;
1435 size = SizeofResource( hInstance, hRsrc );
1436 if (fCursor)
1437 dirEntry = CURSORICON_FindBestCursorRes( dir, size, width, height, depth, loadflags );
1438 else
1439 dirEntry = CURSORICON_FindBestIconRes( dir, size, width, height, depth, loadflags );
1440 if (!dirEntry) return 0;
1441 wResId = dirEntry->wResId;
1442 FreeResource( handle );
1444 /* Load the resource */
1446 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1447 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1449 /* If shared icon, check whether it was already loaded */
1450 if (loadflags & LR_SHARED)
1452 struct cursoricon_object *ptr;
1454 USER_Lock();
1455 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1457 if (ptr->module != hInstance) continue;
1458 if (ptr->rsrc != hRsrc) continue;
1459 hIcon = ptr->obj.handle;
1460 break;
1462 USER_Unlock();
1463 if (hIcon) return hIcon;
1466 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1467 size = SizeofResource( hInstance, hRsrc );
1468 bits = LockResource( handle );
1470 if (!fCursor)
1472 hotspot.x = width / 2;
1473 hotspot.y = height / 2;
1475 else /* get the hotspot */
1477 const SHORT *pt = (const SHORT *)bits;
1478 hotspot.x = pt[0];
1479 hotspot.y = pt[1];
1480 bits += 2 * sizeof(SHORT);
1481 size -= 2 * sizeof(SHORT);
1483 hIcon = create_icon_from_bmi( (const BITMAPINFO *)bits, size, hInstance, name, hRsrc,
1484 hotspot, !fCursor, width, height, loadflags );
1485 FreeResource( handle );
1486 return hIcon;
1490 /***********************************************************************
1491 * CreateCursor (USER32.@)
1493 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1494 INT xHotSpot, INT yHotSpot,
1495 INT nWidth, INT nHeight,
1496 LPCVOID lpANDbits, LPCVOID lpXORbits )
1498 ICONINFO info;
1499 HCURSOR hCursor;
1501 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1502 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1504 info.fIcon = FALSE;
1505 info.xHotspot = xHotSpot;
1506 info.yHotspot = yHotSpot;
1507 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1508 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1509 hCursor = CreateIconIndirect( &info );
1510 DeleteObject( info.hbmMask );
1511 DeleteObject( info.hbmColor );
1512 return hCursor;
1516 /***********************************************************************
1517 * CreateIcon (USER32.@)
1519 * Creates an icon based on the specified bitmaps. The bitmaps must be
1520 * provided in a device dependent format and will be resized to
1521 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1522 * depth. The provided bitmaps must be top-down bitmaps.
1523 * Although Windows does not support 15bpp(*) this API must support it
1524 * for Winelib applications.
1526 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1527 * format!
1529 * RETURNS
1530 * Success: handle to an icon
1531 * Failure: NULL
1533 * FIXME: Do we need to resize the bitmaps?
1535 HICON WINAPI CreateIcon(
1536 HINSTANCE hInstance, /* [in] the application's hInstance */
1537 INT nWidth, /* [in] the width of the provided bitmaps */
1538 INT nHeight, /* [in] the height of the provided bitmaps */
1539 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1540 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1541 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1542 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1544 ICONINFO iinfo;
1545 HICON hIcon;
1547 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1548 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1550 iinfo.fIcon = TRUE;
1551 iinfo.xHotspot = nWidth / 2;
1552 iinfo.yHotspot = nHeight / 2;
1553 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1554 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1556 hIcon = CreateIconIndirect( &iinfo );
1558 DeleteObject( iinfo.hbmMask );
1559 DeleteObject( iinfo.hbmColor );
1561 return hIcon;
1565 /***********************************************************************
1566 * CopyIcon (USER32.@)
1568 HICON WINAPI CopyIcon( HICON hIcon )
1570 struct cursoricon_object *ptrOld, *ptrNew;
1571 HICON hNew;
1573 if (!(ptrOld = get_icon_ptr( hIcon )))
1575 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1576 return 0;
1578 if ((hNew = alloc_icon_handle( FALSE, 0 )))
1580 struct cursoricon_frame *frameOld, *frameNew;
1582 ptrNew = get_icon_ptr( hNew );
1583 ptrNew->is_icon = ptrOld->is_icon;
1584 ptrNew->hotspot = ptrOld->hotspot;
1585 if (!(frameOld = get_icon_frame( ptrOld, 0 )))
1587 release_user_handle_ptr( ptrOld );
1588 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1589 return 0;
1591 if (!(frameNew = get_icon_frame( ptrNew, 0 )))
1593 release_icon_frame( ptrOld, frameOld );
1594 release_user_handle_ptr( ptrOld );
1595 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1596 return 0;
1598 frameNew->delay = 0;
1599 frameNew->width = frameOld->width;
1600 frameNew->height = frameOld->height;
1601 frameNew->mask = copy_bitmap( frameOld->mask );
1602 frameNew->color = copy_bitmap( frameOld->color );
1603 frameNew->alpha = copy_bitmap( frameOld->alpha );
1604 release_icon_frame( ptrOld, frameOld );
1605 release_icon_frame( ptrNew, frameNew );
1606 release_user_handle_ptr( ptrNew );
1608 release_user_handle_ptr( ptrOld );
1609 return hNew;
1613 /***********************************************************************
1614 * DestroyIcon (USER32.@)
1616 BOOL WINAPI DestroyIcon( HICON hIcon )
1618 BOOL ret = FALSE;
1619 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1621 TRACE_(icon)("%p\n", hIcon );
1623 if (obj)
1625 BOOL shared = (obj->rsrc != NULL);
1626 release_user_handle_ptr( obj );
1627 ret = (GetCursor() != hIcon);
1628 if (!shared) free_icon_handle( hIcon );
1630 return ret;
1634 /***********************************************************************
1635 * DestroyCursor (USER32.@)
1637 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1639 return DestroyIcon( hCursor );
1642 /***********************************************************************
1643 * DrawIcon (USER32.@)
1645 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1647 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1650 /***********************************************************************
1651 * SetCursor (USER32.@)
1653 * Set the cursor shape.
1655 * RETURNS
1656 * A handle to the previous cursor shape.
1658 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1660 struct cursoricon_object *obj;
1661 HCURSOR hOldCursor;
1662 int show_count;
1663 BOOL ret;
1665 TRACE("%p\n", hCursor);
1667 SERVER_START_REQ( set_cursor )
1669 req->flags = SET_CURSOR_HANDLE;
1670 req->handle = wine_server_user_handle( hCursor );
1671 if ((ret = !wine_server_call_err( req )))
1673 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1674 show_count = reply->prev_count;
1677 SERVER_END_REQ;
1679 if (!ret) return 0;
1680 USER_Driver->pSetCursor( show_count >= 0 ? hCursor : 0 );
1682 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1683 release_user_handle_ptr( obj );
1684 return hOldCursor;
1687 /***********************************************************************
1688 * ShowCursor (USER32.@)
1690 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1692 HCURSOR cursor;
1693 int increment = bShow ? 1 : -1;
1694 int count;
1696 SERVER_START_REQ( set_cursor )
1698 req->flags = SET_CURSOR_COUNT;
1699 req->show_count = increment;
1700 wine_server_call( req );
1701 cursor = wine_server_ptr_handle( reply->prev_handle );
1702 count = reply->prev_count + increment;
1704 SERVER_END_REQ;
1706 TRACE("%d, count=%d\n", bShow, count );
1708 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1709 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1711 return count;
1714 /***********************************************************************
1715 * GetCursor (USER32.@)
1717 HCURSOR WINAPI GetCursor(void)
1719 HCURSOR ret;
1721 SERVER_START_REQ( set_cursor )
1723 req->flags = 0;
1724 wine_server_call( req );
1725 ret = wine_server_ptr_handle( reply->prev_handle );
1727 SERVER_END_REQ;
1728 return ret;
1732 /***********************************************************************
1733 * ClipCursor (USER32.@)
1735 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1737 BOOL ret;
1738 RECT new_rect;
1740 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1742 if (rect && (rect->left > rect->right || rect->top > rect->bottom)) return FALSE;
1744 SERVER_START_REQ( set_cursor )
1746 req->clip_msg = WM_WINE_CLIPCURSOR;
1747 if (rect)
1749 req->flags = SET_CURSOR_CLIP;
1750 req->clip.left = rect->left;
1751 req->clip.top = rect->top;
1752 req->clip.right = rect->right;
1753 req->clip.bottom = rect->bottom;
1755 else req->flags = SET_CURSOR_NOCLIP;
1757 if ((ret = !wine_server_call( req )))
1759 new_rect.left = reply->new_clip.left;
1760 new_rect.top = reply->new_clip.top;
1761 new_rect.right = reply->new_clip.right;
1762 new_rect.bottom = reply->new_clip.bottom;
1765 SERVER_END_REQ;
1766 if (ret) USER_Driver->pClipCursor( &new_rect );
1767 return ret;
1771 /***********************************************************************
1772 * GetClipCursor (USER32.@)
1774 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1776 BOOL ret;
1778 if (!rect) return FALSE;
1780 SERVER_START_REQ( set_cursor )
1782 req->flags = 0;
1783 if ((ret = !wine_server_call( req )))
1785 rect->left = reply->new_clip.left;
1786 rect->top = reply->new_clip.top;
1787 rect->right = reply->new_clip.right;
1788 rect->bottom = reply->new_clip.bottom;
1791 SERVER_END_REQ;
1792 return ret;
1796 /***********************************************************************
1797 * SetSystemCursor (USER32.@)
1799 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1801 FIXME("(%p,%08x),stub!\n", hcur, id);
1802 return TRUE;
1806 /**********************************************************************
1807 * LookupIconIdFromDirectoryEx (USER32.@)
1809 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1810 INT width, INT height, UINT cFlag )
1812 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1813 UINT retVal = 0;
1814 if( dir && !dir->idReserved && (dir->idType & 3) )
1816 const CURSORICONDIRENTRY* entry;
1818 const HDC hdc = GetDC(0);
1819 const int depth = (cFlag & LR_MONOCHROME) ?
1820 1 : GetDeviceCaps(hdc, BITSPIXEL);
1821 ReleaseDC(0, hdc);
1823 if( bIcon )
1824 entry = CURSORICON_FindBestIconRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1825 else
1826 entry = CURSORICON_FindBestCursorRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1828 if( entry ) retVal = entry->wResId;
1830 else WARN_(cursor)("invalid resource directory\n");
1831 return retVal;
1834 /**********************************************************************
1835 * LookupIconIdFromDirectory (USER32.@)
1837 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1839 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1842 /***********************************************************************
1843 * LoadCursorW (USER32.@)
1845 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1847 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1849 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1850 LR_SHARED | LR_DEFAULTSIZE );
1853 /***********************************************************************
1854 * LoadCursorA (USER32.@)
1856 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1858 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1860 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1861 LR_SHARED | LR_DEFAULTSIZE );
1864 /***********************************************************************
1865 * LoadCursorFromFileW (USER32.@)
1867 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1869 TRACE("%s\n", debugstr_w(name));
1871 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1872 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1875 /***********************************************************************
1876 * LoadCursorFromFileA (USER32.@)
1878 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1880 TRACE("%s\n", debugstr_a(name));
1882 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1883 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1886 /***********************************************************************
1887 * LoadIconW (USER32.@)
1889 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1891 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1893 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1894 LR_SHARED | LR_DEFAULTSIZE );
1897 /***********************************************************************
1898 * LoadIconA (USER32.@)
1900 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1902 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1904 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1905 LR_SHARED | LR_DEFAULTSIZE );
1908 /**********************************************************************
1909 * GetCursorFrameInfo (USER32.@)
1911 * NOTES
1912 * So far no use has been found for the second parameter, it is currently presumed
1913 * that this parameter is reserved for future use.
1915 * PARAMS
1916 * hCursor [I] Handle to cursor for which to retrieve information
1917 * reserved [I] No purpose has been found for this parameter (may be NULL)
1918 * istep [I] The step of the cursor for which to retrieve information
1919 * rate_jiffies [O] Pointer to DWORD that receives the frame-specific delay (cannot be NULL)
1920 * num_steps [O] Pointer to DWORD that receives the number of steps in the cursor (cannot be NULL)
1922 * RETURNS
1923 * Success: Handle to a frame of the cursor (specified by istep)
1924 * Failure: NULL cursor (0)
1926 HCURSOR WINAPI GetCursorFrameInfo(HCURSOR hCursor, DWORD reserved, DWORD istep, DWORD *rate_jiffies, DWORD *num_steps)
1928 struct cursoricon_object *ptr;
1929 HCURSOR ret = 0;
1930 UINT icon_steps;
1932 if (rate_jiffies == NULL || num_steps == NULL) return 0;
1934 if (!(ptr = get_icon_ptr( hCursor ))) return 0;
1936 TRACE("%p => %d %d %p %p\n", hCursor, reserved, istep, rate_jiffies, num_steps);
1937 if (reserved != 0)
1938 FIXME("Second parameter non-zero (%d), please report this!\n", reserved);
1940 icon_steps = get_icon_steps(ptr);
1941 if (istep < icon_steps || !ptr->is_ani)
1943 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) ptr;
1944 UINT icon_frames = 1;
1946 if (ptr->is_ani)
1947 icon_frames = ani_icon_data->num_frames;
1948 if (ptr->is_ani && icon_frames > 1)
1949 ret = ani_icon_data->frames[istep];
1950 else
1951 ret = hCursor;
1952 if (icon_frames == 1)
1954 *rate_jiffies = 0;
1955 *num_steps = 1;
1957 else if (icon_steps == 1)
1959 *num_steps = ~0;
1960 *rate_jiffies = ptr->delay;
1962 else if (istep < icon_steps)
1964 struct cursoricon_frame *frame;
1966 *num_steps = icon_steps;
1967 frame = get_icon_frame( ptr, istep );
1968 if (get_icon_steps(ptr) == 1)
1969 *num_steps = ~0;
1970 else
1971 *num_steps = get_icon_steps(ptr);
1972 /* If this specific frame does not have a delay then use the global delay */
1973 if (frame->delay == ~0)
1974 *rate_jiffies = ptr->delay;
1975 else
1976 *rate_jiffies = frame->delay;
1977 release_icon_frame( ptr, frame );
1981 release_user_handle_ptr( ptr );
1983 return ret;
1986 /**********************************************************************
1987 * GetIconInfo (USER32.@)
1989 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1991 ICONINFOEXW infoW;
1993 infoW.cbSize = sizeof(infoW);
1994 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
1995 iconinfo->fIcon = infoW.fIcon;
1996 iconinfo->xHotspot = infoW.xHotspot;
1997 iconinfo->yHotspot = infoW.yHotspot;
1998 iconinfo->hbmColor = infoW.hbmColor;
1999 iconinfo->hbmMask = infoW.hbmMask;
2000 return TRUE;
2003 /**********************************************************************
2004 * GetIconInfoExA (USER32.@)
2006 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
2008 ICONINFOEXW infoW;
2010 if (info->cbSize != sizeof(*info))
2012 SetLastError( ERROR_INVALID_PARAMETER );
2013 return FALSE;
2015 infoW.cbSize = sizeof(infoW);
2016 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
2017 info->fIcon = infoW.fIcon;
2018 info->xHotspot = infoW.xHotspot;
2019 info->yHotspot = infoW.yHotspot;
2020 info->hbmColor = infoW.hbmColor;
2021 info->hbmMask = infoW.hbmMask;
2022 info->wResID = infoW.wResID;
2023 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
2024 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
2025 return TRUE;
2028 /**********************************************************************
2029 * GetIconInfoExW (USER32.@)
2031 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
2033 struct cursoricon_frame *frame;
2034 struct cursoricon_object *ptr;
2035 HMODULE module;
2036 BOOL ret = TRUE;
2038 if (info->cbSize != sizeof(*info))
2040 SetLastError( ERROR_INVALID_PARAMETER );
2041 return FALSE;
2043 if (!(ptr = get_icon_ptr( icon )))
2045 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2046 return FALSE;
2049 frame = get_icon_frame( ptr, 0 );
2050 if (!frame)
2052 release_user_handle_ptr( ptr );
2053 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2054 return FALSE;
2057 TRACE("%p => %dx%d\n", icon, frame->width, frame->height);
2059 info->fIcon = ptr->is_icon;
2060 info->xHotspot = ptr->hotspot.x;
2061 info->yHotspot = ptr->hotspot.y;
2062 info->hbmColor = copy_bitmap( frame->color );
2063 info->hbmMask = copy_bitmap( frame->mask );
2064 info->wResID = 0;
2065 info->szModName[0] = 0;
2066 info->szResName[0] = 0;
2067 if (ptr->module)
2069 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
2070 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
2072 if (!info->hbmMask || (!info->hbmColor && frame->color))
2074 DeleteObject( info->hbmMask );
2075 DeleteObject( info->hbmColor );
2076 ret = FALSE;
2078 module = ptr->module;
2079 release_icon_frame( ptr, frame );
2080 release_user_handle_ptr( ptr );
2081 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
2082 return ret;
2085 /* copy an icon bitmap, even when it can't be selected into a DC */
2086 /* helper for CreateIconIndirect */
2087 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
2088 HBITMAP src, int width, int height )
2090 HDC hdc = CreateCompatibleDC( 0 );
2092 if (!SelectObject( hdc, src )) /* do it the hard way */
2094 BITMAPINFO *info;
2095 void *bits;
2097 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
2098 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2099 info->bmiHeader.biWidth = width;
2100 info->bmiHeader.biHeight = height;
2101 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
2102 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
2103 info->bmiHeader.biCompression = BI_RGB;
2104 info->bmiHeader.biSizeImage = get_dib_image_size( width, height, info->bmiHeader.biBitCount );
2105 info->bmiHeader.biXPelsPerMeter = 0;
2106 info->bmiHeader.biYPelsPerMeter = 0;
2107 info->bmiHeader.biClrUsed = 0;
2108 info->bmiHeader.biClrImportant = 0;
2109 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
2110 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
2111 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
2112 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
2114 HeapFree( GetProcessHeap(), 0, bits );
2115 HeapFree( GetProcessHeap(), 0, info );
2117 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
2119 DeleteDC( hdc );
2122 /**********************************************************************
2123 * CreateIconIndirect (USER32.@)
2125 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2127 BITMAP bmpXor, bmpAnd;
2128 HICON hObj;
2129 HBITMAP color = 0, mask;
2130 int width, height;
2131 HDC hdc;
2133 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2134 iconinfo->hbmColor, iconinfo->hbmMask,
2135 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2137 if (!iconinfo->hbmMask) return 0;
2139 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2140 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2141 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2142 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2144 if (iconinfo->hbmColor)
2146 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2147 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2148 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2149 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2151 width = bmpXor.bmWidth;
2152 height = bmpXor.bmHeight;
2153 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1 || bmpAnd.bmPlanes * bmpAnd.bmBitsPixel != 1)
2155 color = CreateCompatibleBitmap( screen_dc, width, height );
2156 mask = CreateBitmap( width, height, 1, 1, NULL );
2158 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2160 else
2162 width = bmpAnd.bmWidth;
2163 height = bmpAnd.bmHeight;
2164 mask = CreateBitmap( width, height, 1, 1, NULL );
2167 hdc = CreateCompatibleDC( 0 );
2168 SelectObject( hdc, mask );
2169 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
2171 if (color)
2173 SelectObject( hdc, color );
2174 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
2176 else if (iconinfo->hbmColor)
2178 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
2180 else height /= 2;
2182 DeleteDC( hdc );
2184 hObj = alloc_icon_handle( FALSE, 0 );
2185 if (hObj)
2187 struct cursoricon_object *info = get_icon_ptr( hObj );
2188 struct cursoricon_frame *frame;
2190 info->is_icon = iconinfo->fIcon;
2191 frame = get_icon_frame( info, 0 );
2192 frame->delay = ~0;
2193 frame->width = width;
2194 frame->height = height;
2195 frame->color = color;
2196 frame->mask = mask;
2197 frame->alpha = create_alpha_bitmap( iconinfo->hbmColor, NULL, NULL );
2198 release_icon_frame( info, frame );
2199 if (info->is_icon)
2201 info->hotspot.x = width / 2;
2202 info->hotspot.y = height / 2;
2204 else
2206 info->hotspot.x = iconinfo->xHotspot;
2207 info->hotspot.y = iconinfo->yHotspot;
2210 release_user_handle_ptr( info );
2212 return hObj;
2215 /******************************************************************************
2216 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2218 * NOTES
2219 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2221 * PARAMS
2222 * hdc [I] Handle to device context
2223 * x0 [I] X coordinate of upper left corner
2224 * y0 [I] Y coordinate of upper left corner
2225 * hIcon [I] Handle to icon to draw
2226 * cxWidth [I] Width of icon
2227 * cyWidth [I] Height of icon
2228 * istep [I] Index of frame in animated cursor
2229 * hbr [I] Handle to background brush
2230 * flags [I] Icon-drawing flags
2232 * RETURNS
2233 * Success: TRUE
2234 * Failure: FALSE
2236 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2237 INT cxWidth, INT cyWidth, UINT istep,
2238 HBRUSH hbr, UINT flags )
2240 struct cursoricon_frame *frame;
2241 struct cursoricon_object *ptr;
2242 HDC hdc_dest, hMemDC;
2243 BOOL result = FALSE, DoOffscreen;
2244 HBITMAP hB_off = 0;
2245 COLORREF oldFg, oldBg;
2246 INT x, y, nStretchMode;
2248 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2249 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2251 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2252 if (istep >= get_icon_steps( ptr ))
2254 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
2255 release_user_handle_ptr( ptr );
2256 return FALSE;
2258 if (!(frame = get_icon_frame( ptr, istep )))
2260 FIXME_(icon)("Error retrieving icon frame %d\n", istep);
2261 release_user_handle_ptr( ptr );
2262 return FALSE;
2264 if (!(hMemDC = CreateCompatibleDC( hdc )))
2266 release_icon_frame( ptr, frame );
2267 release_user_handle_ptr( ptr );
2268 return FALSE;
2271 if (flags & DI_NOMIRROR)
2272 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2274 /* Calculate the size of the destination image. */
2275 if (cxWidth == 0)
2277 if (flags & DI_DEFAULTSIZE)
2278 cxWidth = GetSystemMetrics (SM_CXICON);
2279 else
2280 cxWidth = frame->width;
2282 if (cyWidth == 0)
2284 if (flags & DI_DEFAULTSIZE)
2285 cyWidth = GetSystemMetrics (SM_CYICON);
2286 else
2287 cyWidth = frame->height;
2290 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2292 if (DoOffscreen) {
2293 RECT r;
2295 r.left = 0;
2296 r.top = 0;
2297 r.right = cxWidth;
2298 r.bottom = cxWidth;
2300 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2301 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2303 DeleteDC( hdc_dest );
2304 goto failed;
2306 SelectObject(hdc_dest, hB_off);
2307 FillRect(hdc_dest, &r, hbr);
2308 x = y = 0;
2310 else
2312 hdc_dest = hdc;
2313 x = x0;
2314 y = y0;
2317 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2319 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2320 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2322 if (frame->alpha && (flags & DI_IMAGE))
2324 BOOL alpha_blend = TRUE;
2326 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2328 BITMAP bm;
2329 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2330 alpha_blend = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel > 8;
2332 if (alpha_blend)
2334 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2335 SelectObject( hMemDC, frame->alpha );
2336 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2337 0, 0, frame->width, frame->height,
2338 pixelblend )) goto done;
2342 if (flags & DI_MASK)
2344 DWORD rop = (flags & DI_IMAGE) ? SRCAND : SRCCOPY;
2345 SelectObject( hMemDC, frame->mask );
2346 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2347 hMemDC, 0, 0, frame->width, frame->height, rop );
2350 if (flags & DI_IMAGE)
2352 if (frame->color)
2354 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2355 SelectObject( hMemDC, frame->color );
2356 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2357 hMemDC, 0, 0, frame->width, frame->height, rop );
2359 else
2361 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2362 SelectObject( hMemDC, frame->mask );
2363 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2364 hMemDC, 0, frame->height, frame->width,
2365 frame->height, rop );
2369 done:
2370 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2372 SetTextColor( hdc, oldFg );
2373 SetBkColor( hdc, oldBg );
2374 SetStretchBltMode (hdc, nStretchMode);
2375 result = TRUE;
2376 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2377 if (hB_off) DeleteObject(hB_off);
2378 failed:
2379 DeleteDC( hMemDC );
2380 release_icon_frame( ptr, frame );
2381 release_user_handle_ptr( ptr );
2382 return result;
2385 /***********************************************************************
2386 * DIB_FixColorsToLoadflags
2388 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2389 * are in loadflags
2391 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2393 int colors;
2394 COLORREF c_W, c_S, c_F, c_L, c_C;
2395 int incr,i;
2396 RGBQUAD *ptr;
2397 int bitmap_type;
2398 LONG width;
2399 LONG height;
2400 WORD bpp;
2401 DWORD compr;
2403 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2405 WARN_(resource)("Invalid bitmap\n");
2406 return;
2409 if (bpp > 8) return;
2411 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2413 incr = 3;
2414 colors = 1 << bpp;
2416 else
2418 incr = 4;
2419 colors = bmi->bmiHeader.biClrUsed;
2420 if (colors > 256) colors = 256;
2421 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2424 c_W = GetSysColor(COLOR_WINDOW);
2425 c_S = GetSysColor(COLOR_3DSHADOW);
2426 c_F = GetSysColor(COLOR_3DFACE);
2427 c_L = GetSysColor(COLOR_3DLIGHT);
2429 if (loadflags & LR_LOADTRANSPARENT) {
2430 switch (bpp) {
2431 case 1: pix = pix >> 7; break;
2432 case 4: pix = pix >> 4; break;
2433 case 8: break;
2434 default:
2435 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2436 return;
2438 if (pix >= colors) {
2439 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2440 return;
2442 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2443 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2444 ptr->rgbBlue = GetBValue(c_W);
2445 ptr->rgbGreen = GetGValue(c_W);
2446 ptr->rgbRed = GetRValue(c_W);
2448 if (loadflags & LR_LOADMAP3DCOLORS)
2449 for (i=0; i<colors; i++) {
2450 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2451 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2452 if (c_C == RGB(128, 128, 128)) {
2453 ptr->rgbRed = GetRValue(c_S);
2454 ptr->rgbGreen = GetGValue(c_S);
2455 ptr->rgbBlue = GetBValue(c_S);
2456 } else if (c_C == RGB(192, 192, 192)) {
2457 ptr->rgbRed = GetRValue(c_F);
2458 ptr->rgbGreen = GetGValue(c_F);
2459 ptr->rgbBlue = GetBValue(c_F);
2460 } else if (c_C == RGB(223, 223, 223)) {
2461 ptr->rgbRed = GetRValue(c_L);
2462 ptr->rgbGreen = GetGValue(c_L);
2463 ptr->rgbBlue = GetBValue(c_L);
2469 /**********************************************************************
2470 * BITMAP_Load
2472 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2473 INT desiredx, INT desiredy, UINT loadflags )
2475 HBITMAP hbitmap = 0, orig_bm;
2476 HRSRC hRsrc;
2477 HGLOBAL handle;
2478 const char *ptr = NULL;
2479 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2480 int size;
2481 BYTE pix;
2482 char *bits;
2483 LONG width, height, new_width, new_height;
2484 WORD bpp_dummy;
2485 DWORD compr_dummy, offbits = 0;
2486 INT bm_type;
2487 HDC screen_mem_dc = NULL;
2489 if (!(loadflags & LR_LOADFROMFILE))
2491 if (!instance)
2493 /* OEM bitmap: try to load the resource from user32.dll */
2494 instance = user32_module;
2497 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2498 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2500 if ((info = LockResource( handle )) == NULL) return 0;
2502 else
2504 BITMAPFILEHEADER * bmfh;
2506 if (!(ptr = map_fileW( name, NULL ))) return 0;
2507 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2508 bmfh = (BITMAPFILEHEADER *)ptr;
2509 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2511 WARN("Invalid/unsupported bitmap format!\n");
2512 goto end;
2514 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2517 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2518 &bpp_dummy, &compr_dummy);
2519 if (bm_type == -1)
2521 WARN("Invalid bitmap format!\n");
2522 goto end;
2525 size = bitmap_info_size(info, DIB_RGB_COLORS);
2526 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2527 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2529 if (!fix_info || !scaled_info) goto end;
2530 memcpy(fix_info, info, size);
2532 pix = *((LPBYTE)info + size);
2533 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2535 memcpy(scaled_info, fix_info, size);
2537 if(desiredx != 0)
2538 new_width = desiredx;
2539 else
2540 new_width = width;
2542 if(desiredy != 0)
2543 new_height = height > 0 ? desiredy : -desiredy;
2544 else
2545 new_height = height;
2547 if(bm_type == 0)
2549 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2550 core->bcWidth = new_width;
2551 core->bcHeight = new_height;
2553 else
2555 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2556 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2557 WARN("Broken BitmapInfoHeader!\n");
2558 goto end;
2561 scaled_info->bmiHeader.biWidth = new_width;
2562 scaled_info->bmiHeader.biHeight = new_height;
2565 if (new_height < 0) new_height = -new_height;
2567 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2568 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2570 bits = (char *)info + (offbits ? offbits : size);
2572 if (loadflags & LR_CREATEDIBSECTION)
2574 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2575 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2577 else
2579 if (is_dib_monochrome(fix_info))
2580 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2581 else
2582 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2585 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2586 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2587 SelectObject(screen_mem_dc, orig_bm);
2589 end:
2590 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2591 HeapFree(GetProcessHeap(), 0, scaled_info);
2592 HeapFree(GetProcessHeap(), 0, fix_info);
2593 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2595 return hbitmap;
2598 /**********************************************************************
2599 * LoadImageA (USER32.@)
2601 * See LoadImageW.
2603 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2604 INT desiredx, INT desiredy, UINT loadflags)
2606 HANDLE res;
2607 LPWSTR u_name;
2609 if (IS_INTRESOURCE(name))
2610 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2612 __TRY {
2613 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2614 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2615 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2617 __EXCEPT_PAGE_FAULT {
2618 SetLastError( ERROR_INVALID_PARAMETER );
2619 return 0;
2621 __ENDTRY
2622 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2623 HeapFree(GetProcessHeap(), 0, u_name);
2624 return res;
2628 /******************************************************************************
2629 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2631 * PARAMS
2632 * hinst [I] Handle of instance that contains image
2633 * name [I] Name of image
2634 * type [I] Type of image
2635 * desiredx [I] Desired width
2636 * desiredy [I] Desired height
2637 * loadflags [I] Load flags
2639 * RETURNS
2640 * Success: Handle to newly loaded image
2641 * Failure: NULL
2643 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2645 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2646 INT desiredx, INT desiredy, UINT loadflags )
2648 int depth;
2650 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2651 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2653 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2654 switch (type) {
2655 case IMAGE_BITMAP:
2656 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2658 case IMAGE_ICON:
2659 case IMAGE_CURSOR:
2660 depth = 1;
2661 if (!(loadflags & LR_MONOCHROME))
2663 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2664 if (screen_dc) depth = GetDeviceCaps( screen_dc, BITSPIXEL );
2666 return CURSORICON_Load(hinst, name, desiredx, desiredy, depth, (type == IMAGE_CURSOR), loadflags);
2668 return 0;
2671 /******************************************************************************
2672 * CopyImage (USER32.@) Creates new image and copies attributes to it
2674 * PARAMS
2675 * hnd [I] Handle to image to copy
2676 * type [I] Type of image to copy
2677 * desiredx [I] Desired width of new image
2678 * desiredy [I] Desired height of new image
2679 * flags [I] Copy flags
2681 * RETURNS
2682 * Success: Handle to newly created image
2683 * Failure: NULL
2685 * BUGS
2686 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2687 * all other versions (95/2000/XP have been tested) ignore it.
2689 * NOTES
2690 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2691 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2692 * the copy will have the same depth as the screen.
2693 * The content of the image will only be copied if the bit depth of the
2694 * original image is compatible with the bit depth of the screen, or
2695 * if the source is a DIB section.
2696 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2698 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2699 INT desiredy, UINT flags )
2701 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2702 hnd, type, desiredx, desiredy, flags);
2704 switch (type)
2706 case IMAGE_BITMAP:
2708 HBITMAP res = NULL;
2709 DIBSECTION ds;
2710 int objSize;
2711 BITMAPINFO * bi;
2713 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2714 if (!objSize) return 0;
2715 if ((desiredx < 0) || (desiredy < 0)) return 0;
2717 if (flags & LR_COPYFROMRESOURCE)
2719 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2722 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2723 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2725 /* Allocate memory for a BITMAPINFOHEADER structure and a
2726 color table. The maximum number of colors in a color table
2727 is 256 which corresponds to a bitmap with depth 8.
2728 Bitmaps with higher depths don't have color tables. */
2729 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2730 if (!bi) return 0;
2732 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2733 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2734 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2735 bi->bmiHeader.biCompression = BI_RGB;
2737 if (flags & LR_CREATEDIBSECTION)
2739 /* Create a DIB section. LR_MONOCHROME is ignored */
2740 void * bits;
2741 HDC dc = CreateCompatibleDC(NULL);
2743 if (objSize == sizeof(DIBSECTION))
2745 /* The source bitmap is a DIB.
2746 Get its attributes to create an exact copy */
2747 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2750 bi->bmiHeader.biWidth = desiredx;
2751 bi->bmiHeader.biHeight = desiredy;
2753 /* Get the color table or the color masks */
2754 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2756 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2757 DeleteDC(dc);
2759 else
2761 /* Create a device-dependent bitmap */
2763 BOOL monochrome = (flags & LR_MONOCHROME);
2765 if (objSize == sizeof(DIBSECTION))
2767 /* The source bitmap is a DIB section.
2768 Get its attributes */
2769 HDC dc = CreateCompatibleDC(NULL);
2770 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2771 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2772 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2773 DeleteDC(dc);
2775 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2777 /* Look if the colors of the DIB are black and white */
2779 monochrome =
2780 (bi->bmiColors[0].rgbRed == 0xff
2781 && bi->bmiColors[0].rgbGreen == 0xff
2782 && bi->bmiColors[0].rgbBlue == 0xff
2783 && bi->bmiColors[0].rgbReserved == 0
2784 && bi->bmiColors[1].rgbRed == 0
2785 && bi->bmiColors[1].rgbGreen == 0
2786 && bi->bmiColors[1].rgbBlue == 0
2787 && bi->bmiColors[1].rgbReserved == 0)
2789 (bi->bmiColors[0].rgbRed == 0
2790 && bi->bmiColors[0].rgbGreen == 0
2791 && bi->bmiColors[0].rgbBlue == 0
2792 && bi->bmiColors[0].rgbReserved == 0
2793 && bi->bmiColors[1].rgbRed == 0xff
2794 && bi->bmiColors[1].rgbGreen == 0xff
2795 && bi->bmiColors[1].rgbBlue == 0xff
2796 && bi->bmiColors[1].rgbReserved == 0);
2799 else if (!monochrome)
2801 monochrome = ds.dsBm.bmBitsPixel == 1;
2804 if (monochrome)
2806 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2808 else
2810 HDC screenDC = GetDC(NULL);
2811 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2812 ReleaseDC(NULL, screenDC);
2816 if (res)
2818 /* Only copy the bitmap if it's a DIB section or if it's
2819 compatible to the screen */
2820 BOOL copyContents;
2822 if (objSize == sizeof(DIBSECTION))
2824 copyContents = TRUE;
2826 else
2828 HDC screenDC = GetDC(NULL);
2829 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2830 ReleaseDC(NULL, screenDC);
2832 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2835 if (copyContents)
2837 /* The source bitmap may already be selected in a device context,
2838 use GetDIBits/StretchDIBits and not StretchBlt */
2840 HDC dc;
2841 void * bits;
2843 dc = CreateCompatibleDC(NULL);
2845 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2846 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2847 bi->bmiHeader.biSizeImage = 0;
2848 bi->bmiHeader.biClrUsed = 0;
2849 bi->bmiHeader.biClrImportant = 0;
2851 /* Fill in biSizeImage */
2852 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2853 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2855 if (bits)
2857 HBITMAP oldBmp;
2859 /* Get the image bits of the source bitmap */
2860 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2862 /* Copy it to the destination bitmap */
2863 oldBmp = SelectObject(dc, res);
2864 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2865 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2866 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2867 SelectObject(dc, oldBmp);
2869 HeapFree(GetProcessHeap(), 0, bits);
2872 DeleteDC(dc);
2875 if (flags & LR_COPYDELETEORG)
2877 DeleteObject(hnd);
2880 HeapFree(GetProcessHeap(), 0, bi);
2881 return res;
2883 case IMAGE_ICON:
2884 case IMAGE_CURSOR:
2886 struct cursoricon_object *icon;
2887 HICON res = 0;
2888 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( screen_dc, BITSPIXEL );
2890 if (flags & LR_DEFAULTSIZE)
2892 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2893 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2896 if (!(icon = get_icon_ptr( hnd ))) return 0;
2898 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2899 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2900 !icon->is_icon, flags );
2901 else
2902 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2903 release_user_handle_ptr( icon );
2905 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2906 return res;
2909 return 0;
2913 /******************************************************************************
2914 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2916 * RETURNS
2917 * Success: Handle to specified bitmap
2918 * Failure: NULL
2920 HBITMAP WINAPI LoadBitmapW(
2921 HINSTANCE instance, /* [in] Handle to application instance */
2922 LPCWSTR name) /* [in] Address of bitmap resource name */
2924 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2927 /**********************************************************************
2928 * LoadBitmapA (USER32.@)
2930 * See LoadBitmapW.
2932 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2934 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );