comctl32: Store style bits provided with WM_CREATE.
[wine/multimedia.git] / dlls / user32 / cursoricon.c
blobd8068d8d28ed23c38ffdf0d515228f9c65e2a454
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 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, 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, 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, 0, &width, &height, &bits )) return -1;
518 iTotalDiff = 0;
521 for ( i = 0; iTotalDiff && get_entry( dir, 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, 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, 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 icon = &resdir->idEntries[n].ResInfo.icon;
560 *width = icon->bWidth;
561 *height = icon->bHeight;
562 *bits = resdir->idEntries[n].wBitCount;
563 return TRUE;
566 /**********************************************************************
567 * CURSORICON_FindBestCursor
569 * Find the cursor closest to the requested size.
571 * FIXME: parameter 'color' ignored.
573 static int CURSORICON_FindBestCursor( LPCVOID dir, fnGetCIEntry get_entry,
574 int width, int height, int depth, UINT loadflags )
576 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
578 if (loadflags & LR_DEFAULTSIZE)
580 if (!width) width = GetSystemMetrics( SM_CXCURSOR );
581 if (!height) height = GetSystemMetrics( SM_CYCURSOR );
583 else if (!width && !height)
585 /* use the first entry */
586 if (!get_entry( dir, 0, &width, &height, &bits )) return -1;
587 return 0;
590 /* Double height to account for AND and XOR masks */
592 height *= 2;
594 /* First find the largest one smaller than or equal to the requested size*/
596 maxwidth = maxheight = 0;
597 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
599 if ((cx <= width) && (cy <= height) &&
600 (cx > maxwidth) && (cy > maxheight))
602 bestEntry = i;
603 maxwidth = cx;
604 maxheight = cy;
607 if (bestEntry != -1) return bestEntry;
609 /* Now find the smallest one larger than the requested size */
611 maxwidth = maxheight = 255;
612 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
614 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
616 bestEntry = i;
617 maxwidth = cx;
618 maxheight = cy;
622 return bestEntry;
625 static BOOL CURSORICON_GetResCursorEntry( LPCVOID dir, int n,
626 int *width, int *height, int *bits )
628 const CURSORICONDIR *resdir = dir;
629 const CURSORDIR *cursor;
631 if ( resdir->idCount <= n )
632 return FALSE;
633 cursor = &resdir->idEntries[n].ResInfo.cursor;
634 *width = cursor->wWidth;
635 *height = cursor->wHeight;
636 *bits = resdir->idEntries[n].wBitCount;
637 return TRUE;
640 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir,
641 int width, int height, int depth,
642 UINT loadflags )
644 int n;
646 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
647 width, height, depth, loadflags );
648 if ( n < 0 )
649 return NULL;
650 return &dir->idEntries[n];
653 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir,
654 int width, int height, int depth,
655 UINT loadflags )
657 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
658 width, height, depth, loadflags );
659 if ( n < 0 )
660 return NULL;
661 return &dir->idEntries[n];
664 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, int n,
665 int *width, int *height, int *bits )
667 const CURSORICONFILEDIR *filedir = dir;
668 const CURSORICONFILEDIRENTRY *entry;
669 const BITMAPINFOHEADER *info;
671 if ( filedir->idCount <= n )
672 return FALSE;
673 entry = &filedir->idEntries[n];
674 /* FIXME: check against file size */
675 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
676 *width = entry->bWidth;
677 *height = entry->bHeight;
678 *bits = info->biBitCount;
679 return TRUE;
682 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir,
683 int width, int height, int depth,
684 UINT loadflags )
686 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
687 width, height, depth, loadflags );
688 if ( n < 0 )
689 return NULL;
690 return &dir->idEntries[n];
693 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir,
694 int width, int height, int depth,
695 UINT loadflags )
697 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
698 width, height, depth, loadflags );
699 if ( n < 0 )
700 return NULL;
701 return &dir->idEntries[n];
704 /***********************************************************************
705 * bmi_has_alpha
707 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
709 int i;
710 BOOL has_alpha = FALSE;
711 const unsigned char *ptr = bits;
713 if (info->bmiHeader.biBitCount != 32) return FALSE;
714 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
715 if ((has_alpha = (ptr[3] != 0))) break;
716 return has_alpha;
719 /***********************************************************************
720 * create_alpha_bitmap
722 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
724 static HBITMAP create_alpha_bitmap( HBITMAP color, HBITMAP mask,
725 const BITMAPINFO *src_info, const void *color_bits )
727 HBITMAP alpha = 0;
728 BITMAPINFO *info = NULL;
729 BITMAP bm;
730 HDC hdc;
731 void *bits;
732 unsigned char *ptr;
733 int i;
735 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
736 if (bm.bmBitsPixel != 32) return 0;
738 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
739 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
740 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
741 info->bmiHeader.biWidth = bm.bmWidth;
742 info->bmiHeader.biHeight = -bm.bmHeight;
743 info->bmiHeader.biPlanes = 1;
744 info->bmiHeader.biBitCount = 32;
745 info->bmiHeader.biCompression = BI_RGB;
746 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
747 info->bmiHeader.biXPelsPerMeter = 0;
748 info->bmiHeader.biYPelsPerMeter = 0;
749 info->bmiHeader.biClrUsed = 0;
750 info->bmiHeader.biClrImportant = 0;
751 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
753 if (src_info)
755 SelectObject( hdc, alpha );
756 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
757 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
758 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
761 else
763 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
764 if (!bmi_has_alpha( info, bits ))
766 DeleteObject( alpha );
767 alpha = 0;
768 goto done;
772 /* pre-multiply by alpha */
773 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
775 unsigned int alpha = ptr[3];
776 ptr[0] = ptr[0] * alpha / 255;
777 ptr[1] = ptr[1] * alpha / 255;
778 ptr[2] = ptr[2] * alpha / 255;
781 done:
782 DeleteDC( hdc );
783 HeapFree( GetProcessHeap(), 0, info );
784 return alpha;
788 /***********************************************************************
789 * create_icon_from_bmi
791 * Create an icon from its BITMAPINFO.
793 static HICON create_icon_from_bmi( BITMAPINFO *bmi, HMODULE module, LPCWSTR resname, HRSRC rsrc,
794 POINT hotspot, BOOL bIcon, INT width, INT height, UINT cFlag )
796 unsigned int size = bitmap_info_size( bmi, DIB_RGB_COLORS );
797 BOOL monochrome = is_dib_monochrome( bmi );
798 HBITMAP color = 0, mask = 0, alpha = 0;
799 const void *color_bits, *mask_bits;
800 BITMAPINFO *bmi_copy;
801 BOOL ret = FALSE;
802 BOOL do_stretch;
803 HICON hObj = 0;
804 HDC hdc = 0;
806 /* Check bitmap header */
808 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
809 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
810 bmi->bmiHeader.biCompression != BI_RGB) )
812 WARN_(cursor)("\tinvalid resource bitmap header.\n");
813 return 0;
816 if (cFlag & LR_DEFAULTSIZE)
818 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
819 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
821 else
823 if (!width) width = bmi->bmiHeader.biWidth;
824 if (!height) height = bmi->bmiHeader.biHeight/2;
826 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
827 (bmi->bmiHeader.biWidth != width);
829 /* Scale the hotspot */
830 if (bIcon)
832 hotspot.x = width / 2;
833 hotspot.y = height / 2;
835 else if (do_stretch)
837 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
838 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
841 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
842 if (!screen_dc) return 0;
844 if (!(bmi_copy = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
845 return 0;
846 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
848 memcpy( bmi_copy, bmi, size );
849 bmi_copy->bmiHeader.biHeight /= 2;
851 color_bits = (const char*)bmi + size;
852 mask_bits = (const char*)color_bits +
853 get_dib_image_size( bmi->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight, bmi->bmiHeader.biBitCount );
855 alpha = 0;
856 if (monochrome)
858 if (!(mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
859 color = 0;
861 /* copy color data into second half of mask bitmap */
862 SelectObject( hdc, mask );
863 StretchDIBits( hdc, 0, height, width, height,
864 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
865 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
867 else
869 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
870 if (!(color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
871 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
873 DeleteObject( mask );
874 goto done;
876 SelectObject( hdc, color );
877 StretchDIBits( hdc, 0, 0, width, height,
878 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
879 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
881 if (bmi_has_alpha( bmi_copy, color_bits ))
882 alpha = create_alpha_bitmap( color, mask, bmi_copy, color_bits );
884 /* convert info to monochrome to copy the mask */
885 bmi_copy->bmiHeader.biBitCount = 1;
886 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
888 RGBQUAD *rgb = bmi_copy->bmiColors;
890 bmi_copy->bmiHeader.biClrUsed = bmi_copy->bmiHeader.biClrImportant = 2;
891 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
892 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
893 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
895 else
897 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)bmi_copy) + 1);
899 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
900 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
904 SelectObject( hdc, mask );
905 StretchDIBits( hdc, 0, 0, width, height,
906 0, 0, bmi_copy->bmiHeader.biWidth, bmi_copy->bmiHeader.biHeight,
907 mask_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
908 ret = TRUE;
910 done:
911 DeleteDC( hdc );
912 HeapFree( GetProcessHeap(), 0, bmi_copy );
914 if (ret)
915 hObj = alloc_icon_handle( FALSE, 1 );
916 if (hObj)
918 struct cursoricon_object *info = get_icon_ptr( hObj );
919 struct cursoricon_frame *frame;
921 info->is_icon = bIcon;
922 info->module = module;
923 info->hotspot = hotspot;
924 frame = get_icon_frame( info, 0 );
925 frame->delay = ~0;
926 frame->width = width;
927 frame->height = height;
928 frame->color = color;
929 frame->mask = mask;
930 frame->alpha = alpha;
931 release_icon_frame( info, 0, frame );
932 if (!IS_INTRESOURCE(resname))
934 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
935 if (info->resname) strcpyW( info->resname, resname );
937 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
939 if (module && (cFlag & LR_SHARED))
941 info->rsrc = rsrc;
942 list_add_head( &icon_cache, &info->entry );
944 release_icon_ptr( hObj, info );
945 USER_Driver->pCreateCursorIcon( hObj );
947 else
949 DeleteObject( color );
950 DeleteObject( alpha );
951 DeleteObject( mask );
953 return hObj;
957 /**********************************************************************
958 * .ANI cursor support
960 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
961 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
962 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
964 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
965 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
966 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
967 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
968 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
969 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
970 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
972 #define ANI_FLAG_ICON 0x1
973 #define ANI_FLAG_SEQUENCE 0x2
975 typedef struct {
976 DWORD header_size;
977 DWORD num_frames;
978 DWORD num_steps;
979 DWORD width;
980 DWORD height;
981 DWORD bpp;
982 DWORD num_planes;
983 DWORD display_rate;
984 DWORD flags;
985 } ani_header;
987 typedef struct {
988 DWORD data_size;
989 const unsigned char *data;
990 } riff_chunk_t;
992 static void dump_ani_header( const ani_header *header )
994 TRACE(" header size: %d\n", header->header_size);
995 TRACE(" frames: %d\n", header->num_frames);
996 TRACE(" steps: %d\n", header->num_steps);
997 TRACE(" width: %d\n", header->width);
998 TRACE(" height: %d\n", header->height);
999 TRACE(" bpp: %d\n", header->bpp);
1000 TRACE(" planes: %d\n", header->num_planes);
1001 TRACE(" display rate: %d\n", header->display_rate);
1002 TRACE(" flags: 0x%08x\n", header->flags);
1007 * RIFF:
1008 * DWORD "RIFF"
1009 * DWORD size
1010 * DWORD riff_id
1011 * BYTE[] data
1013 * LIST:
1014 * DWORD "LIST"
1015 * DWORD size
1016 * DWORD list_id
1017 * BYTE[] data
1019 * CHUNK:
1020 * DWORD chunk_id
1021 * DWORD size
1022 * BYTE[] data
1024 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1026 const unsigned char *ptr = parent_chunk->data;
1027 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1029 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1031 while (ptr < end)
1033 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1034 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1036 ptr += sizeof(DWORD);
1037 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1038 ptr += sizeof(DWORD);
1039 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1040 chunk->data = ptr;
1042 return;
1045 ptr += sizeof(DWORD);
1046 ptr += (*(const DWORD *)ptr + 1) & ~1;
1047 ptr += sizeof(DWORD);
1053 * .ANI layout:
1055 * RIFF:'ACON' RIFF chunk
1056 * |- CHUNK:'anih' Header
1057 * |- CHUNK:'seq ' Sequence information (optional)
1058 * \- LIST:'fram' Frame list
1059 * |- CHUNK:icon Cursor frames
1060 * |- CHUNK:icon
1061 * |- ...
1062 * \- CHUNK:icon
1064 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size, INT width, INT height,
1065 INT depth, BOOL is_icon, UINT loadflags )
1067 struct animated_cursoricon_object *ani_icon_data;
1068 struct cursoricon_object *info;
1069 DWORD *frame_rates = NULL;
1070 DWORD *frame_seq = NULL;
1071 ani_header header = {0};
1072 BOOL use_seq = FALSE;
1073 HCURSOR cursor = 0;
1074 UINT i, error = 0;
1075 HICON *frames;
1077 riff_chunk_t root_chunk = { bits_size, bits };
1078 riff_chunk_t ACON_chunk = {0};
1079 riff_chunk_t anih_chunk = {0};
1080 riff_chunk_t fram_chunk = {0};
1081 riff_chunk_t rate_chunk = {0};
1082 riff_chunk_t seq_chunk = {0};
1083 const unsigned char *icon_chunk;
1084 const unsigned char *icon_data;
1086 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1088 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1089 if (!ACON_chunk.data)
1091 ERR("Failed to get root chunk.\n");
1092 return 0;
1095 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1096 if (!anih_chunk.data)
1098 ERR("Failed to get 'anih' chunk.\n");
1099 return 0;
1101 memcpy( &header, anih_chunk.data, sizeof(header) );
1102 dump_ani_header( &header );
1104 if (!(header.flags & ANI_FLAG_ICON))
1106 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1107 return 0;
1110 if (header.flags & ANI_FLAG_SEQUENCE)
1112 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1113 if (seq_chunk.data)
1115 frame_seq = (DWORD *) seq_chunk.data;
1116 use_seq = TRUE;
1118 else
1120 FIXME("Sequence data expected but not found, assuming steps == frames.\n");
1121 header.num_steps = header.num_frames;
1125 riff_find_chunk( ANI_rate_ID, 0, &ACON_chunk, &rate_chunk );
1126 if (rate_chunk.data)
1127 frame_rates = (DWORD *) rate_chunk.data;
1129 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1130 if (!fram_chunk.data)
1132 ERR("Failed to get icon list.\n");
1133 return 0;
1136 cursor = alloc_icon_handle( TRUE, header.num_steps );
1137 if (!cursor) return 0;
1138 frames = HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD)*header.num_frames );
1139 if (!frames)
1141 free_icon_handle( cursor );
1142 return 0;
1145 info = get_icon_ptr( cursor );
1146 ani_icon_data = (struct animated_cursoricon_object *) info;
1147 info->is_icon = is_icon;
1148 ani_icon_data->num_frames = header.num_frames;
1150 /* The .ANI stores the display rate in jiffies (1/60s) */
1151 info->delay = header.display_rate;
1153 icon_chunk = fram_chunk.data;
1154 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1155 for (i=0; i<header.num_frames; i++)
1157 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1158 const CURSORICONFILEDIRENTRY *entry;
1159 INT frameWidth, frameHeight;
1160 const BITMAPINFO *bmi;
1162 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1163 width, height, depth, loadflags );
1165 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1166 info->hotspot.x = entry->xHotspot;
1167 info->hotspot.y = entry->yHotspot;
1168 if (!header.width || !header.height)
1170 frameWidth = entry->bWidth;
1171 frameHeight = entry->bHeight;
1173 else
1175 frameWidth = header.width;
1176 frameHeight = header.height;
1179 /* Grab a frame from the animation */
1180 frames[i] = create_icon_from_bmi( (BITMAPINFO *)bmi, NULL, NULL, NULL, info->hotspot,
1181 is_icon, frameWidth, frameHeight, loadflags );
1182 if (!frames[i])
1184 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1185 error = TRUE;
1186 if (i == 0)
1188 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1189 ani_icon_data->num_frames = 0;
1190 release_icon_ptr( cursor, info );
1191 free_icon_handle( cursor );
1192 HeapFree( GetProcessHeap(), 0, frames );
1193 return 0;
1195 break;
1198 /* Advance to the next chunk */
1199 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1200 icon_data = icon_chunk + (2 * sizeof(DWORD));
1203 /* There was an error but we at least decoded the first frame, so just use that frame */
1204 if (error)
1206 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1207 for (i=1; i<ani_icon_data->num_frames; i++)
1208 free_icon_handle( ani_icon_data->frames[i] );
1209 use_seq = FALSE;
1210 info->delay = 0;
1211 ani_icon_data->num_steps = 1;
1212 ani_icon_data->num_frames = 1;
1215 /* Setup the animated frames in the correct sequence */
1216 for (i=0; i<ani_icon_data->num_steps; i++)
1218 DWORD frame_id = use_seq ? frame_seq[i] : i;
1219 struct cursoricon_frame *frame;
1221 if (frame_id >= ani_icon_data->num_frames)
1223 frame_id = ani_icon_data->num_frames-1;
1224 ERR_(cursor)("Sequence indicates frame past end of list, corrupt?\n");
1226 ani_icon_data->frames[i] = frames[frame_id];
1227 frame = get_icon_frame( info, i );
1228 if (frame_rates)
1229 frame->delay = frame_rates[i];
1230 else
1231 frame->delay = ~0;
1232 release_icon_frame( info, i, frame );
1235 HeapFree( GetProcessHeap(), 0, frames );
1236 release_icon_ptr( cursor, info );
1238 return cursor;
1242 /**********************************************************************
1243 * CreateIconFromResourceEx (USER32.@)
1245 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1246 * with cbSize parameter as well.
1248 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1249 BOOL bIcon, DWORD dwVersion,
1250 INT width, INT height,
1251 UINT cFlag )
1253 POINT hotspot;
1254 BITMAPINFO *bmi;
1256 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1257 bits, cbSize, dwVersion, width, height,
1258 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1260 if (!bits) return 0;
1262 if (dwVersion == 0x00020000)
1264 FIXME_(cursor)("\t2.xx resources are not supported\n");
1265 return 0;
1268 /* Check if the resource is an animated icon/cursor */
1269 if (!memcmp(bits, "RIFF", 4))
1270 return CURSORICON_CreateIconFromANI( bits, cbSize, width, height,
1271 0 /* default depth */, bIcon, cFlag );
1273 if (bIcon)
1275 hotspot.x = width / 2;
1276 hotspot.y = height / 2;
1277 bmi = (BITMAPINFO *)bits;
1279 else /* get the hotspot */
1281 SHORT *pt = (SHORT *)bits;
1282 hotspot.x = pt[0];
1283 hotspot.y = pt[1];
1284 bmi = (BITMAPINFO *)(pt + 2);
1287 return create_icon_from_bmi( bmi, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1291 /**********************************************************************
1292 * CreateIconFromResource (USER32.@)
1294 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1295 BOOL bIcon, DWORD dwVersion)
1297 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1301 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1302 INT width, INT height, INT depth,
1303 BOOL fCursor, UINT loadflags)
1305 const CURSORICONFILEDIRENTRY *entry;
1306 const CURSORICONFILEDIR *dir;
1307 DWORD filesize = 0;
1308 HICON hIcon = 0;
1309 LPBYTE bits;
1310 POINT hotspot;
1312 TRACE("loading %s\n", debugstr_w( filename ));
1314 bits = map_fileW( filename, &filesize );
1315 if (!bits)
1316 return hIcon;
1318 /* Check for .ani. */
1319 if (memcmp( bits, "RIFF", 4 ) == 0)
1321 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, !fCursor, loadflags );
1322 goto end;
1325 dir = (const CURSORICONFILEDIR*) bits;
1326 if ( filesize < sizeof(*dir) )
1327 goto end;
1329 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1330 goto end;
1332 if ( fCursor )
1333 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth, loadflags );
1334 else
1335 entry = CURSORICON_FindBestIconFile( dir, width, height, depth, loadflags );
1337 if ( !entry )
1338 goto end;
1340 /* check that we don't run off the end of the file */
1341 if ( entry->dwDIBOffset > filesize )
1342 goto end;
1343 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1344 goto end;
1346 hotspot.x = entry->xHotspot;
1347 hotspot.y = entry->yHotspot;
1348 hIcon = create_icon_from_bmi( (BITMAPINFO *)&bits[entry->dwDIBOffset], NULL, NULL, NULL,
1349 hotspot, !fCursor, width, height, loadflags );
1350 end:
1351 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1352 UnmapViewOfFile( bits );
1353 return hIcon;
1356 /**********************************************************************
1357 * CURSORICON_Load
1359 * Load a cursor or icon from resource or file.
1361 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1362 INT width, INT height, INT depth,
1363 BOOL fCursor, UINT loadflags)
1365 HANDLE handle = 0;
1366 HICON hIcon = 0;
1367 HRSRC hRsrc;
1368 const CURSORICONDIR *dir;
1369 const CURSORICONDIRENTRY *dirEntry;
1370 LPBYTE bits;
1371 WORD wResId;
1372 POINT hotspot;
1374 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1375 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1377 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1378 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1380 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1382 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1383 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1385 /* Get directory resource ID */
1387 if (!(hRsrc = FindResourceW( hInstance, name,
1388 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1390 /* try animated resource */
1391 if (!(hRsrc = FindResourceW( hInstance, name,
1392 (LPWSTR)(fCursor ? RT_ANICURSOR : RT_ANIICON) ))) return 0;
1393 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1394 bits = LockResource( handle );
1395 return CURSORICON_CreateIconFromANI( bits, SizeofResource( hInstance, handle ),
1396 width, height, depth, !fCursor, loadflags );
1399 /* Find the best entry in the directory */
1401 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1402 if (!(dir = LockResource( handle ))) return 0;
1403 if (fCursor)
1404 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth, loadflags );
1405 else
1406 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth, loadflags );
1407 if (!dirEntry) return 0;
1408 wResId = dirEntry->wResId;
1409 FreeResource( handle );
1411 /* Load the resource */
1413 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1414 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1416 /* If shared icon, check whether it was already loaded */
1417 if (loadflags & LR_SHARED)
1419 struct cursoricon_object *ptr;
1421 USER_Lock();
1422 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1424 if (ptr->module != hInstance) continue;
1425 if (ptr->rsrc != hRsrc) continue;
1426 hIcon = ptr->obj.handle;
1427 break;
1429 USER_Unlock();
1430 if (hIcon) return hIcon;
1433 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1434 bits = LockResource( handle );
1436 if (!fCursor)
1438 hotspot.x = width / 2;
1439 hotspot.y = height / 2;
1441 else /* get the hotspot */
1443 SHORT *pt = (SHORT *)bits;
1444 hotspot.x = pt[0];
1445 hotspot.y = pt[1];
1446 bits += 2 * sizeof(SHORT);
1448 hIcon = create_icon_from_bmi( (BITMAPINFO *)bits, hInstance, name, hRsrc,
1449 hotspot, !fCursor, width, height, loadflags );
1450 FreeResource( handle );
1451 return hIcon;
1455 /***********************************************************************
1456 * CreateCursor (USER32.@)
1458 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1459 INT xHotSpot, INT yHotSpot,
1460 INT nWidth, INT nHeight,
1461 LPCVOID lpANDbits, LPCVOID lpXORbits )
1463 ICONINFO info;
1464 HCURSOR hCursor;
1466 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1467 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1469 info.fIcon = FALSE;
1470 info.xHotspot = xHotSpot;
1471 info.yHotspot = yHotSpot;
1472 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1473 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1474 hCursor = CreateIconIndirect( &info );
1475 DeleteObject( info.hbmMask );
1476 DeleteObject( info.hbmColor );
1477 return hCursor;
1481 /***********************************************************************
1482 * CreateIcon (USER32.@)
1484 * Creates an icon based on the specified bitmaps. The bitmaps must be
1485 * provided in a device dependent format and will be resized to
1486 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1487 * depth. The provided bitmaps must be top-down bitmaps.
1488 * Although Windows does not support 15bpp(*) this API must support it
1489 * for Winelib applications.
1491 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1492 * format!
1494 * RETURNS
1495 * Success: handle to an icon
1496 * Failure: NULL
1498 * FIXME: Do we need to resize the bitmaps?
1500 HICON WINAPI CreateIcon(
1501 HINSTANCE hInstance, /* [in] the application's hInstance */
1502 INT nWidth, /* [in] the width of the provided bitmaps */
1503 INT nHeight, /* [in] the height of the provided bitmaps */
1504 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1505 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1506 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1507 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1509 ICONINFO iinfo;
1510 HICON hIcon;
1512 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1513 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1515 iinfo.fIcon = TRUE;
1516 iinfo.xHotspot = nWidth / 2;
1517 iinfo.yHotspot = nHeight / 2;
1518 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1519 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1521 hIcon = CreateIconIndirect( &iinfo );
1523 DeleteObject( iinfo.hbmMask );
1524 DeleteObject( iinfo.hbmColor );
1526 return hIcon;
1530 /***********************************************************************
1531 * CopyIcon (USER32.@)
1533 HICON WINAPI CopyIcon( HICON hIcon )
1535 struct cursoricon_object *ptrOld, *ptrNew;
1536 HICON hNew;
1538 if (!(ptrOld = get_icon_ptr( hIcon )))
1540 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1541 return 0;
1543 if ((hNew = alloc_icon_handle( FALSE, 1 )))
1545 struct cursoricon_frame *frameOld, *frameNew;
1547 ptrNew = get_icon_ptr( hNew );
1548 ptrNew->is_icon = ptrOld->is_icon;
1549 ptrNew->hotspot = ptrOld->hotspot;
1550 if (!(frameOld = get_icon_frame( ptrOld, 0 )))
1552 release_icon_ptr( hIcon, ptrOld );
1553 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1554 return 0;
1556 if (!(frameNew = get_icon_frame( ptrNew, 0 )))
1558 release_icon_frame( ptrOld, 0, frameOld );
1559 release_icon_ptr( hIcon, ptrOld );
1560 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1561 return 0;
1563 frameNew->delay = 0;
1564 frameNew->width = frameOld->width;
1565 frameNew->height = frameOld->height;
1566 frameNew->mask = copy_bitmap( frameOld->mask );
1567 frameNew->color = copy_bitmap( frameOld->color );
1568 frameNew->alpha = copy_bitmap( frameOld->alpha );
1569 release_icon_frame( ptrOld, 0, frameOld );
1570 release_icon_frame( ptrNew, 0, frameNew );
1571 release_icon_ptr( hNew, ptrNew );
1573 release_icon_ptr( hIcon, ptrOld );
1574 if (hNew) USER_Driver->pCreateCursorIcon( hNew );
1575 return hNew;
1579 /***********************************************************************
1580 * DestroyIcon (USER32.@)
1582 BOOL WINAPI DestroyIcon( HICON hIcon )
1584 BOOL ret = FALSE;
1585 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1587 TRACE_(icon)("%p\n", hIcon );
1589 if (obj)
1591 BOOL shared = (obj->rsrc != NULL);
1592 release_icon_ptr( hIcon, obj );
1593 ret = (GetCursor() != hIcon);
1594 if (!shared) free_icon_handle( hIcon );
1596 return ret;
1600 /***********************************************************************
1601 * DestroyCursor (USER32.@)
1603 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1605 return DestroyIcon( hCursor );
1608 /***********************************************************************
1609 * DrawIcon (USER32.@)
1611 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1613 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1616 /***********************************************************************
1617 * SetCursor (USER32.@)
1619 * Set the cursor shape.
1621 * RETURNS
1622 * A handle to the previous cursor shape.
1624 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1626 struct cursoricon_object *obj;
1627 HCURSOR hOldCursor;
1628 int show_count;
1629 BOOL ret;
1631 TRACE("%p\n", hCursor);
1633 SERVER_START_REQ( set_cursor )
1635 req->flags = SET_CURSOR_HANDLE;
1636 req->handle = wine_server_user_handle( hCursor );
1637 if ((ret = !wine_server_call_err( req )))
1639 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1640 show_count = reply->prev_count;
1643 SERVER_END_REQ;
1645 if (!ret) return 0;
1646 USER_Driver->pSetCursor( show_count >= 0 ? hCursor : 0 );
1648 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1649 release_icon_ptr( hOldCursor, obj );
1650 return hOldCursor;
1653 /***********************************************************************
1654 * ShowCursor (USER32.@)
1656 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1658 HCURSOR cursor;
1659 int increment = bShow ? 1 : -1;
1660 int count;
1662 SERVER_START_REQ( set_cursor )
1664 req->flags = SET_CURSOR_COUNT;
1665 req->show_count = increment;
1666 wine_server_call( req );
1667 cursor = wine_server_ptr_handle( reply->prev_handle );
1668 count = reply->prev_count + increment;
1670 SERVER_END_REQ;
1672 TRACE("%d, count=%d\n", bShow, count );
1674 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1675 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1677 return count;
1680 /***********************************************************************
1681 * GetCursor (USER32.@)
1683 HCURSOR WINAPI GetCursor(void)
1685 HCURSOR ret;
1687 SERVER_START_REQ( set_cursor )
1689 req->flags = 0;
1690 wine_server_call( req );
1691 ret = wine_server_ptr_handle( reply->prev_handle );
1693 SERVER_END_REQ;
1694 return ret;
1698 /***********************************************************************
1699 * ClipCursor (USER32.@)
1701 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1703 BOOL ret;
1704 RECT new_rect;
1706 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1708 if (rect && (rect->left > rect->right || rect->top > rect->bottom)) return FALSE;
1710 SERVER_START_REQ( set_cursor )
1712 req->clip_msg = WM_WINE_CLIPCURSOR;
1713 if (rect)
1715 req->flags = SET_CURSOR_CLIP;
1716 req->clip.left = rect->left;
1717 req->clip.top = rect->top;
1718 req->clip.right = rect->right;
1719 req->clip.bottom = rect->bottom;
1721 else req->flags = SET_CURSOR_NOCLIP;
1723 if ((ret = !wine_server_call( req )))
1725 new_rect.left = reply->new_clip.left;
1726 new_rect.top = reply->new_clip.top;
1727 new_rect.right = reply->new_clip.right;
1728 new_rect.bottom = reply->new_clip.bottom;
1731 SERVER_END_REQ;
1732 if (ret) USER_Driver->pClipCursor( &new_rect );
1733 return ret;
1737 /***********************************************************************
1738 * GetClipCursor (USER32.@)
1740 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1742 BOOL ret;
1744 if (!rect) return FALSE;
1746 SERVER_START_REQ( set_cursor )
1748 req->flags = 0;
1749 if ((ret = !wine_server_call( req )))
1751 rect->left = reply->new_clip.left;
1752 rect->top = reply->new_clip.top;
1753 rect->right = reply->new_clip.right;
1754 rect->bottom = reply->new_clip.bottom;
1757 SERVER_END_REQ;
1758 return ret;
1762 /***********************************************************************
1763 * SetSystemCursor (USER32.@)
1765 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1767 FIXME("(%p,%08x),stub!\n", hcur, id);
1768 return TRUE;
1772 /**********************************************************************
1773 * LookupIconIdFromDirectoryEx (USER32.@)
1775 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1776 INT width, INT height, UINT cFlag )
1778 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1779 UINT retVal = 0;
1780 if( dir && !dir->idReserved && (dir->idType & 3) )
1782 const CURSORICONDIRENTRY* entry;
1784 const HDC hdc = GetDC(0);
1785 const int depth = (cFlag & LR_MONOCHROME) ?
1786 1 : GetDeviceCaps(hdc, BITSPIXEL);
1787 ReleaseDC(0, hdc);
1789 if( bIcon )
1790 entry = CURSORICON_FindBestIconRes( dir, width, height, depth, LR_DEFAULTSIZE );
1791 else
1792 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth, LR_DEFAULTSIZE );
1794 if( entry ) retVal = entry->wResId;
1796 else WARN_(cursor)("invalid resource directory\n");
1797 return retVal;
1800 /**********************************************************************
1801 * LookupIconIdFromDirectory (USER32.@)
1803 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1805 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1808 /***********************************************************************
1809 * LoadCursorW (USER32.@)
1811 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1813 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1815 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1816 LR_SHARED | LR_DEFAULTSIZE );
1819 /***********************************************************************
1820 * LoadCursorA (USER32.@)
1822 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1824 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1826 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1827 LR_SHARED | LR_DEFAULTSIZE );
1830 /***********************************************************************
1831 * LoadCursorFromFileW (USER32.@)
1833 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1835 TRACE("%s\n", debugstr_w(name));
1837 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1838 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1841 /***********************************************************************
1842 * LoadCursorFromFileA (USER32.@)
1844 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1846 TRACE("%s\n", debugstr_a(name));
1848 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1849 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1852 /***********************************************************************
1853 * LoadIconW (USER32.@)
1855 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1857 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1859 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1860 LR_SHARED | LR_DEFAULTSIZE );
1863 /***********************************************************************
1864 * LoadIconA (USER32.@)
1866 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1868 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1870 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1871 LR_SHARED | LR_DEFAULTSIZE );
1874 /**********************************************************************
1875 * GetCursorFrameInfo (USER32.@)
1877 * NOTES
1878 * So far no use has been found for the second parameter, it is currently presumed
1879 * that this parameter is reserved for future use.
1881 * PARAMS
1882 * hCursor [I] Handle to cursor for which to retrieve information
1883 * reserved [I] No purpose has been found for this parameter (may be NULL)
1884 * istep [I] The step of the cursor for which to retrieve information
1885 * rate_jiffies [O] Pointer to DWORD that receives the frame-specific delay (cannot be NULL)
1886 * num_steps [O] Pointer to DWORD that receives the number of steps in the cursor (cannot be NULL)
1888 * RETURNS
1889 * Success: Handle to a frame of the cursor (specified by istep)
1890 * Failure: NULL cursor (0)
1892 HCURSOR WINAPI GetCursorFrameInfo(HCURSOR hCursor, DWORD reserved, DWORD istep, DWORD *rate_jiffies, DWORD *num_steps)
1894 struct cursoricon_object *ptr;
1895 HCURSOR ret = 0;
1896 UINT icon_steps;
1898 if (rate_jiffies == NULL || num_steps == NULL) return 0;
1900 if (!(ptr = get_icon_ptr( hCursor ))) return 0;
1902 TRACE("%p => %d %d %p %p\n", hCursor, reserved, istep, rate_jiffies, num_steps);
1903 if (reserved != 0)
1904 FIXME("Second parameter non-zero (%d), please report this!\n", reserved);
1906 icon_steps = get_icon_steps(ptr);
1907 if (istep < icon_steps || !ptr->is_ani)
1909 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) ptr;
1910 UINT icon_frames = 1;
1912 if (ptr->is_ani)
1913 icon_frames = ani_icon_data->num_frames;
1914 if (ptr->is_ani && icon_frames > 1)
1915 ret = ani_icon_data->frames[istep];
1916 else
1917 ret = hCursor;
1918 if (icon_frames == 1)
1920 *rate_jiffies = 0;
1921 *num_steps = 1;
1923 else if (icon_steps == 1)
1925 *num_steps = ~0;
1926 *rate_jiffies = ptr->delay;
1928 else if (istep < icon_steps)
1930 struct cursoricon_frame *frame;
1932 *num_steps = icon_steps;
1933 frame = get_icon_frame( ptr, istep );
1934 if (get_icon_steps(ptr) == 1)
1935 *num_steps = ~0;
1936 else
1937 *num_steps = get_icon_steps(ptr);
1938 /* If this specific frame does not have a delay then use the global delay */
1939 if (frame->delay == ~0)
1940 *rate_jiffies = ptr->delay;
1941 else
1942 *rate_jiffies = frame->delay;
1943 release_icon_frame( ptr, istep, frame );
1947 release_icon_ptr( hCursor, ptr );
1949 return ret;
1952 /**********************************************************************
1953 * GetIconInfo (USER32.@)
1955 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1957 ICONINFOEXW infoW;
1959 infoW.cbSize = sizeof(infoW);
1960 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
1961 iconinfo->fIcon = infoW.fIcon;
1962 iconinfo->xHotspot = infoW.xHotspot;
1963 iconinfo->yHotspot = infoW.yHotspot;
1964 iconinfo->hbmColor = infoW.hbmColor;
1965 iconinfo->hbmMask = infoW.hbmMask;
1966 return TRUE;
1969 /**********************************************************************
1970 * GetIconInfoExA (USER32.@)
1972 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
1974 ICONINFOEXW infoW;
1976 if (info->cbSize != sizeof(*info))
1978 SetLastError( ERROR_INVALID_PARAMETER );
1979 return FALSE;
1981 infoW.cbSize = sizeof(infoW);
1982 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
1983 info->fIcon = infoW.fIcon;
1984 info->xHotspot = infoW.xHotspot;
1985 info->yHotspot = infoW.yHotspot;
1986 info->hbmColor = infoW.hbmColor;
1987 info->hbmMask = infoW.hbmMask;
1988 info->wResID = infoW.wResID;
1989 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
1990 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
1991 return TRUE;
1994 /**********************************************************************
1995 * GetIconInfoExW (USER32.@)
1997 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
1999 struct cursoricon_frame *frame;
2000 struct cursoricon_object *ptr;
2001 HMODULE module;
2002 BOOL ret = TRUE;
2004 if (info->cbSize != sizeof(*info))
2006 SetLastError( ERROR_INVALID_PARAMETER );
2007 return FALSE;
2009 if (!(ptr = get_icon_ptr( icon )))
2011 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2012 return FALSE;
2015 frame = get_icon_frame( ptr, 0 );
2016 if (!frame)
2018 release_icon_ptr( icon, ptr );
2019 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2020 return FALSE;
2023 TRACE("%p => %dx%d\n", icon, frame->width, frame->height);
2025 info->fIcon = ptr->is_icon;
2026 info->xHotspot = ptr->hotspot.x;
2027 info->yHotspot = ptr->hotspot.y;
2028 info->hbmColor = copy_bitmap( frame->color );
2029 info->hbmMask = copy_bitmap( frame->mask );
2030 info->wResID = 0;
2031 info->szModName[0] = 0;
2032 info->szResName[0] = 0;
2033 if (ptr->module)
2035 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
2036 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
2038 if (!info->hbmMask || (!info->hbmColor && frame->color))
2040 DeleteObject( info->hbmMask );
2041 DeleteObject( info->hbmColor );
2042 ret = FALSE;
2044 module = ptr->module;
2045 release_icon_frame( ptr, 0, frame );
2046 release_icon_ptr( icon, ptr );
2047 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
2048 return ret;
2051 /* copy an icon bitmap, even when it can't be selected into a DC */
2052 /* helper for CreateIconIndirect */
2053 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
2054 HBITMAP src, int width, int height )
2056 HDC hdc = CreateCompatibleDC( 0 );
2058 if (!SelectObject( hdc, src )) /* do it the hard way */
2060 BITMAPINFO *info;
2061 void *bits;
2063 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
2064 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2065 info->bmiHeader.biWidth = width;
2066 info->bmiHeader.biHeight = height;
2067 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
2068 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
2069 info->bmiHeader.biCompression = BI_RGB;
2070 info->bmiHeader.biSizeImage = get_dib_image_size( width, height, info->bmiHeader.biBitCount );
2071 info->bmiHeader.biXPelsPerMeter = 0;
2072 info->bmiHeader.biYPelsPerMeter = 0;
2073 info->bmiHeader.biClrUsed = 0;
2074 info->bmiHeader.biClrImportant = 0;
2075 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
2076 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
2077 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
2078 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
2080 HeapFree( GetProcessHeap(), 0, bits );
2081 HeapFree( GetProcessHeap(), 0, info );
2083 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
2085 DeleteDC( hdc );
2088 /**********************************************************************
2089 * CreateIconIndirect (USER32.@)
2091 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2093 BITMAP bmpXor, bmpAnd;
2094 HICON hObj;
2095 HBITMAP color = 0, mask;
2096 int width, height;
2097 HDC hdc;
2099 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2100 iconinfo->hbmColor, iconinfo->hbmMask,
2101 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2103 if (!iconinfo->hbmMask) return 0;
2105 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2106 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2107 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2108 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2110 if (iconinfo->hbmColor)
2112 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2113 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2114 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2115 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2117 width = bmpXor.bmWidth;
2118 height = bmpXor.bmHeight;
2119 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1 || bmpAnd.bmPlanes * bmpAnd.bmBitsPixel != 1)
2121 color = CreateCompatibleBitmap( screen_dc, width, height );
2122 mask = CreateBitmap( width, height, 1, 1, NULL );
2124 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2126 else
2128 width = bmpAnd.bmWidth;
2129 height = bmpAnd.bmHeight;
2130 mask = CreateBitmap( width, height, 1, 1, NULL );
2133 hdc = CreateCompatibleDC( 0 );
2134 SelectObject( hdc, mask );
2135 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
2137 if (color)
2139 SelectObject( hdc, color );
2140 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
2142 else if (iconinfo->hbmColor)
2144 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
2146 else height /= 2;
2148 DeleteDC( hdc );
2150 hObj = alloc_icon_handle( FALSE, 1 );
2151 if (hObj)
2153 struct cursoricon_object *info = get_icon_ptr( hObj );
2154 struct cursoricon_frame *frame;
2156 info->is_icon = iconinfo->fIcon;
2157 frame = get_icon_frame( info, 0 );
2158 frame->delay = ~0;
2159 frame->width = width;
2160 frame->height = height;
2161 frame->color = color;
2162 frame->mask = mask;
2163 frame->alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
2164 release_icon_frame( info, 0, frame );
2165 if (info->is_icon)
2167 info->hotspot.x = width / 2;
2168 info->hotspot.y = height / 2;
2170 else
2172 info->hotspot.x = iconinfo->xHotspot;
2173 info->hotspot.y = iconinfo->yHotspot;
2176 release_icon_ptr( hObj, info );
2177 USER_Driver->pCreateCursorIcon( hObj );
2179 return hObj;
2182 /******************************************************************************
2183 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2185 * NOTES
2186 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2188 * PARAMS
2189 * hdc [I] Handle to device context
2190 * x0 [I] X coordinate of upper left corner
2191 * y0 [I] Y coordinate of upper left corner
2192 * hIcon [I] Handle to icon to draw
2193 * cxWidth [I] Width of icon
2194 * cyWidth [I] Height of icon
2195 * istep [I] Index of frame in animated cursor
2196 * hbr [I] Handle to background brush
2197 * flags [I] Icon-drawing flags
2199 * RETURNS
2200 * Success: TRUE
2201 * Failure: FALSE
2203 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2204 INT cxWidth, INT cyWidth, UINT istep,
2205 HBRUSH hbr, UINT flags )
2207 struct cursoricon_frame *frame;
2208 struct cursoricon_object *ptr;
2209 HDC hdc_dest, hMemDC;
2210 BOOL result = FALSE, DoOffscreen;
2211 HBITMAP hB_off = 0;
2212 COLORREF oldFg, oldBg;
2213 INT x, y, nStretchMode;
2215 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2216 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2218 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2219 if (istep >= get_icon_steps( ptr ))
2221 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
2222 release_icon_ptr( hIcon, ptr );
2223 return FALSE;
2225 if (!(frame = get_icon_frame( ptr, istep )))
2227 FIXME_(icon)("Error retrieving icon frame %d\n", istep);
2228 release_icon_ptr( hIcon, ptr );
2229 return FALSE;
2231 if (!(hMemDC = CreateCompatibleDC( hdc )))
2233 release_icon_frame( ptr, istep, frame );
2234 release_icon_ptr( hIcon, ptr );
2235 return FALSE;
2238 if (flags & DI_NOMIRROR)
2239 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2241 /* Calculate the size of the destination image. */
2242 if (cxWidth == 0)
2244 if (flags & DI_DEFAULTSIZE)
2245 cxWidth = GetSystemMetrics (SM_CXICON);
2246 else
2247 cxWidth = frame->width;
2249 if (cyWidth == 0)
2251 if (flags & DI_DEFAULTSIZE)
2252 cyWidth = GetSystemMetrics (SM_CYICON);
2253 else
2254 cyWidth = frame->height;
2257 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2259 if (DoOffscreen) {
2260 RECT r;
2262 r.left = 0;
2263 r.top = 0;
2264 r.right = cxWidth;
2265 r.bottom = cxWidth;
2267 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2268 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2270 DeleteDC( hdc_dest );
2271 goto failed;
2273 SelectObject(hdc_dest, hB_off);
2274 FillRect(hdc_dest, &r, hbr);
2275 x = y = 0;
2277 else
2279 hdc_dest = hdc;
2280 x = x0;
2281 y = y0;
2284 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2286 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2287 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2289 if (frame->alpha && (flags & DI_IMAGE))
2291 BOOL alpha_blend = TRUE;
2293 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2295 BITMAP bm;
2296 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2297 alpha_blend = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel > 8;
2299 if (alpha_blend)
2301 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2302 SelectObject( hMemDC, frame->alpha );
2303 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2304 0, 0, frame->width, frame->height,
2305 pixelblend )) goto done;
2309 if (flags & DI_MASK)
2311 SelectObject( hMemDC, frame->mask );
2312 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2313 hMemDC, 0, 0, frame->width, frame->height, SRCAND );
2316 if (flags & DI_IMAGE)
2318 if (frame->color)
2320 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2321 SelectObject( hMemDC, frame->color );
2322 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2323 hMemDC, 0, 0, frame->width, frame->height, rop );
2325 else
2327 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2328 SelectObject( hMemDC, frame->mask );
2329 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2330 hMemDC, 0, frame->height, frame->width,
2331 frame->height, rop );
2335 done:
2336 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2338 SetTextColor( hdc, oldFg );
2339 SetBkColor( hdc, oldBg );
2340 SetStretchBltMode (hdc, nStretchMode);
2341 result = TRUE;
2342 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2343 if (hB_off) DeleteObject(hB_off);
2344 failed:
2345 DeleteDC( hMemDC );
2346 release_icon_frame( ptr, istep, frame );
2347 release_icon_ptr( hIcon, ptr );
2348 return result;
2351 /***********************************************************************
2352 * DIB_FixColorsToLoadflags
2354 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2355 * are in loadflags
2357 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2359 int colors;
2360 COLORREF c_W, c_S, c_F, c_L, c_C;
2361 int incr,i;
2362 RGBQUAD *ptr;
2363 int bitmap_type;
2364 LONG width;
2365 LONG height;
2366 WORD bpp;
2367 DWORD compr;
2369 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2371 WARN_(resource)("Invalid bitmap\n");
2372 return;
2375 if (bpp > 8) return;
2377 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2379 incr = 3;
2380 colors = 1 << bpp;
2382 else
2384 incr = 4;
2385 colors = bmi->bmiHeader.biClrUsed;
2386 if (colors > 256) colors = 256;
2387 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2390 c_W = GetSysColor(COLOR_WINDOW);
2391 c_S = GetSysColor(COLOR_3DSHADOW);
2392 c_F = GetSysColor(COLOR_3DFACE);
2393 c_L = GetSysColor(COLOR_3DLIGHT);
2395 if (loadflags & LR_LOADTRANSPARENT) {
2396 switch (bpp) {
2397 case 1: pix = pix >> 7; break;
2398 case 4: pix = pix >> 4; break;
2399 case 8: break;
2400 default:
2401 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2402 return;
2404 if (pix >= colors) {
2405 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2406 return;
2408 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2409 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2410 ptr->rgbBlue = GetBValue(c_W);
2411 ptr->rgbGreen = GetGValue(c_W);
2412 ptr->rgbRed = GetRValue(c_W);
2414 if (loadflags & LR_LOADMAP3DCOLORS)
2415 for (i=0; i<colors; i++) {
2416 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2417 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2418 if (c_C == RGB(128, 128, 128)) {
2419 ptr->rgbRed = GetRValue(c_S);
2420 ptr->rgbGreen = GetGValue(c_S);
2421 ptr->rgbBlue = GetBValue(c_S);
2422 } else if (c_C == RGB(192, 192, 192)) {
2423 ptr->rgbRed = GetRValue(c_F);
2424 ptr->rgbGreen = GetGValue(c_F);
2425 ptr->rgbBlue = GetBValue(c_F);
2426 } else if (c_C == RGB(223, 223, 223)) {
2427 ptr->rgbRed = GetRValue(c_L);
2428 ptr->rgbGreen = GetGValue(c_L);
2429 ptr->rgbBlue = GetBValue(c_L);
2435 /**********************************************************************
2436 * BITMAP_Load
2438 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2439 INT desiredx, INT desiredy, UINT loadflags )
2441 HBITMAP hbitmap = 0, orig_bm;
2442 HRSRC hRsrc;
2443 HGLOBAL handle;
2444 char *ptr = NULL;
2445 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2446 int size;
2447 BYTE pix;
2448 char *bits;
2449 LONG width, height, new_width, new_height;
2450 WORD bpp_dummy;
2451 DWORD compr_dummy, offbits = 0;
2452 INT bm_type;
2453 HDC screen_mem_dc = NULL;
2455 if (!(loadflags & LR_LOADFROMFILE))
2457 if (!instance)
2459 /* OEM bitmap: try to load the resource from user32.dll */
2460 instance = user32_module;
2463 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2464 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2466 if ((info = LockResource( handle )) == NULL) return 0;
2468 else
2470 BITMAPFILEHEADER * bmfh;
2472 if (!(ptr = map_fileW( name, NULL ))) return 0;
2473 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2474 bmfh = (BITMAPFILEHEADER *)ptr;
2475 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2477 WARN("Invalid/unsupported bitmap format!\n");
2478 goto end;
2480 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2483 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2484 &bpp_dummy, &compr_dummy);
2485 if (bm_type == -1)
2487 WARN("Invalid bitmap format!\n");
2488 goto end;
2491 size = bitmap_info_size(info, DIB_RGB_COLORS);
2492 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2493 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2495 if (!fix_info || !scaled_info) goto end;
2496 memcpy(fix_info, info, size);
2498 pix = *((LPBYTE)info + size);
2499 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2501 memcpy(scaled_info, fix_info, size);
2503 if(desiredx != 0)
2504 new_width = desiredx;
2505 else
2506 new_width = width;
2508 if(desiredy != 0)
2509 new_height = height > 0 ? desiredy : -desiredy;
2510 else
2511 new_height = height;
2513 if(bm_type == 0)
2515 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2516 core->bcWidth = new_width;
2517 core->bcHeight = new_height;
2519 else
2521 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2522 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2523 WARN("Broken BitmapInfoHeader!\n");
2524 goto end;
2527 scaled_info->bmiHeader.biWidth = new_width;
2528 scaled_info->bmiHeader.biHeight = new_height;
2531 if (new_height < 0) new_height = -new_height;
2533 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2534 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2536 bits = (char *)info + (offbits ? offbits : size);
2538 if (loadflags & LR_CREATEDIBSECTION)
2540 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2541 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2543 else
2545 if (is_dib_monochrome(fix_info))
2546 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2547 else
2548 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2551 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2552 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2553 SelectObject(screen_mem_dc, orig_bm);
2555 end:
2556 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2557 HeapFree(GetProcessHeap(), 0, scaled_info);
2558 HeapFree(GetProcessHeap(), 0, fix_info);
2559 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2561 return hbitmap;
2564 /**********************************************************************
2565 * LoadImageA (USER32.@)
2567 * See LoadImageW.
2569 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2570 INT desiredx, INT desiredy, UINT loadflags)
2572 HANDLE res;
2573 LPWSTR u_name;
2575 if (IS_INTRESOURCE(name))
2576 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2578 __TRY {
2579 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2580 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2581 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2583 __EXCEPT_PAGE_FAULT {
2584 SetLastError( ERROR_INVALID_PARAMETER );
2585 return 0;
2587 __ENDTRY
2588 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2589 HeapFree(GetProcessHeap(), 0, u_name);
2590 return res;
2594 /******************************************************************************
2595 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2597 * PARAMS
2598 * hinst [I] Handle of instance that contains image
2599 * name [I] Name of image
2600 * type [I] Type of image
2601 * desiredx [I] Desired width
2602 * desiredy [I] Desired height
2603 * loadflags [I] Load flags
2605 * RETURNS
2606 * Success: Handle to newly loaded image
2607 * Failure: NULL
2609 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2611 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2612 INT desiredx, INT desiredy, UINT loadflags )
2614 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2615 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2617 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2618 switch (type) {
2619 case IMAGE_BITMAP:
2620 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2622 case IMAGE_ICON:
2623 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2624 if (screen_dc)
2626 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2627 GetDeviceCaps(screen_dc, BITSPIXEL),
2628 FALSE, loadflags);
2630 break;
2632 case IMAGE_CURSOR:
2633 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2634 1, TRUE, loadflags);
2636 return 0;
2639 /******************************************************************************
2640 * CopyImage (USER32.@) Creates new image and copies attributes to it
2642 * PARAMS
2643 * hnd [I] Handle to image to copy
2644 * type [I] Type of image to copy
2645 * desiredx [I] Desired width of new image
2646 * desiredy [I] Desired height of new image
2647 * flags [I] Copy flags
2649 * RETURNS
2650 * Success: Handle to newly created image
2651 * Failure: NULL
2653 * BUGS
2654 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2655 * all other versions (95/2000/XP have been tested) ignore it.
2657 * NOTES
2658 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2659 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2660 * the copy will have the same depth as the screen.
2661 * The content of the image will only be copied if the bit depth of the
2662 * original image is compatible with the bit depth of the screen, or
2663 * if the source is a DIB section.
2664 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2666 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2667 INT desiredy, UINT flags )
2669 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2670 hnd, type, desiredx, desiredy, flags);
2672 switch (type)
2674 case IMAGE_BITMAP:
2676 HBITMAP res = NULL;
2677 DIBSECTION ds;
2678 int objSize;
2679 BITMAPINFO * bi;
2681 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2682 if (!objSize) return 0;
2683 if ((desiredx < 0) || (desiredy < 0)) return 0;
2685 if (flags & LR_COPYFROMRESOURCE)
2687 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2690 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2691 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2693 /* Allocate memory for a BITMAPINFOHEADER structure and a
2694 color table. The maximum number of colors in a color table
2695 is 256 which corresponds to a bitmap with depth 8.
2696 Bitmaps with higher depths don't have color tables. */
2697 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2698 if (!bi) return 0;
2700 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2701 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2702 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2703 bi->bmiHeader.biCompression = BI_RGB;
2705 if (flags & LR_CREATEDIBSECTION)
2707 /* Create a DIB section. LR_MONOCHROME is ignored */
2708 void * bits;
2709 HDC dc = CreateCompatibleDC(NULL);
2711 if (objSize == sizeof(DIBSECTION))
2713 /* The source bitmap is a DIB.
2714 Get its attributes to create an exact copy */
2715 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2718 bi->bmiHeader.biWidth = desiredx;
2719 bi->bmiHeader.biHeight = desiredy;
2721 /* Get the color table or the color masks */
2722 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2724 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2725 DeleteDC(dc);
2727 else
2729 /* Create a device-dependent bitmap */
2731 BOOL monochrome = (flags & LR_MONOCHROME);
2733 if (objSize == sizeof(DIBSECTION))
2735 /* The source bitmap is a DIB section.
2736 Get its attributes */
2737 HDC dc = CreateCompatibleDC(NULL);
2738 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2739 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2740 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2741 DeleteDC(dc);
2743 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2745 /* Look if the colors of the DIB are black and white */
2747 monochrome =
2748 (bi->bmiColors[0].rgbRed == 0xff
2749 && bi->bmiColors[0].rgbGreen == 0xff
2750 && bi->bmiColors[0].rgbBlue == 0xff
2751 && bi->bmiColors[0].rgbReserved == 0
2752 && bi->bmiColors[1].rgbRed == 0
2753 && bi->bmiColors[1].rgbGreen == 0
2754 && bi->bmiColors[1].rgbBlue == 0
2755 && bi->bmiColors[1].rgbReserved == 0)
2757 (bi->bmiColors[0].rgbRed == 0
2758 && bi->bmiColors[0].rgbGreen == 0
2759 && bi->bmiColors[0].rgbBlue == 0
2760 && bi->bmiColors[0].rgbReserved == 0
2761 && bi->bmiColors[1].rgbRed == 0xff
2762 && bi->bmiColors[1].rgbGreen == 0xff
2763 && bi->bmiColors[1].rgbBlue == 0xff
2764 && bi->bmiColors[1].rgbReserved == 0);
2767 else if (!monochrome)
2769 monochrome = ds.dsBm.bmBitsPixel == 1;
2772 if (monochrome)
2774 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2776 else
2778 HDC screenDC = GetDC(NULL);
2779 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2780 ReleaseDC(NULL, screenDC);
2784 if (res)
2786 /* Only copy the bitmap if it's a DIB section or if it's
2787 compatible to the screen */
2788 BOOL copyContents;
2790 if (objSize == sizeof(DIBSECTION))
2792 copyContents = TRUE;
2794 else
2796 HDC screenDC = GetDC(NULL);
2797 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2798 ReleaseDC(NULL, screenDC);
2800 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2803 if (copyContents)
2805 /* The source bitmap may already be selected in a device context,
2806 use GetDIBits/StretchDIBits and not StretchBlt */
2808 HDC dc;
2809 void * bits;
2811 dc = CreateCompatibleDC(NULL);
2813 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2814 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2815 bi->bmiHeader.biSizeImage = 0;
2816 bi->bmiHeader.biClrUsed = 0;
2817 bi->bmiHeader.biClrImportant = 0;
2819 /* Fill in biSizeImage */
2820 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2821 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2823 if (bits)
2825 HBITMAP oldBmp;
2827 /* Get the image bits of the source bitmap */
2828 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2830 /* Copy it to the destination bitmap */
2831 oldBmp = SelectObject(dc, res);
2832 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2833 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2834 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2835 SelectObject(dc, oldBmp);
2837 HeapFree(GetProcessHeap(), 0, bits);
2840 DeleteDC(dc);
2843 if (flags & LR_COPYDELETEORG)
2845 DeleteObject(hnd);
2848 HeapFree(GetProcessHeap(), 0, bi);
2849 return res;
2851 case IMAGE_ICON:
2852 case IMAGE_CURSOR:
2854 struct cursoricon_object *icon;
2855 HICON res = 0;
2856 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( screen_dc, BITSPIXEL );
2858 if (flags & LR_DEFAULTSIZE)
2860 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2861 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2864 if (!(icon = get_icon_ptr( hnd ))) return 0;
2866 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2867 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2868 type == IMAGE_CURSOR, flags );
2869 else
2870 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2871 release_icon_ptr( hnd, icon );
2873 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2874 return res;
2877 return 0;
2881 /******************************************************************************
2882 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2884 * RETURNS
2885 * Success: Handle to specified bitmap
2886 * Failure: NULL
2888 HBITMAP WINAPI LoadBitmapW(
2889 HINSTANCE instance, /* [in] Handle to application instance */
2890 LPCWSTR name) /* [in] Address of bitmap resource name */
2892 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2895 /**********************************************************************
2896 * LoadBitmapA (USER32.@)
2898 * See LoadBitmapW.
2900 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2902 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );