opengl32: Fix some style issues.
[wine.git] / dlls / user32 / cursoricon.c
blob53089c275d95deb9d0334c3c4ad68a4fa094e8ee
1 /*
2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
6 * 1997 Alex Korobka
7 * 1998 Turchanov Sergey
8 * 2007 Henri Verbeet
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <stdlib.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "winerror.h"
37 #include "winnls.h"
38 #include "wine/exception.h"
39 #include "wine/server.h"
40 #include "controls.h"
41 #include "win.h"
42 #include "user_private.h"
43 #include "wine/list.h"
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
48 WINE_DECLARE_DEBUG_CHANNEL(icon);
49 WINE_DECLARE_DEBUG_CHANNEL(resource);
51 #include "pshpack1.h"
53 typedef struct {
54 BYTE bWidth;
55 BYTE bHeight;
56 BYTE bColorCount;
57 BYTE bReserved;
58 WORD xHotspot;
59 WORD yHotspot;
60 DWORD dwDIBSize;
61 DWORD dwDIBOffset;
62 } CURSORICONFILEDIRENTRY;
64 typedef struct
66 WORD idReserved;
67 WORD idType;
68 WORD idCount;
69 CURSORICONFILEDIRENTRY idEntries[1];
70 } CURSORICONFILEDIR;
72 #include "poppack.h"
74 static HDC screen_dc;
76 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
78 static struct list icon_cache = LIST_INIT( icon_cache );
80 /**********************************************************************
81 * User objects management
84 struct cursoricon_frame
86 UINT width; /* frame-specific width */
87 UINT height; /* frame-specific height */
88 UINT delay; /* frame-specific delay between this frame and the next (in jiffies) */
89 HBITMAP color; /* color bitmap */
90 HBITMAP alpha; /* pre-multiplied alpha bitmap for 32-bpp icons */
91 HBITMAP mask; /* mask bitmap (followed by color for 1-bpp icons) */
94 struct cursoricon_object
96 struct user_object obj; /* object header */
97 struct list entry; /* entry in shared icons list */
98 ULONG_PTR param; /* opaque param used by 16-bit code */
99 HMODULE module; /* module for icons loaded from resources */
100 LPWSTR resname; /* resource name for icons loaded from resources */
101 HRSRC rsrc; /* resource for shared icons */
102 BOOL is_icon; /* whether icon or cursor */
103 BOOL is_ani; /* whether this object is a static cursor or an animated cursor */
104 UINT delay; /* delay between this frame and the next (in jiffies) */
105 POINT hotspot;
108 struct static_cursoricon_object
110 struct cursoricon_object shared;
111 struct cursoricon_frame frame; /* frame-specific icon data */
114 struct animated_cursoricon_object
116 struct cursoricon_object shared;
117 UINT num_frames; /* number of frames in the icon/cursor */
118 UINT num_steps; /* number of sequence steps in the icon/cursor */
119 HICON frames[1]; /* list of animated cursor frames */
122 static HICON alloc_icon_handle( BOOL is_ani, UINT num_steps )
124 struct cursoricon_object *obj;
125 int icon_size;
127 if (is_ani)
128 icon_size = FIELD_OFFSET( struct animated_cursoricon_object, frames[num_steps] );
129 else
130 icon_size = sizeof( struct static_cursoricon_object );
131 obj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, icon_size );
133 obj->delay = 0;
134 obj->is_ani = is_ani;
135 if (is_ani)
137 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
139 ani_icon_data->num_steps = num_steps;
140 ani_icon_data->num_frames = num_steps; /* changed later for some animated cursors */
142 return alloc_user_handle( &obj->obj, USER_ICON );
145 static struct cursoricon_object *get_icon_ptr( HICON handle )
147 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
148 if (obj == OBJ_OTHER_PROCESS)
150 WARN( "icon handle %p from other process\n", handle );
151 obj = NULL;
153 return obj;
156 static void release_icon_ptr( HICON handle, struct cursoricon_object *ptr )
158 release_user_handle_ptr( ptr );
161 static struct cursoricon_frame *get_icon_frame( struct cursoricon_object *obj, int istep )
163 struct static_cursoricon_object *req_frame;
165 if (obj->is_ani)
167 struct animated_cursoricon_object *ani_icon_data;
168 struct cursoricon_object *frameobj;
170 ani_icon_data = (struct animated_cursoricon_object *) obj;
171 if (!(frameobj = get_icon_ptr( ani_icon_data->frames[istep] )))
172 return 0;
173 req_frame = (struct static_cursoricon_object *) frameobj;
175 else
176 req_frame = (struct static_cursoricon_object *) obj;
178 return &req_frame->frame;
181 static void release_icon_frame( struct cursoricon_object *obj, int istep, struct cursoricon_frame *frame )
183 if (obj->is_ani)
185 struct animated_cursoricon_object *ani_icon_data;
186 struct cursoricon_object *frameobj;
188 ani_icon_data = (struct animated_cursoricon_object *) obj;
189 frameobj = (struct cursoricon_object *) (((char *)frame) - FIELD_OFFSET(struct static_cursoricon_object, frame));
190 release_icon_ptr( ani_icon_data->frames[istep], frameobj );
194 static UINT get_icon_steps( struct cursoricon_object *obj )
196 if (obj->is_ani)
198 struct animated_cursoricon_object *ani_icon_data;
200 ani_icon_data = (struct animated_cursoricon_object *) obj;
201 return ani_icon_data->num_steps;
203 return 1;
206 static BOOL free_icon_handle( HICON handle )
208 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
210 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
211 else if (obj)
213 ULONG_PTR param = obj->param;
214 UINT i;
216 assert( !obj->rsrc ); /* shared icons can't be freed */
218 if (!obj->is_ani)
220 struct cursoricon_frame *frame = get_icon_frame( obj, 0 );
222 if (frame->alpha) DeleteObject( frame->alpha );
223 if (frame->color) DeleteObject( frame->color );
224 DeleteObject( frame->mask );
225 release_icon_frame( obj, 0, frame );
227 else
229 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
231 for (i=0; i<ani_icon_data->num_steps; i++)
233 HICON hFrame = ani_icon_data->frames[i];
235 if (hFrame)
237 UINT j;
239 free_icon_handle( ani_icon_data->frames[i] );
240 for (j=0; j<ani_icon_data->num_steps; j++)
242 if (ani_icon_data->frames[j] == hFrame)
243 ani_icon_data->frames[j] = 0;
248 if (!IS_INTRESOURCE( obj->resname )) HeapFree( GetProcessHeap(), 0, obj->resname );
249 HeapFree( GetProcessHeap(), 0, obj );
250 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
251 USER_Driver->pDestroyCursorIcon( handle );
252 return TRUE;
254 return FALSE;
257 ULONG_PTR get_icon_param( HICON handle )
259 ULONG_PTR ret = 0;
260 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
262 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
263 else if (obj)
265 ret = obj->param;
266 release_user_handle_ptr( obj );
268 return ret;
271 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
273 ULONG_PTR ret = 0;
274 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
276 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
277 else if (obj)
279 ret = obj->param;
280 obj->param = param;
281 release_user_handle_ptr( obj );
283 return ret;
287 /***********************************************************************
288 * map_fileW
290 * Helper function to map a file to memory:
291 * name - file name
292 * [RETURN] ptr - pointer to mapped file
293 * [RETURN] filesize - pointer size of file to be stored if not NULL
295 static const void *map_fileW( LPCWSTR name, LPDWORD filesize )
297 HANDLE hFile, hMapping;
298 LPVOID ptr = NULL;
300 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
301 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
302 if (hFile != INVALID_HANDLE_VALUE)
304 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
305 if (hMapping)
307 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
308 CloseHandle( hMapping );
309 if (filesize)
310 *filesize = GetFileSize( hFile, NULL );
312 CloseHandle( hFile );
314 return ptr;
318 /***********************************************************************
319 * get_dib_image_size
321 * Return the size of a DIB bitmap in bytes.
323 static int get_dib_image_size( int width, int height, int depth )
325 return (((width * depth + 31) / 8) & ~3) * abs( height );
329 /***********************************************************************
330 * bitmap_info_size
332 * Return the size of the bitmap info structure including color table.
334 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
336 unsigned int colors, size, masks = 0;
338 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
340 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
341 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
342 return sizeof(BITMAPCOREHEADER) + colors *
343 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
345 else /* assume BITMAPINFOHEADER */
347 colors = info->bmiHeader.biClrUsed;
348 if (colors > 256) /* buffer overflow otherwise */
349 colors = 256;
350 if (!colors && (info->bmiHeader.biBitCount <= 8))
351 colors = 1 << info->bmiHeader.biBitCount;
352 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
353 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
354 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
359 /***********************************************************************
360 * copy_bitmap
362 * Helper function to duplicate a bitmap.
364 static HBITMAP copy_bitmap( HBITMAP bitmap )
366 HDC src, dst = 0;
367 HBITMAP new_bitmap = 0;
368 BITMAP bmp;
370 if (!bitmap) return 0;
371 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
373 if ((src = CreateCompatibleDC( 0 )) && (dst = CreateCompatibleDC( 0 )))
375 SelectObject( src, bitmap );
376 if ((new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight )))
378 SelectObject( dst, new_bitmap );
379 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
382 DeleteDC( dst );
383 DeleteDC( src );
384 return new_bitmap;
388 /***********************************************************************
389 * is_dib_monochrome
391 * Returns whether a DIB can be converted to a monochrome DDB.
393 * A DIB can be converted if its color table contains only black and
394 * white. Black must be the first color in the color table.
396 * Note : If the first color in the color table is white followed by
397 * black, we can't convert it to a monochrome DDB with
398 * SetDIBits, because black and white would be inverted.
400 static BOOL is_dib_monochrome( const BITMAPINFO* info )
402 if (info->bmiHeader.biBitCount != 1) return FALSE;
404 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
406 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
408 /* Check if the first color is black */
409 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
411 rgb++;
413 /* Check if the second color is white */
414 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
415 && (rgb->rgbtBlue == 0xff));
417 else return FALSE;
419 else /* assume BITMAPINFOHEADER */
421 const RGBQUAD *rgb = info->bmiColors;
423 /* Check if the first color is black */
424 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
425 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
427 rgb++;
429 /* Check if the second color is white */
430 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
431 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
433 else return FALSE;
437 /***********************************************************************
438 * DIB_GetBitmapInfo
440 * Get the info from a bitmap header.
441 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
443 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
444 LONG *height, WORD *bpp, DWORD *compr )
446 if (header->biSize == sizeof(BITMAPCOREHEADER))
448 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
449 *width = core->bcWidth;
450 *height = core->bcHeight;
451 *bpp = core->bcBitCount;
452 *compr = 0;
453 return 0;
455 else if (header->biSize == sizeof(BITMAPINFOHEADER) ||
456 header->biSize == sizeof(BITMAPV4HEADER) ||
457 header->biSize == sizeof(BITMAPV5HEADER))
459 *width = header->biWidth;
460 *height = header->biHeight;
461 *bpp = header->biBitCount;
462 *compr = header->biCompression;
463 return 1;
465 WARN("unknown/wrong size (%u) for header\n", header->biSize);
466 return -1;
469 /**********************************************************************
470 * get_icon_size
472 BOOL get_icon_size( HICON handle, SIZE *size )
474 struct cursoricon_object *info;
475 struct cursoricon_frame *frame;
477 if (!(info = get_icon_ptr( handle ))) return FALSE;
478 frame = get_icon_frame( info, 0 );
479 size->cx = frame->width;
480 size->cy = frame->height;
481 release_icon_frame( info, 0, frame);
482 release_icon_ptr( handle, info );
483 return TRUE;
487 * The following macro functions account for the irregularities of
488 * accessing cursor and icon resources in files and resource entries.
490 typedef BOOL (*fnGetCIEntry)( LPCVOID dir, DWORD size, int n,
491 int *width, int *height, int *bits );
493 /**********************************************************************
494 * CURSORICON_FindBestIcon
496 * Find the icon closest to the requested size and bit depth.
498 static int CURSORICON_FindBestIcon( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
499 int width, int height, int depth, UINT loadflags )
501 int i, cx, cy, bits, bestEntry = -1;
502 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
503 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
505 /* Find Best Fit */
506 iTotalDiff = 0xFFFFFFFF;
507 iColorDiff = 0xFFFFFFFF;
509 if (loadflags & LR_DEFAULTSIZE)
511 if (!width) width = GetSystemMetrics( SM_CXICON );
512 if (!height) height = GetSystemMetrics( SM_CYICON );
514 else if (!width && !height)
516 /* use the size of the first entry */
517 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
518 iTotalDiff = 0;
521 for ( i = 0; iTotalDiff && get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
523 iTempXDiff = abs(width - cx);
524 iTempYDiff = abs(height - cy);
526 if(iTotalDiff > (iTempXDiff + iTempYDiff))
528 iXDiff = iTempXDiff;
529 iYDiff = iTempYDiff;
530 iTotalDiff = iXDiff + iYDiff;
534 /* Find Best Colors for Best Fit */
535 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
537 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
539 iTempColorDiff = abs(depth - bits);
540 if(iColorDiff > iTempColorDiff)
542 bestEntry = i;
543 iColorDiff = iTempColorDiff;
548 return bestEntry;
551 static BOOL CURSORICON_GetResIconEntry( LPCVOID dir, DWORD size, int n,
552 int *width, int *height, int *bits )
554 const CURSORICONDIR *resdir = dir;
555 const ICONRESDIR *icon;
557 if ( resdir->idCount <= n )
558 return FALSE;
559 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
560 return FALSE;
561 icon = &resdir->idEntries[n].ResInfo.icon;
562 *width = icon->bWidth;
563 *height = icon->bHeight;
564 *bits = resdir->idEntries[n].wBitCount;
565 return TRUE;
568 /**********************************************************************
569 * CURSORICON_FindBestCursor
571 * Find the cursor closest to the requested size.
573 * FIXME: parameter 'color' ignored.
575 static int CURSORICON_FindBestCursor( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
576 int width, int height, int depth, UINT loadflags )
578 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
580 if (loadflags & LR_DEFAULTSIZE)
582 if (!width) width = GetSystemMetrics( SM_CXCURSOR );
583 if (!height) height = GetSystemMetrics( SM_CYCURSOR );
585 else if (!width && !height)
587 /* use the first entry */
588 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
589 return 0;
592 /* Double height to account for AND and XOR masks */
594 height *= 2;
596 /* First find the largest one smaller than or equal to the requested size*/
598 maxwidth = maxheight = 0;
599 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
601 if ((cx <= width) && (cy <= height) &&
602 (cx > maxwidth) && (cy > maxheight))
604 bestEntry = i;
605 maxwidth = cx;
606 maxheight = cy;
609 if (bestEntry != -1) return bestEntry;
611 /* Now find the smallest one larger than the requested size */
613 maxwidth = maxheight = 255;
614 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
616 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
618 bestEntry = i;
619 maxwidth = cx;
620 maxheight = cy;
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 return TRUE;
644 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir, DWORD size,
645 int width, int height, int depth,
646 UINT loadflags )
648 int n;
650 n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetResIconEntry,
651 width, height, depth, loadflags );
652 if ( n < 0 )
653 return NULL;
654 return &dir->idEntries[n];
657 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir, DWORD size,
658 int width, int height, int depth,
659 UINT loadflags )
661 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetResCursorEntry,
662 width, height, depth, loadflags );
663 if ( n < 0 )
664 return NULL;
665 return &dir->idEntries[n];
668 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, DWORD size, int n,
669 int *width, int *height, int *bits )
671 const CURSORICONFILEDIR *filedir = dir;
672 const CURSORICONFILEDIRENTRY *entry;
673 const BITMAPINFOHEADER *info;
675 if ( filedir->idCount <= n )
676 return FALSE;
677 if ((const char *)&filedir->idEntries[n + 1] - (const char *)dir > size)
678 return FALSE;
679 entry = &filedir->idEntries[n];
680 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
681 if ((const char *)(info + 1) - (const char *)dir > size) return FALSE;
682 *width = entry->bWidth;
683 *height = entry->bHeight;
684 *bits = info->biBitCount;
685 return TRUE;
688 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir, DWORD size,
689 int width, int height, int depth,
690 UINT loadflags )
692 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetFileEntry,
693 width, height, depth, loadflags );
694 if ( n < 0 )
695 return NULL;
696 return &dir->idEntries[n];
699 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir, DWORD size,
700 int width, int height, int depth,
701 UINT loadflags )
703 int n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetFileEntry,
704 width, height, depth, loadflags );
705 if ( n < 0 )
706 return NULL;
707 return &dir->idEntries[n];
710 /***********************************************************************
711 * bmi_has_alpha
713 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
715 int i;
716 BOOL has_alpha = FALSE;
717 const unsigned char *ptr = bits;
719 if (info->bmiHeader.biBitCount != 32) return FALSE;
720 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
721 if ((has_alpha = (ptr[3] != 0))) break;
722 return has_alpha;
725 /***********************************************************************
726 * create_alpha_bitmap
728 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
730 static HBITMAP create_alpha_bitmap( HBITMAP color, HBITMAP mask,
731 const BITMAPINFO *src_info, const void *color_bits )
733 HBITMAP alpha = 0;
734 BITMAPINFO *info = NULL;
735 BITMAP bm;
736 HDC hdc;
737 void *bits;
738 unsigned char *ptr;
739 int i;
741 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
742 if (bm.bmBitsPixel != 32) return 0;
744 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
745 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
746 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
747 info->bmiHeader.biWidth = bm.bmWidth;
748 info->bmiHeader.biHeight = -bm.bmHeight;
749 info->bmiHeader.biPlanes = 1;
750 info->bmiHeader.biBitCount = 32;
751 info->bmiHeader.biCompression = BI_RGB;
752 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
753 info->bmiHeader.biXPelsPerMeter = 0;
754 info->bmiHeader.biYPelsPerMeter = 0;
755 info->bmiHeader.biClrUsed = 0;
756 info->bmiHeader.biClrImportant = 0;
757 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
759 if (src_info)
761 SelectObject( hdc, alpha );
762 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
763 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
764 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
767 else
769 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
770 if (!bmi_has_alpha( info, bits ))
772 DeleteObject( alpha );
773 alpha = 0;
774 goto done;
778 /* pre-multiply by alpha */
779 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
781 unsigned int alpha = ptr[3];
782 ptr[0] = ptr[0] * alpha / 255;
783 ptr[1] = ptr[1] * alpha / 255;
784 ptr[2] = ptr[2] * alpha / 255;
787 done:
788 DeleteDC( hdc );
789 HeapFree( GetProcessHeap(), 0, info );
790 return alpha;
794 /***********************************************************************
795 * create_icon_from_bmi
797 * Create an icon from its BITMAPINFO.
799 static HICON create_icon_from_bmi( const BITMAPINFO *bmi, DWORD maxsize, HMODULE module, LPCWSTR resname,
800 HRSRC rsrc, POINT hotspot, BOOL bIcon, INT width, INT height,
801 UINT cFlag )
803 DWORD size, color_size, mask_size;
804 HBITMAP color = 0, mask = 0, alpha = 0;
805 const void *color_bits, *mask_bits;
806 BITMAPINFO *bmi_copy;
807 BOOL ret = FALSE;
808 BOOL do_stretch;
809 HICON hObj = 0;
810 HDC hdc = 0;
812 /* Check bitmap header */
814 if (maxsize < sizeof(BITMAPCOREHEADER))
816 WARN( "invalid size %u\n", maxsize );
817 return 0;
819 if (maxsize < bmi->bmiHeader.biSize)
821 WARN( "invalid header size %u\n", bmi->bmiHeader.biSize );
822 return 0;
824 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
825 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
826 (bmi->bmiHeader.biCompression != BI_RGB &&
827 bmi->bmiHeader.biCompression != BI_BITFIELDS)) )
829 WARN( "invalid bitmap header %u\n", bmi->bmiHeader.biSize );
830 return 0;
833 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
834 color_size = get_dib_image_size( bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight / 2,
835 bmi->bmiHeader.biBitCount );
836 mask_size = get_dib_image_size( bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight / 2, 1 );
837 if (size > maxsize || color_size > maxsize - size)
839 WARN( "truncated file %u < %u+%u+%u\n", maxsize, size, color_size, mask_size );
840 return 0;
842 if (mask_size > maxsize - size - color_size) mask_size = 0; /* no mask */
844 if (cFlag & LR_DEFAULTSIZE)
846 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
847 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
849 else
851 if (!width) width = bmi->bmiHeader.biWidth;
852 if (!height) height = bmi->bmiHeader.biHeight/2;
854 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
855 (bmi->bmiHeader.biWidth != width);
857 /* Scale the hotspot */
858 if (bIcon)
860 hotspot.x = width / 2;
861 hotspot.y = height / 2;
863 else if (do_stretch)
865 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
866 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
869 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
870 if (!screen_dc) return 0;
872 if (!(bmi_copy = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
873 return 0;
874 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
876 memcpy( bmi_copy, bmi, size );
877 bmi_copy->bmiHeader.biHeight /= 2;
879 color_bits = (const char*)bmi + size;
880 mask_bits = (const char*)color_bits + color_size;
882 alpha = 0;
883 if (is_dib_monochrome( bmi ))
885 if (!(mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
886 color = 0;
888 /* copy color data into second half of mask bitmap */
889 SelectObject( hdc, mask );
890 StretchDIBits( hdc, 0, height, width, height,
891 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
892 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
894 else
896 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
897 if (!(color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
898 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
900 DeleteObject( mask );
901 goto done;
903 SelectObject( hdc, color );
904 StretchDIBits( hdc, 0, 0, width, height,
905 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
906 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
908 if (bmi_has_alpha( bmi_copy, color_bits ))
909 alpha = create_alpha_bitmap( color, mask, bmi_copy, color_bits );
911 /* convert info to monochrome to copy the mask */
912 bmi_copy->bmiHeader.biBitCount = 1;
913 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
915 RGBQUAD *rgb = bmi_copy->bmiColors;
917 bmi_copy->bmiHeader.biClrUsed = bmi_copy->bmiHeader.biClrImportant = 2;
918 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
919 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
920 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
922 else
924 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)bmi_copy) + 1);
926 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
927 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
931 if (mask_size)
933 SelectObject( hdc, mask );
934 StretchDIBits( hdc, 0, 0, width, height,
935 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
936 mask_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
938 ret = TRUE;
940 done:
941 DeleteDC( hdc );
942 HeapFree( GetProcessHeap(), 0, bmi_copy );
944 if (ret)
945 hObj = alloc_icon_handle( FALSE, 1 );
946 if (hObj)
948 struct cursoricon_object *info = get_icon_ptr( hObj );
949 struct cursoricon_frame *frame;
951 info->is_icon = bIcon;
952 info->module = module;
953 info->hotspot = hotspot;
954 frame = get_icon_frame( info, 0 );
955 frame->delay = ~0;
956 frame->width = width;
957 frame->height = height;
958 frame->color = color;
959 frame->mask = mask;
960 frame->alpha = alpha;
961 release_icon_frame( info, 0, frame );
962 if (!IS_INTRESOURCE(resname))
964 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
965 if (info->resname) strcpyW( info->resname, resname );
967 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
969 if (module && (cFlag & LR_SHARED))
971 info->rsrc = rsrc;
972 list_add_head( &icon_cache, &info->entry );
974 release_icon_ptr( hObj, info );
976 else
978 DeleteObject( color );
979 DeleteObject( alpha );
980 DeleteObject( mask );
982 return hObj;
986 /**********************************************************************
987 * .ANI cursor support
989 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
990 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
991 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
993 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
994 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
995 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
996 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
997 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
998 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
999 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
1001 #define ANI_FLAG_ICON 0x1
1002 #define ANI_FLAG_SEQUENCE 0x2
1004 typedef struct {
1005 DWORD header_size;
1006 DWORD num_frames;
1007 DWORD num_steps;
1008 DWORD width;
1009 DWORD height;
1010 DWORD bpp;
1011 DWORD num_planes;
1012 DWORD display_rate;
1013 DWORD flags;
1014 } ani_header;
1016 typedef struct {
1017 DWORD data_size;
1018 const unsigned char *data;
1019 } riff_chunk_t;
1021 static void dump_ani_header( const ani_header *header )
1023 TRACE(" header size: %d\n", header->header_size);
1024 TRACE(" frames: %d\n", header->num_frames);
1025 TRACE(" steps: %d\n", header->num_steps);
1026 TRACE(" width: %d\n", header->width);
1027 TRACE(" height: %d\n", header->height);
1028 TRACE(" bpp: %d\n", header->bpp);
1029 TRACE(" planes: %d\n", header->num_planes);
1030 TRACE(" display rate: %d\n", header->display_rate);
1031 TRACE(" flags: 0x%08x\n", header->flags);
1036 * RIFF:
1037 * DWORD "RIFF"
1038 * DWORD size
1039 * DWORD riff_id
1040 * BYTE[] data
1042 * LIST:
1043 * DWORD "LIST"
1044 * DWORD size
1045 * DWORD list_id
1046 * BYTE[] data
1048 * CHUNK:
1049 * DWORD chunk_id
1050 * DWORD size
1051 * BYTE[] data
1053 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1055 const unsigned char *ptr = parent_chunk->data;
1056 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1058 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1060 while (ptr < end)
1062 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1063 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1065 ptr += sizeof(DWORD);
1066 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1067 ptr += sizeof(DWORD);
1068 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1069 chunk->data = ptr;
1071 return;
1074 ptr += sizeof(DWORD);
1075 ptr += (*(const DWORD *)ptr + 1) & ~1;
1076 ptr += sizeof(DWORD);
1082 * .ANI layout:
1084 * RIFF:'ACON' RIFF chunk
1085 * |- CHUNK:'anih' Header
1086 * |- CHUNK:'seq ' Sequence information (optional)
1087 * \- LIST:'fram' Frame list
1088 * |- CHUNK:icon Cursor frames
1089 * |- CHUNK:icon
1090 * |- ...
1091 * \- CHUNK:icon
1093 static HCURSOR CURSORICON_CreateIconFromANI( const BYTE *bits, DWORD bits_size, INT width, INT height,
1094 INT depth, BOOL is_icon, UINT loadflags )
1096 struct animated_cursoricon_object *ani_icon_data;
1097 struct cursoricon_object *info;
1098 DWORD *frame_rates = NULL;
1099 DWORD *frame_seq = NULL;
1100 ani_header header = {0};
1101 BOOL use_seq = FALSE;
1102 HCURSOR cursor = 0;
1103 UINT i, error = 0;
1104 HICON *frames;
1106 riff_chunk_t root_chunk = { bits_size, bits };
1107 riff_chunk_t ACON_chunk = {0};
1108 riff_chunk_t anih_chunk = {0};
1109 riff_chunk_t fram_chunk = {0};
1110 riff_chunk_t rate_chunk = {0};
1111 riff_chunk_t seq_chunk = {0};
1112 const unsigned char *icon_chunk;
1113 const unsigned char *icon_data;
1115 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1117 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1118 if (!ACON_chunk.data)
1120 ERR("Failed to get root chunk.\n");
1121 return 0;
1124 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1125 if (!anih_chunk.data)
1127 ERR("Failed to get 'anih' chunk.\n");
1128 return 0;
1130 memcpy( &header, anih_chunk.data, sizeof(header) );
1131 dump_ani_header( &header );
1133 if (!(header.flags & ANI_FLAG_ICON))
1135 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1136 return 0;
1139 if (header.flags & ANI_FLAG_SEQUENCE)
1141 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1142 if (seq_chunk.data)
1144 frame_seq = (DWORD *) seq_chunk.data;
1145 use_seq = TRUE;
1147 else
1149 FIXME("Sequence data expected but not found, assuming steps == frames.\n");
1150 header.num_steps = header.num_frames;
1154 riff_find_chunk( ANI_rate_ID, 0, &ACON_chunk, &rate_chunk );
1155 if (rate_chunk.data)
1156 frame_rates = (DWORD *) rate_chunk.data;
1158 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1159 if (!fram_chunk.data)
1161 ERR("Failed to get icon list.\n");
1162 return 0;
1165 cursor = alloc_icon_handle( TRUE, header.num_steps );
1166 if (!cursor) return 0;
1167 frames = HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD)*header.num_frames );
1168 if (!frames)
1170 free_icon_handle( cursor );
1171 return 0;
1174 info = get_icon_ptr( cursor );
1175 ani_icon_data = (struct animated_cursoricon_object *) info;
1176 info->is_icon = is_icon;
1177 ani_icon_data->num_frames = header.num_frames;
1179 /* The .ANI stores the display rate in jiffies (1/60s) */
1180 info->delay = header.display_rate;
1182 icon_chunk = fram_chunk.data;
1183 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1184 for (i=0; i<header.num_frames; i++)
1186 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1187 const CURSORICONFILEDIRENTRY *entry;
1188 INT frameWidth, frameHeight;
1189 const BITMAPINFO *bmi;
1191 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1192 bits + bits_size - icon_data,
1193 width, height, depth, loadflags );
1195 info->hotspot.x = entry->xHotspot;
1196 info->hotspot.y = entry->yHotspot;
1197 if (!header.width || !header.height)
1199 frameWidth = entry->bWidth;
1200 frameHeight = entry->bHeight;
1202 else
1204 frameWidth = header.width;
1205 frameHeight = header.height;
1208 frames[i] = NULL;
1209 if (entry->dwDIBOffset < bits + bits_size - icon_data)
1211 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1212 /* Grab a frame from the animation */
1213 frames[i] = create_icon_from_bmi( bmi, bits + bits_size - (const BYTE *)bmi,
1214 NULL, NULL, NULL, info->hotspot,
1215 is_icon, frameWidth, frameHeight, loadflags );
1218 if (!frames[i])
1220 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1221 error = TRUE;
1222 if (i == 0)
1224 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1225 ani_icon_data->num_frames = 0;
1226 release_icon_ptr( cursor, info );
1227 free_icon_handle( cursor );
1228 HeapFree( GetProcessHeap(), 0, frames );
1229 return 0;
1231 break;
1234 /* Advance to the next chunk */
1235 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1236 icon_data = icon_chunk + (2 * sizeof(DWORD));
1239 /* There was an error but we at least decoded the first frame, so just use that frame */
1240 if (error)
1242 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1243 for (i=1; i<ani_icon_data->num_frames; i++)
1244 free_icon_handle( ani_icon_data->frames[i] );
1245 use_seq = FALSE;
1246 info->delay = 0;
1247 ani_icon_data->num_steps = 1;
1248 ani_icon_data->num_frames = 1;
1251 /* Setup the animated frames in the correct sequence */
1252 for (i=0; i<ani_icon_data->num_steps; i++)
1254 DWORD frame_id = use_seq ? frame_seq[i] : i;
1255 struct cursoricon_frame *frame;
1257 if (frame_id >= ani_icon_data->num_frames)
1259 frame_id = ani_icon_data->num_frames-1;
1260 ERR_(cursor)("Sequence indicates frame past end of list, corrupt?\n");
1262 ani_icon_data->frames[i] = frames[frame_id];
1263 frame = get_icon_frame( info, i );
1264 if (frame_rates)
1265 frame->delay = frame_rates[i];
1266 else
1267 frame->delay = ~0;
1268 release_icon_frame( info, i, frame );
1271 HeapFree( GetProcessHeap(), 0, frames );
1272 release_icon_ptr( cursor, info );
1274 return cursor;
1278 /**********************************************************************
1279 * CreateIconFromResourceEx (USER32.@)
1281 * FIXME: Convert to mono when cFlag is LR_MONOCHROME.
1283 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1284 BOOL bIcon, DWORD dwVersion,
1285 INT width, INT height,
1286 UINT cFlag )
1288 POINT hotspot;
1289 const BITMAPINFO *bmi;
1291 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1292 bits, cbSize, dwVersion, width, height,
1293 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1295 if (!bits) return 0;
1297 if (dwVersion == 0x00020000)
1299 FIXME_(cursor)("\t2.xx resources are not supported\n");
1300 return 0;
1303 /* Check if the resource is an animated icon/cursor */
1304 if (!memcmp(bits, "RIFF", 4))
1305 return CURSORICON_CreateIconFromANI( bits, cbSize, width, height,
1306 0 /* default depth */, bIcon, cFlag );
1308 if (bIcon)
1310 hotspot.x = width / 2;
1311 hotspot.y = height / 2;
1312 bmi = (BITMAPINFO *)bits;
1314 else /* get the hotspot */
1316 const SHORT *pt = (const SHORT *)bits;
1317 hotspot.x = pt[0];
1318 hotspot.y = pt[1];
1319 bmi = (const BITMAPINFO *)(pt + 2);
1320 cbSize -= 2 * sizeof(*pt);
1323 return create_icon_from_bmi( bmi, cbSize, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1327 /**********************************************************************
1328 * CreateIconFromResource (USER32.@)
1330 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1331 BOOL bIcon, DWORD dwVersion)
1333 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1337 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1338 INT width, INT height, INT depth,
1339 BOOL fCursor, UINT loadflags)
1341 const CURSORICONFILEDIRENTRY *entry;
1342 const CURSORICONFILEDIR *dir;
1343 DWORD filesize = 0;
1344 HICON hIcon = 0;
1345 const BYTE *bits;
1346 POINT hotspot;
1348 TRACE("loading %s\n", debugstr_w( filename ));
1350 bits = map_fileW( filename, &filesize );
1351 if (!bits)
1352 return hIcon;
1354 /* Check for .ani. */
1355 if (memcmp( bits, "RIFF", 4 ) == 0)
1357 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, !fCursor, loadflags );
1358 goto end;
1361 dir = (const CURSORICONFILEDIR*) bits;
1362 if ( filesize < FIELD_OFFSET( CURSORICONFILEDIR, idEntries[dir->idCount] ))
1363 goto end;
1365 if ( fCursor )
1366 entry = CURSORICON_FindBestCursorFile( dir, filesize, width, height, depth, loadflags );
1367 else
1368 entry = CURSORICON_FindBestIconFile( dir, filesize, width, height, depth, loadflags );
1370 if ( !entry )
1371 goto end;
1373 /* check that we don't run off the end of the file */
1374 if ( entry->dwDIBOffset > filesize )
1375 goto end;
1376 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1377 goto end;
1379 hotspot.x = entry->xHotspot;
1380 hotspot.y = entry->yHotspot;
1381 hIcon = create_icon_from_bmi( (const BITMAPINFO *)&bits[entry->dwDIBOffset], filesize - entry->dwDIBOffset,
1382 NULL, NULL, NULL, hotspot, !fCursor, width, height, loadflags );
1383 end:
1384 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1385 UnmapViewOfFile( bits );
1386 return hIcon;
1389 /**********************************************************************
1390 * CURSORICON_Load
1392 * Load a cursor or icon from resource or file.
1394 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1395 INT width, INT height, INT depth,
1396 BOOL fCursor, UINT loadflags)
1398 HANDLE handle = 0;
1399 HICON hIcon = 0;
1400 HRSRC hRsrc;
1401 DWORD size;
1402 const CURSORICONDIR *dir;
1403 const CURSORICONDIRENTRY *dirEntry;
1404 const BYTE *bits;
1405 WORD wResId;
1406 POINT hotspot;
1408 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1409 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1411 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1412 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1414 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1416 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1417 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1419 /* Get directory resource ID */
1421 if (!(hRsrc = FindResourceW( hInstance, name,
1422 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1424 /* try animated resource */
1425 if (!(hRsrc = FindResourceW( hInstance, name,
1426 (LPWSTR)(fCursor ? RT_ANICURSOR : RT_ANIICON) ))) return 0;
1427 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1428 bits = LockResource( handle );
1429 return CURSORICON_CreateIconFromANI( bits, SizeofResource( hInstance, handle ),
1430 width, height, depth, !fCursor, loadflags );
1433 /* Find the best entry in the directory */
1435 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1436 if (!(dir = LockResource( handle ))) return 0;
1437 size = SizeofResource( hInstance, hRsrc );
1438 if (fCursor)
1439 dirEntry = CURSORICON_FindBestCursorRes( dir, size, width, height, depth, loadflags );
1440 else
1441 dirEntry = CURSORICON_FindBestIconRes( dir, size, width, height, depth, loadflags );
1442 if (!dirEntry) return 0;
1443 wResId = dirEntry->wResId;
1444 FreeResource( handle );
1446 /* Load the resource */
1448 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1449 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1451 /* If shared icon, check whether it was already loaded */
1452 if (loadflags & LR_SHARED)
1454 struct cursoricon_object *ptr;
1456 USER_Lock();
1457 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1459 if (ptr->module != hInstance) continue;
1460 if (ptr->rsrc != hRsrc) continue;
1461 hIcon = ptr->obj.handle;
1462 break;
1464 USER_Unlock();
1465 if (hIcon) return hIcon;
1468 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1469 size = SizeofResource( hInstance, hRsrc );
1470 bits = LockResource( handle );
1472 if (!fCursor)
1474 hotspot.x = width / 2;
1475 hotspot.y = height / 2;
1477 else /* get the hotspot */
1479 const SHORT *pt = (const SHORT *)bits;
1480 hotspot.x = pt[0];
1481 hotspot.y = pt[1];
1482 bits += 2 * sizeof(SHORT);
1483 size -= 2 * sizeof(SHORT);
1485 hIcon = create_icon_from_bmi( (const BITMAPINFO *)bits, size, hInstance, name, hRsrc,
1486 hotspot, !fCursor, width, height, loadflags );
1487 FreeResource( handle );
1488 return hIcon;
1492 /***********************************************************************
1493 * CreateCursor (USER32.@)
1495 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1496 INT xHotSpot, INT yHotSpot,
1497 INT nWidth, INT nHeight,
1498 LPCVOID lpANDbits, LPCVOID lpXORbits )
1500 ICONINFO info;
1501 HCURSOR hCursor;
1503 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1504 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1506 info.fIcon = FALSE;
1507 info.xHotspot = xHotSpot;
1508 info.yHotspot = yHotSpot;
1509 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1510 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1511 hCursor = CreateIconIndirect( &info );
1512 DeleteObject( info.hbmMask );
1513 DeleteObject( info.hbmColor );
1514 return hCursor;
1518 /***********************************************************************
1519 * CreateIcon (USER32.@)
1521 * Creates an icon based on the specified bitmaps. The bitmaps must be
1522 * provided in a device dependent format and will be resized to
1523 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1524 * depth. The provided bitmaps must be top-down bitmaps.
1525 * Although Windows does not support 15bpp(*) this API must support it
1526 * for Winelib applications.
1528 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1529 * format!
1531 * RETURNS
1532 * Success: handle to an icon
1533 * Failure: NULL
1535 * FIXME: Do we need to resize the bitmaps?
1537 HICON WINAPI CreateIcon(
1538 HINSTANCE hInstance, /* [in] the application's hInstance */
1539 INT nWidth, /* [in] the width of the provided bitmaps */
1540 INT nHeight, /* [in] the height of the provided bitmaps */
1541 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1542 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1543 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1544 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1546 ICONINFO iinfo;
1547 HICON hIcon;
1549 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1550 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1552 iinfo.fIcon = TRUE;
1553 iinfo.xHotspot = nWidth / 2;
1554 iinfo.yHotspot = nHeight / 2;
1555 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1556 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1558 hIcon = CreateIconIndirect( &iinfo );
1560 DeleteObject( iinfo.hbmMask );
1561 DeleteObject( iinfo.hbmColor );
1563 return hIcon;
1567 /***********************************************************************
1568 * CopyIcon (USER32.@)
1570 HICON WINAPI CopyIcon( HICON hIcon )
1572 struct cursoricon_object *ptrOld, *ptrNew;
1573 HICON hNew;
1575 if (!(ptrOld = get_icon_ptr( hIcon )))
1577 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1578 return 0;
1580 if ((hNew = alloc_icon_handle( FALSE, 1 )))
1582 struct cursoricon_frame *frameOld, *frameNew;
1584 ptrNew = get_icon_ptr( hNew );
1585 ptrNew->is_icon = ptrOld->is_icon;
1586 ptrNew->hotspot = ptrOld->hotspot;
1587 if (!(frameOld = get_icon_frame( ptrOld, 0 )))
1589 release_icon_ptr( hIcon, ptrOld );
1590 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1591 return 0;
1593 if (!(frameNew = get_icon_frame( ptrNew, 0 )))
1595 release_icon_frame( ptrOld, 0, frameOld );
1596 release_icon_ptr( hIcon, ptrOld );
1597 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1598 return 0;
1600 frameNew->delay = 0;
1601 frameNew->width = frameOld->width;
1602 frameNew->height = frameOld->height;
1603 frameNew->mask = copy_bitmap( frameOld->mask );
1604 frameNew->color = copy_bitmap( frameOld->color );
1605 frameNew->alpha = copy_bitmap( frameOld->alpha );
1606 release_icon_frame( ptrOld, 0, frameOld );
1607 release_icon_frame( ptrNew, 0, frameNew );
1608 release_icon_ptr( hNew, ptrNew );
1610 release_icon_ptr( hIcon, ptrOld );
1611 return hNew;
1615 /***********************************************************************
1616 * DestroyIcon (USER32.@)
1618 BOOL WINAPI DestroyIcon( HICON hIcon )
1620 BOOL ret = FALSE;
1621 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1623 TRACE_(icon)("%p\n", hIcon );
1625 if (obj)
1627 BOOL shared = (obj->rsrc != NULL);
1628 release_icon_ptr( hIcon, obj );
1629 ret = (GetCursor() != hIcon);
1630 if (!shared) free_icon_handle( hIcon );
1632 return ret;
1636 /***********************************************************************
1637 * DestroyCursor (USER32.@)
1639 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1641 return DestroyIcon( hCursor );
1644 /***********************************************************************
1645 * DrawIcon (USER32.@)
1647 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1649 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1652 /***********************************************************************
1653 * SetCursor (USER32.@)
1655 * Set the cursor shape.
1657 * RETURNS
1658 * A handle to the previous cursor shape.
1660 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1662 struct cursoricon_object *obj;
1663 HCURSOR hOldCursor;
1664 int show_count;
1665 BOOL ret;
1667 TRACE("%p\n", hCursor);
1669 SERVER_START_REQ( set_cursor )
1671 req->flags = SET_CURSOR_HANDLE;
1672 req->handle = wine_server_user_handle( hCursor );
1673 if ((ret = !wine_server_call_err( req )))
1675 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1676 show_count = reply->prev_count;
1679 SERVER_END_REQ;
1681 if (!ret) return 0;
1682 USER_Driver->pSetCursor( show_count >= 0 ? hCursor : 0 );
1684 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1685 release_icon_ptr( hOldCursor, obj );
1686 return hOldCursor;
1689 /***********************************************************************
1690 * ShowCursor (USER32.@)
1692 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1694 HCURSOR cursor;
1695 int increment = bShow ? 1 : -1;
1696 int count;
1698 SERVER_START_REQ( set_cursor )
1700 req->flags = SET_CURSOR_COUNT;
1701 req->show_count = increment;
1702 wine_server_call( req );
1703 cursor = wine_server_ptr_handle( reply->prev_handle );
1704 count = reply->prev_count + increment;
1706 SERVER_END_REQ;
1708 TRACE("%d, count=%d\n", bShow, count );
1710 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1711 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1713 return count;
1716 /***********************************************************************
1717 * GetCursor (USER32.@)
1719 HCURSOR WINAPI GetCursor(void)
1721 HCURSOR ret;
1723 SERVER_START_REQ( set_cursor )
1725 req->flags = 0;
1726 wine_server_call( req );
1727 ret = wine_server_ptr_handle( reply->prev_handle );
1729 SERVER_END_REQ;
1730 return ret;
1734 /***********************************************************************
1735 * ClipCursor (USER32.@)
1737 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1739 BOOL ret;
1740 RECT new_rect;
1742 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1744 if (rect && (rect->left > rect->right || rect->top > rect->bottom)) return FALSE;
1746 SERVER_START_REQ( set_cursor )
1748 req->clip_msg = WM_WINE_CLIPCURSOR;
1749 if (rect)
1751 req->flags = SET_CURSOR_CLIP;
1752 req->clip.left = rect->left;
1753 req->clip.top = rect->top;
1754 req->clip.right = rect->right;
1755 req->clip.bottom = rect->bottom;
1757 else req->flags = SET_CURSOR_NOCLIP;
1759 if ((ret = !wine_server_call( req )))
1761 new_rect.left = reply->new_clip.left;
1762 new_rect.top = reply->new_clip.top;
1763 new_rect.right = reply->new_clip.right;
1764 new_rect.bottom = reply->new_clip.bottom;
1767 SERVER_END_REQ;
1768 if (ret) USER_Driver->pClipCursor( &new_rect );
1769 return ret;
1773 /***********************************************************************
1774 * GetClipCursor (USER32.@)
1776 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1778 BOOL ret;
1780 if (!rect) return FALSE;
1782 SERVER_START_REQ( set_cursor )
1784 req->flags = 0;
1785 if ((ret = !wine_server_call( req )))
1787 rect->left = reply->new_clip.left;
1788 rect->top = reply->new_clip.top;
1789 rect->right = reply->new_clip.right;
1790 rect->bottom = reply->new_clip.bottom;
1793 SERVER_END_REQ;
1794 return ret;
1798 /***********************************************************************
1799 * SetSystemCursor (USER32.@)
1801 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1803 FIXME("(%p,%08x),stub!\n", hcur, id);
1804 return TRUE;
1808 /**********************************************************************
1809 * LookupIconIdFromDirectoryEx (USER32.@)
1811 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1812 INT width, INT height, UINT cFlag )
1814 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1815 UINT retVal = 0;
1816 if( dir && !dir->idReserved && (dir->idType & 3) )
1818 const CURSORICONDIRENTRY* entry;
1820 const HDC hdc = GetDC(0);
1821 const int depth = (cFlag & LR_MONOCHROME) ?
1822 1 : GetDeviceCaps(hdc, BITSPIXEL);
1823 ReleaseDC(0, hdc);
1825 if( bIcon )
1826 entry = CURSORICON_FindBestIconRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1827 else
1828 entry = CURSORICON_FindBestCursorRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1830 if( entry ) retVal = entry->wResId;
1832 else WARN_(cursor)("invalid resource directory\n");
1833 return retVal;
1836 /**********************************************************************
1837 * LookupIconIdFromDirectory (USER32.@)
1839 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1841 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1844 /***********************************************************************
1845 * LoadCursorW (USER32.@)
1847 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1849 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1851 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1852 LR_SHARED | LR_DEFAULTSIZE );
1855 /***********************************************************************
1856 * LoadCursorA (USER32.@)
1858 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1860 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1862 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1863 LR_SHARED | LR_DEFAULTSIZE );
1866 /***********************************************************************
1867 * LoadCursorFromFileW (USER32.@)
1869 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1871 TRACE("%s\n", debugstr_w(name));
1873 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1874 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1877 /***********************************************************************
1878 * LoadCursorFromFileA (USER32.@)
1880 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1882 TRACE("%s\n", debugstr_a(name));
1884 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1885 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1888 /***********************************************************************
1889 * LoadIconW (USER32.@)
1891 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1893 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1895 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1896 LR_SHARED | LR_DEFAULTSIZE );
1899 /***********************************************************************
1900 * LoadIconA (USER32.@)
1902 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1904 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1906 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1907 LR_SHARED | LR_DEFAULTSIZE );
1910 /**********************************************************************
1911 * GetCursorFrameInfo (USER32.@)
1913 * NOTES
1914 * So far no use has been found for the second parameter, it is currently presumed
1915 * that this parameter is reserved for future use.
1917 * PARAMS
1918 * hCursor [I] Handle to cursor for which to retrieve information
1919 * reserved [I] No purpose has been found for this parameter (may be NULL)
1920 * istep [I] The step of the cursor for which to retrieve information
1921 * rate_jiffies [O] Pointer to DWORD that receives the frame-specific delay (cannot be NULL)
1922 * num_steps [O] Pointer to DWORD that receives the number of steps in the cursor (cannot be NULL)
1924 * RETURNS
1925 * Success: Handle to a frame of the cursor (specified by istep)
1926 * Failure: NULL cursor (0)
1928 HCURSOR WINAPI GetCursorFrameInfo(HCURSOR hCursor, DWORD reserved, DWORD istep, DWORD *rate_jiffies, DWORD *num_steps)
1930 struct cursoricon_object *ptr;
1931 HCURSOR ret = 0;
1932 UINT icon_steps;
1934 if (rate_jiffies == NULL || num_steps == NULL) return 0;
1936 if (!(ptr = get_icon_ptr( hCursor ))) return 0;
1938 TRACE("%p => %d %d %p %p\n", hCursor, reserved, istep, rate_jiffies, num_steps);
1939 if (reserved != 0)
1940 FIXME("Second parameter non-zero (%d), please report this!\n", reserved);
1942 icon_steps = get_icon_steps(ptr);
1943 if (istep < icon_steps || !ptr->is_ani)
1945 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) ptr;
1946 UINT icon_frames = 1;
1948 if (ptr->is_ani)
1949 icon_frames = ani_icon_data->num_frames;
1950 if (ptr->is_ani && icon_frames > 1)
1951 ret = ani_icon_data->frames[istep];
1952 else
1953 ret = hCursor;
1954 if (icon_frames == 1)
1956 *rate_jiffies = 0;
1957 *num_steps = 1;
1959 else if (icon_steps == 1)
1961 *num_steps = ~0;
1962 *rate_jiffies = ptr->delay;
1964 else if (istep < icon_steps)
1966 struct cursoricon_frame *frame;
1968 *num_steps = icon_steps;
1969 frame = get_icon_frame( ptr, istep );
1970 if (get_icon_steps(ptr) == 1)
1971 *num_steps = ~0;
1972 else
1973 *num_steps = get_icon_steps(ptr);
1974 /* If this specific frame does not have a delay then use the global delay */
1975 if (frame->delay == ~0)
1976 *rate_jiffies = ptr->delay;
1977 else
1978 *rate_jiffies = frame->delay;
1979 release_icon_frame( ptr, istep, frame );
1983 release_icon_ptr( hCursor, ptr );
1985 return ret;
1988 /**********************************************************************
1989 * GetIconInfo (USER32.@)
1991 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1993 ICONINFOEXW infoW;
1995 infoW.cbSize = sizeof(infoW);
1996 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
1997 iconinfo->fIcon = infoW.fIcon;
1998 iconinfo->xHotspot = infoW.xHotspot;
1999 iconinfo->yHotspot = infoW.yHotspot;
2000 iconinfo->hbmColor = infoW.hbmColor;
2001 iconinfo->hbmMask = infoW.hbmMask;
2002 return TRUE;
2005 /**********************************************************************
2006 * GetIconInfoExA (USER32.@)
2008 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
2010 ICONINFOEXW infoW;
2012 if (info->cbSize != sizeof(*info))
2014 SetLastError( ERROR_INVALID_PARAMETER );
2015 return FALSE;
2017 infoW.cbSize = sizeof(infoW);
2018 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
2019 info->fIcon = infoW.fIcon;
2020 info->xHotspot = infoW.xHotspot;
2021 info->yHotspot = infoW.yHotspot;
2022 info->hbmColor = infoW.hbmColor;
2023 info->hbmMask = infoW.hbmMask;
2024 info->wResID = infoW.wResID;
2025 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
2026 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
2027 return TRUE;
2030 /**********************************************************************
2031 * GetIconInfoExW (USER32.@)
2033 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
2035 struct cursoricon_frame *frame;
2036 struct cursoricon_object *ptr;
2037 HMODULE module;
2038 BOOL ret = TRUE;
2040 if (info->cbSize != sizeof(*info))
2042 SetLastError( ERROR_INVALID_PARAMETER );
2043 return FALSE;
2045 if (!(ptr = get_icon_ptr( icon )))
2047 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2048 return FALSE;
2051 frame = get_icon_frame( ptr, 0 );
2052 if (!frame)
2054 release_icon_ptr( icon, ptr );
2055 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2056 return FALSE;
2059 TRACE("%p => %dx%d\n", icon, frame->width, frame->height);
2061 info->fIcon = ptr->is_icon;
2062 info->xHotspot = ptr->hotspot.x;
2063 info->yHotspot = ptr->hotspot.y;
2064 info->hbmColor = copy_bitmap( frame->color );
2065 info->hbmMask = copy_bitmap( frame->mask );
2066 info->wResID = 0;
2067 info->szModName[0] = 0;
2068 info->szResName[0] = 0;
2069 if (ptr->module)
2071 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
2072 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
2074 if (!info->hbmMask || (!info->hbmColor && frame->color))
2076 DeleteObject( info->hbmMask );
2077 DeleteObject( info->hbmColor );
2078 ret = FALSE;
2080 module = ptr->module;
2081 release_icon_frame( ptr, 0, frame );
2082 release_icon_ptr( icon, ptr );
2083 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
2084 return ret;
2087 /* copy an icon bitmap, even when it can't be selected into a DC */
2088 /* helper for CreateIconIndirect */
2089 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
2090 HBITMAP src, int width, int height )
2092 HDC hdc = CreateCompatibleDC( 0 );
2094 if (!SelectObject( hdc, src )) /* do it the hard way */
2096 BITMAPINFO *info;
2097 void *bits;
2099 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
2100 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2101 info->bmiHeader.biWidth = width;
2102 info->bmiHeader.biHeight = height;
2103 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
2104 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
2105 info->bmiHeader.biCompression = BI_RGB;
2106 info->bmiHeader.biSizeImage = get_dib_image_size( width, height, info->bmiHeader.biBitCount );
2107 info->bmiHeader.biXPelsPerMeter = 0;
2108 info->bmiHeader.biYPelsPerMeter = 0;
2109 info->bmiHeader.biClrUsed = 0;
2110 info->bmiHeader.biClrImportant = 0;
2111 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
2112 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
2113 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
2114 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
2116 HeapFree( GetProcessHeap(), 0, bits );
2117 HeapFree( GetProcessHeap(), 0, info );
2119 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
2121 DeleteDC( hdc );
2124 /**********************************************************************
2125 * CreateIconIndirect (USER32.@)
2127 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2129 BITMAP bmpXor, bmpAnd;
2130 HICON hObj;
2131 HBITMAP color = 0, mask;
2132 int width, height;
2133 HDC hdc;
2135 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2136 iconinfo->hbmColor, iconinfo->hbmMask,
2137 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2139 if (!iconinfo->hbmMask) return 0;
2141 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2142 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2143 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2144 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2146 if (iconinfo->hbmColor)
2148 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2149 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2150 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2151 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2153 width = bmpXor.bmWidth;
2154 height = bmpXor.bmHeight;
2155 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1 || bmpAnd.bmPlanes * bmpAnd.bmBitsPixel != 1)
2157 color = CreateCompatibleBitmap( screen_dc, width, height );
2158 mask = CreateBitmap( width, height, 1, 1, NULL );
2160 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2162 else
2164 width = bmpAnd.bmWidth;
2165 height = bmpAnd.bmHeight;
2166 mask = CreateBitmap( width, height, 1, 1, NULL );
2169 hdc = CreateCompatibleDC( 0 );
2170 SelectObject( hdc, mask );
2171 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
2173 if (color)
2175 SelectObject( hdc, color );
2176 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
2178 else if (iconinfo->hbmColor)
2180 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
2182 else height /= 2;
2184 DeleteDC( hdc );
2186 hObj = alloc_icon_handle( FALSE, 1 );
2187 if (hObj)
2189 struct cursoricon_object *info = get_icon_ptr( hObj );
2190 struct cursoricon_frame *frame;
2192 info->is_icon = iconinfo->fIcon;
2193 frame = get_icon_frame( info, 0 );
2194 frame->delay = ~0;
2195 frame->width = width;
2196 frame->height = height;
2197 frame->color = color;
2198 frame->mask = mask;
2199 frame->alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
2200 release_icon_frame( info, 0, frame );
2201 if (info->is_icon)
2203 info->hotspot.x = width / 2;
2204 info->hotspot.y = height / 2;
2206 else
2208 info->hotspot.x = iconinfo->xHotspot;
2209 info->hotspot.y = iconinfo->yHotspot;
2212 release_icon_ptr( hObj, info );
2214 return hObj;
2217 /******************************************************************************
2218 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2220 * NOTES
2221 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2223 * PARAMS
2224 * hdc [I] Handle to device context
2225 * x0 [I] X coordinate of upper left corner
2226 * y0 [I] Y coordinate of upper left corner
2227 * hIcon [I] Handle to icon to draw
2228 * cxWidth [I] Width of icon
2229 * cyWidth [I] Height of icon
2230 * istep [I] Index of frame in animated cursor
2231 * hbr [I] Handle to background brush
2232 * flags [I] Icon-drawing flags
2234 * RETURNS
2235 * Success: TRUE
2236 * Failure: FALSE
2238 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2239 INT cxWidth, INT cyWidth, UINT istep,
2240 HBRUSH hbr, UINT flags )
2242 struct cursoricon_frame *frame;
2243 struct cursoricon_object *ptr;
2244 HDC hdc_dest, hMemDC;
2245 BOOL result = FALSE, DoOffscreen;
2246 HBITMAP hB_off = 0;
2247 COLORREF oldFg, oldBg;
2248 INT x, y, nStretchMode;
2250 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2251 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2253 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2254 if (istep >= get_icon_steps( ptr ))
2256 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
2257 release_icon_ptr( hIcon, ptr );
2258 return FALSE;
2260 if (!(frame = get_icon_frame( ptr, istep )))
2262 FIXME_(icon)("Error retrieving icon frame %d\n", istep);
2263 release_icon_ptr( hIcon, ptr );
2264 return FALSE;
2266 if (!(hMemDC = CreateCompatibleDC( hdc )))
2268 release_icon_frame( ptr, istep, frame );
2269 release_icon_ptr( hIcon, ptr );
2270 return FALSE;
2273 if (flags & DI_NOMIRROR)
2274 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2276 /* Calculate the size of the destination image. */
2277 if (cxWidth == 0)
2279 if (flags & DI_DEFAULTSIZE)
2280 cxWidth = GetSystemMetrics (SM_CXICON);
2281 else
2282 cxWidth = frame->width;
2284 if (cyWidth == 0)
2286 if (flags & DI_DEFAULTSIZE)
2287 cyWidth = GetSystemMetrics (SM_CYICON);
2288 else
2289 cyWidth = frame->height;
2292 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2294 if (DoOffscreen) {
2295 RECT r;
2297 r.left = 0;
2298 r.top = 0;
2299 r.right = cxWidth;
2300 r.bottom = cxWidth;
2302 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2303 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2305 DeleteDC( hdc_dest );
2306 goto failed;
2308 SelectObject(hdc_dest, hB_off);
2309 FillRect(hdc_dest, &r, hbr);
2310 x = y = 0;
2312 else
2314 hdc_dest = hdc;
2315 x = x0;
2316 y = y0;
2319 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2321 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2322 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2324 if (frame->alpha && (flags & DI_IMAGE))
2326 BOOL alpha_blend = TRUE;
2328 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2330 BITMAP bm;
2331 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2332 alpha_blend = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel > 8;
2334 if (alpha_blend)
2336 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2337 SelectObject( hMemDC, frame->alpha );
2338 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2339 0, 0, frame->width, frame->height,
2340 pixelblend )) goto done;
2344 if (flags & DI_MASK)
2346 DWORD rop = (flags & DI_IMAGE) ? SRCAND : SRCCOPY;
2347 SelectObject( hMemDC, frame->mask );
2348 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2349 hMemDC, 0, 0, frame->width, frame->height, rop );
2352 if (flags & DI_IMAGE)
2354 if (frame->color)
2356 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2357 SelectObject( hMemDC, frame->color );
2358 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2359 hMemDC, 0, 0, frame->width, frame->height, rop );
2361 else
2363 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2364 SelectObject( hMemDC, frame->mask );
2365 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2366 hMemDC, 0, frame->height, frame->width,
2367 frame->height, rop );
2371 done:
2372 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2374 SetTextColor( hdc, oldFg );
2375 SetBkColor( hdc, oldBg );
2376 SetStretchBltMode (hdc, nStretchMode);
2377 result = TRUE;
2378 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2379 if (hB_off) DeleteObject(hB_off);
2380 failed:
2381 DeleteDC( hMemDC );
2382 release_icon_frame( ptr, istep, frame );
2383 release_icon_ptr( hIcon, ptr );
2384 return result;
2387 /***********************************************************************
2388 * DIB_FixColorsToLoadflags
2390 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2391 * are in loadflags
2393 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2395 int colors;
2396 COLORREF c_W, c_S, c_F, c_L, c_C;
2397 int incr,i;
2398 RGBQUAD *ptr;
2399 int bitmap_type;
2400 LONG width;
2401 LONG height;
2402 WORD bpp;
2403 DWORD compr;
2405 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2407 WARN_(resource)("Invalid bitmap\n");
2408 return;
2411 if (bpp > 8) return;
2413 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2415 incr = 3;
2416 colors = 1 << bpp;
2418 else
2420 incr = 4;
2421 colors = bmi->bmiHeader.biClrUsed;
2422 if (colors > 256) colors = 256;
2423 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2426 c_W = GetSysColor(COLOR_WINDOW);
2427 c_S = GetSysColor(COLOR_3DSHADOW);
2428 c_F = GetSysColor(COLOR_3DFACE);
2429 c_L = GetSysColor(COLOR_3DLIGHT);
2431 if (loadflags & LR_LOADTRANSPARENT) {
2432 switch (bpp) {
2433 case 1: pix = pix >> 7; break;
2434 case 4: pix = pix >> 4; break;
2435 case 8: break;
2436 default:
2437 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2438 return;
2440 if (pix >= colors) {
2441 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2442 return;
2444 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2445 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2446 ptr->rgbBlue = GetBValue(c_W);
2447 ptr->rgbGreen = GetGValue(c_W);
2448 ptr->rgbRed = GetRValue(c_W);
2450 if (loadflags & LR_LOADMAP3DCOLORS)
2451 for (i=0; i<colors; i++) {
2452 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2453 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2454 if (c_C == RGB(128, 128, 128)) {
2455 ptr->rgbRed = GetRValue(c_S);
2456 ptr->rgbGreen = GetGValue(c_S);
2457 ptr->rgbBlue = GetBValue(c_S);
2458 } else if (c_C == RGB(192, 192, 192)) {
2459 ptr->rgbRed = GetRValue(c_F);
2460 ptr->rgbGreen = GetGValue(c_F);
2461 ptr->rgbBlue = GetBValue(c_F);
2462 } else if (c_C == RGB(223, 223, 223)) {
2463 ptr->rgbRed = GetRValue(c_L);
2464 ptr->rgbGreen = GetGValue(c_L);
2465 ptr->rgbBlue = GetBValue(c_L);
2471 /**********************************************************************
2472 * BITMAP_Load
2474 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2475 INT desiredx, INT desiredy, UINT loadflags )
2477 HBITMAP hbitmap = 0, orig_bm;
2478 HRSRC hRsrc;
2479 HGLOBAL handle;
2480 const char *ptr = NULL;
2481 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2482 int size;
2483 BYTE pix;
2484 char *bits;
2485 LONG width, height, new_width, new_height;
2486 WORD bpp_dummy;
2487 DWORD compr_dummy, offbits = 0;
2488 INT bm_type;
2489 HDC screen_mem_dc = NULL;
2491 if (!(loadflags & LR_LOADFROMFILE))
2493 if (!instance)
2495 /* OEM bitmap: try to load the resource from user32.dll */
2496 instance = user32_module;
2499 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2500 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2502 if ((info = LockResource( handle )) == NULL) return 0;
2504 else
2506 BITMAPFILEHEADER * bmfh;
2508 if (!(ptr = map_fileW( name, NULL ))) return 0;
2509 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2510 bmfh = (BITMAPFILEHEADER *)ptr;
2511 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2513 WARN("Invalid/unsupported bitmap format!\n");
2514 goto end;
2516 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2519 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2520 &bpp_dummy, &compr_dummy);
2521 if (bm_type == -1)
2523 WARN("Invalid bitmap format!\n");
2524 goto end;
2527 size = bitmap_info_size(info, DIB_RGB_COLORS);
2528 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2529 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2531 if (!fix_info || !scaled_info) goto end;
2532 memcpy(fix_info, info, size);
2534 pix = *((LPBYTE)info + size);
2535 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2537 memcpy(scaled_info, fix_info, size);
2539 if(desiredx != 0)
2540 new_width = desiredx;
2541 else
2542 new_width = width;
2544 if(desiredy != 0)
2545 new_height = height > 0 ? desiredy : -desiredy;
2546 else
2547 new_height = height;
2549 if(bm_type == 0)
2551 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2552 core->bcWidth = new_width;
2553 core->bcHeight = new_height;
2555 else
2557 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2558 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2559 WARN("Broken BitmapInfoHeader!\n");
2560 goto end;
2563 scaled_info->bmiHeader.biWidth = new_width;
2564 scaled_info->bmiHeader.biHeight = new_height;
2567 if (new_height < 0) new_height = -new_height;
2569 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2570 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2572 bits = (char *)info + (offbits ? offbits : size);
2574 if (loadflags & LR_CREATEDIBSECTION)
2576 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2577 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2579 else
2581 if (is_dib_monochrome(fix_info))
2582 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2583 else
2584 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2587 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2588 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2589 SelectObject(screen_mem_dc, orig_bm);
2591 end:
2592 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2593 HeapFree(GetProcessHeap(), 0, scaled_info);
2594 HeapFree(GetProcessHeap(), 0, fix_info);
2595 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2597 return hbitmap;
2600 /**********************************************************************
2601 * LoadImageA (USER32.@)
2603 * See LoadImageW.
2605 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2606 INT desiredx, INT desiredy, UINT loadflags)
2608 HANDLE res;
2609 LPWSTR u_name;
2611 if (IS_INTRESOURCE(name))
2612 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2614 __TRY {
2615 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2616 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2617 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2619 __EXCEPT_PAGE_FAULT {
2620 SetLastError( ERROR_INVALID_PARAMETER );
2621 return 0;
2623 __ENDTRY
2624 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2625 HeapFree(GetProcessHeap(), 0, u_name);
2626 return res;
2630 /******************************************************************************
2631 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2633 * PARAMS
2634 * hinst [I] Handle of instance that contains image
2635 * name [I] Name of image
2636 * type [I] Type of image
2637 * desiredx [I] Desired width
2638 * desiredy [I] Desired height
2639 * loadflags [I] Load flags
2641 * RETURNS
2642 * Success: Handle to newly loaded image
2643 * Failure: NULL
2645 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2647 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2648 INT desiredx, INT desiredy, UINT loadflags )
2650 int depth;
2652 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2653 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2655 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2656 switch (type) {
2657 case IMAGE_BITMAP:
2658 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2660 case IMAGE_ICON:
2661 case IMAGE_CURSOR:
2662 depth = 1;
2663 if (!(loadflags & LR_MONOCHROME))
2665 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2666 if (screen_dc) depth = GetDeviceCaps( screen_dc, BITSPIXEL );
2668 return CURSORICON_Load(hinst, name, desiredx, desiredy, depth, (type == IMAGE_CURSOR), loadflags);
2670 return 0;
2673 /******************************************************************************
2674 * CopyImage (USER32.@) Creates new image and copies attributes to it
2676 * PARAMS
2677 * hnd [I] Handle to image to copy
2678 * type [I] Type of image to copy
2679 * desiredx [I] Desired width of new image
2680 * desiredy [I] Desired height of new image
2681 * flags [I] Copy flags
2683 * RETURNS
2684 * Success: Handle to newly created image
2685 * Failure: NULL
2687 * BUGS
2688 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2689 * all other versions (95/2000/XP have been tested) ignore it.
2691 * NOTES
2692 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2693 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2694 * the copy will have the same depth as the screen.
2695 * The content of the image will only be copied if the bit depth of the
2696 * original image is compatible with the bit depth of the screen, or
2697 * if the source is a DIB section.
2698 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2700 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2701 INT desiredy, UINT flags )
2703 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2704 hnd, type, desiredx, desiredy, flags);
2706 switch (type)
2708 case IMAGE_BITMAP:
2710 HBITMAP res = NULL;
2711 DIBSECTION ds;
2712 int objSize;
2713 BITMAPINFO * bi;
2715 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2716 if (!objSize) return 0;
2717 if ((desiredx < 0) || (desiredy < 0)) return 0;
2719 if (flags & LR_COPYFROMRESOURCE)
2721 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2724 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2725 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2727 /* Allocate memory for a BITMAPINFOHEADER structure and a
2728 color table. The maximum number of colors in a color table
2729 is 256 which corresponds to a bitmap with depth 8.
2730 Bitmaps with higher depths don't have color tables. */
2731 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2732 if (!bi) return 0;
2734 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2735 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2736 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2737 bi->bmiHeader.biCompression = BI_RGB;
2739 if (flags & LR_CREATEDIBSECTION)
2741 /* Create a DIB section. LR_MONOCHROME is ignored */
2742 void * bits;
2743 HDC dc = CreateCompatibleDC(NULL);
2745 if (objSize == sizeof(DIBSECTION))
2747 /* The source bitmap is a DIB.
2748 Get its attributes to create an exact copy */
2749 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2752 bi->bmiHeader.biWidth = desiredx;
2753 bi->bmiHeader.biHeight = desiredy;
2755 /* Get the color table or the color masks */
2756 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2758 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2759 DeleteDC(dc);
2761 else
2763 /* Create a device-dependent bitmap */
2765 BOOL monochrome = (flags & LR_MONOCHROME);
2767 if (objSize == sizeof(DIBSECTION))
2769 /* The source bitmap is a DIB section.
2770 Get its attributes */
2771 HDC dc = CreateCompatibleDC(NULL);
2772 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2773 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2774 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2775 DeleteDC(dc);
2777 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2779 /* Look if the colors of the DIB are black and white */
2781 monochrome =
2782 (bi->bmiColors[0].rgbRed == 0xff
2783 && bi->bmiColors[0].rgbGreen == 0xff
2784 && bi->bmiColors[0].rgbBlue == 0xff
2785 && bi->bmiColors[0].rgbReserved == 0
2786 && bi->bmiColors[1].rgbRed == 0
2787 && bi->bmiColors[1].rgbGreen == 0
2788 && bi->bmiColors[1].rgbBlue == 0
2789 && bi->bmiColors[1].rgbReserved == 0)
2791 (bi->bmiColors[0].rgbRed == 0
2792 && bi->bmiColors[0].rgbGreen == 0
2793 && bi->bmiColors[0].rgbBlue == 0
2794 && bi->bmiColors[0].rgbReserved == 0
2795 && bi->bmiColors[1].rgbRed == 0xff
2796 && bi->bmiColors[1].rgbGreen == 0xff
2797 && bi->bmiColors[1].rgbBlue == 0xff
2798 && bi->bmiColors[1].rgbReserved == 0);
2801 else if (!monochrome)
2803 monochrome = ds.dsBm.bmBitsPixel == 1;
2806 if (monochrome)
2808 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2810 else
2812 HDC screenDC = GetDC(NULL);
2813 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2814 ReleaseDC(NULL, screenDC);
2818 if (res)
2820 /* Only copy the bitmap if it's a DIB section or if it's
2821 compatible to the screen */
2822 BOOL copyContents;
2824 if (objSize == sizeof(DIBSECTION))
2826 copyContents = TRUE;
2828 else
2830 HDC screenDC = GetDC(NULL);
2831 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2832 ReleaseDC(NULL, screenDC);
2834 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2837 if (copyContents)
2839 /* The source bitmap may already be selected in a device context,
2840 use GetDIBits/StretchDIBits and not StretchBlt */
2842 HDC dc;
2843 void * bits;
2845 dc = CreateCompatibleDC(NULL);
2847 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2848 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2849 bi->bmiHeader.biSizeImage = 0;
2850 bi->bmiHeader.biClrUsed = 0;
2851 bi->bmiHeader.biClrImportant = 0;
2853 /* Fill in biSizeImage */
2854 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2855 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2857 if (bits)
2859 HBITMAP oldBmp;
2861 /* Get the image bits of the source bitmap */
2862 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2864 /* Copy it to the destination bitmap */
2865 oldBmp = SelectObject(dc, res);
2866 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2867 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2868 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2869 SelectObject(dc, oldBmp);
2871 HeapFree(GetProcessHeap(), 0, bits);
2874 DeleteDC(dc);
2877 if (flags & LR_COPYDELETEORG)
2879 DeleteObject(hnd);
2882 HeapFree(GetProcessHeap(), 0, bi);
2883 return res;
2885 case IMAGE_ICON:
2886 case IMAGE_CURSOR:
2888 struct cursoricon_object *icon;
2889 HICON res = 0;
2890 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( screen_dc, BITSPIXEL );
2892 if (flags & LR_DEFAULTSIZE)
2894 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2895 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2898 if (!(icon = get_icon_ptr( hnd ))) return 0;
2900 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2901 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2902 !icon->is_icon, flags );
2903 else
2904 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2905 release_icon_ptr( hnd, icon );
2907 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2908 return res;
2911 return 0;
2915 /******************************************************************************
2916 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2918 * RETURNS
2919 * Success: Handle to specified bitmap
2920 * Failure: NULL
2922 HBITMAP WINAPI LoadBitmapW(
2923 HINSTANCE instance, /* [in] Handle to application instance */
2924 LPCWSTR name) /* [in] Address of bitmap resource name */
2926 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2929 /**********************************************************************
2930 * LoadBitmapA (USER32.@)
2932 * See LoadBitmapW.
2934 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2936 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );