usp10: Avoid leaking "open_stack" and "stack_index" in case of 0 "pair_count" in...
[wine.git] / dlls / user32 / cursoricon.c
blob8e272f09ce99eccd77f2f38a26b62c6b0fbdf55d
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 struct list icon_cache = LIST_INIT( icon_cache );
53 /**********************************************************************
54 * User objects management
57 struct cursoricon_frame
59 UINT width; /* frame-specific width */
60 UINT height; /* frame-specific height */
61 UINT delay; /* frame-specific delay between this frame and the next (in jiffies) */
62 HBITMAP color; /* color bitmap */
63 HBITMAP alpha; /* pre-multiplied alpha bitmap for 32-bpp icons */
64 HBITMAP mask; /* mask bitmap (followed by color for 1-bpp icons) */
67 struct cursoricon_object
69 struct user_object obj; /* object header */
70 struct list entry; /* entry in shared icons list */
71 ULONG_PTR param; /* opaque param used by 16-bit code */
72 HMODULE module; /* module for icons loaded from resources */
73 LPWSTR resname; /* resource name for icons loaded from resources */
74 HRSRC rsrc; /* resource for shared icons */
75 BOOL is_icon; /* whether icon or cursor */
76 BOOL is_ani; /* whether this object is a static cursor or an animated cursor */
77 UINT delay; /* delay between this frame and the next (in jiffies) */
78 POINT hotspot;
81 struct static_cursoricon_object
83 struct cursoricon_object shared;
84 struct cursoricon_frame frame; /* frame-specific icon data */
87 struct animated_cursoricon_object
89 struct cursoricon_object shared;
90 UINT num_frames; /* number of frames in the icon/cursor */
91 UINT num_steps; /* number of sequence steps in the icon/cursor */
92 HICON frames[1]; /* list of animated cursor frames */
95 static HBITMAP create_color_bitmap( int width, int height )
97 HDC hdc = get_display_dc();
98 HBITMAP ret = CreateCompatibleBitmap( hdc, width, height );
99 release_display_dc( hdc );
100 return ret;
103 static int get_display_bpp(void)
105 HDC hdc = get_display_dc();
106 int ret = GetDeviceCaps( hdc, BITSPIXEL );
107 release_display_dc( hdc );
108 return ret;
111 static HICON alloc_icon_handle( BOOL is_ani, UINT num_steps )
113 struct cursoricon_object *obj;
114 int icon_size;
115 HICON handle;
117 if (is_ani)
118 icon_size = FIELD_OFFSET( struct animated_cursoricon_object, frames[num_steps] );
119 else
120 icon_size = sizeof( struct static_cursoricon_object );
121 obj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, icon_size );
122 if (!obj) return NULL;
124 obj->delay = 0;
125 obj->is_ani = is_ani;
126 if (is_ani)
128 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
130 ani_icon_data->num_steps = num_steps;
131 ani_icon_data->num_frames = num_steps; /* changed later for some animated cursors */
134 if (!(handle = alloc_user_handle( &obj->obj, USER_ICON )))
135 HeapFree( GetProcessHeap(), 0, obj );
136 return handle;
139 static struct cursoricon_object *get_icon_ptr( HICON handle )
141 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
142 if (obj == OBJ_OTHER_PROCESS)
144 WARN( "icon handle %p from other process\n", handle );
145 obj = NULL;
147 return obj;
150 static struct cursoricon_frame *get_icon_frame( struct cursoricon_object *obj, int istep )
152 struct static_cursoricon_object *req_frame;
154 if (obj->is_ani)
156 struct animated_cursoricon_object *ani_icon_data;
157 struct cursoricon_object *frameobj;
159 ani_icon_data = (struct animated_cursoricon_object *) obj;
160 if (!(frameobj = get_icon_ptr( ani_icon_data->frames[istep] )))
161 return 0;
162 req_frame = (struct static_cursoricon_object *) frameobj;
164 else
165 req_frame = (struct static_cursoricon_object *) obj;
167 return &req_frame->frame;
170 static void release_icon_frame( struct cursoricon_object *obj, struct cursoricon_frame *frame )
172 if (obj->is_ani)
174 struct cursoricon_object *frameobj;
176 frameobj = (struct cursoricon_object *) (((char *)frame) - FIELD_OFFSET(struct static_cursoricon_object, frame));
177 release_user_handle_ptr( frameobj );
181 static UINT get_icon_steps( struct cursoricon_object *obj )
183 if (obj->is_ani)
185 struct animated_cursoricon_object *ani_icon_data;
187 ani_icon_data = (struct animated_cursoricon_object *) obj;
188 return ani_icon_data->num_steps;
190 return 1;
193 static BOOL free_icon_handle( HICON handle )
195 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
197 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
198 else if (obj)
200 ULONG_PTR param = obj->param;
201 UINT i;
203 assert( !obj->rsrc ); /* shared icons can't be freed */
205 if (!obj->is_ani)
207 struct cursoricon_frame *frame = get_icon_frame( obj, 0 );
209 if (frame->alpha) DeleteObject( frame->alpha );
210 if (frame->color) DeleteObject( frame->color );
211 DeleteObject( frame->mask );
212 release_icon_frame( obj, frame );
214 else
216 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
218 for (i=0; i<ani_icon_data->num_steps; i++)
220 HICON hFrame = ani_icon_data->frames[i];
222 if (hFrame)
224 UINT j;
226 free_icon_handle( ani_icon_data->frames[i] );
227 for (j=0; j<ani_icon_data->num_steps; j++)
229 if (ani_icon_data->frames[j] == hFrame)
230 ani_icon_data->frames[j] = 0;
235 if (!IS_INTRESOURCE( obj->resname )) HeapFree( GetProcessHeap(), 0, obj->resname );
236 HeapFree( GetProcessHeap(), 0, obj );
237 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
238 USER_Driver->pDestroyCursorIcon( handle );
239 return TRUE;
241 return FALSE;
244 ULONG_PTR get_icon_param( HICON handle )
246 ULONG_PTR ret = 0;
247 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
249 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
250 else if (obj)
252 ret = obj->param;
253 release_user_handle_ptr( obj );
255 return ret;
258 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
260 ULONG_PTR ret = 0;
261 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
263 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
264 else if (obj)
266 ret = obj->param;
267 obj->param = param;
268 release_user_handle_ptr( obj );
270 return ret;
274 /***********************************************************************
275 * map_fileW
277 * Helper function to map a file to memory:
278 * name - file name
279 * [RETURN] ptr - pointer to mapped file
280 * [RETURN] filesize - pointer size of file to be stored if not NULL
282 static const void *map_fileW( LPCWSTR name, LPDWORD filesize )
284 HANDLE hFile, hMapping;
285 LPVOID ptr = NULL;
287 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
288 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
289 if (hFile != INVALID_HANDLE_VALUE)
291 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
292 if (hMapping)
294 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
295 CloseHandle( hMapping );
296 if (filesize)
297 *filesize = GetFileSize( hFile, NULL );
299 CloseHandle( hFile );
301 return ptr;
305 /***********************************************************************
306 * get_dib_image_size
308 * Return the size of a DIB bitmap in bytes.
310 static int get_dib_image_size( int width, int height, int depth )
312 return (((width * depth + 31) / 8) & ~3) * abs( height );
316 /***********************************************************************
317 * bitmap_info_size
319 * Return the size of the bitmap info structure including color table.
321 int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
323 unsigned int colors, size, masks = 0;
325 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
327 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
328 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
329 return sizeof(BITMAPCOREHEADER) + colors *
330 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
332 else /* assume BITMAPINFOHEADER */
334 colors = info->bmiHeader.biClrUsed;
335 if (colors > 256) /* buffer overflow otherwise */
336 colors = 256;
337 if (!colors && (info->bmiHeader.biBitCount <= 8))
338 colors = 1 << info->bmiHeader.biBitCount;
339 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
340 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
341 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
346 /***********************************************************************
347 * copy_bitmap
349 * Helper function to duplicate a bitmap.
351 static HBITMAP copy_bitmap( HBITMAP bitmap )
353 HDC src, dst = 0;
354 HBITMAP new_bitmap = 0;
355 BITMAP bmp;
357 if (!bitmap) return 0;
358 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
360 if ((src = CreateCompatibleDC( 0 )) && (dst = CreateCompatibleDC( 0 )))
362 SelectObject( src, bitmap );
363 if ((new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight )))
365 SelectObject( dst, new_bitmap );
366 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
369 DeleteDC( dst );
370 DeleteDC( src );
371 return new_bitmap;
375 /***********************************************************************
376 * is_dib_monochrome
378 * Returns whether a DIB can be converted to a monochrome DDB.
380 * A DIB can be converted if its color table contains only black and
381 * white. Black must be the first color in the color table.
383 * Note : If the first color in the color table is white followed by
384 * black, we can't convert it to a monochrome DDB with
385 * SetDIBits, because black and white would be inverted.
387 static BOOL is_dib_monochrome( const BITMAPINFO* info )
389 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
391 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
393 if (((const BITMAPCOREINFO*)info)->bmciHeader.bcBitCount != 1) return FALSE;
395 /* Check if the first color is black */
396 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
398 rgb++;
400 /* Check if the second color is white */
401 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
402 && (rgb->rgbtBlue == 0xff));
404 else return FALSE;
406 else /* assume BITMAPINFOHEADER */
408 const RGBQUAD *rgb = info->bmiColors;
410 if (info->bmiHeader.biBitCount != 1) return FALSE;
412 /* Check if the first color is black */
413 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
414 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
416 rgb++;
418 /* Check if the second color is white */
419 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
420 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
422 else return FALSE;
426 /***********************************************************************
427 * DIB_GetBitmapInfo
429 * Get the info from a bitmap header.
430 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
432 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
433 LONG *height, WORD *bpp, DWORD *compr )
435 if (header->biSize == sizeof(BITMAPCOREHEADER))
437 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
438 *width = core->bcWidth;
439 *height = core->bcHeight;
440 *bpp = core->bcBitCount;
441 *compr = 0;
442 return 0;
444 else if (header->biSize == sizeof(BITMAPINFOHEADER) ||
445 header->biSize == sizeof(BITMAPV4HEADER) ||
446 header->biSize == sizeof(BITMAPV5HEADER))
448 *width = header->biWidth;
449 *height = header->biHeight;
450 *bpp = header->biBitCount;
451 *compr = header->biCompression;
452 return 1;
454 WARN("unknown/wrong size (%u) for header\n", header->biSize);
455 return -1;
458 /**********************************************************************
459 * get_icon_size
461 BOOL get_icon_size( HICON handle, SIZE *size )
463 struct cursoricon_object *info;
464 struct cursoricon_frame *frame;
466 if (!(info = get_icon_ptr( handle ))) return FALSE;
467 frame = get_icon_frame( info, 0 );
468 size->cx = frame->width;
469 size->cy = frame->height;
470 release_icon_frame( info, frame);
471 release_user_handle_ptr( info );
472 return TRUE;
476 * The following macro functions account for the irregularities of
477 * accessing cursor and icon resources in files and resource entries.
479 typedef BOOL (*fnGetCIEntry)( LPCVOID dir, DWORD size, int n,
480 int *width, int *height, int *bits );
482 /**********************************************************************
483 * CURSORICON_FindBestIcon
485 * Find the icon closest to the requested size and bit depth.
487 static int CURSORICON_FindBestIcon( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
488 int width, int height, int depth, UINT loadflags )
490 int i, cx, cy, bits, bestEntry = -1;
491 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
492 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
494 /* Find Best Fit */
495 iTotalDiff = 0xFFFFFFFF;
496 iColorDiff = 0xFFFFFFFF;
498 if (loadflags & LR_DEFAULTSIZE)
500 if (!width) width = GetSystemMetrics( SM_CXICON );
501 if (!height) height = GetSystemMetrics( SM_CYICON );
503 else if (!width && !height)
505 /* use the size of the first entry */
506 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
507 iTotalDiff = 0;
510 for ( i = 0; iTotalDiff && get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
512 iTempXDiff = abs(width - cx);
513 iTempYDiff = abs(height - cy);
515 if(iTotalDiff > (iTempXDiff + iTempYDiff))
517 iXDiff = iTempXDiff;
518 iYDiff = iTempYDiff;
519 iTotalDiff = iXDiff + iYDiff;
523 /* Find Best Colors for Best Fit */
524 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
526 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
528 iTempColorDiff = abs(depth - bits);
529 if(iColorDiff > iTempColorDiff)
531 bestEntry = i;
532 iColorDiff = iTempColorDiff;
537 return bestEntry;
540 static BOOL CURSORICON_GetResIconEntry( LPCVOID dir, DWORD size, int n,
541 int *width, int *height, int *bits )
543 const CURSORICONDIR *resdir = dir;
544 const ICONRESDIR *icon;
546 if ( resdir->idCount <= n )
547 return FALSE;
548 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
549 return FALSE;
550 icon = &resdir->idEntries[n].ResInfo.icon;
551 *width = icon->bWidth;
552 *height = icon->bHeight;
553 *bits = resdir->idEntries[n].wBitCount;
554 return TRUE;
557 /**********************************************************************
558 * CURSORICON_FindBestCursor
560 * Find the cursor closest to the requested size.
562 * FIXME: parameter 'color' ignored.
564 static int CURSORICON_FindBestCursor( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
565 int width, int height, int depth, UINT loadflags )
567 int i, maxwidth, maxheight, maxbits, cx, cy, bits, bestEntry = -1;
569 if (loadflags & LR_DEFAULTSIZE)
571 if (!width) width = GetSystemMetrics( SM_CXCURSOR );
572 if (!height) height = GetSystemMetrics( SM_CYCURSOR );
574 else if (!width && !height)
576 /* use the first entry */
577 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
578 return 0;
581 /* First find the largest one smaller than or equal to the requested size*/
583 maxwidth = maxheight = maxbits = 0;
584 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
586 if (cx > width || cy > height) continue;
587 if (cx < maxwidth || cy < maxheight) continue;
588 if (cx == maxwidth && cy == maxheight)
590 if (loadflags & LR_MONOCHROME)
592 if (maxbits && bits >= maxbits) continue;
594 else if (bits <= maxbits) continue;
596 bestEntry = i;
597 maxwidth = cx;
598 maxheight = cy;
599 maxbits = bits;
601 if (bestEntry != -1) return bestEntry;
603 /* Now find the smallest one larger than the requested size */
605 maxwidth = maxheight = 255;
606 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
608 if (cx > maxwidth || cy > maxheight) continue;
609 if (cx == maxwidth && cy == maxheight)
611 if (loadflags & LR_MONOCHROME)
613 if (maxbits && bits >= maxbits) continue;
615 else if (bits <= maxbits) continue;
617 bestEntry = i;
618 maxwidth = cx;
619 maxheight = cy;
620 maxbits = bits;
622 if (bestEntry == -1) bestEntry = 0;
624 return bestEntry;
627 static BOOL CURSORICON_GetResCursorEntry( LPCVOID dir, DWORD size, int n,
628 int *width, int *height, int *bits )
630 const CURSORICONDIR *resdir = dir;
631 const CURSORDIR *cursor;
633 if ( resdir->idCount <= n )
634 return FALSE;
635 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
636 return FALSE;
637 cursor = &resdir->idEntries[n].ResInfo.cursor;
638 *width = cursor->wWidth;
639 *height = cursor->wHeight;
640 *bits = resdir->idEntries[n].wBitCount;
641 if (*height == *width * 2) *height /= 2;
642 return TRUE;
645 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir, DWORD size,
646 int width, int height, int depth,
647 UINT loadflags )
649 int n;
651 n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetResIconEntry,
652 width, height, depth, loadflags );
653 if ( n < 0 )
654 return NULL;
655 return &dir->idEntries[n];
658 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir, DWORD size,
659 int width, int height, int depth,
660 UINT loadflags )
662 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetResCursorEntry,
663 width, height, depth, loadflags );
664 if ( n < 0 )
665 return NULL;
666 return &dir->idEntries[n];
669 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, DWORD size, int n,
670 int *width, int *height, int *bits )
672 const CURSORICONFILEDIR *filedir = dir;
673 const CURSORICONFILEDIRENTRY *entry;
674 const BITMAPINFOHEADER *info;
676 if ( filedir->idCount <= n )
677 return FALSE;
678 if ((const char *)&filedir->idEntries[n + 1] - (const char *)dir > size)
679 return FALSE;
680 entry = &filedir->idEntries[n];
681 if (entry->dwDIBOffset > size - sizeof(info->biSize)) return FALSE;
682 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
683 if (info->biSize != sizeof(BITMAPCOREHEADER))
685 if ((const char *)(info + 1) - (const char *)dir > size) return FALSE;
686 *bits = info->biBitCount;
688 else
690 const BITMAPCOREHEADER *coreinfo = (const BITMAPCOREHEADER *)((const char *)dir + entry->dwDIBOffset);
691 if ((const char *)(coreinfo + 1) - (const char *)dir > size) return FALSE;
692 *bits = coreinfo->bcBitCount;
694 *width = entry->bWidth;
695 *height = entry->bHeight;
696 return TRUE;
699 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir, DWORD size,
700 int width, int height, int depth,
701 UINT loadflags )
703 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetFileEntry,
704 width, height, depth, loadflags );
705 if ( n < 0 )
706 return NULL;
707 return &dir->idEntries[n];
710 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir, DWORD size,
711 int width, int height, int depth,
712 UINT loadflags )
714 int n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetFileEntry,
715 width, height, depth, loadflags );
716 if ( n < 0 )
717 return NULL;
718 return &dir->idEntries[n];
721 /***********************************************************************
722 * bmi_has_alpha
724 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
726 int i;
727 BOOL has_alpha = FALSE;
728 const unsigned char *ptr = bits;
730 if (info->bmiHeader.biBitCount != 32) return FALSE;
731 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
732 if ((has_alpha = (ptr[3] != 0))) break;
733 return has_alpha;
736 /***********************************************************************
737 * create_alpha_bitmap
739 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
741 static HBITMAP create_alpha_bitmap( HBITMAP color, const BITMAPINFO *src_info, const void *color_bits )
743 HBITMAP alpha = 0;
744 BITMAPINFO *info = NULL;
745 BITMAP bm;
746 HDC hdc;
747 void *bits;
748 unsigned char *ptr;
749 int i;
751 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
752 if (bm.bmBitsPixel != 32) return 0;
754 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
755 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
756 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
757 info->bmiHeader.biWidth = bm.bmWidth;
758 info->bmiHeader.biHeight = -bm.bmHeight;
759 info->bmiHeader.biPlanes = 1;
760 info->bmiHeader.biBitCount = 32;
761 info->bmiHeader.biCompression = BI_RGB;
762 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
763 info->bmiHeader.biXPelsPerMeter = 0;
764 info->bmiHeader.biYPelsPerMeter = 0;
765 info->bmiHeader.biClrUsed = 0;
766 info->bmiHeader.biClrImportant = 0;
767 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
769 if (src_info)
771 SelectObject( hdc, alpha );
772 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
773 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
774 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
777 else
779 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
780 if (!bmi_has_alpha( info, bits ))
782 DeleteObject( alpha );
783 alpha = 0;
784 goto done;
788 /* pre-multiply by alpha */
789 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
791 unsigned int alpha = ptr[3];
792 ptr[0] = ptr[0] * alpha / 255;
793 ptr[1] = ptr[1] * alpha / 255;
794 ptr[2] = ptr[2] * alpha / 255;
797 done:
798 DeleteDC( hdc );
799 HeapFree( GetProcessHeap(), 0, info );
800 return alpha;
804 /***********************************************************************
805 * create_icon_from_bmi
807 * Create an icon from its BITMAPINFO.
809 static HICON create_icon_from_bmi( const BITMAPINFO *bmi, DWORD maxsize, HMODULE module, LPCWSTR resname,
810 HRSRC rsrc, POINT hotspot, BOOL bIcon, INT width, INT height,
811 UINT cFlag )
813 DWORD size, color_size, mask_size;
814 HBITMAP color = 0, mask = 0, alpha = 0;
815 const void *color_bits, *mask_bits;
816 BITMAPINFO *bmi_copy;
817 BOOL ret = FALSE;
818 BOOL do_stretch;
819 HICON hObj = 0;
820 HDC hdc = 0;
821 LONG bmi_width, bmi_height;
822 WORD bpp;
823 DWORD compr;
825 /* Check bitmap header */
827 if (maxsize < sizeof(BITMAPCOREHEADER))
829 WARN( "invalid size %u\n", maxsize );
830 return 0;
832 if (maxsize < bmi->bmiHeader.biSize)
834 WARN( "invalid header size %u\n", bmi->bmiHeader.biSize );
835 return 0;
837 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
838 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
839 (bmi->bmiHeader.biCompression != BI_RGB &&
840 bmi->bmiHeader.biCompression != BI_BITFIELDS)) )
842 WARN( "invalid bitmap header %u\n", bmi->bmiHeader.biSize );
843 return 0;
846 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
847 DIB_GetBitmapInfo(&bmi->bmiHeader, &bmi_width, &bmi_height, &bpp, &compr);
848 color_size = get_dib_image_size( bmi_width, bmi_height / 2,
849 bpp );
850 mask_size = get_dib_image_size( bmi_width, bmi_height / 2, 1 );
851 if (size > maxsize || color_size > maxsize - size)
853 WARN( "truncated file %u < %u+%u+%u\n", maxsize, size, color_size, mask_size );
854 return 0;
856 if (mask_size > maxsize - size - color_size) mask_size = 0; /* no mask */
858 if (cFlag & LR_DEFAULTSIZE)
860 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
861 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
863 else
865 if (!width) width = bmi_width;
866 if (!height) height = bmi_height/2;
868 do_stretch = (bmi_height/2 != height) ||
869 (bmi_width != width);
871 /* Scale the hotspot */
872 if (bIcon)
874 hotspot.x = width / 2;
875 hotspot.y = height / 2;
877 else if (do_stretch)
879 hotspot.x = (hotspot.x * width) / bmi_width;
880 hotspot.y = (hotspot.y * height) / (bmi_height / 2);
883 if (!(bmi_copy = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
884 return 0;
885 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
887 memcpy( bmi_copy, bmi, size );
888 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
889 bmi_copy->bmiHeader.biHeight /= 2;
890 else
891 ((BITMAPCOREINFO *)bmi_copy)->bmciHeader.bcHeight /= 2;
892 bmi_height /= 2;
894 color_bits = (const char*)bmi + size;
895 mask_bits = (const char*)color_bits + color_size;
897 alpha = 0;
898 if (is_dib_monochrome( bmi ))
900 if (!(mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
901 color = 0;
903 /* copy color data into second half of mask bitmap */
904 SelectObject( hdc, mask );
905 StretchDIBits( hdc, 0, height, width, height,
906 0, 0, bmi_width, bmi_height,
907 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
909 else
911 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
912 if (!(color = create_color_bitmap( width, height )))
914 DeleteObject( mask );
915 goto done;
917 SelectObject( hdc, color );
918 StretchDIBits( hdc, 0, 0, width, height,
919 0, 0, bmi_width, bmi_height,
920 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
922 if (bmi_has_alpha( bmi_copy, color_bits ))
923 alpha = create_alpha_bitmap( color, bmi_copy, color_bits );
925 /* convert info to monochrome to copy the mask */
926 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
928 RGBQUAD *rgb = bmi_copy->bmiColors;
930 bmi_copy->bmiHeader.biBitCount = 1;
931 bmi_copy->bmiHeader.biClrUsed = bmi_copy->bmiHeader.biClrImportant = 2;
932 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
933 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
934 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
936 else
938 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)bmi_copy) + 1);
940 ((BITMAPCOREINFO *)bmi_copy)->bmciHeader.bcBitCount = 1;
941 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
942 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
946 if (mask_size)
948 SelectObject( hdc, mask );
949 StretchDIBits( hdc, 0, 0, width, height,
950 0, 0, bmi_width, bmi_height,
951 mask_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
953 ret = TRUE;
955 done:
956 DeleteDC( hdc );
957 HeapFree( GetProcessHeap(), 0, bmi_copy );
959 if (ret)
960 hObj = alloc_icon_handle( FALSE, 0 );
961 if (hObj)
963 struct cursoricon_object *info = get_icon_ptr( hObj );
964 struct cursoricon_frame *frame;
966 info->is_icon = bIcon;
967 info->module = module;
968 info->hotspot = hotspot;
969 frame = get_icon_frame( info, 0 );
970 frame->delay = ~0;
971 frame->width = width;
972 frame->height = height;
973 frame->color = color;
974 frame->mask = mask;
975 frame->alpha = alpha;
976 release_icon_frame( info, frame );
977 if (!IS_INTRESOURCE(resname))
979 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
980 if (info->resname) strcpyW( info->resname, resname );
982 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
984 if (module && (cFlag & LR_SHARED))
986 info->rsrc = rsrc;
987 list_add_head( &icon_cache, &info->entry );
989 release_user_handle_ptr( info );
991 else
993 DeleteObject( color );
994 DeleteObject( alpha );
995 DeleteObject( mask );
997 return hObj;
1001 /**********************************************************************
1002 * .ANI cursor support
1004 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
1005 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
1006 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
1008 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
1009 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
1010 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
1011 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
1012 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
1013 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
1014 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
1016 #define ANI_FLAG_ICON 0x1
1017 #define ANI_FLAG_SEQUENCE 0x2
1019 typedef struct {
1020 DWORD header_size;
1021 DWORD num_frames;
1022 DWORD num_steps;
1023 DWORD width;
1024 DWORD height;
1025 DWORD bpp;
1026 DWORD num_planes;
1027 DWORD display_rate;
1028 DWORD flags;
1029 } ani_header;
1031 typedef struct {
1032 DWORD data_size;
1033 const unsigned char *data;
1034 } riff_chunk_t;
1036 static void dump_ani_header( const ani_header *header )
1038 TRACE(" header size: %d\n", header->header_size);
1039 TRACE(" frames: %d\n", header->num_frames);
1040 TRACE(" steps: %d\n", header->num_steps);
1041 TRACE(" width: %d\n", header->width);
1042 TRACE(" height: %d\n", header->height);
1043 TRACE(" bpp: %d\n", header->bpp);
1044 TRACE(" planes: %d\n", header->num_planes);
1045 TRACE(" display rate: %d\n", header->display_rate);
1046 TRACE(" flags: 0x%08x\n", header->flags);
1051 * RIFF:
1052 * DWORD "RIFF"
1053 * DWORD size
1054 * DWORD riff_id
1055 * BYTE[] data
1057 * LIST:
1058 * DWORD "LIST"
1059 * DWORD size
1060 * DWORD list_id
1061 * BYTE[] data
1063 * CHUNK:
1064 * DWORD chunk_id
1065 * DWORD size
1066 * BYTE[] data
1068 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1070 const unsigned char *ptr = parent_chunk->data;
1071 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1073 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1075 while (ptr < end)
1077 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1078 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1080 ptr += sizeof(DWORD);
1081 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1082 ptr += sizeof(DWORD);
1083 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1084 chunk->data = ptr;
1086 return;
1089 ptr += sizeof(DWORD);
1090 ptr += (*(const DWORD *)ptr + 1) & ~1;
1091 ptr += sizeof(DWORD);
1097 * .ANI layout:
1099 * RIFF:'ACON' RIFF chunk
1100 * |- CHUNK:'anih' Header
1101 * |- CHUNK:'seq ' Sequence information (optional)
1102 * \- LIST:'fram' Frame list
1103 * |- CHUNK:icon Cursor frames
1104 * |- CHUNK:icon
1105 * |- ...
1106 * \- CHUNK:icon
1108 static HCURSOR CURSORICON_CreateIconFromANI( const BYTE *bits, DWORD bits_size, INT width, INT height,
1109 INT depth, BOOL is_icon, UINT loadflags )
1111 struct animated_cursoricon_object *ani_icon_data;
1112 struct cursoricon_object *info;
1113 DWORD *frame_rates = NULL;
1114 DWORD *frame_seq = NULL;
1115 ani_header header;
1116 BOOL use_seq = FALSE;
1117 HCURSOR cursor;
1118 UINT i;
1119 BOOL error = FALSE;
1120 HICON *frames;
1122 riff_chunk_t root_chunk = { bits_size, bits };
1123 riff_chunk_t ACON_chunk = {0};
1124 riff_chunk_t anih_chunk = {0};
1125 riff_chunk_t fram_chunk = {0};
1126 riff_chunk_t rate_chunk = {0};
1127 riff_chunk_t seq_chunk = {0};
1128 const unsigned char *icon_chunk;
1129 const unsigned char *icon_data;
1131 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1133 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1134 if (!ACON_chunk.data)
1136 ERR("Failed to get root chunk.\n");
1137 return 0;
1140 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1141 if (!anih_chunk.data)
1143 ERR("Failed to get 'anih' chunk.\n");
1144 return 0;
1146 memcpy( &header, anih_chunk.data, sizeof(header) );
1147 dump_ani_header( &header );
1149 if (!(header.flags & ANI_FLAG_ICON))
1151 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1152 return 0;
1155 if (header.flags & ANI_FLAG_SEQUENCE)
1157 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1158 if (seq_chunk.data)
1160 frame_seq = (DWORD *) seq_chunk.data;
1161 use_seq = TRUE;
1163 else
1165 FIXME("Sequence data expected but not found, assuming steps == frames.\n");
1166 header.num_steps = header.num_frames;
1170 riff_find_chunk( ANI_rate_ID, 0, &ACON_chunk, &rate_chunk );
1171 if (rate_chunk.data)
1172 frame_rates = (DWORD *) rate_chunk.data;
1174 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1175 if (!fram_chunk.data)
1177 ERR("Failed to get icon list.\n");
1178 return 0;
1181 cursor = alloc_icon_handle( TRUE, header.num_steps );
1182 if (!cursor) return 0;
1183 frames = HeapAlloc( GetProcessHeap(), 0, sizeof(*frames) * header.num_frames );
1184 if (!frames)
1186 free_icon_handle( cursor );
1187 return 0;
1190 info = get_icon_ptr( cursor );
1191 ani_icon_data = (struct animated_cursoricon_object *) info;
1192 info->is_icon = is_icon;
1193 ani_icon_data->num_frames = header.num_frames;
1195 /* The .ANI stores the display rate in jiffies (1/60s) */
1196 info->delay = header.display_rate;
1198 icon_chunk = fram_chunk.data;
1199 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1200 for (i=0; i<header.num_frames; i++)
1202 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1203 const CURSORICONFILEDIRENTRY *entry;
1204 INT frameWidth, frameHeight;
1205 const BITMAPINFO *bmi;
1207 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1208 bits + bits_size - icon_data,
1209 width, height, depth, loadflags );
1211 info->hotspot.x = entry->xHotspot;
1212 info->hotspot.y = entry->yHotspot;
1213 if (!header.width || !header.height)
1215 frameWidth = entry->bWidth;
1216 frameHeight = entry->bHeight;
1218 else
1220 frameWidth = header.width;
1221 frameHeight = header.height;
1224 frames[i] = NULL;
1225 if (entry->dwDIBOffset < bits + bits_size - icon_data)
1227 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1228 /* Grab a frame from the animation */
1229 frames[i] = create_icon_from_bmi( bmi, bits + bits_size - (const BYTE *)bmi,
1230 NULL, NULL, NULL, info->hotspot,
1231 is_icon, frameWidth, frameHeight, loadflags );
1234 if (!frames[i])
1236 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1237 error = TRUE;
1238 if (i == 0)
1240 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1241 ani_icon_data->num_frames = 0;
1242 release_user_handle_ptr( info );
1243 free_icon_handle( cursor );
1244 HeapFree( GetProcessHeap(), 0, frames );
1245 return 0;
1247 break;
1250 /* Advance to the next chunk */
1251 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1252 icon_data = icon_chunk + (2 * sizeof(DWORD));
1255 /* There was an error but we at least decoded the first frame, so just use that frame */
1256 if (error)
1258 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1259 for (i=1; i<ani_icon_data->num_frames; i++)
1260 free_icon_handle( ani_icon_data->frames[i] );
1261 use_seq = FALSE;
1262 info->delay = 0;
1263 ani_icon_data->num_steps = 1;
1264 ani_icon_data->num_frames = 1;
1267 /* Setup the animated frames in the correct sequence */
1268 for (i=0; i<ani_icon_data->num_steps; i++)
1270 DWORD frame_id = use_seq ? frame_seq[i] : i;
1271 struct cursoricon_frame *frame;
1273 if (frame_id >= ani_icon_data->num_frames)
1275 frame_id = ani_icon_data->num_frames-1;
1276 ERR_(cursor)("Sequence indicates frame past end of list, corrupt?\n");
1278 ani_icon_data->frames[i] = frames[frame_id];
1279 frame = get_icon_frame( info, i );
1280 if (frame_rates)
1281 frame->delay = frame_rates[i];
1282 else
1283 frame->delay = ~0;
1284 release_icon_frame( info, frame );
1287 HeapFree( GetProcessHeap(), 0, frames );
1288 release_user_handle_ptr( info );
1290 return cursor;
1294 /**********************************************************************
1295 * CreateIconFromResourceEx (USER32.@)
1297 * FIXME: Convert to mono when cFlag is LR_MONOCHROME.
1299 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1300 BOOL bIcon, DWORD dwVersion,
1301 INT width, INT height,
1302 UINT cFlag )
1304 POINT hotspot;
1305 const BITMAPINFO *bmi;
1307 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1308 bits, cbSize, dwVersion, width, height,
1309 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1311 if (!bits) return 0;
1313 if (dwVersion == 0x00020000)
1315 FIXME_(cursor)("\t2.xx resources are not supported\n");
1316 return 0;
1319 /* Check if the resource is an animated icon/cursor */
1320 if (!memcmp(bits, "RIFF", 4))
1321 return CURSORICON_CreateIconFromANI( bits, cbSize, width, height,
1322 0 /* default depth */, bIcon, cFlag );
1324 if (bIcon)
1326 hotspot.x = width / 2;
1327 hotspot.y = height / 2;
1328 bmi = (BITMAPINFO *)bits;
1330 else /* get the hotspot */
1332 const SHORT *pt = (const SHORT *)bits;
1333 hotspot.x = pt[0];
1334 hotspot.y = pt[1];
1335 bmi = (const BITMAPINFO *)(pt + 2);
1336 cbSize -= 2 * sizeof(*pt);
1339 return create_icon_from_bmi( bmi, cbSize, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1343 /**********************************************************************
1344 * CreateIconFromResource (USER32.@)
1346 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1347 BOOL bIcon, DWORD dwVersion)
1349 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1353 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1354 INT width, INT height, INT depth,
1355 BOOL fCursor, UINT loadflags)
1357 const CURSORICONFILEDIRENTRY *entry;
1358 const CURSORICONFILEDIR *dir;
1359 DWORD filesize = 0;
1360 HICON hIcon = 0;
1361 const BYTE *bits;
1362 POINT hotspot;
1364 TRACE("loading %s\n", debugstr_w( filename ));
1366 bits = map_fileW( filename, &filesize );
1367 if (!bits)
1368 return hIcon;
1370 /* Check for .ani. */
1371 if (memcmp( bits, "RIFF", 4 ) == 0)
1373 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, !fCursor, loadflags );
1374 goto end;
1377 dir = (const CURSORICONFILEDIR*) bits;
1378 if ( filesize < FIELD_OFFSET( CURSORICONFILEDIR, idEntries[dir->idCount] ))
1379 goto end;
1381 if ( fCursor )
1382 entry = CURSORICON_FindBestCursorFile( dir, filesize, width, height, depth, loadflags );
1383 else
1384 entry = CURSORICON_FindBestIconFile( dir, filesize, width, height, depth, loadflags );
1386 if ( !entry )
1387 goto end;
1389 /* check that we don't run off the end of the file */
1390 if ( entry->dwDIBOffset > filesize )
1391 goto end;
1392 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1393 goto end;
1395 hotspot.x = entry->xHotspot;
1396 hotspot.y = entry->yHotspot;
1397 hIcon = create_icon_from_bmi( (const BITMAPINFO *)&bits[entry->dwDIBOffset], filesize - entry->dwDIBOffset,
1398 NULL, NULL, NULL, hotspot, !fCursor, width, height, loadflags );
1399 end:
1400 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1401 UnmapViewOfFile( bits );
1402 return hIcon;
1405 /**********************************************************************
1406 * CURSORICON_Load
1408 * Load a cursor or icon from resource or file.
1410 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1411 INT width, INT height, INT depth,
1412 BOOL fCursor, UINT loadflags)
1414 HANDLE handle = 0;
1415 HICON hIcon = 0;
1416 HRSRC hRsrc;
1417 DWORD size;
1418 const CURSORICONDIR *dir;
1419 const CURSORICONDIRENTRY *dirEntry;
1420 const BYTE *bits;
1421 WORD wResId;
1422 POINT hotspot;
1424 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1425 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1427 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1428 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1430 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1432 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1433 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1435 /* Get directory resource ID */
1437 if (!(hRsrc = FindResourceW( hInstance, name,
1438 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1440 /* try animated resource */
1441 if (!(hRsrc = FindResourceW( hInstance, name,
1442 (LPWSTR)(fCursor ? RT_ANICURSOR : RT_ANIICON) ))) return 0;
1443 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1444 bits = LockResource( handle );
1445 return CURSORICON_CreateIconFromANI( bits, SizeofResource( hInstance, handle ),
1446 width, height, depth, !fCursor, loadflags );
1449 /* Find the best entry in the directory */
1451 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1452 if (!(dir = LockResource( handle ))) return 0;
1453 size = SizeofResource( hInstance, hRsrc );
1454 if (fCursor)
1455 dirEntry = CURSORICON_FindBestCursorRes( dir, size, width, height, depth, loadflags );
1456 else
1457 dirEntry = CURSORICON_FindBestIconRes( dir, size, width, height, depth, loadflags );
1458 if (!dirEntry) return 0;
1459 wResId = dirEntry->wResId;
1460 FreeResource( handle );
1462 /* Load the resource */
1464 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1465 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1467 /* If shared icon, check whether it was already loaded */
1468 if (loadflags & LR_SHARED)
1470 struct cursoricon_object *ptr;
1472 USER_Lock();
1473 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1475 if (ptr->module != hInstance) continue;
1476 if (ptr->rsrc != hRsrc) continue;
1477 hIcon = ptr->obj.handle;
1478 break;
1480 USER_Unlock();
1481 if (hIcon) return hIcon;
1484 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1485 size = SizeofResource( hInstance, hRsrc );
1486 bits = LockResource( handle );
1488 if (!fCursor)
1490 hotspot.x = width / 2;
1491 hotspot.y = height / 2;
1493 else /* get the hotspot */
1495 const SHORT *pt = (const SHORT *)bits;
1496 hotspot.x = pt[0];
1497 hotspot.y = pt[1];
1498 bits += 2 * sizeof(SHORT);
1499 size -= 2 * sizeof(SHORT);
1501 hIcon = create_icon_from_bmi( (const BITMAPINFO *)bits, size, hInstance, name, hRsrc,
1502 hotspot, !fCursor, width, height, loadflags );
1503 FreeResource( handle );
1504 return hIcon;
1508 /***********************************************************************
1509 * CreateCursor (USER32.@)
1511 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1512 INT xHotSpot, INT yHotSpot,
1513 INT nWidth, INT nHeight,
1514 LPCVOID lpANDbits, LPCVOID lpXORbits )
1516 ICONINFO info;
1517 HCURSOR hCursor;
1519 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1520 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1522 info.fIcon = FALSE;
1523 info.xHotspot = xHotSpot;
1524 info.yHotspot = yHotSpot;
1525 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1526 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1527 hCursor = CreateIconIndirect( &info );
1528 DeleteObject( info.hbmMask );
1529 DeleteObject( info.hbmColor );
1530 return hCursor;
1534 /***********************************************************************
1535 * CreateIcon (USER32.@)
1537 * Creates an icon based on the specified bitmaps. The bitmaps must be
1538 * provided in a device dependent format and will be resized to
1539 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1540 * depth. The provided bitmaps must be top-down bitmaps.
1541 * Although Windows does not support 15bpp(*) this API must support it
1542 * for Winelib applications.
1544 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1545 * format!
1547 * RETURNS
1548 * Success: handle to an icon
1549 * Failure: NULL
1551 * FIXME: Do we need to resize the bitmaps?
1553 HICON WINAPI CreateIcon(
1554 HINSTANCE hInstance, /* [in] the application's hInstance */
1555 INT nWidth, /* [in] the width of the provided bitmaps */
1556 INT nHeight, /* [in] the height of the provided bitmaps */
1557 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1558 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1559 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1560 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1562 ICONINFO iinfo;
1563 HICON hIcon;
1565 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1566 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1568 iinfo.fIcon = TRUE;
1569 iinfo.xHotspot = nWidth / 2;
1570 iinfo.yHotspot = nHeight / 2;
1571 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1572 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1574 hIcon = CreateIconIndirect( &iinfo );
1576 DeleteObject( iinfo.hbmMask );
1577 DeleteObject( iinfo.hbmColor );
1579 return hIcon;
1583 /***********************************************************************
1584 * CopyIcon (USER32.@)
1586 HICON WINAPI CopyIcon( HICON hIcon )
1588 struct cursoricon_object *ptrOld, *ptrNew;
1589 HICON hNew;
1591 if (!(ptrOld = get_icon_ptr( hIcon )))
1593 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1594 return 0;
1596 if ((hNew = alloc_icon_handle( FALSE, 0 )))
1598 struct cursoricon_frame *frameOld, *frameNew;
1600 ptrNew = get_icon_ptr( hNew );
1601 ptrNew->is_icon = ptrOld->is_icon;
1602 ptrNew->hotspot = ptrOld->hotspot;
1603 if (!(frameOld = get_icon_frame( ptrOld, 0 )))
1605 release_user_handle_ptr( ptrOld );
1606 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1607 return 0;
1609 if (!(frameNew = get_icon_frame( ptrNew, 0 )))
1611 release_icon_frame( ptrOld, frameOld );
1612 release_user_handle_ptr( ptrOld );
1613 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1614 return 0;
1616 frameNew->delay = 0;
1617 frameNew->width = frameOld->width;
1618 frameNew->height = frameOld->height;
1619 frameNew->mask = copy_bitmap( frameOld->mask );
1620 frameNew->color = copy_bitmap( frameOld->color );
1621 frameNew->alpha = copy_bitmap( frameOld->alpha );
1622 release_icon_frame( ptrOld, frameOld );
1623 release_icon_frame( ptrNew, frameNew );
1624 release_user_handle_ptr( ptrNew );
1626 release_user_handle_ptr( ptrOld );
1627 return hNew;
1631 /***********************************************************************
1632 * DestroyIcon (USER32.@)
1634 BOOL WINAPI DestroyIcon( HICON hIcon )
1636 BOOL ret = FALSE;
1637 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1639 TRACE_(icon)("%p\n", hIcon );
1641 if (obj)
1643 BOOL shared = (obj->rsrc != NULL);
1644 release_user_handle_ptr( obj );
1645 ret = (GetCursor() != hIcon);
1646 if (!shared) free_icon_handle( hIcon );
1648 return ret;
1652 /***********************************************************************
1653 * DestroyCursor (USER32.@)
1655 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1657 return DestroyIcon( hCursor );
1660 /***********************************************************************
1661 * DrawIcon (USER32.@)
1663 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1665 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1668 /***********************************************************************
1669 * SetCursor (USER32.@)
1671 * Set the cursor shape.
1673 * RETURNS
1674 * A handle to the previous cursor shape.
1676 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1678 struct cursoricon_object *obj;
1679 HCURSOR hOldCursor;
1680 int show_count;
1681 BOOL ret;
1683 TRACE("%p\n", hCursor);
1685 SERVER_START_REQ( set_cursor )
1687 req->flags = SET_CURSOR_HANDLE;
1688 req->handle = wine_server_user_handle( hCursor );
1689 if ((ret = !wine_server_call_err( req )))
1691 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1692 show_count = reply->prev_count;
1695 SERVER_END_REQ;
1697 if (!ret) return 0;
1698 USER_Driver->pSetCursor( show_count >= 0 ? hCursor : 0 );
1700 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1701 release_user_handle_ptr( obj );
1702 return hOldCursor;
1705 /***********************************************************************
1706 * ShowCursor (USER32.@)
1708 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1710 HCURSOR cursor;
1711 int increment = bShow ? 1 : -1;
1712 int count;
1714 SERVER_START_REQ( set_cursor )
1716 req->flags = SET_CURSOR_COUNT;
1717 req->show_count = increment;
1718 wine_server_call( req );
1719 cursor = wine_server_ptr_handle( reply->prev_handle );
1720 count = reply->prev_count + increment;
1722 SERVER_END_REQ;
1724 TRACE("%d, count=%d\n", bShow, count );
1726 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1727 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1729 return count;
1732 /***********************************************************************
1733 * GetCursor (USER32.@)
1735 HCURSOR WINAPI GetCursor(void)
1737 HCURSOR ret;
1739 SERVER_START_REQ( set_cursor )
1741 req->flags = 0;
1742 wine_server_call( req );
1743 ret = wine_server_ptr_handle( reply->prev_handle );
1745 SERVER_END_REQ;
1746 return ret;
1750 /***********************************************************************
1751 * ClipCursor (USER32.@)
1753 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1755 BOOL ret;
1756 RECT new_rect;
1758 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1760 if (rect && (rect->left > rect->right || rect->top > rect->bottom)) return FALSE;
1762 SERVER_START_REQ( set_cursor )
1764 req->clip_msg = WM_WINE_CLIPCURSOR;
1765 if (rect)
1767 req->flags = SET_CURSOR_CLIP;
1768 req->clip.left = rect->left;
1769 req->clip.top = rect->top;
1770 req->clip.right = rect->right;
1771 req->clip.bottom = rect->bottom;
1773 else req->flags = SET_CURSOR_NOCLIP;
1775 if ((ret = !wine_server_call( req )))
1777 new_rect.left = reply->new_clip.left;
1778 new_rect.top = reply->new_clip.top;
1779 new_rect.right = reply->new_clip.right;
1780 new_rect.bottom = reply->new_clip.bottom;
1783 SERVER_END_REQ;
1784 if (ret) USER_Driver->pClipCursor( &new_rect );
1785 return ret;
1789 /***********************************************************************
1790 * GetClipCursor (USER32.@)
1792 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1794 BOOL ret;
1796 if (!rect) return FALSE;
1798 SERVER_START_REQ( set_cursor )
1800 req->flags = 0;
1801 if ((ret = !wine_server_call( req )))
1803 rect->left = reply->new_clip.left;
1804 rect->top = reply->new_clip.top;
1805 rect->right = reply->new_clip.right;
1806 rect->bottom = reply->new_clip.bottom;
1809 SERVER_END_REQ;
1810 return ret;
1814 /***********************************************************************
1815 * SetSystemCursor (USER32.@)
1817 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1819 FIXME("(%p,%08x),stub!\n", hcur, id);
1820 return TRUE;
1824 /**********************************************************************
1825 * LookupIconIdFromDirectoryEx (USER32.@)
1827 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1828 INT width, INT height, UINT cFlag )
1830 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1831 UINT retVal = 0;
1832 if( dir && !dir->idReserved && (dir->idType & 3) )
1834 const CURSORICONDIRENTRY* entry;
1835 int depth = (cFlag & LR_MONOCHROME) ? 1 : get_display_bpp();
1837 if( bIcon )
1838 entry = CURSORICON_FindBestIconRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1839 else
1840 entry = CURSORICON_FindBestCursorRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1842 if( entry ) retVal = entry->wResId;
1844 else WARN_(cursor)("invalid resource directory\n");
1845 return retVal;
1848 /**********************************************************************
1849 * LookupIconIdFromDirectory (USER32.@)
1851 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1853 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1856 /***********************************************************************
1857 * LoadCursorW (USER32.@)
1859 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1861 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1863 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1864 LR_SHARED | LR_DEFAULTSIZE );
1867 /***********************************************************************
1868 * LoadCursorA (USER32.@)
1870 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1872 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1874 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1875 LR_SHARED | LR_DEFAULTSIZE );
1878 /***********************************************************************
1879 * LoadCursorFromFileW (USER32.@)
1881 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1883 TRACE("%s\n", debugstr_w(name));
1885 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1886 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1889 /***********************************************************************
1890 * LoadCursorFromFileA (USER32.@)
1892 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1894 TRACE("%s\n", debugstr_a(name));
1896 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1897 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1900 /***********************************************************************
1901 * LoadIconW (USER32.@)
1903 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1905 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1907 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1908 LR_SHARED | LR_DEFAULTSIZE );
1911 /***********************************************************************
1912 * LoadIconA (USER32.@)
1914 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1916 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1918 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1919 LR_SHARED | LR_DEFAULTSIZE );
1922 /**********************************************************************
1923 * GetCursorFrameInfo (USER32.@)
1925 * NOTES
1926 * So far no use has been found for the second parameter, it is currently presumed
1927 * that this parameter is reserved for future use.
1929 * PARAMS
1930 * hCursor [I] Handle to cursor for which to retrieve information
1931 * reserved [I] No purpose has been found for this parameter (may be NULL)
1932 * istep [I] The step of the cursor for which to retrieve information
1933 * rate_jiffies [O] Pointer to DWORD that receives the frame-specific delay (cannot be NULL)
1934 * num_steps [O] Pointer to DWORD that receives the number of steps in the cursor (cannot be NULL)
1936 * RETURNS
1937 * Success: Handle to a frame of the cursor (specified by istep)
1938 * Failure: NULL cursor (0)
1940 HCURSOR WINAPI GetCursorFrameInfo(HCURSOR hCursor, DWORD reserved, DWORD istep, DWORD *rate_jiffies, DWORD *num_steps)
1942 struct cursoricon_object *ptr;
1943 HCURSOR ret = 0;
1944 UINT icon_steps;
1946 if (rate_jiffies == NULL || num_steps == NULL) return 0;
1948 if (!(ptr = get_icon_ptr( hCursor ))) return 0;
1950 TRACE("%p => %d %d %p %p\n", hCursor, reserved, istep, rate_jiffies, num_steps);
1951 if (reserved != 0)
1952 FIXME("Second parameter non-zero (%d), please report this!\n", reserved);
1954 icon_steps = get_icon_steps(ptr);
1955 if (istep < icon_steps || !ptr->is_ani)
1957 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) ptr;
1958 UINT icon_frames = 1;
1960 if (ptr->is_ani)
1961 icon_frames = ani_icon_data->num_frames;
1962 if (ptr->is_ani && icon_frames > 1)
1963 ret = ani_icon_data->frames[istep];
1964 else
1965 ret = hCursor;
1966 if (icon_frames == 1)
1968 *rate_jiffies = 0;
1969 *num_steps = 1;
1971 else if (icon_steps == 1)
1973 *num_steps = ~0;
1974 *rate_jiffies = ptr->delay;
1976 else if (istep < icon_steps)
1978 struct cursoricon_frame *frame;
1980 *num_steps = icon_steps;
1981 frame = get_icon_frame( ptr, istep );
1982 if (get_icon_steps(ptr) == 1)
1983 *num_steps = ~0;
1984 else
1985 *num_steps = get_icon_steps(ptr);
1986 /* If this specific frame does not have a delay then use the global delay */
1987 if (frame->delay == ~0)
1988 *rate_jiffies = ptr->delay;
1989 else
1990 *rate_jiffies = frame->delay;
1991 release_icon_frame( ptr, frame );
1995 release_user_handle_ptr( ptr );
1997 return ret;
2000 /**********************************************************************
2001 * GetIconInfo (USER32.@)
2003 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
2005 ICONINFOEXW infoW;
2007 infoW.cbSize = sizeof(infoW);
2008 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
2009 iconinfo->fIcon = infoW.fIcon;
2010 iconinfo->xHotspot = infoW.xHotspot;
2011 iconinfo->yHotspot = infoW.yHotspot;
2012 iconinfo->hbmColor = infoW.hbmColor;
2013 iconinfo->hbmMask = infoW.hbmMask;
2014 return TRUE;
2017 /**********************************************************************
2018 * GetIconInfoExA (USER32.@)
2020 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
2022 ICONINFOEXW infoW;
2024 if (info->cbSize != sizeof(*info))
2026 SetLastError( ERROR_INVALID_PARAMETER );
2027 return FALSE;
2029 infoW.cbSize = sizeof(infoW);
2030 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
2031 info->fIcon = infoW.fIcon;
2032 info->xHotspot = infoW.xHotspot;
2033 info->yHotspot = infoW.yHotspot;
2034 info->hbmColor = infoW.hbmColor;
2035 info->hbmMask = infoW.hbmMask;
2036 info->wResID = infoW.wResID;
2037 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
2038 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
2039 return TRUE;
2042 /**********************************************************************
2043 * GetIconInfoExW (USER32.@)
2045 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
2047 struct cursoricon_frame *frame;
2048 struct cursoricon_object *ptr;
2049 HMODULE module;
2050 BOOL ret = TRUE;
2052 if (info->cbSize != sizeof(*info))
2054 SetLastError( ERROR_INVALID_PARAMETER );
2055 return FALSE;
2057 if (!(ptr = get_icon_ptr( icon )))
2059 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2060 return FALSE;
2063 frame = get_icon_frame( ptr, 0 );
2064 if (!frame)
2066 release_user_handle_ptr( ptr );
2067 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2068 return FALSE;
2071 TRACE("%p => %dx%d\n", icon, frame->width, frame->height);
2073 info->fIcon = ptr->is_icon;
2074 info->xHotspot = ptr->hotspot.x;
2075 info->yHotspot = ptr->hotspot.y;
2076 info->hbmColor = copy_bitmap( frame->color );
2077 info->hbmMask = copy_bitmap( frame->mask );
2078 info->wResID = 0;
2079 info->szModName[0] = 0;
2080 info->szResName[0] = 0;
2081 if (ptr->module)
2083 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
2084 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
2086 if (!info->hbmMask || (!info->hbmColor && frame->color))
2088 DeleteObject( info->hbmMask );
2089 DeleteObject( info->hbmColor );
2090 ret = FALSE;
2092 module = ptr->module;
2093 release_icon_frame( ptr, frame );
2094 release_user_handle_ptr( ptr );
2095 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
2096 return ret;
2099 /* copy an icon bitmap, even when it can't be selected into a DC */
2100 /* helper for CreateIconIndirect */
2101 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
2102 HBITMAP src, int width, int height )
2104 HDC hdc = CreateCompatibleDC( 0 );
2106 if (!SelectObject( hdc, src )) /* do it the hard way */
2108 BITMAPINFO *info;
2109 void *bits;
2111 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
2112 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2113 info->bmiHeader.biWidth = width;
2114 info->bmiHeader.biHeight = height;
2115 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
2116 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
2117 info->bmiHeader.biCompression = BI_RGB;
2118 info->bmiHeader.biSizeImage = get_dib_image_size( width, height, info->bmiHeader.biBitCount );
2119 info->bmiHeader.biXPelsPerMeter = 0;
2120 info->bmiHeader.biYPelsPerMeter = 0;
2121 info->bmiHeader.biClrUsed = 0;
2122 info->bmiHeader.biClrImportant = 0;
2123 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
2124 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
2125 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
2126 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
2128 HeapFree( GetProcessHeap(), 0, bits );
2129 HeapFree( GetProcessHeap(), 0, info );
2131 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
2133 DeleteDC( hdc );
2136 /**********************************************************************
2137 * CreateIconIndirect (USER32.@)
2139 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2141 BITMAP bmpXor, bmpAnd;
2142 HICON hObj;
2143 HBITMAP color = 0, mask;
2144 int width, height;
2145 HDC hdc;
2147 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2148 iconinfo->hbmColor, iconinfo->hbmMask,
2149 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2151 if (!iconinfo->hbmMask) return 0;
2153 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2154 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2155 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2156 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2158 if (iconinfo->hbmColor)
2160 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2161 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2162 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2163 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2165 width = bmpXor.bmWidth;
2166 height = bmpXor.bmHeight;
2167 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1 || bmpAnd.bmPlanes * bmpAnd.bmBitsPixel != 1)
2169 color = create_color_bitmap( width, height );
2170 mask = CreateBitmap( width, height, 1, 1, NULL );
2172 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2174 else
2176 width = bmpAnd.bmWidth;
2177 height = bmpAnd.bmHeight;
2178 mask = CreateBitmap( width, height, 1, 1, NULL );
2181 hdc = CreateCompatibleDC( 0 );
2182 SelectObject( hdc, mask );
2183 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
2185 if (color)
2187 SelectObject( hdc, color );
2188 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
2190 else if (iconinfo->hbmColor)
2192 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
2194 else height /= 2;
2196 DeleteDC( hdc );
2198 hObj = alloc_icon_handle( FALSE, 0 );
2199 if (hObj)
2201 struct cursoricon_object *info = get_icon_ptr( hObj );
2202 struct cursoricon_frame *frame;
2204 info->is_icon = iconinfo->fIcon;
2205 frame = get_icon_frame( info, 0 );
2206 frame->delay = ~0;
2207 frame->width = width;
2208 frame->height = height;
2209 frame->color = color;
2210 frame->mask = mask;
2211 frame->alpha = create_alpha_bitmap( iconinfo->hbmColor, NULL, NULL );
2212 release_icon_frame( info, frame );
2213 if (info->is_icon)
2215 info->hotspot.x = width / 2;
2216 info->hotspot.y = height / 2;
2218 else
2220 info->hotspot.x = iconinfo->xHotspot;
2221 info->hotspot.y = iconinfo->yHotspot;
2224 release_user_handle_ptr( info );
2226 return hObj;
2229 /******************************************************************************
2230 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2232 * NOTES
2233 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2235 * PARAMS
2236 * hdc [I] Handle to device context
2237 * x0 [I] X coordinate of upper left corner
2238 * y0 [I] Y coordinate of upper left corner
2239 * hIcon [I] Handle to icon to draw
2240 * cxWidth [I] Width of icon
2241 * cyWidth [I] Height of icon
2242 * istep [I] Index of frame in animated cursor
2243 * hbr [I] Handle to background brush
2244 * flags [I] Icon-drawing flags
2246 * RETURNS
2247 * Success: TRUE
2248 * Failure: FALSE
2250 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2251 INT cxWidth, INT cyWidth, UINT istep,
2252 HBRUSH hbr, UINT flags )
2254 struct cursoricon_frame *frame;
2255 struct cursoricon_object *ptr;
2256 HDC hdc_dest, hMemDC;
2257 BOOL result = FALSE, DoOffscreen;
2258 HBITMAP hB_off = 0;
2259 COLORREF oldFg, oldBg;
2260 INT x, y, nStretchMode;
2262 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2263 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2265 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2266 if (istep >= get_icon_steps( ptr ))
2268 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
2269 release_user_handle_ptr( ptr );
2270 return FALSE;
2272 if (!(frame = get_icon_frame( ptr, istep )))
2274 FIXME_(icon)("Error retrieving icon frame %d\n", istep);
2275 release_user_handle_ptr( ptr );
2276 return FALSE;
2278 if (!(hMemDC = CreateCompatibleDC( hdc )))
2280 release_icon_frame( ptr, frame );
2281 release_user_handle_ptr( ptr );
2282 return FALSE;
2285 if (flags & DI_NOMIRROR)
2286 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2288 /* Calculate the size of the destination image. */
2289 if (cxWidth == 0)
2291 if (flags & DI_DEFAULTSIZE)
2292 cxWidth = GetSystemMetrics (SM_CXICON);
2293 else
2294 cxWidth = frame->width;
2296 if (cyWidth == 0)
2298 if (flags & DI_DEFAULTSIZE)
2299 cyWidth = GetSystemMetrics (SM_CYICON);
2300 else
2301 cyWidth = frame->height;
2304 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2306 if (DoOffscreen) {
2307 RECT r;
2309 SetRect(&r, 0, 0, cxWidth, cxWidth);
2311 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2312 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2314 DeleteDC( hdc_dest );
2315 goto failed;
2317 SelectObject(hdc_dest, hB_off);
2318 FillRect(hdc_dest, &r, hbr);
2319 x = y = 0;
2321 else
2323 hdc_dest = hdc;
2324 x = x0;
2325 y = y0;
2328 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2330 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2331 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2333 if (frame->alpha && (flags & DI_IMAGE))
2335 BOOL alpha_blend = TRUE;
2337 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2339 BITMAP bm;
2340 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2341 alpha_blend = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel > 8;
2343 if (alpha_blend)
2345 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2346 SelectObject( hMemDC, frame->alpha );
2347 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2348 0, 0, frame->width, frame->height,
2349 pixelblend )) goto done;
2353 if (flags & DI_MASK)
2355 DWORD rop = (flags & DI_IMAGE) ? SRCAND : SRCCOPY;
2356 SelectObject( hMemDC, frame->mask );
2357 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2358 hMemDC, 0, 0, frame->width, frame->height, rop );
2361 if (flags & DI_IMAGE)
2363 if (frame->color)
2365 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2366 SelectObject( hMemDC, frame->color );
2367 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2368 hMemDC, 0, 0, frame->width, frame->height, rop );
2370 else
2372 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2373 SelectObject( hMemDC, frame->mask );
2374 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2375 hMemDC, 0, frame->height, frame->width,
2376 frame->height, rop );
2380 done:
2381 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2383 SetTextColor( hdc, oldFg );
2384 SetBkColor( hdc, oldBg );
2385 SetStretchBltMode (hdc, nStretchMode);
2386 result = TRUE;
2387 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2388 if (hB_off) DeleteObject(hB_off);
2389 failed:
2390 DeleteDC( hMemDC );
2391 release_icon_frame( ptr, frame );
2392 release_user_handle_ptr( ptr );
2393 return result;
2396 /***********************************************************************
2397 * DIB_FixColorsToLoadflags
2399 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2400 * are in loadflags
2402 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2404 int colors;
2405 COLORREF c_W, c_S, c_F, c_L, c_C;
2406 int incr,i;
2407 RGBQUAD *ptr;
2408 int bitmap_type;
2409 LONG width;
2410 LONG height;
2411 WORD bpp;
2412 DWORD compr;
2414 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2416 WARN_(resource)("Invalid bitmap\n");
2417 return;
2420 if (bpp > 8) return;
2422 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2424 incr = 3;
2425 colors = 1 << bpp;
2427 else
2429 incr = 4;
2430 colors = bmi->bmiHeader.biClrUsed;
2431 if (colors > 256) colors = 256;
2432 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2435 c_W = GetSysColor(COLOR_WINDOW);
2436 c_S = GetSysColor(COLOR_3DSHADOW);
2437 c_F = GetSysColor(COLOR_3DFACE);
2438 c_L = GetSysColor(COLOR_3DLIGHT);
2440 if (loadflags & LR_LOADTRANSPARENT) {
2441 switch (bpp) {
2442 case 1: pix = pix >> 7; break;
2443 case 4: pix = pix >> 4; break;
2444 case 8: break;
2445 default:
2446 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2447 return;
2449 if (pix >= colors) {
2450 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2451 return;
2453 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2454 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2455 ptr->rgbBlue = GetBValue(c_W);
2456 ptr->rgbGreen = GetGValue(c_W);
2457 ptr->rgbRed = GetRValue(c_W);
2459 if (loadflags & LR_LOADMAP3DCOLORS)
2460 for (i=0; i<colors; i++) {
2461 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2462 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2463 if (c_C == RGB(128, 128, 128)) {
2464 ptr->rgbRed = GetRValue(c_S);
2465 ptr->rgbGreen = GetGValue(c_S);
2466 ptr->rgbBlue = GetBValue(c_S);
2467 } else if (c_C == RGB(192, 192, 192)) {
2468 ptr->rgbRed = GetRValue(c_F);
2469 ptr->rgbGreen = GetGValue(c_F);
2470 ptr->rgbBlue = GetBValue(c_F);
2471 } else if (c_C == RGB(223, 223, 223)) {
2472 ptr->rgbRed = GetRValue(c_L);
2473 ptr->rgbGreen = GetGValue(c_L);
2474 ptr->rgbBlue = GetBValue(c_L);
2480 /**********************************************************************
2481 * BITMAP_Load
2483 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2484 INT desiredx, INT desiredy, UINT loadflags )
2486 HBITMAP hbitmap = 0, orig_bm;
2487 HRSRC hRsrc;
2488 HGLOBAL handle;
2489 const char *ptr = NULL;
2490 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2491 int size;
2492 BYTE pix;
2493 char *bits;
2494 LONG width, height, new_width, new_height;
2495 WORD bpp_dummy;
2496 DWORD compr_dummy, offbits = 0;
2497 INT bm_type;
2498 HDC screen_mem_dc = NULL;
2500 if (!(loadflags & LR_LOADFROMFILE))
2502 if (!instance)
2504 /* OEM bitmap: try to load the resource from user32.dll */
2505 instance = user32_module;
2508 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2509 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2511 if ((info = LockResource( handle )) == NULL) return 0;
2513 else
2515 BITMAPFILEHEADER * bmfh;
2517 if (!(ptr = map_fileW( name, NULL ))) return 0;
2518 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2519 bmfh = (BITMAPFILEHEADER *)ptr;
2520 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2522 WARN("Invalid/unsupported bitmap format!\n");
2523 goto end;
2525 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2528 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2529 &bpp_dummy, &compr_dummy);
2530 if (bm_type == -1)
2532 WARN("Invalid bitmap format!\n");
2533 goto end;
2536 size = bitmap_info_size(info, DIB_RGB_COLORS);
2537 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2538 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2540 if (!fix_info || !scaled_info) goto end;
2541 memcpy(fix_info, info, size);
2543 pix = *((LPBYTE)info + size);
2544 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2546 memcpy(scaled_info, fix_info, size);
2548 if(desiredx != 0)
2549 new_width = desiredx;
2550 else
2551 new_width = width;
2553 if(desiredy != 0)
2554 new_height = height > 0 ? desiredy : -desiredy;
2555 else
2556 new_height = height;
2558 if(bm_type == 0)
2560 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2561 core->bcWidth = new_width;
2562 core->bcHeight = new_height;
2564 else
2566 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2567 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2568 WARN("Broken BitmapInfoHeader!\n");
2569 goto end;
2572 scaled_info->bmiHeader.biWidth = new_width;
2573 scaled_info->bmiHeader.biHeight = new_height;
2576 if (new_height < 0) new_height = -new_height;
2578 if (!(screen_mem_dc = CreateCompatibleDC( 0 ))) goto end;
2580 bits = (char *)info + (offbits ? offbits : size);
2582 if (loadflags & LR_CREATEDIBSECTION)
2584 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2585 hbitmap = CreateDIBSection(0, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2587 else
2589 if (is_dib_monochrome(fix_info))
2590 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2591 else
2592 hbitmap = create_color_bitmap(new_width, new_height);
2595 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2596 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2597 SelectObject(screen_mem_dc, orig_bm);
2599 end:
2600 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2601 HeapFree(GetProcessHeap(), 0, scaled_info);
2602 HeapFree(GetProcessHeap(), 0, fix_info);
2603 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2605 return hbitmap;
2608 /**********************************************************************
2609 * LoadImageA (USER32.@)
2611 * See LoadImageW.
2613 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2614 INT desiredx, INT desiredy, UINT loadflags)
2616 HANDLE res;
2617 LPWSTR u_name;
2619 if (IS_INTRESOURCE(name))
2620 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2622 __TRY {
2623 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2624 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2625 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2627 __EXCEPT_PAGE_FAULT {
2628 SetLastError( ERROR_INVALID_PARAMETER );
2629 return 0;
2631 __ENDTRY
2632 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2633 HeapFree(GetProcessHeap(), 0, u_name);
2634 return res;
2638 /******************************************************************************
2639 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2641 * PARAMS
2642 * hinst [I] Handle of instance that contains image
2643 * name [I] Name of image
2644 * type [I] Type of image
2645 * desiredx [I] Desired width
2646 * desiredy [I] Desired height
2647 * loadflags [I] Load flags
2649 * RETURNS
2650 * Success: Handle to newly loaded image
2651 * Failure: NULL
2653 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2655 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2656 INT desiredx, INT desiredy, UINT loadflags )
2658 int depth;
2660 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2661 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2663 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2664 switch (type) {
2665 case IMAGE_BITMAP:
2666 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2668 case IMAGE_ICON:
2669 case IMAGE_CURSOR:
2670 depth = 1;
2671 if (!(loadflags & LR_MONOCHROME)) depth = get_display_bpp();
2672 return CURSORICON_Load(hinst, name, desiredx, desiredy, depth, (type == IMAGE_CURSOR), loadflags);
2674 return 0;
2677 /******************************************************************************
2678 * CopyImage (USER32.@) Creates new image and copies attributes to it
2680 * PARAMS
2681 * hnd [I] Handle to image to copy
2682 * type [I] Type of image to copy
2683 * desiredx [I] Desired width of new image
2684 * desiredy [I] Desired height of new image
2685 * flags [I] Copy flags
2687 * RETURNS
2688 * Success: Handle to newly created image
2689 * Failure: NULL
2691 * BUGS
2692 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2693 * all other versions (95/2000/XP have been tested) ignore it.
2695 * NOTES
2696 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2697 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2698 * the copy will have the same depth as the screen.
2699 * The content of the image will only be copied if the bit depth of the
2700 * original image is compatible with the bit depth of the screen, or
2701 * if the source is a DIB section.
2702 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2704 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2705 INT desiredy, UINT flags )
2707 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2708 hnd, type, desiredx, desiredy, flags);
2710 switch (type)
2712 case IMAGE_BITMAP:
2714 HBITMAP res = NULL;
2715 DIBSECTION ds;
2716 int objSize;
2717 BITMAPINFO * bi;
2719 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2720 if (!objSize) return 0;
2721 if ((desiredx < 0) || (desiredy < 0)) return 0;
2723 if (flags & LR_COPYFROMRESOURCE)
2725 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2728 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2729 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2731 /* Allocate memory for a BITMAPINFOHEADER structure and a
2732 color table. The maximum number of colors in a color table
2733 is 256 which corresponds to a bitmap with depth 8.
2734 Bitmaps with higher depths don't have color tables. */
2735 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2736 if (!bi) return 0;
2738 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2739 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2740 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2741 bi->bmiHeader.biCompression = BI_RGB;
2743 if (flags & LR_CREATEDIBSECTION)
2745 /* Create a DIB section. LR_MONOCHROME is ignored */
2746 void * bits;
2747 HDC dc = CreateCompatibleDC(NULL);
2749 if (objSize == sizeof(DIBSECTION))
2751 /* The source bitmap is a DIB.
2752 Get its attributes to create an exact copy */
2753 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2756 bi->bmiHeader.biWidth = desiredx;
2757 bi->bmiHeader.biHeight = desiredy;
2759 /* Get the color table or the color masks */
2760 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2762 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2763 DeleteDC(dc);
2765 else
2767 /* Create a device-dependent bitmap */
2769 BOOL monochrome = (flags & LR_MONOCHROME);
2771 if (objSize == sizeof(DIBSECTION))
2773 /* The source bitmap is a DIB section.
2774 Get its attributes */
2775 HDC dc = CreateCompatibleDC(NULL);
2776 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2777 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2778 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2779 DeleteDC(dc);
2781 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2783 /* Look if the colors of the DIB are black and white */
2785 monochrome =
2786 (bi->bmiColors[0].rgbRed == 0xff
2787 && bi->bmiColors[0].rgbGreen == 0xff
2788 && bi->bmiColors[0].rgbBlue == 0xff
2789 && bi->bmiColors[0].rgbReserved == 0
2790 && bi->bmiColors[1].rgbRed == 0
2791 && bi->bmiColors[1].rgbGreen == 0
2792 && bi->bmiColors[1].rgbBlue == 0
2793 && bi->bmiColors[1].rgbReserved == 0)
2795 (bi->bmiColors[0].rgbRed == 0
2796 && bi->bmiColors[0].rgbGreen == 0
2797 && bi->bmiColors[0].rgbBlue == 0
2798 && bi->bmiColors[0].rgbReserved == 0
2799 && bi->bmiColors[1].rgbRed == 0xff
2800 && bi->bmiColors[1].rgbGreen == 0xff
2801 && bi->bmiColors[1].rgbBlue == 0xff
2802 && bi->bmiColors[1].rgbReserved == 0);
2805 else if (!monochrome)
2807 monochrome = ds.dsBm.bmBitsPixel == 1;
2810 if (monochrome)
2811 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2812 else
2813 res = create_color_bitmap(desiredx, desiredy);
2816 if (res)
2818 /* Only copy the bitmap if it's a DIB section or if it's
2819 compatible to the screen */
2820 if (objSize == sizeof(DIBSECTION) ||
2821 ds.dsBm.bmBitsPixel == 1 ||
2822 ds.dsBm.bmBitsPixel == get_display_bpp())
2824 /* The source bitmap may already be selected in a device context,
2825 use GetDIBits/StretchDIBits and not StretchBlt */
2827 HDC dc;
2828 void * bits;
2830 dc = CreateCompatibleDC(NULL);
2832 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2833 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2834 bi->bmiHeader.biSizeImage = 0;
2835 bi->bmiHeader.biClrUsed = 0;
2836 bi->bmiHeader.biClrImportant = 0;
2838 /* Fill in biSizeImage */
2839 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2840 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2842 if (bits)
2844 HBITMAP oldBmp;
2846 /* Get the image bits of the source bitmap */
2847 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2849 /* Copy it to the destination bitmap */
2850 oldBmp = SelectObject(dc, res);
2851 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2852 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2853 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2854 SelectObject(dc, oldBmp);
2856 HeapFree(GetProcessHeap(), 0, bits);
2859 DeleteDC(dc);
2862 if (flags & LR_COPYDELETEORG)
2864 DeleteObject(hnd);
2867 HeapFree(GetProcessHeap(), 0, bi);
2868 return res;
2870 case IMAGE_ICON:
2871 case IMAGE_CURSOR:
2873 struct cursoricon_object *icon;
2874 HICON res = 0;
2875 int depth = (flags & LR_MONOCHROME) ? 1 : get_display_bpp();
2877 if (flags & LR_DEFAULTSIZE)
2879 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2880 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2883 if (!(icon = get_icon_ptr( hnd ))) return 0;
2885 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2886 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2887 !icon->is_icon, flags );
2888 else
2889 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2890 release_user_handle_ptr( icon );
2892 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2893 return res;
2896 return 0;
2900 /******************************************************************************
2901 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2903 * RETURNS
2904 * Success: Handle to specified bitmap
2905 * Failure: NULL
2907 HBITMAP WINAPI LoadBitmapW(
2908 HINSTANCE instance, /* [in] Handle to application instance */
2909 LPCWSTR name) /* [in] Address of bitmap resource name */
2911 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2914 /**********************************************************************
2915 * LoadBitmapA (USER32.@)
2917 * See LoadBitmapW.
2919 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2921 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );