d3d10: Initialise the type tree in D3D10CreateEffectFromMemory() (AFL).
[wine.git] / dlls / user32 / cursoricon.c
blob4f931958f2201225c4247052cfcebc86517a5d59
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 static HDC screen_dc;
53 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
55 static struct list icon_cache = LIST_INIT( icon_cache );
57 /**********************************************************************
58 * User objects management
61 struct cursoricon_frame
63 UINT width; /* frame-specific width */
64 UINT height; /* frame-specific height */
65 UINT delay; /* frame-specific delay between this frame and the next (in jiffies) */
66 HBITMAP color; /* color bitmap */
67 HBITMAP alpha; /* pre-multiplied alpha bitmap for 32-bpp icons */
68 HBITMAP mask; /* mask bitmap (followed by color for 1-bpp icons) */
71 struct cursoricon_object
73 struct user_object obj; /* object header */
74 struct list entry; /* entry in shared icons list */
75 ULONG_PTR param; /* opaque param used by 16-bit code */
76 HMODULE module; /* module for icons loaded from resources */
77 LPWSTR resname; /* resource name for icons loaded from resources */
78 HRSRC rsrc; /* resource for shared icons */
79 BOOL is_icon; /* whether icon or cursor */
80 BOOL is_ani; /* whether this object is a static cursor or an animated cursor */
81 UINT delay; /* delay between this frame and the next (in jiffies) */
82 POINT hotspot;
85 struct static_cursoricon_object
87 struct cursoricon_object shared;
88 struct cursoricon_frame frame; /* frame-specific icon data */
91 struct animated_cursoricon_object
93 struct cursoricon_object shared;
94 UINT num_frames; /* number of frames in the icon/cursor */
95 UINT num_steps; /* number of sequence steps in the icon/cursor */
96 HICON frames[1]; /* list of animated cursor frames */
99 static HICON alloc_icon_handle( BOOL is_ani, UINT num_steps )
101 struct cursoricon_object *obj;
102 int icon_size;
103 HICON handle;
105 if (is_ani)
106 icon_size = FIELD_OFFSET( struct animated_cursoricon_object, frames[num_steps] );
107 else
108 icon_size = sizeof( struct static_cursoricon_object );
109 obj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, icon_size );
110 if (!obj) return NULL;
112 obj->delay = 0;
113 obj->is_ani = is_ani;
114 if (is_ani)
116 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
118 ani_icon_data->num_steps = num_steps;
119 ani_icon_data->num_frames = num_steps; /* changed later for some animated cursors */
122 if (!(handle = alloc_user_handle( &obj->obj, USER_ICON )))
123 HeapFree( GetProcessHeap(), 0, obj );
124 return handle;
127 static struct cursoricon_object *get_icon_ptr( HICON handle )
129 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
130 if (obj == OBJ_OTHER_PROCESS)
132 WARN( "icon handle %p from other process\n", handle );
133 obj = NULL;
135 return obj;
138 static struct cursoricon_frame *get_icon_frame( struct cursoricon_object *obj, int istep )
140 struct static_cursoricon_object *req_frame;
142 if (obj->is_ani)
144 struct animated_cursoricon_object *ani_icon_data;
145 struct cursoricon_object *frameobj;
147 ani_icon_data = (struct animated_cursoricon_object *) obj;
148 if (!(frameobj = get_icon_ptr( ani_icon_data->frames[istep] )))
149 return 0;
150 req_frame = (struct static_cursoricon_object *) frameobj;
152 else
153 req_frame = (struct static_cursoricon_object *) obj;
155 return &req_frame->frame;
158 static void release_icon_frame( struct cursoricon_object *obj, struct cursoricon_frame *frame )
160 if (obj->is_ani)
162 struct cursoricon_object *frameobj;
164 frameobj = (struct cursoricon_object *) (((char *)frame) - FIELD_OFFSET(struct static_cursoricon_object, frame));
165 release_user_handle_ptr( frameobj );
169 static UINT get_icon_steps( struct cursoricon_object *obj )
171 if (obj->is_ani)
173 struct animated_cursoricon_object *ani_icon_data;
175 ani_icon_data = (struct animated_cursoricon_object *) obj;
176 return ani_icon_data->num_steps;
178 return 1;
181 static BOOL free_icon_handle( HICON handle )
183 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
185 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
186 else if (obj)
188 ULONG_PTR param = obj->param;
189 UINT i;
191 assert( !obj->rsrc ); /* shared icons can't be freed */
193 if (!obj->is_ani)
195 struct cursoricon_frame *frame = get_icon_frame( obj, 0 );
197 if (frame->alpha) DeleteObject( frame->alpha );
198 if (frame->color) DeleteObject( frame->color );
199 DeleteObject( frame->mask );
200 release_icon_frame( obj, frame );
202 else
204 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
206 for (i=0; i<ani_icon_data->num_steps; i++)
208 HICON hFrame = ani_icon_data->frames[i];
210 if (hFrame)
212 UINT j;
214 free_icon_handle( ani_icon_data->frames[i] );
215 for (j=0; j<ani_icon_data->num_steps; j++)
217 if (ani_icon_data->frames[j] == hFrame)
218 ani_icon_data->frames[j] = 0;
223 if (!IS_INTRESOURCE( obj->resname )) HeapFree( GetProcessHeap(), 0, obj->resname );
224 HeapFree( GetProcessHeap(), 0, obj );
225 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
226 USER_Driver->pDestroyCursorIcon( handle );
227 return TRUE;
229 return FALSE;
232 ULONG_PTR get_icon_param( HICON handle )
234 ULONG_PTR ret = 0;
235 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
237 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
238 else if (obj)
240 ret = obj->param;
241 release_user_handle_ptr( obj );
243 return ret;
246 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
248 ULONG_PTR ret = 0;
249 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
251 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
252 else if (obj)
254 ret = obj->param;
255 obj->param = param;
256 release_user_handle_ptr( obj );
258 return ret;
262 /***********************************************************************
263 * map_fileW
265 * Helper function to map a file to memory:
266 * name - file name
267 * [RETURN] ptr - pointer to mapped file
268 * [RETURN] filesize - pointer size of file to be stored if not NULL
270 static const void *map_fileW( LPCWSTR name, LPDWORD filesize )
272 HANDLE hFile, hMapping;
273 LPVOID ptr = NULL;
275 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
276 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
277 if (hFile != INVALID_HANDLE_VALUE)
279 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
280 if (hMapping)
282 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
283 CloseHandle( hMapping );
284 if (filesize)
285 *filesize = GetFileSize( hFile, NULL );
287 CloseHandle( hFile );
289 return ptr;
293 /***********************************************************************
294 * get_dib_image_size
296 * Return the size of a DIB bitmap in bytes.
298 static int get_dib_image_size( int width, int height, int depth )
300 return (((width * depth + 31) / 8) & ~3) * abs( height );
304 /***********************************************************************
305 * bitmap_info_size
307 * Return the size of the bitmap info structure including color table.
309 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
311 unsigned int colors, size, masks = 0;
313 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
315 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
316 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
317 return sizeof(BITMAPCOREHEADER) + colors *
318 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
320 else /* assume BITMAPINFOHEADER */
322 colors = info->bmiHeader.biClrUsed;
323 if (colors > 256) /* buffer overflow otherwise */
324 colors = 256;
325 if (!colors && (info->bmiHeader.biBitCount <= 8))
326 colors = 1 << info->bmiHeader.biBitCount;
327 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
328 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
329 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
334 /***********************************************************************
335 * copy_bitmap
337 * Helper function to duplicate a bitmap.
339 static HBITMAP copy_bitmap( HBITMAP bitmap )
341 HDC src, dst = 0;
342 HBITMAP new_bitmap = 0;
343 BITMAP bmp;
345 if (!bitmap) return 0;
346 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
348 if ((src = CreateCompatibleDC( 0 )) && (dst = CreateCompatibleDC( 0 )))
350 SelectObject( src, bitmap );
351 if ((new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight )))
353 SelectObject( dst, new_bitmap );
354 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
357 DeleteDC( dst );
358 DeleteDC( src );
359 return new_bitmap;
363 /***********************************************************************
364 * is_dib_monochrome
366 * Returns whether a DIB can be converted to a monochrome DDB.
368 * A DIB can be converted if its color table contains only black and
369 * white. Black must be the first color in the color table.
371 * Note : If the first color in the color table is white followed by
372 * black, we can't convert it to a monochrome DDB with
373 * SetDIBits, because black and white would be inverted.
375 static BOOL is_dib_monochrome( const BITMAPINFO* info )
377 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
379 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
381 if (((const BITMAPCOREINFO*)info)->bmciHeader.bcBitCount != 1) return FALSE;
383 /* Check if the first color is black */
384 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
386 rgb++;
388 /* Check if the second color is white */
389 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
390 && (rgb->rgbtBlue == 0xff));
392 else return FALSE;
394 else /* assume BITMAPINFOHEADER */
396 const RGBQUAD *rgb = info->bmiColors;
398 if (info->bmiHeader.biBitCount != 1) return FALSE;
400 /* Check if the first color is black */
401 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
402 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
404 rgb++;
406 /* Check if the second color is white */
407 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
408 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
410 else return FALSE;
414 /***********************************************************************
415 * DIB_GetBitmapInfo
417 * Get the info from a bitmap header.
418 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
420 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
421 LONG *height, WORD *bpp, DWORD *compr )
423 if (header->biSize == sizeof(BITMAPCOREHEADER))
425 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
426 *width = core->bcWidth;
427 *height = core->bcHeight;
428 *bpp = core->bcBitCount;
429 *compr = 0;
430 return 0;
432 else if (header->biSize == sizeof(BITMAPINFOHEADER) ||
433 header->biSize == sizeof(BITMAPV4HEADER) ||
434 header->biSize == sizeof(BITMAPV5HEADER))
436 *width = header->biWidth;
437 *height = header->biHeight;
438 *bpp = header->biBitCount;
439 *compr = header->biCompression;
440 return 1;
442 WARN("unknown/wrong size (%u) for header\n", header->biSize);
443 return -1;
446 /**********************************************************************
447 * get_icon_size
449 BOOL get_icon_size( HICON handle, SIZE *size )
451 struct cursoricon_object *info;
452 struct cursoricon_frame *frame;
454 if (!(info = get_icon_ptr( handle ))) return FALSE;
455 frame = get_icon_frame( info, 0 );
456 size->cx = frame->width;
457 size->cy = frame->height;
458 release_icon_frame( info, frame);
459 release_user_handle_ptr( info );
460 return TRUE;
464 * The following macro functions account for the irregularities of
465 * accessing cursor and icon resources in files and resource entries.
467 typedef BOOL (*fnGetCIEntry)( LPCVOID dir, DWORD size, int n,
468 int *width, int *height, int *bits );
470 /**********************************************************************
471 * CURSORICON_FindBestIcon
473 * Find the icon closest to the requested size and bit depth.
475 static int CURSORICON_FindBestIcon( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
476 int width, int height, int depth, UINT loadflags )
478 int i, cx, cy, bits, bestEntry = -1;
479 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
480 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
482 /* Find Best Fit */
483 iTotalDiff = 0xFFFFFFFF;
484 iColorDiff = 0xFFFFFFFF;
486 if (loadflags & LR_DEFAULTSIZE)
488 if (!width) width = GetSystemMetrics( SM_CXICON );
489 if (!height) height = GetSystemMetrics( SM_CYICON );
491 else if (!width && !height)
493 /* use the size of the first entry */
494 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
495 iTotalDiff = 0;
498 for ( i = 0; iTotalDiff && get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
500 iTempXDiff = abs(width - cx);
501 iTempYDiff = abs(height - cy);
503 if(iTotalDiff > (iTempXDiff + iTempYDiff))
505 iXDiff = iTempXDiff;
506 iYDiff = iTempYDiff;
507 iTotalDiff = iXDiff + iYDiff;
511 /* Find Best Colors for Best Fit */
512 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
514 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
516 iTempColorDiff = abs(depth - bits);
517 if(iColorDiff > iTempColorDiff)
519 bestEntry = i;
520 iColorDiff = iTempColorDiff;
525 return bestEntry;
528 static BOOL CURSORICON_GetResIconEntry( LPCVOID dir, DWORD size, int n,
529 int *width, int *height, int *bits )
531 const CURSORICONDIR *resdir = dir;
532 const ICONRESDIR *icon;
534 if ( resdir->idCount <= n )
535 return FALSE;
536 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
537 return FALSE;
538 icon = &resdir->idEntries[n].ResInfo.icon;
539 *width = icon->bWidth;
540 *height = icon->bHeight;
541 *bits = resdir->idEntries[n].wBitCount;
542 return TRUE;
545 /**********************************************************************
546 * CURSORICON_FindBestCursor
548 * Find the cursor closest to the requested size.
550 * FIXME: parameter 'color' ignored.
552 static int CURSORICON_FindBestCursor( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
553 int width, int height, int depth, UINT loadflags )
555 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
557 if (loadflags & LR_DEFAULTSIZE)
559 if (!width) width = GetSystemMetrics( SM_CXCURSOR );
560 if (!height) height = GetSystemMetrics( SM_CYCURSOR );
562 else if (!width && !height)
564 /* use the first entry */
565 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
566 return 0;
569 /* Double height to account for AND and XOR masks */
571 height *= 2;
573 /* First find the largest one smaller than or equal to the requested size*/
575 maxwidth = maxheight = 0;
576 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
578 if ((cx <= width) && (cy <= height) &&
579 (cx > maxwidth) && (cy > maxheight))
581 bestEntry = i;
582 maxwidth = cx;
583 maxheight = cy;
586 if (bestEntry != -1) return bestEntry;
588 /* Now find the smallest one larger than the requested size */
590 maxwidth = maxheight = 255;
591 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
593 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
595 bestEntry = i;
596 maxwidth = cx;
597 maxheight = cy;
601 return bestEntry;
604 static BOOL CURSORICON_GetResCursorEntry( LPCVOID dir, DWORD size, int n,
605 int *width, int *height, int *bits )
607 const CURSORICONDIR *resdir = dir;
608 const CURSORDIR *cursor;
610 if ( resdir->idCount <= n )
611 return FALSE;
612 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
613 return FALSE;
614 cursor = &resdir->idEntries[n].ResInfo.cursor;
615 *width = cursor->wWidth;
616 *height = cursor->wHeight;
617 *bits = resdir->idEntries[n].wBitCount;
618 return TRUE;
621 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir, DWORD size,
622 int width, int height, int depth,
623 UINT loadflags )
625 int n;
627 n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetResIconEntry,
628 width, height, depth, loadflags );
629 if ( n < 0 )
630 return NULL;
631 return &dir->idEntries[n];
634 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir, DWORD size,
635 int width, int height, int depth,
636 UINT loadflags )
638 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetResCursorEntry,
639 width, height, depth, loadflags );
640 if ( n < 0 )
641 return NULL;
642 return &dir->idEntries[n];
645 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, DWORD size, int n,
646 int *width, int *height, int *bits )
648 const CURSORICONFILEDIR *filedir = dir;
649 const CURSORICONFILEDIRENTRY *entry;
650 const BITMAPINFOHEADER *info;
652 if ( filedir->idCount <= n )
653 return FALSE;
654 if ((const char *)&filedir->idEntries[n + 1] - (const char *)dir > size)
655 return FALSE;
656 entry = &filedir->idEntries[n];
657 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
658 if (info->biSize != sizeof(BITMAPCOREHEADER))
660 if ((const char *)(info + 1) - (const char *)dir > size) return FALSE;
661 *bits = info->biBitCount;
663 else
665 const BITMAPCOREHEADER *coreinfo = (const BITMAPCOREHEADER *)((const char *)dir + entry->dwDIBOffset);
666 if ((const char *)(coreinfo + 1) - (const char *)dir > size) return FALSE;
667 *bits = coreinfo->bcBitCount;
669 *width = entry->bWidth;
670 *height = entry->bHeight;
671 return TRUE;
674 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir, DWORD size,
675 int width, int height, int depth,
676 UINT loadflags )
678 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetFileEntry,
679 width, height, depth, loadflags );
680 if ( n < 0 )
681 return NULL;
682 return &dir->idEntries[n];
685 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir, DWORD size,
686 int width, int height, int depth,
687 UINT loadflags )
689 int n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetFileEntry,
690 width, height, depth, loadflags );
691 if ( n < 0 )
692 return NULL;
693 return &dir->idEntries[n];
696 /***********************************************************************
697 * bmi_has_alpha
699 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
701 int i;
702 BOOL has_alpha = FALSE;
703 const unsigned char *ptr = bits;
705 if (info->bmiHeader.biBitCount != 32) return FALSE;
706 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
707 if ((has_alpha = (ptr[3] != 0))) break;
708 return has_alpha;
711 /***********************************************************************
712 * create_alpha_bitmap
714 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
716 static HBITMAP create_alpha_bitmap( HBITMAP color, const BITMAPINFO *src_info, const void *color_bits )
718 HBITMAP alpha = 0;
719 BITMAPINFO *info = NULL;
720 BITMAP bm;
721 HDC hdc;
722 void *bits;
723 unsigned char *ptr;
724 int i;
726 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
727 if (bm.bmBitsPixel != 32) return 0;
729 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
730 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
731 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
732 info->bmiHeader.biWidth = bm.bmWidth;
733 info->bmiHeader.biHeight = -bm.bmHeight;
734 info->bmiHeader.biPlanes = 1;
735 info->bmiHeader.biBitCount = 32;
736 info->bmiHeader.biCompression = BI_RGB;
737 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
738 info->bmiHeader.biXPelsPerMeter = 0;
739 info->bmiHeader.biYPelsPerMeter = 0;
740 info->bmiHeader.biClrUsed = 0;
741 info->bmiHeader.biClrImportant = 0;
742 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
744 if (src_info)
746 SelectObject( hdc, alpha );
747 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
748 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
749 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
752 else
754 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
755 if (!bmi_has_alpha( info, bits ))
757 DeleteObject( alpha );
758 alpha = 0;
759 goto done;
763 /* pre-multiply by alpha */
764 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
766 unsigned int alpha = ptr[3];
767 ptr[0] = ptr[0] * alpha / 255;
768 ptr[1] = ptr[1] * alpha / 255;
769 ptr[2] = ptr[2] * alpha / 255;
772 done:
773 DeleteDC( hdc );
774 HeapFree( GetProcessHeap(), 0, info );
775 return alpha;
779 /***********************************************************************
780 * create_icon_from_bmi
782 * Create an icon from its BITMAPINFO.
784 static HICON create_icon_from_bmi( const BITMAPINFO *bmi, DWORD maxsize, HMODULE module, LPCWSTR resname,
785 HRSRC rsrc, POINT hotspot, BOOL bIcon, INT width, INT height,
786 UINT cFlag )
788 DWORD size, color_size, mask_size;
789 HBITMAP color = 0, mask = 0, alpha = 0;
790 const void *color_bits, *mask_bits;
791 BITMAPINFO *bmi_copy;
792 BOOL ret = FALSE;
793 BOOL do_stretch;
794 HICON hObj = 0;
795 HDC hdc = 0;
796 LONG bmi_width, bmi_height;
797 WORD bpp;
798 DWORD compr;
800 /* Check bitmap header */
802 if (maxsize < sizeof(BITMAPCOREHEADER))
804 WARN( "invalid size %u\n", maxsize );
805 return 0;
807 if (maxsize < bmi->bmiHeader.biSize)
809 WARN( "invalid header size %u\n", bmi->bmiHeader.biSize );
810 return 0;
812 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
813 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
814 (bmi->bmiHeader.biCompression != BI_RGB &&
815 bmi->bmiHeader.biCompression != BI_BITFIELDS)) )
817 WARN( "invalid bitmap header %u\n", bmi->bmiHeader.biSize );
818 return 0;
821 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
822 DIB_GetBitmapInfo(&bmi->bmiHeader, &bmi_width, &bmi_height, &bpp, &compr);
823 color_size = get_dib_image_size( bmi_width, bmi_height / 2,
824 bpp );
825 mask_size = get_dib_image_size( bmi_width, bmi_height / 2, 1 );
826 if (size > maxsize || color_size > maxsize - size)
828 WARN( "truncated file %u < %u+%u+%u\n", maxsize, size, color_size, mask_size );
829 return 0;
831 if (mask_size > maxsize - size - color_size) mask_size = 0; /* no mask */
833 if (cFlag & LR_DEFAULTSIZE)
835 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
836 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
838 else
840 if (!width) width = bmi_width;
841 if (!height) height = bmi_height/2;
843 do_stretch = (bmi_height/2 != height) ||
844 (bmi_width != width);
846 /* Scale the hotspot */
847 if (bIcon)
849 hotspot.x = width / 2;
850 hotspot.y = height / 2;
852 else if (do_stretch)
854 hotspot.x = (hotspot.x * width) / bmi_width;
855 hotspot.y = (hotspot.y * height) / (bmi_height / 2);
858 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
859 if (!screen_dc) return 0;
861 if (!(bmi_copy = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
862 return 0;
863 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
865 memcpy( bmi_copy, bmi, size );
866 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
867 bmi_copy->bmiHeader.biHeight /= 2;
868 else
869 ((BITMAPCOREINFO *)bmi_copy)->bmciHeader.bcHeight /= 2;
870 bmi_height /= 2;
872 color_bits = (const char*)bmi + size;
873 mask_bits = (const char*)color_bits + color_size;
875 alpha = 0;
876 if (is_dib_monochrome( bmi ))
878 if (!(mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
879 color = 0;
881 /* copy color data into second half of mask bitmap */
882 SelectObject( hdc, mask );
883 StretchDIBits( hdc, 0, height, width, height,
884 0, 0, bmi_width, bmi_height,
885 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
887 else
889 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
890 if (!(color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
891 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
893 DeleteObject( mask );
894 goto done;
896 SelectObject( hdc, color );
897 StretchDIBits( hdc, 0, 0, width, height,
898 0, 0, bmi_width, bmi_height,
899 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
901 if (bmi_has_alpha( bmi_copy, color_bits ))
902 alpha = create_alpha_bitmap( color, bmi_copy, color_bits );
904 /* convert info to monochrome to copy the mask */
905 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
907 RGBQUAD *rgb = bmi_copy->bmiColors;
909 bmi_copy->bmiHeader.biBitCount = 1;
910 bmi_copy->bmiHeader.biClrUsed = bmi_copy->bmiHeader.biClrImportant = 2;
911 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
912 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
913 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
915 else
917 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)bmi_copy) + 1);
919 ((BITMAPCOREINFO *)bmi_copy)->bmciHeader.bcBitCount = 1;
920 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
921 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
925 if (mask_size)
927 SelectObject( hdc, mask );
928 StretchDIBits( hdc, 0, 0, width, height,
929 0, 0, bmi_width, bmi_height,
930 mask_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
932 ret = TRUE;
934 done:
935 DeleteDC( hdc );
936 HeapFree( GetProcessHeap(), 0, bmi_copy );
938 if (ret)
939 hObj = alloc_icon_handle( FALSE, 0 );
940 if (hObj)
942 struct cursoricon_object *info = get_icon_ptr( hObj );
943 struct cursoricon_frame *frame;
945 info->is_icon = bIcon;
946 info->module = module;
947 info->hotspot = hotspot;
948 frame = get_icon_frame( info, 0 );
949 frame->delay = ~0;
950 frame->width = width;
951 frame->height = height;
952 frame->color = color;
953 frame->mask = mask;
954 frame->alpha = alpha;
955 release_icon_frame( info, frame );
956 if (!IS_INTRESOURCE(resname))
958 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
959 if (info->resname) strcpyW( info->resname, resname );
961 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
963 if (module && (cFlag & LR_SHARED))
965 info->rsrc = rsrc;
966 list_add_head( &icon_cache, &info->entry );
968 release_user_handle_ptr( info );
970 else
972 DeleteObject( color );
973 DeleteObject( alpha );
974 DeleteObject( mask );
976 return hObj;
980 /**********************************************************************
981 * .ANI cursor support
983 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
984 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
985 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
987 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
988 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
989 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
990 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
991 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
992 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
993 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
995 #define ANI_FLAG_ICON 0x1
996 #define ANI_FLAG_SEQUENCE 0x2
998 typedef struct {
999 DWORD header_size;
1000 DWORD num_frames;
1001 DWORD num_steps;
1002 DWORD width;
1003 DWORD height;
1004 DWORD bpp;
1005 DWORD num_planes;
1006 DWORD display_rate;
1007 DWORD flags;
1008 } ani_header;
1010 typedef struct {
1011 DWORD data_size;
1012 const unsigned char *data;
1013 } riff_chunk_t;
1015 static void dump_ani_header( const ani_header *header )
1017 TRACE(" header size: %d\n", header->header_size);
1018 TRACE(" frames: %d\n", header->num_frames);
1019 TRACE(" steps: %d\n", header->num_steps);
1020 TRACE(" width: %d\n", header->width);
1021 TRACE(" height: %d\n", header->height);
1022 TRACE(" bpp: %d\n", header->bpp);
1023 TRACE(" planes: %d\n", header->num_planes);
1024 TRACE(" display rate: %d\n", header->display_rate);
1025 TRACE(" flags: 0x%08x\n", header->flags);
1030 * RIFF:
1031 * DWORD "RIFF"
1032 * DWORD size
1033 * DWORD riff_id
1034 * BYTE[] data
1036 * LIST:
1037 * DWORD "LIST"
1038 * DWORD size
1039 * DWORD list_id
1040 * BYTE[] data
1042 * CHUNK:
1043 * DWORD chunk_id
1044 * DWORD size
1045 * BYTE[] data
1047 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1049 const unsigned char *ptr = parent_chunk->data;
1050 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1052 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1054 while (ptr < end)
1056 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1057 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1059 ptr += sizeof(DWORD);
1060 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1061 ptr += sizeof(DWORD);
1062 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1063 chunk->data = ptr;
1065 return;
1068 ptr += sizeof(DWORD);
1069 ptr += (*(const DWORD *)ptr + 1) & ~1;
1070 ptr += sizeof(DWORD);
1076 * .ANI layout:
1078 * RIFF:'ACON' RIFF chunk
1079 * |- CHUNK:'anih' Header
1080 * |- CHUNK:'seq ' Sequence information (optional)
1081 * \- LIST:'fram' Frame list
1082 * |- CHUNK:icon Cursor frames
1083 * |- CHUNK:icon
1084 * |- ...
1085 * \- CHUNK:icon
1087 static HCURSOR CURSORICON_CreateIconFromANI( const BYTE *bits, DWORD bits_size, INT width, INT height,
1088 INT depth, BOOL is_icon, UINT loadflags )
1090 struct animated_cursoricon_object *ani_icon_data;
1091 struct cursoricon_object *info;
1092 DWORD *frame_rates = NULL;
1093 DWORD *frame_seq = NULL;
1094 ani_header header;
1095 BOOL use_seq = FALSE;
1096 HCURSOR cursor;
1097 UINT i;
1098 BOOL error = FALSE;
1099 HICON *frames;
1101 riff_chunk_t root_chunk = { bits_size, bits };
1102 riff_chunk_t ACON_chunk = {0};
1103 riff_chunk_t anih_chunk = {0};
1104 riff_chunk_t fram_chunk = {0};
1105 riff_chunk_t rate_chunk = {0};
1106 riff_chunk_t seq_chunk = {0};
1107 const unsigned char *icon_chunk;
1108 const unsigned char *icon_data;
1110 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1112 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1113 if (!ACON_chunk.data)
1115 ERR("Failed to get root chunk.\n");
1116 return 0;
1119 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1120 if (!anih_chunk.data)
1122 ERR("Failed to get 'anih' chunk.\n");
1123 return 0;
1125 memcpy( &header, anih_chunk.data, sizeof(header) );
1126 dump_ani_header( &header );
1128 if (!(header.flags & ANI_FLAG_ICON))
1130 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1131 return 0;
1134 if (header.flags & ANI_FLAG_SEQUENCE)
1136 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1137 if (seq_chunk.data)
1139 frame_seq = (DWORD *) seq_chunk.data;
1140 use_seq = TRUE;
1142 else
1144 FIXME("Sequence data expected but not found, assuming steps == frames.\n");
1145 header.num_steps = header.num_frames;
1149 riff_find_chunk( ANI_rate_ID, 0, &ACON_chunk, &rate_chunk );
1150 if (rate_chunk.data)
1151 frame_rates = (DWORD *) rate_chunk.data;
1153 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1154 if (!fram_chunk.data)
1156 ERR("Failed to get icon list.\n");
1157 return 0;
1160 cursor = alloc_icon_handle( TRUE, header.num_steps );
1161 if (!cursor) return 0;
1162 frames = HeapAlloc( GetProcessHeap(), 0, sizeof(*frames) * header.num_frames );
1163 if (!frames)
1165 free_icon_handle( cursor );
1166 return 0;
1169 info = get_icon_ptr( cursor );
1170 ani_icon_data = (struct animated_cursoricon_object *) info;
1171 info->is_icon = is_icon;
1172 ani_icon_data->num_frames = header.num_frames;
1174 /* The .ANI stores the display rate in jiffies (1/60s) */
1175 info->delay = header.display_rate;
1177 icon_chunk = fram_chunk.data;
1178 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1179 for (i=0; i<header.num_frames; i++)
1181 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1182 const CURSORICONFILEDIRENTRY *entry;
1183 INT frameWidth, frameHeight;
1184 const BITMAPINFO *bmi;
1186 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1187 bits + bits_size - icon_data,
1188 width, height, depth, loadflags );
1190 info->hotspot.x = entry->xHotspot;
1191 info->hotspot.y = entry->yHotspot;
1192 if (!header.width || !header.height)
1194 frameWidth = entry->bWidth;
1195 frameHeight = entry->bHeight;
1197 else
1199 frameWidth = header.width;
1200 frameHeight = header.height;
1203 frames[i] = NULL;
1204 if (entry->dwDIBOffset < bits + bits_size - icon_data)
1206 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1207 /* Grab a frame from the animation */
1208 frames[i] = create_icon_from_bmi( bmi, bits + bits_size - (const BYTE *)bmi,
1209 NULL, NULL, NULL, info->hotspot,
1210 is_icon, frameWidth, frameHeight, loadflags );
1213 if (!frames[i])
1215 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1216 error = TRUE;
1217 if (i == 0)
1219 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1220 ani_icon_data->num_frames = 0;
1221 release_user_handle_ptr( info );
1222 free_icon_handle( cursor );
1223 HeapFree( GetProcessHeap(), 0, frames );
1224 return 0;
1226 break;
1229 /* Advance to the next chunk */
1230 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1231 icon_data = icon_chunk + (2 * sizeof(DWORD));
1234 /* There was an error but we at least decoded the first frame, so just use that frame */
1235 if (error)
1237 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1238 for (i=1; i<ani_icon_data->num_frames; i++)
1239 free_icon_handle( ani_icon_data->frames[i] );
1240 use_seq = FALSE;
1241 info->delay = 0;
1242 ani_icon_data->num_steps = 1;
1243 ani_icon_data->num_frames = 1;
1246 /* Setup the animated frames in the correct sequence */
1247 for (i=0; i<ani_icon_data->num_steps; i++)
1249 DWORD frame_id = use_seq ? frame_seq[i] : i;
1250 struct cursoricon_frame *frame;
1252 if (frame_id >= ani_icon_data->num_frames)
1254 frame_id = ani_icon_data->num_frames-1;
1255 ERR_(cursor)("Sequence indicates frame past end of list, corrupt?\n");
1257 ani_icon_data->frames[i] = frames[frame_id];
1258 frame = get_icon_frame( info, i );
1259 if (frame_rates)
1260 frame->delay = frame_rates[i];
1261 else
1262 frame->delay = ~0;
1263 release_icon_frame( info, frame );
1266 HeapFree( GetProcessHeap(), 0, frames );
1267 release_user_handle_ptr( info );
1269 return cursor;
1273 /**********************************************************************
1274 * CreateIconFromResourceEx (USER32.@)
1276 * FIXME: Convert to mono when cFlag is LR_MONOCHROME.
1278 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1279 BOOL bIcon, DWORD dwVersion,
1280 INT width, INT height,
1281 UINT cFlag )
1283 POINT hotspot;
1284 const BITMAPINFO *bmi;
1286 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1287 bits, cbSize, dwVersion, width, height,
1288 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1290 if (!bits) return 0;
1292 if (dwVersion == 0x00020000)
1294 FIXME_(cursor)("\t2.xx resources are not supported\n");
1295 return 0;
1298 /* Check if the resource is an animated icon/cursor */
1299 if (!memcmp(bits, "RIFF", 4))
1300 return CURSORICON_CreateIconFromANI( bits, cbSize, width, height,
1301 0 /* default depth */, bIcon, cFlag );
1303 if (bIcon)
1305 hotspot.x = width / 2;
1306 hotspot.y = height / 2;
1307 bmi = (BITMAPINFO *)bits;
1309 else /* get the hotspot */
1311 const SHORT *pt = (const SHORT *)bits;
1312 hotspot.x = pt[0];
1313 hotspot.y = pt[1];
1314 bmi = (const BITMAPINFO *)(pt + 2);
1315 cbSize -= 2 * sizeof(*pt);
1318 return create_icon_from_bmi( bmi, cbSize, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1322 /**********************************************************************
1323 * CreateIconFromResource (USER32.@)
1325 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1326 BOOL bIcon, DWORD dwVersion)
1328 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1332 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1333 INT width, INT height, INT depth,
1334 BOOL fCursor, UINT loadflags)
1336 const CURSORICONFILEDIRENTRY *entry;
1337 const CURSORICONFILEDIR *dir;
1338 DWORD filesize = 0;
1339 HICON hIcon = 0;
1340 const BYTE *bits;
1341 POINT hotspot;
1343 TRACE("loading %s\n", debugstr_w( filename ));
1345 bits = map_fileW( filename, &filesize );
1346 if (!bits)
1347 return hIcon;
1349 /* Check for .ani. */
1350 if (memcmp( bits, "RIFF", 4 ) == 0)
1352 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, !fCursor, loadflags );
1353 goto end;
1356 dir = (const CURSORICONFILEDIR*) bits;
1357 if ( filesize < FIELD_OFFSET( CURSORICONFILEDIR, idEntries[dir->idCount] ))
1358 goto end;
1360 if ( fCursor )
1361 entry = CURSORICON_FindBestCursorFile( dir, filesize, width, height, depth, loadflags );
1362 else
1363 entry = CURSORICON_FindBestIconFile( dir, filesize, width, height, depth, loadflags );
1365 if ( !entry )
1366 goto end;
1368 /* check that we don't run off the end of the file */
1369 if ( entry->dwDIBOffset > filesize )
1370 goto end;
1371 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1372 goto end;
1374 hotspot.x = entry->xHotspot;
1375 hotspot.y = entry->yHotspot;
1376 hIcon = create_icon_from_bmi( (const BITMAPINFO *)&bits[entry->dwDIBOffset], filesize - entry->dwDIBOffset,
1377 NULL, NULL, NULL, hotspot, !fCursor, width, height, loadflags );
1378 end:
1379 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1380 UnmapViewOfFile( bits );
1381 return hIcon;
1384 /**********************************************************************
1385 * CURSORICON_Load
1387 * Load a cursor or icon from resource or file.
1389 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1390 INT width, INT height, INT depth,
1391 BOOL fCursor, UINT loadflags)
1393 HANDLE handle = 0;
1394 HICON hIcon = 0;
1395 HRSRC hRsrc;
1396 DWORD size;
1397 const CURSORICONDIR *dir;
1398 const CURSORICONDIRENTRY *dirEntry;
1399 const BYTE *bits;
1400 WORD wResId;
1401 POINT hotspot;
1403 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1404 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1406 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1407 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1409 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1411 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1412 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1414 /* Get directory resource ID */
1416 if (!(hRsrc = FindResourceW( hInstance, name,
1417 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1419 /* try animated resource */
1420 if (!(hRsrc = FindResourceW( hInstance, name,
1421 (LPWSTR)(fCursor ? RT_ANICURSOR : RT_ANIICON) ))) return 0;
1422 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1423 bits = LockResource( handle );
1424 return CURSORICON_CreateIconFromANI( bits, SizeofResource( hInstance, handle ),
1425 width, height, depth, !fCursor, loadflags );
1428 /* Find the best entry in the directory */
1430 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1431 if (!(dir = LockResource( handle ))) return 0;
1432 size = SizeofResource( hInstance, hRsrc );
1433 if (fCursor)
1434 dirEntry = CURSORICON_FindBestCursorRes( dir, size, width, height, depth, loadflags );
1435 else
1436 dirEntry = CURSORICON_FindBestIconRes( dir, size, width, height, depth, loadflags );
1437 if (!dirEntry) return 0;
1438 wResId = dirEntry->wResId;
1439 FreeResource( handle );
1441 /* Load the resource */
1443 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1444 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1446 /* If shared icon, check whether it was already loaded */
1447 if (loadflags & LR_SHARED)
1449 struct cursoricon_object *ptr;
1451 USER_Lock();
1452 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1454 if (ptr->module != hInstance) continue;
1455 if (ptr->rsrc != hRsrc) continue;
1456 hIcon = ptr->obj.handle;
1457 break;
1459 USER_Unlock();
1460 if (hIcon) return hIcon;
1463 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1464 size = SizeofResource( hInstance, hRsrc );
1465 bits = LockResource( handle );
1467 if (!fCursor)
1469 hotspot.x = width / 2;
1470 hotspot.y = height / 2;
1472 else /* get the hotspot */
1474 const SHORT *pt = (const SHORT *)bits;
1475 hotspot.x = pt[0];
1476 hotspot.y = pt[1];
1477 bits += 2 * sizeof(SHORT);
1478 size -= 2 * sizeof(SHORT);
1480 hIcon = create_icon_from_bmi( (const BITMAPINFO *)bits, size, hInstance, name, hRsrc,
1481 hotspot, !fCursor, width, height, loadflags );
1482 FreeResource( handle );
1483 return hIcon;
1487 /***********************************************************************
1488 * CreateCursor (USER32.@)
1490 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1491 INT xHotSpot, INT yHotSpot,
1492 INT nWidth, INT nHeight,
1493 LPCVOID lpANDbits, LPCVOID lpXORbits )
1495 ICONINFO info;
1496 HCURSOR hCursor;
1498 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1499 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1501 info.fIcon = FALSE;
1502 info.xHotspot = xHotSpot;
1503 info.yHotspot = yHotSpot;
1504 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1505 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1506 hCursor = CreateIconIndirect( &info );
1507 DeleteObject( info.hbmMask );
1508 DeleteObject( info.hbmColor );
1509 return hCursor;
1513 /***********************************************************************
1514 * CreateIcon (USER32.@)
1516 * Creates an icon based on the specified bitmaps. The bitmaps must be
1517 * provided in a device dependent format and will be resized to
1518 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1519 * depth. The provided bitmaps must be top-down bitmaps.
1520 * Although Windows does not support 15bpp(*) this API must support it
1521 * for Winelib applications.
1523 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1524 * format!
1526 * RETURNS
1527 * Success: handle to an icon
1528 * Failure: NULL
1530 * FIXME: Do we need to resize the bitmaps?
1532 HICON WINAPI CreateIcon(
1533 HINSTANCE hInstance, /* [in] the application's hInstance */
1534 INT nWidth, /* [in] the width of the provided bitmaps */
1535 INT nHeight, /* [in] the height of the provided bitmaps */
1536 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1537 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1538 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1539 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1541 ICONINFO iinfo;
1542 HICON hIcon;
1544 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1545 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1547 iinfo.fIcon = TRUE;
1548 iinfo.xHotspot = nWidth / 2;
1549 iinfo.yHotspot = nHeight / 2;
1550 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1551 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1553 hIcon = CreateIconIndirect( &iinfo );
1555 DeleteObject( iinfo.hbmMask );
1556 DeleteObject( iinfo.hbmColor );
1558 return hIcon;
1562 /***********************************************************************
1563 * CopyIcon (USER32.@)
1565 HICON WINAPI CopyIcon( HICON hIcon )
1567 struct cursoricon_object *ptrOld, *ptrNew;
1568 HICON hNew;
1570 if (!(ptrOld = get_icon_ptr( hIcon )))
1572 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1573 return 0;
1575 if ((hNew = alloc_icon_handle( FALSE, 0 )))
1577 struct cursoricon_frame *frameOld, *frameNew;
1579 ptrNew = get_icon_ptr( hNew );
1580 ptrNew->is_icon = ptrOld->is_icon;
1581 ptrNew->hotspot = ptrOld->hotspot;
1582 if (!(frameOld = get_icon_frame( ptrOld, 0 )))
1584 release_user_handle_ptr( ptrOld );
1585 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1586 return 0;
1588 if (!(frameNew = get_icon_frame( ptrNew, 0 )))
1590 release_icon_frame( ptrOld, frameOld );
1591 release_user_handle_ptr( ptrOld );
1592 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1593 return 0;
1595 frameNew->delay = 0;
1596 frameNew->width = frameOld->width;
1597 frameNew->height = frameOld->height;
1598 frameNew->mask = copy_bitmap( frameOld->mask );
1599 frameNew->color = copy_bitmap( frameOld->color );
1600 frameNew->alpha = copy_bitmap( frameOld->alpha );
1601 release_icon_frame( ptrOld, frameOld );
1602 release_icon_frame( ptrNew, frameNew );
1603 release_user_handle_ptr( ptrNew );
1605 release_user_handle_ptr( ptrOld );
1606 return hNew;
1610 /***********************************************************************
1611 * DestroyIcon (USER32.@)
1613 BOOL WINAPI DestroyIcon( HICON hIcon )
1615 BOOL ret = FALSE;
1616 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1618 TRACE_(icon)("%p\n", hIcon );
1620 if (obj)
1622 BOOL shared = (obj->rsrc != NULL);
1623 release_user_handle_ptr( obj );
1624 ret = (GetCursor() != hIcon);
1625 if (!shared) free_icon_handle( hIcon );
1627 return ret;
1631 /***********************************************************************
1632 * DestroyCursor (USER32.@)
1634 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1636 return DestroyIcon( hCursor );
1639 /***********************************************************************
1640 * DrawIcon (USER32.@)
1642 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1644 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1647 /***********************************************************************
1648 * SetCursor (USER32.@)
1650 * Set the cursor shape.
1652 * RETURNS
1653 * A handle to the previous cursor shape.
1655 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1657 struct cursoricon_object *obj;
1658 HCURSOR hOldCursor;
1659 int show_count;
1660 BOOL ret;
1662 TRACE("%p\n", hCursor);
1664 SERVER_START_REQ( set_cursor )
1666 req->flags = SET_CURSOR_HANDLE;
1667 req->handle = wine_server_user_handle( hCursor );
1668 if ((ret = !wine_server_call_err( req )))
1670 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1671 show_count = reply->prev_count;
1674 SERVER_END_REQ;
1676 if (!ret) return 0;
1677 USER_Driver->pSetCursor( show_count >= 0 ? hCursor : 0 );
1679 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1680 release_user_handle_ptr( obj );
1681 return hOldCursor;
1684 /***********************************************************************
1685 * ShowCursor (USER32.@)
1687 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1689 HCURSOR cursor;
1690 int increment = bShow ? 1 : -1;
1691 int count;
1693 SERVER_START_REQ( set_cursor )
1695 req->flags = SET_CURSOR_COUNT;
1696 req->show_count = increment;
1697 wine_server_call( req );
1698 cursor = wine_server_ptr_handle( reply->prev_handle );
1699 count = reply->prev_count + increment;
1701 SERVER_END_REQ;
1703 TRACE("%d, count=%d\n", bShow, count );
1705 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1706 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1708 return count;
1711 /***********************************************************************
1712 * GetCursor (USER32.@)
1714 HCURSOR WINAPI GetCursor(void)
1716 HCURSOR ret;
1718 SERVER_START_REQ( set_cursor )
1720 req->flags = 0;
1721 wine_server_call( req );
1722 ret = wine_server_ptr_handle( reply->prev_handle );
1724 SERVER_END_REQ;
1725 return ret;
1729 /***********************************************************************
1730 * ClipCursor (USER32.@)
1732 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1734 BOOL ret;
1735 RECT new_rect;
1737 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1739 if (rect && (rect->left > rect->right || rect->top > rect->bottom)) return FALSE;
1741 SERVER_START_REQ( set_cursor )
1743 req->clip_msg = WM_WINE_CLIPCURSOR;
1744 if (rect)
1746 req->flags = SET_CURSOR_CLIP;
1747 req->clip.left = rect->left;
1748 req->clip.top = rect->top;
1749 req->clip.right = rect->right;
1750 req->clip.bottom = rect->bottom;
1752 else req->flags = SET_CURSOR_NOCLIP;
1754 if ((ret = !wine_server_call( req )))
1756 new_rect.left = reply->new_clip.left;
1757 new_rect.top = reply->new_clip.top;
1758 new_rect.right = reply->new_clip.right;
1759 new_rect.bottom = reply->new_clip.bottom;
1762 SERVER_END_REQ;
1763 if (ret) USER_Driver->pClipCursor( &new_rect );
1764 return ret;
1768 /***********************************************************************
1769 * GetClipCursor (USER32.@)
1771 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1773 BOOL ret;
1775 if (!rect) return FALSE;
1777 SERVER_START_REQ( set_cursor )
1779 req->flags = 0;
1780 if ((ret = !wine_server_call( req )))
1782 rect->left = reply->new_clip.left;
1783 rect->top = reply->new_clip.top;
1784 rect->right = reply->new_clip.right;
1785 rect->bottom = reply->new_clip.bottom;
1788 SERVER_END_REQ;
1789 return ret;
1793 /***********************************************************************
1794 * SetSystemCursor (USER32.@)
1796 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1798 FIXME("(%p,%08x),stub!\n", hcur, id);
1799 return TRUE;
1803 /**********************************************************************
1804 * LookupIconIdFromDirectoryEx (USER32.@)
1806 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1807 INT width, INT height, UINT cFlag )
1809 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1810 UINT retVal = 0;
1811 if( dir && !dir->idReserved && (dir->idType & 3) )
1813 const CURSORICONDIRENTRY* entry;
1815 const HDC hdc = GetDC(0);
1816 const int depth = (cFlag & LR_MONOCHROME) ?
1817 1 : GetDeviceCaps(hdc, BITSPIXEL);
1818 ReleaseDC(0, hdc);
1820 if( bIcon )
1821 entry = CURSORICON_FindBestIconRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1822 else
1823 entry = CURSORICON_FindBestCursorRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1825 if( entry ) retVal = entry->wResId;
1827 else WARN_(cursor)("invalid resource directory\n");
1828 return retVal;
1831 /**********************************************************************
1832 * LookupIconIdFromDirectory (USER32.@)
1834 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1836 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1839 /***********************************************************************
1840 * LoadCursorW (USER32.@)
1842 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1844 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1846 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1847 LR_SHARED | LR_DEFAULTSIZE );
1850 /***********************************************************************
1851 * LoadCursorA (USER32.@)
1853 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1855 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1857 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1858 LR_SHARED | LR_DEFAULTSIZE );
1861 /***********************************************************************
1862 * LoadCursorFromFileW (USER32.@)
1864 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1866 TRACE("%s\n", debugstr_w(name));
1868 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1869 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1872 /***********************************************************************
1873 * LoadCursorFromFileA (USER32.@)
1875 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1877 TRACE("%s\n", debugstr_a(name));
1879 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1880 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1883 /***********************************************************************
1884 * LoadIconW (USER32.@)
1886 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1888 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1890 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1891 LR_SHARED | LR_DEFAULTSIZE );
1894 /***********************************************************************
1895 * LoadIconA (USER32.@)
1897 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1899 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1901 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1902 LR_SHARED | LR_DEFAULTSIZE );
1905 /**********************************************************************
1906 * GetCursorFrameInfo (USER32.@)
1908 * NOTES
1909 * So far no use has been found for the second parameter, it is currently presumed
1910 * that this parameter is reserved for future use.
1912 * PARAMS
1913 * hCursor [I] Handle to cursor for which to retrieve information
1914 * reserved [I] No purpose has been found for this parameter (may be NULL)
1915 * istep [I] The step of the cursor for which to retrieve information
1916 * rate_jiffies [O] Pointer to DWORD that receives the frame-specific delay (cannot be NULL)
1917 * num_steps [O] Pointer to DWORD that receives the number of steps in the cursor (cannot be NULL)
1919 * RETURNS
1920 * Success: Handle to a frame of the cursor (specified by istep)
1921 * Failure: NULL cursor (0)
1923 HCURSOR WINAPI GetCursorFrameInfo(HCURSOR hCursor, DWORD reserved, DWORD istep, DWORD *rate_jiffies, DWORD *num_steps)
1925 struct cursoricon_object *ptr;
1926 HCURSOR ret = 0;
1927 UINT icon_steps;
1929 if (rate_jiffies == NULL || num_steps == NULL) return 0;
1931 if (!(ptr = get_icon_ptr( hCursor ))) return 0;
1933 TRACE("%p => %d %d %p %p\n", hCursor, reserved, istep, rate_jiffies, num_steps);
1934 if (reserved != 0)
1935 FIXME("Second parameter non-zero (%d), please report this!\n", reserved);
1937 icon_steps = get_icon_steps(ptr);
1938 if (istep < icon_steps || !ptr->is_ani)
1940 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) ptr;
1941 UINT icon_frames = 1;
1943 if (ptr->is_ani)
1944 icon_frames = ani_icon_data->num_frames;
1945 if (ptr->is_ani && icon_frames > 1)
1946 ret = ani_icon_data->frames[istep];
1947 else
1948 ret = hCursor;
1949 if (icon_frames == 1)
1951 *rate_jiffies = 0;
1952 *num_steps = 1;
1954 else if (icon_steps == 1)
1956 *num_steps = ~0;
1957 *rate_jiffies = ptr->delay;
1959 else if (istep < icon_steps)
1961 struct cursoricon_frame *frame;
1963 *num_steps = icon_steps;
1964 frame = get_icon_frame( ptr, istep );
1965 if (get_icon_steps(ptr) == 1)
1966 *num_steps = ~0;
1967 else
1968 *num_steps = get_icon_steps(ptr);
1969 /* If this specific frame does not have a delay then use the global delay */
1970 if (frame->delay == ~0)
1971 *rate_jiffies = ptr->delay;
1972 else
1973 *rate_jiffies = frame->delay;
1974 release_icon_frame( ptr, frame );
1978 release_user_handle_ptr( ptr );
1980 return ret;
1983 /**********************************************************************
1984 * GetIconInfo (USER32.@)
1986 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1988 ICONINFOEXW infoW;
1990 infoW.cbSize = sizeof(infoW);
1991 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
1992 iconinfo->fIcon = infoW.fIcon;
1993 iconinfo->xHotspot = infoW.xHotspot;
1994 iconinfo->yHotspot = infoW.yHotspot;
1995 iconinfo->hbmColor = infoW.hbmColor;
1996 iconinfo->hbmMask = infoW.hbmMask;
1997 return TRUE;
2000 /**********************************************************************
2001 * GetIconInfoExA (USER32.@)
2003 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
2005 ICONINFOEXW infoW;
2007 if (info->cbSize != sizeof(*info))
2009 SetLastError( ERROR_INVALID_PARAMETER );
2010 return FALSE;
2012 infoW.cbSize = sizeof(infoW);
2013 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
2014 info->fIcon = infoW.fIcon;
2015 info->xHotspot = infoW.xHotspot;
2016 info->yHotspot = infoW.yHotspot;
2017 info->hbmColor = infoW.hbmColor;
2018 info->hbmMask = infoW.hbmMask;
2019 info->wResID = infoW.wResID;
2020 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
2021 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
2022 return TRUE;
2025 /**********************************************************************
2026 * GetIconInfoExW (USER32.@)
2028 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
2030 struct cursoricon_frame *frame;
2031 struct cursoricon_object *ptr;
2032 HMODULE module;
2033 BOOL ret = TRUE;
2035 if (info->cbSize != sizeof(*info))
2037 SetLastError( ERROR_INVALID_PARAMETER );
2038 return FALSE;
2040 if (!(ptr = get_icon_ptr( icon )))
2042 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2043 return FALSE;
2046 frame = get_icon_frame( ptr, 0 );
2047 if (!frame)
2049 release_user_handle_ptr( ptr );
2050 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2051 return FALSE;
2054 TRACE("%p => %dx%d\n", icon, frame->width, frame->height);
2056 info->fIcon = ptr->is_icon;
2057 info->xHotspot = ptr->hotspot.x;
2058 info->yHotspot = ptr->hotspot.y;
2059 info->hbmColor = copy_bitmap( frame->color );
2060 info->hbmMask = copy_bitmap( frame->mask );
2061 info->wResID = 0;
2062 info->szModName[0] = 0;
2063 info->szResName[0] = 0;
2064 if (ptr->module)
2066 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
2067 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
2069 if (!info->hbmMask || (!info->hbmColor && frame->color))
2071 DeleteObject( info->hbmMask );
2072 DeleteObject( info->hbmColor );
2073 ret = FALSE;
2075 module = ptr->module;
2076 release_icon_frame( ptr, frame );
2077 release_user_handle_ptr( ptr );
2078 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
2079 return ret;
2082 /* copy an icon bitmap, even when it can't be selected into a DC */
2083 /* helper for CreateIconIndirect */
2084 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
2085 HBITMAP src, int width, int height )
2087 HDC hdc = CreateCompatibleDC( 0 );
2089 if (!SelectObject( hdc, src )) /* do it the hard way */
2091 BITMAPINFO *info;
2092 void *bits;
2094 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
2095 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2096 info->bmiHeader.biWidth = width;
2097 info->bmiHeader.biHeight = height;
2098 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
2099 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
2100 info->bmiHeader.biCompression = BI_RGB;
2101 info->bmiHeader.biSizeImage = get_dib_image_size( width, height, info->bmiHeader.biBitCount );
2102 info->bmiHeader.biXPelsPerMeter = 0;
2103 info->bmiHeader.biYPelsPerMeter = 0;
2104 info->bmiHeader.biClrUsed = 0;
2105 info->bmiHeader.biClrImportant = 0;
2106 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
2107 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
2108 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
2109 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
2111 HeapFree( GetProcessHeap(), 0, bits );
2112 HeapFree( GetProcessHeap(), 0, info );
2114 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
2116 DeleteDC( hdc );
2119 /**********************************************************************
2120 * CreateIconIndirect (USER32.@)
2122 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2124 BITMAP bmpXor, bmpAnd;
2125 HICON hObj;
2126 HBITMAP color = 0, mask;
2127 int width, height;
2128 HDC hdc;
2130 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2131 iconinfo->hbmColor, iconinfo->hbmMask,
2132 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2134 if (!iconinfo->hbmMask) return 0;
2136 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2137 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2138 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2139 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2141 if (iconinfo->hbmColor)
2143 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2144 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2145 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2146 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2148 width = bmpXor.bmWidth;
2149 height = bmpXor.bmHeight;
2150 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1 || bmpAnd.bmPlanes * bmpAnd.bmBitsPixel != 1)
2152 color = CreateCompatibleBitmap( screen_dc, width, height );
2153 mask = CreateBitmap( width, height, 1, 1, NULL );
2155 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2157 else
2159 width = bmpAnd.bmWidth;
2160 height = bmpAnd.bmHeight;
2161 mask = CreateBitmap( width, height, 1, 1, NULL );
2164 hdc = CreateCompatibleDC( 0 );
2165 SelectObject( hdc, mask );
2166 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
2168 if (color)
2170 SelectObject( hdc, color );
2171 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
2173 else if (iconinfo->hbmColor)
2175 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
2177 else height /= 2;
2179 DeleteDC( hdc );
2181 hObj = alloc_icon_handle( FALSE, 0 );
2182 if (hObj)
2184 struct cursoricon_object *info = get_icon_ptr( hObj );
2185 struct cursoricon_frame *frame;
2187 info->is_icon = iconinfo->fIcon;
2188 frame = get_icon_frame( info, 0 );
2189 frame->delay = ~0;
2190 frame->width = width;
2191 frame->height = height;
2192 frame->color = color;
2193 frame->mask = mask;
2194 frame->alpha = create_alpha_bitmap( iconinfo->hbmColor, NULL, NULL );
2195 release_icon_frame( info, frame );
2196 if (info->is_icon)
2198 info->hotspot.x = width / 2;
2199 info->hotspot.y = height / 2;
2201 else
2203 info->hotspot.x = iconinfo->xHotspot;
2204 info->hotspot.y = iconinfo->yHotspot;
2207 release_user_handle_ptr( info );
2209 return hObj;
2212 /******************************************************************************
2213 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2215 * NOTES
2216 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2218 * PARAMS
2219 * hdc [I] Handle to device context
2220 * x0 [I] X coordinate of upper left corner
2221 * y0 [I] Y coordinate of upper left corner
2222 * hIcon [I] Handle to icon to draw
2223 * cxWidth [I] Width of icon
2224 * cyWidth [I] Height of icon
2225 * istep [I] Index of frame in animated cursor
2226 * hbr [I] Handle to background brush
2227 * flags [I] Icon-drawing flags
2229 * RETURNS
2230 * Success: TRUE
2231 * Failure: FALSE
2233 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2234 INT cxWidth, INT cyWidth, UINT istep,
2235 HBRUSH hbr, UINT flags )
2237 struct cursoricon_frame *frame;
2238 struct cursoricon_object *ptr;
2239 HDC hdc_dest, hMemDC;
2240 BOOL result = FALSE, DoOffscreen;
2241 HBITMAP hB_off = 0;
2242 COLORREF oldFg, oldBg;
2243 INT x, y, nStretchMode;
2245 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2246 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2248 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2249 if (istep >= get_icon_steps( ptr ))
2251 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
2252 release_user_handle_ptr( ptr );
2253 return FALSE;
2255 if (!(frame = get_icon_frame( ptr, istep )))
2257 FIXME_(icon)("Error retrieving icon frame %d\n", istep);
2258 release_user_handle_ptr( ptr );
2259 return FALSE;
2261 if (!(hMemDC = CreateCompatibleDC( hdc )))
2263 release_icon_frame( ptr, frame );
2264 release_user_handle_ptr( ptr );
2265 return FALSE;
2268 if (flags & DI_NOMIRROR)
2269 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2271 /* Calculate the size of the destination image. */
2272 if (cxWidth == 0)
2274 if (flags & DI_DEFAULTSIZE)
2275 cxWidth = GetSystemMetrics (SM_CXICON);
2276 else
2277 cxWidth = frame->width;
2279 if (cyWidth == 0)
2281 if (flags & DI_DEFAULTSIZE)
2282 cyWidth = GetSystemMetrics (SM_CYICON);
2283 else
2284 cyWidth = frame->height;
2287 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2289 if (DoOffscreen) {
2290 RECT r;
2292 r.left = 0;
2293 r.top = 0;
2294 r.right = cxWidth;
2295 r.bottom = cxWidth;
2297 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2298 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2300 DeleteDC( hdc_dest );
2301 goto failed;
2303 SelectObject(hdc_dest, hB_off);
2304 FillRect(hdc_dest, &r, hbr);
2305 x = y = 0;
2307 else
2309 hdc_dest = hdc;
2310 x = x0;
2311 y = y0;
2314 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2316 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2317 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2319 if (frame->alpha && (flags & DI_IMAGE))
2321 BOOL alpha_blend = TRUE;
2323 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2325 BITMAP bm;
2326 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2327 alpha_blend = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel > 8;
2329 if (alpha_blend)
2331 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2332 SelectObject( hMemDC, frame->alpha );
2333 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2334 0, 0, frame->width, frame->height,
2335 pixelblend )) goto done;
2339 if (flags & DI_MASK)
2341 DWORD rop = (flags & DI_IMAGE) ? SRCAND : SRCCOPY;
2342 SelectObject( hMemDC, frame->mask );
2343 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2344 hMemDC, 0, 0, frame->width, frame->height, rop );
2347 if (flags & DI_IMAGE)
2349 if (frame->color)
2351 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2352 SelectObject( hMemDC, frame->color );
2353 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2354 hMemDC, 0, 0, frame->width, frame->height, rop );
2356 else
2358 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2359 SelectObject( hMemDC, frame->mask );
2360 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2361 hMemDC, 0, frame->height, frame->width,
2362 frame->height, rop );
2366 done:
2367 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2369 SetTextColor( hdc, oldFg );
2370 SetBkColor( hdc, oldBg );
2371 SetStretchBltMode (hdc, nStretchMode);
2372 result = TRUE;
2373 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2374 if (hB_off) DeleteObject(hB_off);
2375 failed:
2376 DeleteDC( hMemDC );
2377 release_icon_frame( ptr, frame );
2378 release_user_handle_ptr( ptr );
2379 return result;
2382 /***********************************************************************
2383 * DIB_FixColorsToLoadflags
2385 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2386 * are in loadflags
2388 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2390 int colors;
2391 COLORREF c_W, c_S, c_F, c_L, c_C;
2392 int incr,i;
2393 RGBQUAD *ptr;
2394 int bitmap_type;
2395 LONG width;
2396 LONG height;
2397 WORD bpp;
2398 DWORD compr;
2400 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2402 WARN_(resource)("Invalid bitmap\n");
2403 return;
2406 if (bpp > 8) return;
2408 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2410 incr = 3;
2411 colors = 1 << bpp;
2413 else
2415 incr = 4;
2416 colors = bmi->bmiHeader.biClrUsed;
2417 if (colors > 256) colors = 256;
2418 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2421 c_W = GetSysColor(COLOR_WINDOW);
2422 c_S = GetSysColor(COLOR_3DSHADOW);
2423 c_F = GetSysColor(COLOR_3DFACE);
2424 c_L = GetSysColor(COLOR_3DLIGHT);
2426 if (loadflags & LR_LOADTRANSPARENT) {
2427 switch (bpp) {
2428 case 1: pix = pix >> 7; break;
2429 case 4: pix = pix >> 4; break;
2430 case 8: break;
2431 default:
2432 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2433 return;
2435 if (pix >= colors) {
2436 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2437 return;
2439 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2440 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2441 ptr->rgbBlue = GetBValue(c_W);
2442 ptr->rgbGreen = GetGValue(c_W);
2443 ptr->rgbRed = GetRValue(c_W);
2445 if (loadflags & LR_LOADMAP3DCOLORS)
2446 for (i=0; i<colors; i++) {
2447 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2448 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2449 if (c_C == RGB(128, 128, 128)) {
2450 ptr->rgbRed = GetRValue(c_S);
2451 ptr->rgbGreen = GetGValue(c_S);
2452 ptr->rgbBlue = GetBValue(c_S);
2453 } else if (c_C == RGB(192, 192, 192)) {
2454 ptr->rgbRed = GetRValue(c_F);
2455 ptr->rgbGreen = GetGValue(c_F);
2456 ptr->rgbBlue = GetBValue(c_F);
2457 } else if (c_C == RGB(223, 223, 223)) {
2458 ptr->rgbRed = GetRValue(c_L);
2459 ptr->rgbGreen = GetGValue(c_L);
2460 ptr->rgbBlue = GetBValue(c_L);
2466 /**********************************************************************
2467 * BITMAP_Load
2469 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2470 INT desiredx, INT desiredy, UINT loadflags )
2472 HBITMAP hbitmap = 0, orig_bm;
2473 HRSRC hRsrc;
2474 HGLOBAL handle;
2475 const char *ptr = NULL;
2476 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2477 int size;
2478 BYTE pix;
2479 char *bits;
2480 LONG width, height, new_width, new_height;
2481 WORD bpp_dummy;
2482 DWORD compr_dummy, offbits = 0;
2483 INT bm_type;
2484 HDC screen_mem_dc = NULL;
2486 if (!(loadflags & LR_LOADFROMFILE))
2488 if (!instance)
2490 /* OEM bitmap: try to load the resource from user32.dll */
2491 instance = user32_module;
2494 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2495 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2497 if ((info = LockResource( handle )) == NULL) return 0;
2499 else
2501 BITMAPFILEHEADER * bmfh;
2503 if (!(ptr = map_fileW( name, NULL ))) return 0;
2504 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2505 bmfh = (BITMAPFILEHEADER *)ptr;
2506 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2508 WARN("Invalid/unsupported bitmap format!\n");
2509 goto end;
2511 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2514 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2515 &bpp_dummy, &compr_dummy);
2516 if (bm_type == -1)
2518 WARN("Invalid bitmap format!\n");
2519 goto end;
2522 size = bitmap_info_size(info, DIB_RGB_COLORS);
2523 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2524 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2526 if (!fix_info || !scaled_info) goto end;
2527 memcpy(fix_info, info, size);
2529 pix = *((LPBYTE)info + size);
2530 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2532 memcpy(scaled_info, fix_info, size);
2534 if(desiredx != 0)
2535 new_width = desiredx;
2536 else
2537 new_width = width;
2539 if(desiredy != 0)
2540 new_height = height > 0 ? desiredy : -desiredy;
2541 else
2542 new_height = height;
2544 if(bm_type == 0)
2546 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2547 core->bcWidth = new_width;
2548 core->bcHeight = new_height;
2550 else
2552 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2553 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2554 WARN("Broken BitmapInfoHeader!\n");
2555 goto end;
2558 scaled_info->bmiHeader.biWidth = new_width;
2559 scaled_info->bmiHeader.biHeight = new_height;
2562 if (new_height < 0) new_height = -new_height;
2564 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2565 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2567 bits = (char *)info + (offbits ? offbits : size);
2569 if (loadflags & LR_CREATEDIBSECTION)
2571 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2572 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2574 else
2576 if (is_dib_monochrome(fix_info))
2577 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2578 else
2579 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2582 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2583 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2584 SelectObject(screen_mem_dc, orig_bm);
2586 end:
2587 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2588 HeapFree(GetProcessHeap(), 0, scaled_info);
2589 HeapFree(GetProcessHeap(), 0, fix_info);
2590 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2592 return hbitmap;
2595 /**********************************************************************
2596 * LoadImageA (USER32.@)
2598 * See LoadImageW.
2600 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2601 INT desiredx, INT desiredy, UINT loadflags)
2603 HANDLE res;
2604 LPWSTR u_name;
2606 if (IS_INTRESOURCE(name))
2607 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2609 __TRY {
2610 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2611 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2612 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2614 __EXCEPT_PAGE_FAULT {
2615 SetLastError( ERROR_INVALID_PARAMETER );
2616 return 0;
2618 __ENDTRY
2619 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2620 HeapFree(GetProcessHeap(), 0, u_name);
2621 return res;
2625 /******************************************************************************
2626 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2628 * PARAMS
2629 * hinst [I] Handle of instance that contains image
2630 * name [I] Name of image
2631 * type [I] Type of image
2632 * desiredx [I] Desired width
2633 * desiredy [I] Desired height
2634 * loadflags [I] Load flags
2636 * RETURNS
2637 * Success: Handle to newly loaded image
2638 * Failure: NULL
2640 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2642 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2643 INT desiredx, INT desiredy, UINT loadflags )
2645 int depth;
2647 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2648 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2650 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2651 switch (type) {
2652 case IMAGE_BITMAP:
2653 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2655 case IMAGE_ICON:
2656 case IMAGE_CURSOR:
2657 depth = 1;
2658 if (!(loadflags & LR_MONOCHROME))
2660 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2661 if (screen_dc) depth = GetDeviceCaps( screen_dc, BITSPIXEL );
2663 return CURSORICON_Load(hinst, name, desiredx, desiredy, depth, (type == IMAGE_CURSOR), loadflags);
2665 return 0;
2668 /******************************************************************************
2669 * CopyImage (USER32.@) Creates new image and copies attributes to it
2671 * PARAMS
2672 * hnd [I] Handle to image to copy
2673 * type [I] Type of image to copy
2674 * desiredx [I] Desired width of new image
2675 * desiredy [I] Desired height of new image
2676 * flags [I] Copy flags
2678 * RETURNS
2679 * Success: Handle to newly created image
2680 * Failure: NULL
2682 * BUGS
2683 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2684 * all other versions (95/2000/XP have been tested) ignore it.
2686 * NOTES
2687 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2688 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2689 * the copy will have the same depth as the screen.
2690 * The content of the image will only be copied if the bit depth of the
2691 * original image is compatible with the bit depth of the screen, or
2692 * if the source is a DIB section.
2693 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2695 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2696 INT desiredy, UINT flags )
2698 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2699 hnd, type, desiredx, desiredy, flags);
2701 switch (type)
2703 case IMAGE_BITMAP:
2705 HBITMAP res = NULL;
2706 DIBSECTION ds;
2707 int objSize;
2708 BITMAPINFO * bi;
2710 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2711 if (!objSize) return 0;
2712 if ((desiredx < 0) || (desiredy < 0)) return 0;
2714 if (flags & LR_COPYFROMRESOURCE)
2716 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2719 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2720 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2722 /* Allocate memory for a BITMAPINFOHEADER structure and a
2723 color table. The maximum number of colors in a color table
2724 is 256 which corresponds to a bitmap with depth 8.
2725 Bitmaps with higher depths don't have color tables. */
2726 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2727 if (!bi) return 0;
2729 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2730 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2731 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2732 bi->bmiHeader.biCompression = BI_RGB;
2734 if (flags & LR_CREATEDIBSECTION)
2736 /* Create a DIB section. LR_MONOCHROME is ignored */
2737 void * bits;
2738 HDC dc = CreateCompatibleDC(NULL);
2740 if (objSize == sizeof(DIBSECTION))
2742 /* The source bitmap is a DIB.
2743 Get its attributes to create an exact copy */
2744 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2747 bi->bmiHeader.biWidth = desiredx;
2748 bi->bmiHeader.biHeight = desiredy;
2750 /* Get the color table or the color masks */
2751 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2753 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2754 DeleteDC(dc);
2756 else
2758 /* Create a device-dependent bitmap */
2760 BOOL monochrome = (flags & LR_MONOCHROME);
2762 if (objSize == sizeof(DIBSECTION))
2764 /* The source bitmap is a DIB section.
2765 Get its attributes */
2766 HDC dc = CreateCompatibleDC(NULL);
2767 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2768 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2769 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2770 DeleteDC(dc);
2772 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2774 /* Look if the colors of the DIB are black and white */
2776 monochrome =
2777 (bi->bmiColors[0].rgbRed == 0xff
2778 && bi->bmiColors[0].rgbGreen == 0xff
2779 && bi->bmiColors[0].rgbBlue == 0xff
2780 && bi->bmiColors[0].rgbReserved == 0
2781 && bi->bmiColors[1].rgbRed == 0
2782 && bi->bmiColors[1].rgbGreen == 0
2783 && bi->bmiColors[1].rgbBlue == 0
2784 && bi->bmiColors[1].rgbReserved == 0)
2786 (bi->bmiColors[0].rgbRed == 0
2787 && bi->bmiColors[0].rgbGreen == 0
2788 && bi->bmiColors[0].rgbBlue == 0
2789 && bi->bmiColors[0].rgbReserved == 0
2790 && bi->bmiColors[1].rgbRed == 0xff
2791 && bi->bmiColors[1].rgbGreen == 0xff
2792 && bi->bmiColors[1].rgbBlue == 0xff
2793 && bi->bmiColors[1].rgbReserved == 0);
2796 else if (!monochrome)
2798 monochrome = ds.dsBm.bmBitsPixel == 1;
2801 if (monochrome)
2803 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2805 else
2807 HDC screenDC = GetDC(NULL);
2808 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2809 ReleaseDC(NULL, screenDC);
2813 if (res)
2815 /* Only copy the bitmap if it's a DIB section or if it's
2816 compatible to the screen */
2817 BOOL copyContents;
2819 if (objSize == sizeof(DIBSECTION))
2821 copyContents = TRUE;
2823 else
2825 HDC screenDC = GetDC(NULL);
2826 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2827 ReleaseDC(NULL, screenDC);
2829 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2832 if (copyContents)
2834 /* The source bitmap may already be selected in a device context,
2835 use GetDIBits/StretchDIBits and not StretchBlt */
2837 HDC dc;
2838 void * bits;
2840 dc = CreateCompatibleDC(NULL);
2842 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2843 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2844 bi->bmiHeader.biSizeImage = 0;
2845 bi->bmiHeader.biClrUsed = 0;
2846 bi->bmiHeader.biClrImportant = 0;
2848 /* Fill in biSizeImage */
2849 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2850 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2852 if (bits)
2854 HBITMAP oldBmp;
2856 /* Get the image bits of the source bitmap */
2857 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2859 /* Copy it to the destination bitmap */
2860 oldBmp = SelectObject(dc, res);
2861 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2862 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2863 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2864 SelectObject(dc, oldBmp);
2866 HeapFree(GetProcessHeap(), 0, bits);
2869 DeleteDC(dc);
2872 if (flags & LR_COPYDELETEORG)
2874 DeleteObject(hnd);
2877 HeapFree(GetProcessHeap(), 0, bi);
2878 return res;
2880 case IMAGE_ICON:
2881 case IMAGE_CURSOR:
2883 struct cursoricon_object *icon;
2884 HICON res = 0;
2885 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( screen_dc, BITSPIXEL );
2887 if (flags & LR_DEFAULTSIZE)
2889 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2890 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2893 if (!(icon = get_icon_ptr( hnd ))) return 0;
2895 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2896 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2897 !icon->is_icon, flags );
2898 else
2899 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2900 release_user_handle_ptr( icon );
2902 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2903 return res;
2906 return 0;
2910 /******************************************************************************
2911 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2913 * RETURNS
2914 * Success: Handle to specified bitmap
2915 * Failure: NULL
2917 HBITMAP WINAPI LoadBitmapW(
2918 HINSTANCE instance, /* [in] Handle to application instance */
2919 LPCWSTR name) /* [in] Address of bitmap resource name */
2921 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2924 /**********************************************************************
2925 * LoadBitmapA (USER32.@)
2927 * See LoadBitmapW.
2929 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2931 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );