user32: Add support for retrieving individual animated frames.
[wine/multimedia.git] / dlls / user32 / cursoricon.c
blob96027d5cfcf05ef350582a0b5129ec5848a7713e
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_frames )
124 struct cursoricon_object *obj;
125 int icon_size;
127 if (is_ani)
128 icon_size = FIELD_OFFSET( struct animated_cursoricon_object, frames[num_frames] );
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_frames; /* changed later for some animated cursors */
140 ani_icon_data->num_frames = num_frames;
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_frames; i++)
232 free_icon_handle( ani_icon_data->frames[i] );
234 if (!IS_INTRESOURCE( obj->resname )) HeapFree( GetProcessHeap(), 0, obj->resname );
235 HeapFree( GetProcessHeap(), 0, obj );
236 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
237 USER_Driver->pDestroyCursorIcon( handle );
238 return TRUE;
240 return FALSE;
243 ULONG_PTR get_icon_param( HICON handle )
245 ULONG_PTR ret = 0;
246 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
248 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
249 else if (obj)
251 ret = obj->param;
252 release_user_handle_ptr( obj );
254 return ret;
257 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
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 obj->param = param;
267 release_user_handle_ptr( obj );
269 return ret;
273 /***********************************************************************
274 * map_fileW
276 * Helper function to map a file to memory:
277 * name - file name
278 * [RETURN] ptr - pointer to mapped file
279 * [RETURN] filesize - pointer size of file to be stored if not NULL
281 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
283 HANDLE hFile, hMapping;
284 LPVOID ptr = NULL;
286 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
287 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
288 if (hFile != INVALID_HANDLE_VALUE)
290 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
291 if (hMapping)
293 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
294 CloseHandle( hMapping );
295 if (filesize)
296 *filesize = GetFileSize( hFile, NULL );
298 CloseHandle( hFile );
300 return ptr;
304 /***********************************************************************
305 * get_dib_width_bytes
307 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
309 static int get_dib_width_bytes( int width, int depth )
311 int words;
313 switch(depth)
315 case 1: words = (width + 31) / 32; break;
316 case 4: words = (width + 7) / 8; break;
317 case 8: words = (width + 3) / 4; break;
318 case 15:
319 case 16: words = (width + 1) / 2; break;
320 case 24: words = (width * 3 + 3)/4; break;
321 default:
322 WARN("(%d): Unsupported depth\n", depth );
323 /* fall through */
324 case 32:
325 words = width;
327 return 4 * words;
331 /***********************************************************************
332 * bitmap_info_size
334 * Return the size of the bitmap info structure including color table.
336 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
338 unsigned int colors, size, masks = 0;
340 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
342 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
343 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
344 return sizeof(BITMAPCOREHEADER) + colors *
345 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
347 else /* assume BITMAPINFOHEADER */
349 colors = info->bmiHeader.biClrUsed;
350 if (colors > 256) /* buffer overflow otherwise */
351 colors = 256;
352 if (!colors && (info->bmiHeader.biBitCount <= 8))
353 colors = 1 << info->bmiHeader.biBitCount;
354 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
355 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
356 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
361 /***********************************************************************
362 * copy_bitmap
364 * Helper function to duplicate a bitmap.
366 static HBITMAP copy_bitmap( HBITMAP bitmap )
368 HDC src, dst = 0;
369 HBITMAP new_bitmap = 0;
370 BITMAP bmp;
372 if (!bitmap) return 0;
373 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
375 if ((src = CreateCompatibleDC( 0 )) && (dst = CreateCompatibleDC( 0 )))
377 SelectObject( src, bitmap );
378 if ((new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight )))
380 SelectObject( dst, new_bitmap );
381 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
384 DeleteDC( dst );
385 DeleteDC( src );
386 return new_bitmap;
390 /***********************************************************************
391 * is_dib_monochrome
393 * Returns whether a DIB can be converted to a monochrome DDB.
395 * A DIB can be converted if its color table contains only black and
396 * white. Black must be the first color in the color table.
398 * Note : If the first color in the color table is white followed by
399 * black, we can't convert it to a monochrome DDB with
400 * SetDIBits, because black and white would be inverted.
402 static BOOL is_dib_monochrome( const BITMAPINFO* info )
404 if (info->bmiHeader.biBitCount != 1) return FALSE;
406 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
408 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
410 /* Check if the first color is black */
411 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
413 rgb++;
415 /* Check if the second color is white */
416 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
417 && (rgb->rgbtBlue == 0xff));
419 else return FALSE;
421 else /* assume BITMAPINFOHEADER */
423 const RGBQUAD *rgb = info->bmiColors;
425 /* Check if the first color is black */
426 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
427 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
429 rgb++;
431 /* Check if the second color is white */
432 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
433 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
435 else return FALSE;
439 /***********************************************************************
440 * DIB_GetBitmapInfo
442 * Get the info from a bitmap header.
443 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
445 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
446 LONG *height, WORD *bpp, DWORD *compr )
448 if (header->biSize == sizeof(BITMAPCOREHEADER))
450 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
451 *width = core->bcWidth;
452 *height = core->bcHeight;
453 *bpp = core->bcBitCount;
454 *compr = 0;
455 return 0;
457 else if (header->biSize == sizeof(BITMAPINFOHEADER) ||
458 header->biSize == sizeof(BITMAPV4HEADER) ||
459 header->biSize == sizeof(BITMAPV5HEADER))
461 *width = header->biWidth;
462 *height = header->biHeight;
463 *bpp = header->biBitCount;
464 *compr = header->biCompression;
465 return 1;
467 WARN("unknown/wrong size (%u) for header\n", header->biSize);
468 return -1;
471 /**********************************************************************
472 * get_icon_size
474 BOOL get_icon_size( HICON handle, SIZE *size )
476 struct cursoricon_object *info;
477 struct cursoricon_frame *frame;
479 if (!(info = get_icon_ptr( handle ))) return FALSE;
480 frame = get_icon_frame( info, 0 );
481 size->cx = frame->width;
482 size->cy = frame->height;
483 release_icon_frame( info, 0, frame);
484 release_icon_ptr( handle, info );
485 return TRUE;
489 * The following macro functions account for the irregularities of
490 * accessing cursor and icon resources in files and resource entries.
492 typedef BOOL (*fnGetCIEntry)( LPCVOID dir, int n,
493 int *width, int *height, int *bits );
495 /**********************************************************************
496 * CURSORICON_FindBestIcon
498 * Find the icon closest to the requested size and bit depth.
500 static int CURSORICON_FindBestIcon( LPCVOID dir, fnGetCIEntry get_entry,
501 int width, int height, int depth, UINT loadflags )
503 int i, cx, cy, bits, bestEntry = -1;
504 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
505 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
507 /* Find Best Fit */
508 iTotalDiff = 0xFFFFFFFF;
509 iColorDiff = 0xFFFFFFFF;
511 if (loadflags & LR_DEFAULTSIZE)
513 if (!width) width = GetSystemMetrics( SM_CXICON );
514 if (!height) height = GetSystemMetrics( SM_CYICON );
516 else if (!width && !height)
518 /* use the size of the first entry */
519 if (!get_entry( dir, 0, &width, &height, &bits )) return -1;
520 iTotalDiff = 0;
523 for ( i = 0; iTotalDiff && get_entry( dir, i, &cx, &cy, &bits ); i++ )
525 iTempXDiff = abs(width - cx);
526 iTempYDiff = abs(height - cy);
528 if(iTotalDiff > (iTempXDiff + iTempYDiff))
530 iXDiff = iTempXDiff;
531 iYDiff = iTempYDiff;
532 iTotalDiff = iXDiff + iYDiff;
536 /* Find Best Colors for Best Fit */
537 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
539 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
541 iTempColorDiff = abs(depth - bits);
542 if(iColorDiff > iTempColorDiff)
544 bestEntry = i;
545 iColorDiff = iTempColorDiff;
550 return bestEntry;
553 static BOOL CURSORICON_GetResIconEntry( LPCVOID dir, int n,
554 int *width, int *height, int *bits )
556 const CURSORICONDIR *resdir = dir;
557 const ICONRESDIR *icon;
559 if ( resdir->idCount <= n )
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, 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, 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, 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, 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, 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 cursor = &resdir->idEntries[n].ResInfo.cursor;
636 *width = cursor->wWidth;
637 *height = cursor->wHeight;
638 *bits = resdir->idEntries[n].wBitCount;
639 return TRUE;
642 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir,
643 int width, int height, int depth,
644 UINT loadflags )
646 int n;
648 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
649 width, height, depth, loadflags );
650 if ( n < 0 )
651 return NULL;
652 return &dir->idEntries[n];
655 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir,
656 int width, int height, int depth,
657 UINT loadflags )
659 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
660 width, height, depth, loadflags );
661 if ( n < 0 )
662 return NULL;
663 return &dir->idEntries[n];
666 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, int n,
667 int *width, int *height, int *bits )
669 const CURSORICONFILEDIR *filedir = dir;
670 const CURSORICONFILEDIRENTRY *entry;
671 const BITMAPINFOHEADER *info;
673 if ( filedir->idCount <= n )
674 return FALSE;
675 entry = &filedir->idEntries[n];
676 /* FIXME: check against file size */
677 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
678 *width = entry->bWidth;
679 *height = entry->bHeight;
680 *bits = info->biBitCount;
681 return TRUE;
684 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir,
685 int width, int height, int depth,
686 UINT loadflags )
688 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
689 width, height, depth, loadflags );
690 if ( n < 0 )
691 return NULL;
692 return &dir->idEntries[n];
695 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir,
696 int width, int height, int depth,
697 UINT loadflags )
699 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
700 width, height, depth, loadflags );
701 if ( n < 0 )
702 return NULL;
703 return &dir->idEntries[n];
706 /***********************************************************************
707 * bmi_has_alpha
709 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
711 int i;
712 BOOL has_alpha = FALSE;
713 const unsigned char *ptr = bits;
715 if (info->bmiHeader.biBitCount != 32) return FALSE;
716 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
717 if ((has_alpha = (ptr[3] != 0))) break;
718 return has_alpha;
721 /***********************************************************************
722 * create_alpha_bitmap
724 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
726 static HBITMAP create_alpha_bitmap( HBITMAP color, HBITMAP mask,
727 const BITMAPINFO *src_info, const void *color_bits )
729 HBITMAP alpha = 0;
730 BITMAPINFO *info = NULL;
731 BITMAP bm;
732 HDC hdc;
733 void *bits;
734 unsigned char *ptr;
735 int i;
737 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
738 if (bm.bmBitsPixel != 32) return 0;
740 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
741 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
742 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
743 info->bmiHeader.biWidth = bm.bmWidth;
744 info->bmiHeader.biHeight = -bm.bmHeight;
745 info->bmiHeader.biPlanes = 1;
746 info->bmiHeader.biBitCount = 32;
747 info->bmiHeader.biCompression = BI_RGB;
748 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
749 info->bmiHeader.biXPelsPerMeter = 0;
750 info->bmiHeader.biYPelsPerMeter = 0;
751 info->bmiHeader.biClrUsed = 0;
752 info->bmiHeader.biClrImportant = 0;
753 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
755 if (src_info)
757 SelectObject( hdc, alpha );
758 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
759 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
760 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
763 else
765 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
766 if (!bmi_has_alpha( info, bits ))
768 DeleteObject( alpha );
769 alpha = 0;
770 goto done;
774 /* pre-multiply by alpha */
775 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
777 unsigned int alpha = ptr[3];
778 ptr[0] = ptr[0] * alpha / 255;
779 ptr[1] = ptr[1] * alpha / 255;
780 ptr[2] = ptr[2] * alpha / 255;
783 done:
784 DeleteDC( hdc );
785 HeapFree( GetProcessHeap(), 0, info );
786 return alpha;
790 /***********************************************************************
791 * create_icon_bitmaps
793 * Create the color, mask and alpha bitmaps from the DIB info.
795 static BOOL create_icon_bitmaps( const BITMAPINFO *bmi, int width, int height,
796 HBITMAP *color, HBITMAP *mask, HBITMAP *alpha )
798 BOOL monochrome = is_dib_monochrome( bmi );
799 unsigned int size = bitmap_info_size( bmi, DIB_RGB_COLORS );
800 BITMAPINFO *info;
801 const void *color_bits, *mask_bits;
802 BOOL ret = FALSE;
803 HDC hdc = 0;
805 if (!(info = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
806 return FALSE;
807 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
809 memcpy( info, bmi, size );
810 info->bmiHeader.biHeight /= 2;
812 color_bits = (const char*)bmi + size;
813 mask_bits = (const char*)color_bits +
814 get_dib_width_bytes( bmi->bmiHeader.biWidth,
815 bmi->bmiHeader.biBitCount ) * abs(info->bmiHeader.biHeight);
817 *alpha = 0;
818 if (monochrome)
820 if (!(*mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
821 *color = 0;
823 /* copy color data into second half of mask bitmap */
824 SelectObject( hdc, *mask );
825 StretchDIBits( hdc, 0, height, width, height,
826 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
827 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
829 else
831 if (!(*mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
832 if (!(*color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
833 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
835 DeleteObject( *mask );
836 goto done;
838 SelectObject( hdc, *color );
839 StretchDIBits( hdc, 0, 0, width, height,
840 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
841 color_bits, info, DIB_RGB_COLORS, SRCCOPY );
843 if (bmi_has_alpha( info, color_bits ))
844 *alpha = create_alpha_bitmap( *color, *mask, info, color_bits );
846 /* convert info to monochrome to copy the mask */
847 info->bmiHeader.biBitCount = 1;
848 if (info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
850 RGBQUAD *rgb = info->bmiColors;
852 info->bmiHeader.biClrUsed = info->bmiHeader.biClrImportant = 2;
853 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
854 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
855 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
857 else
859 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)info) + 1);
861 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
862 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
866 SelectObject( hdc, *mask );
867 StretchDIBits( hdc, 0, 0, width, height,
868 0, 0, info->bmiHeader.biWidth, info->bmiHeader.biHeight,
869 mask_bits, info, DIB_RGB_COLORS, SRCCOPY );
870 ret = TRUE;
872 done:
873 DeleteDC( hdc );
874 HeapFree( GetProcessHeap(), 0, info );
875 return ret;
878 static HICON CURSORICON_CreateIconFromBMI( BITMAPINFO *bmi, HMODULE module, LPCWSTR resname, HRSRC rsrc,
879 POINT hotspot, BOOL bIcon, INT width, INT height, UINT cFlag )
881 HICON hObj;
882 HBITMAP color = 0, mask = 0, alpha = 0;
883 BOOL do_stretch;
885 /* Check bitmap header */
887 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
888 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
889 bmi->bmiHeader.biCompression != BI_RGB) )
891 WARN_(cursor)("\tinvalid resource bitmap header.\n");
892 return 0;
895 if (cFlag & LR_DEFAULTSIZE)
897 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
898 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
900 else
902 if (!width) width = bmi->bmiHeader.biWidth;
903 if (!height) height = bmi->bmiHeader.biHeight/2;
905 do_stretch = (bmi->bmiHeader.biHeight/2 != height) ||
906 (bmi->bmiHeader.biWidth != width);
908 /* Scale the hotspot */
909 if (bIcon)
911 hotspot.x = width / 2;
912 hotspot.y = height / 2;
914 else if (do_stretch)
916 hotspot.x = (hotspot.x * width) / bmi->bmiHeader.biWidth;
917 hotspot.y = (hotspot.y * height) / (bmi->bmiHeader.biHeight / 2);
920 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
921 if (!screen_dc) return 0;
923 if (!create_icon_bitmaps( bmi, width, height, &color, &mask, &alpha )) return 0;
925 hObj = alloc_icon_handle( FALSE, 1 );
926 if (hObj)
928 struct cursoricon_object *info = get_icon_ptr( hObj );
929 struct cursoricon_frame *frame;
931 info->is_icon = bIcon;
932 info->module = module;
933 info->hotspot = hotspot;
934 frame = get_icon_frame( info, 0 );
935 frame->delay = ~0;
936 frame->width = width;
937 frame->height = height;
938 frame->color = color;
939 frame->mask = mask;
940 frame->alpha = alpha;
941 release_icon_frame( info, 0, frame );
942 if (!IS_INTRESOURCE(resname))
944 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
945 if (info->resname) strcpyW( info->resname, resname );
947 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
949 if (module && (cFlag & LR_SHARED))
951 info->rsrc = rsrc;
952 list_add_head( &icon_cache, &info->entry );
954 release_icon_ptr( hObj, info );
955 USER_Driver->pCreateCursorIcon( hObj );
957 else
959 DeleteObject( color );
960 DeleteObject( alpha );
961 DeleteObject( mask );
963 return hObj;
967 /**********************************************************************
968 * .ANI cursor support
970 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
971 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
972 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
974 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
975 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
976 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
977 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
978 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
979 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
980 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
982 #define ANI_FLAG_ICON 0x1
983 #define ANI_FLAG_SEQUENCE 0x2
985 typedef struct {
986 DWORD header_size;
987 DWORD num_frames;
988 DWORD num_steps;
989 DWORD width;
990 DWORD height;
991 DWORD bpp;
992 DWORD num_planes;
993 DWORD display_rate;
994 DWORD flags;
995 } ani_header;
997 typedef struct {
998 DWORD data_size;
999 const unsigned char *data;
1000 } riff_chunk_t;
1002 static void dump_ani_header( const ani_header *header )
1004 TRACE(" header size: %d\n", header->header_size);
1005 TRACE(" frames: %d\n", header->num_frames);
1006 TRACE(" steps: %d\n", header->num_steps);
1007 TRACE(" width: %d\n", header->width);
1008 TRACE(" height: %d\n", header->height);
1009 TRACE(" bpp: %d\n", header->bpp);
1010 TRACE(" planes: %d\n", header->num_planes);
1011 TRACE(" display rate: %d\n", header->display_rate);
1012 TRACE(" flags: 0x%08x\n", header->flags);
1017 * RIFF:
1018 * DWORD "RIFF"
1019 * DWORD size
1020 * DWORD riff_id
1021 * BYTE[] data
1023 * LIST:
1024 * DWORD "LIST"
1025 * DWORD size
1026 * DWORD list_id
1027 * BYTE[] data
1029 * CHUNK:
1030 * DWORD chunk_id
1031 * DWORD size
1032 * BYTE[] data
1034 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1036 const unsigned char *ptr = parent_chunk->data;
1037 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1039 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1041 while (ptr < end)
1043 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1044 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1046 ptr += sizeof(DWORD);
1047 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1048 ptr += sizeof(DWORD);
1049 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1050 chunk->data = ptr;
1052 return;
1055 ptr += sizeof(DWORD);
1056 ptr += (*(const DWORD *)ptr + 1) & ~1;
1057 ptr += sizeof(DWORD);
1063 * .ANI layout:
1065 * RIFF:'ACON' RIFF chunk
1066 * |- CHUNK:'anih' Header
1067 * |- CHUNK:'seq ' Sequence information (optional)
1068 * \- LIST:'fram' Frame list
1069 * |- CHUNK:icon Cursor frames
1070 * |- CHUNK:icon
1071 * |- ...
1072 * \- CHUNK:icon
1074 static HCURSOR CURSORICON_CreateIconFromANI( const LPBYTE bits, DWORD bits_size,
1075 INT width, INT height, INT depth, UINT loadflags )
1077 struct animated_cursoricon_object *ani_icon_data;
1078 struct cursoricon_object *info;
1079 DWORD *frame_rates = NULL;
1080 ani_header header = {0};
1081 HCURSOR cursor = 0;
1082 UINT i, error = 0;
1084 riff_chunk_t root_chunk = { bits_size, bits };
1085 riff_chunk_t ACON_chunk = {0};
1086 riff_chunk_t anih_chunk = {0};
1087 riff_chunk_t fram_chunk = {0};
1088 riff_chunk_t rate_chunk = {0};
1089 const unsigned char *icon_chunk;
1090 const unsigned char *icon_data;
1092 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1094 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1095 if (!ACON_chunk.data)
1097 ERR("Failed to get root chunk.\n");
1098 return 0;
1101 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1102 if (!anih_chunk.data)
1104 ERR("Failed to get 'anih' chunk.\n");
1105 return 0;
1107 memcpy( &header, anih_chunk.data, sizeof(header) );
1108 dump_ani_header( &header );
1110 if (!(header.flags & ANI_FLAG_ICON))
1112 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1113 return 0;
1116 if (header.flags & ANI_FLAG_SEQUENCE)
1117 FIXME("Animated icon/cursor sequence data is not currently supported, frames may appear out of sequence.\n");
1119 riff_find_chunk( ANI_rate_ID, 0, &ACON_chunk, &rate_chunk );
1120 if (rate_chunk.data && header.num_steps == header.num_frames)
1121 frame_rates = (DWORD *) rate_chunk.data;
1122 else if (rate_chunk.data && header.num_steps != 1)
1123 FIXME("Animated icon/cursor rate data for sequence-based cursors not supported.\n");
1125 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1126 if (!fram_chunk.data)
1128 ERR("Failed to get icon list.\n");
1129 return 0;
1132 cursor = alloc_icon_handle( TRUE, header.num_frames );
1133 if (!cursor) return 0;
1135 info = get_icon_ptr( cursor );
1136 ani_icon_data = (struct animated_cursoricon_object *) info;
1137 info->is_icon = FALSE;
1138 if (header.num_steps > header.num_frames)
1140 FIXME("More steps than frames and sequence-based cursors not yet supported.\n");
1141 ani_icon_data->num_steps = header.num_frames;
1143 else
1144 ani_icon_data->num_steps = header.num_steps;
1146 /* The .ANI stores the display rate in jiffies (1/60s) */
1147 info->delay = header.display_rate;
1149 icon_chunk = fram_chunk.data;
1150 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1151 for (i=0; i<header.num_frames; i++)
1153 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1154 const CURSORICONFILEDIRENTRY *entry;
1155 struct cursoricon_frame *frame;
1156 INT frameWidth, frameHeight;
1157 const BITMAPINFO *bmi;
1158 HICON hIconFrame;
1160 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1161 width, height, depth, loadflags );
1163 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1164 info->hotspot.x = entry->xHotspot;
1165 info->hotspot.y = entry->yHotspot;
1166 if (!header.width || !header.height)
1168 frameWidth = entry->bWidth;
1169 frameHeight = entry->bHeight;
1171 else
1173 frameWidth = header.width;
1174 frameHeight = header.height;
1177 /* Grab a frame from the animation */
1178 hIconFrame = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)bmi, NULL, NULL, NULL, info->hotspot,
1179 FALSE, frameWidth, frameHeight, loadflags );
1180 if (!hIconFrame)
1182 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1183 error = TRUE;
1184 if (i == 0)
1186 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1187 ani_icon_data->num_frames = 0;
1188 release_icon_ptr( cursor, info );
1189 free_icon_handle( cursor );
1190 return 0;
1192 break;
1195 /* Setup the animated frame */
1196 ani_icon_data->frames[i] = hIconFrame;
1197 frame = get_icon_frame( info, i );
1198 if (frame_rates)
1199 frame->delay = frame_rates[i];
1200 else
1201 frame->delay = ~0;
1202 release_icon_frame( info, i, frame );
1204 /* Advance to the next chunk */
1205 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1206 icon_data = icon_chunk + (2 * sizeof(DWORD));
1209 /* There was an error but we at least decoded the first frame, so just use that frame */
1210 if (error)
1212 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1213 for (i=1; i<ani_icon_data->num_frames; i++)
1214 free_icon_handle( ani_icon_data->frames[i] );
1215 info->delay = 0;
1216 ani_icon_data->num_steps = 1;
1217 ani_icon_data->num_frames = 1;
1219 release_icon_ptr( cursor, info );
1221 return cursor;
1225 /**********************************************************************
1226 * CreateIconFromResourceEx (USER32.@)
1228 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
1229 * with cbSize parameter as well.
1231 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1232 BOOL bIcon, DWORD dwVersion,
1233 INT width, INT height,
1234 UINT cFlag )
1236 POINT hotspot;
1237 BITMAPINFO *bmi;
1239 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1240 bits, cbSize, dwVersion, width, height,
1241 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1243 if (!bits) return 0;
1245 if (dwVersion == 0x00020000)
1247 FIXME_(cursor)("\t2.xx resources are not supported\n");
1248 return 0;
1251 /* Check if the resource is an animated icon/cursor */
1252 if (!memcmp(bits, "RIFF", 4))
1253 return CURSORICON_CreateIconFromANI( bits, cbSize, width, height, 0 /* default depth */, cFlag );
1255 if (bIcon)
1257 hotspot.x = width / 2;
1258 hotspot.y = height / 2;
1259 bmi = (BITMAPINFO *)bits;
1261 else /* get the hotspot */
1263 SHORT *pt = (SHORT *)bits;
1264 hotspot.x = pt[0];
1265 hotspot.y = pt[1];
1266 bmi = (BITMAPINFO *)(pt + 2);
1269 return CURSORICON_CreateIconFromBMI( bmi, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1273 /**********************************************************************
1274 * CreateIconFromResource (USER32.@)
1276 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1277 BOOL bIcon, DWORD dwVersion)
1279 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1283 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1284 INT width, INT height, INT depth,
1285 BOOL fCursor, UINT loadflags)
1287 const CURSORICONFILEDIRENTRY *entry;
1288 const CURSORICONFILEDIR *dir;
1289 DWORD filesize = 0;
1290 HICON hIcon = 0;
1291 LPBYTE bits;
1292 POINT hotspot;
1294 TRACE("loading %s\n", debugstr_w( filename ));
1296 bits = map_fileW( filename, &filesize );
1297 if (!bits)
1298 return hIcon;
1300 /* Check for .ani. */
1301 if (memcmp( bits, "RIFF", 4 ) == 0)
1303 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, loadflags );
1304 goto end;
1307 dir = (const CURSORICONFILEDIR*) bits;
1308 if ( filesize < sizeof(*dir) )
1309 goto end;
1311 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
1312 goto end;
1314 if ( fCursor )
1315 entry = CURSORICON_FindBestCursorFile( dir, width, height, depth, loadflags );
1316 else
1317 entry = CURSORICON_FindBestIconFile( dir, width, height, depth, loadflags );
1319 if ( !entry )
1320 goto end;
1322 /* check that we don't run off the end of the file */
1323 if ( entry->dwDIBOffset > filesize )
1324 goto end;
1325 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1326 goto end;
1328 hotspot.x = entry->xHotspot;
1329 hotspot.y = entry->yHotspot;
1330 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)&bits[entry->dwDIBOffset], NULL, NULL, NULL,
1331 hotspot, !fCursor, width, height, loadflags );
1332 end:
1333 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1334 UnmapViewOfFile( bits );
1335 return hIcon;
1338 /**********************************************************************
1339 * CURSORICON_Load
1341 * Load a cursor or icon from resource or file.
1343 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1344 INT width, INT height, INT depth,
1345 BOOL fCursor, UINT loadflags)
1347 HANDLE handle = 0;
1348 HICON hIcon = 0;
1349 HRSRC hRsrc;
1350 const CURSORICONDIR *dir;
1351 const CURSORICONDIRENTRY *dirEntry;
1352 LPBYTE bits;
1353 WORD wResId;
1354 POINT hotspot;
1356 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1357 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1359 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1360 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1362 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1364 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1365 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1367 /* Get directory resource ID */
1369 if (!(hRsrc = FindResourceW( hInstance, name,
1370 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1371 return 0;
1373 /* Find the best entry in the directory */
1375 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1376 if (!(dir = LockResource( handle ))) return 0;
1377 if (fCursor)
1378 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, depth, loadflags );
1379 else
1380 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, depth, loadflags );
1381 if (!dirEntry) return 0;
1382 wResId = dirEntry->wResId;
1383 FreeResource( handle );
1385 /* Load the resource */
1387 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1388 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1390 /* If shared icon, check whether it was already loaded */
1391 if (loadflags & LR_SHARED)
1393 struct cursoricon_object *ptr;
1395 USER_Lock();
1396 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1398 if (ptr->module != hInstance) continue;
1399 if (ptr->rsrc != hRsrc) continue;
1400 hIcon = ptr->obj.handle;
1401 break;
1403 USER_Unlock();
1404 if (hIcon) return hIcon;
1407 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1408 bits = LockResource( handle );
1410 if (!fCursor)
1412 hotspot.x = width / 2;
1413 hotspot.y = height / 2;
1415 else /* get the hotspot */
1417 SHORT *pt = (SHORT *)bits;
1418 hotspot.x = pt[0];
1419 hotspot.y = pt[1];
1420 bits += 2 * sizeof(SHORT);
1422 hIcon = CURSORICON_CreateIconFromBMI( (BITMAPINFO *)bits, hInstance, name, hRsrc,
1423 hotspot, !fCursor, width, height, loadflags );
1424 FreeResource( handle );
1425 return hIcon;
1429 /***********************************************************************
1430 * CreateCursor (USER32.@)
1432 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1433 INT xHotSpot, INT yHotSpot,
1434 INT nWidth, INT nHeight,
1435 LPCVOID lpANDbits, LPCVOID lpXORbits )
1437 ICONINFO info;
1438 HCURSOR hCursor;
1440 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1441 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1443 info.fIcon = FALSE;
1444 info.xHotspot = xHotSpot;
1445 info.yHotspot = yHotSpot;
1446 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1447 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1448 hCursor = CreateIconIndirect( &info );
1449 DeleteObject( info.hbmMask );
1450 DeleteObject( info.hbmColor );
1451 return hCursor;
1455 /***********************************************************************
1456 * CreateIcon (USER32.@)
1458 * Creates an icon based on the specified bitmaps. The bitmaps must be
1459 * provided in a device dependent format and will be resized to
1460 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1461 * depth. The provided bitmaps must be top-down bitmaps.
1462 * Although Windows does not support 15bpp(*) this API must support it
1463 * for Winelib applications.
1465 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1466 * format!
1468 * RETURNS
1469 * Success: handle to an icon
1470 * Failure: NULL
1472 * FIXME: Do we need to resize the bitmaps?
1474 HICON WINAPI CreateIcon(
1475 HINSTANCE hInstance, /* [in] the application's hInstance */
1476 INT nWidth, /* [in] the width of the provided bitmaps */
1477 INT nHeight, /* [in] the height of the provided bitmaps */
1478 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1479 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1480 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1481 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1483 ICONINFO iinfo;
1484 HICON hIcon;
1486 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1487 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1489 iinfo.fIcon = TRUE;
1490 iinfo.xHotspot = nWidth / 2;
1491 iinfo.yHotspot = nHeight / 2;
1492 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1493 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1495 hIcon = CreateIconIndirect( &iinfo );
1497 DeleteObject( iinfo.hbmMask );
1498 DeleteObject( iinfo.hbmColor );
1500 return hIcon;
1504 /***********************************************************************
1505 * CopyIcon (USER32.@)
1507 HICON WINAPI CopyIcon( HICON hIcon )
1509 struct cursoricon_object *ptrOld, *ptrNew;
1510 HICON hNew;
1512 if (!(ptrOld = get_icon_ptr( hIcon )))
1514 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1515 return 0;
1517 if ((hNew = alloc_icon_handle( FALSE, 1 )))
1519 struct cursoricon_frame *frameOld, *frameNew;
1521 ptrNew = get_icon_ptr( hNew );
1522 ptrNew->is_icon = ptrOld->is_icon;
1523 ptrNew->hotspot = ptrOld->hotspot;
1524 if (!(frameOld = get_icon_frame( ptrOld, 0 )))
1526 release_icon_ptr( hIcon, ptrOld );
1527 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1528 return 0;
1530 if (!(frameNew = get_icon_frame( ptrNew, 0 )))
1532 release_icon_frame( ptrOld, 0, frameOld );
1533 release_icon_ptr( hIcon, ptrOld );
1534 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1535 return 0;
1537 frameNew->delay = 0;
1538 frameNew->width = frameOld->width;
1539 frameNew->height = frameOld->height;
1540 frameNew->mask = copy_bitmap( frameOld->mask );
1541 frameNew->color = copy_bitmap( frameOld->color );
1542 frameNew->alpha = copy_bitmap( frameOld->alpha );
1543 release_icon_frame( ptrOld, 0, frameOld );
1544 release_icon_frame( ptrNew, 0, frameNew );
1545 release_icon_ptr( hNew, ptrNew );
1547 release_icon_ptr( hIcon, ptrOld );
1548 if (hNew) USER_Driver->pCreateCursorIcon( hNew );
1549 return hNew;
1553 /***********************************************************************
1554 * DestroyIcon (USER32.@)
1556 BOOL WINAPI DestroyIcon( HICON hIcon )
1558 BOOL ret = FALSE;
1559 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1561 TRACE_(icon)("%p\n", hIcon );
1563 if (obj)
1565 BOOL shared = (obj->rsrc != NULL);
1566 release_icon_ptr( hIcon, obj );
1567 ret = (GetCursor() != hIcon);
1568 if (!shared) free_icon_handle( hIcon );
1570 return ret;
1574 /***********************************************************************
1575 * DestroyCursor (USER32.@)
1577 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1579 return DestroyIcon( hCursor );
1582 /***********************************************************************
1583 * DrawIcon (USER32.@)
1585 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1587 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1590 /***********************************************************************
1591 * SetCursor (USER32.@)
1593 * Set the cursor shape.
1595 * RETURNS
1596 * A handle to the previous cursor shape.
1598 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1600 struct cursoricon_object *obj;
1601 HCURSOR hOldCursor;
1602 int show_count;
1603 BOOL ret;
1605 TRACE("%p\n", hCursor);
1607 SERVER_START_REQ( set_cursor )
1609 req->flags = SET_CURSOR_HANDLE;
1610 req->handle = wine_server_user_handle( hCursor );
1611 if ((ret = !wine_server_call_err( req )))
1613 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1614 show_count = reply->prev_count;
1617 SERVER_END_REQ;
1619 if (!ret) return 0;
1621 /* Change the cursor shape only if it is visible */
1622 if (show_count >= 0 && hOldCursor != hCursor) USER_Driver->pSetCursor( hCursor );
1624 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1625 release_icon_ptr( hOldCursor, obj );
1626 return hOldCursor;
1629 /***********************************************************************
1630 * ShowCursor (USER32.@)
1632 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1634 HCURSOR cursor;
1635 int increment = bShow ? 1 : -1;
1636 int count;
1638 SERVER_START_REQ( set_cursor )
1640 req->flags = SET_CURSOR_COUNT;
1641 req->show_count = increment;
1642 wine_server_call( req );
1643 cursor = wine_server_ptr_handle( reply->prev_handle );
1644 count = reply->prev_count + increment;
1646 SERVER_END_REQ;
1648 TRACE("%d, count=%d\n", bShow, count );
1650 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1651 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1653 return count;
1656 /***********************************************************************
1657 * GetCursor (USER32.@)
1659 HCURSOR WINAPI GetCursor(void)
1661 HCURSOR ret;
1663 SERVER_START_REQ( set_cursor )
1665 req->flags = 0;
1666 wine_server_call( req );
1667 ret = wine_server_ptr_handle( reply->prev_handle );
1669 SERVER_END_REQ;
1670 return ret;
1674 /***********************************************************************
1675 * ClipCursor (USER32.@)
1677 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1679 BOOL ret;
1680 RECT new_rect;
1682 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1684 SERVER_START_REQ( set_cursor )
1686 req->flags = SET_CURSOR_CLIP;
1687 if (rect)
1689 req->clip.left = rect->left;
1690 req->clip.top = rect->top;
1691 req->clip.right = rect->right;
1692 req->clip.bottom = rect->bottom;
1694 if ((ret = !wine_server_call( req )))
1696 new_rect.left = reply->new_clip.left;
1697 new_rect.top = reply->new_clip.top;
1698 new_rect.right = reply->new_clip.right;
1699 new_rect.bottom = reply->new_clip.bottom;
1702 SERVER_END_REQ;
1703 if (ret) USER_Driver->pClipCursor( &new_rect );
1704 return ret;
1708 /***********************************************************************
1709 * GetClipCursor (USER32.@)
1711 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1713 BOOL ret;
1715 if (!rect) return FALSE;
1717 SERVER_START_REQ( set_cursor )
1719 req->flags = 0;
1720 if ((ret = !wine_server_call( req )))
1722 rect->left = reply->new_clip.left;
1723 rect->top = reply->new_clip.top;
1724 rect->right = reply->new_clip.right;
1725 rect->bottom = reply->new_clip.bottom;
1728 SERVER_END_REQ;
1729 return ret;
1733 /***********************************************************************
1734 * SetSystemCursor (USER32.@)
1736 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1738 FIXME("(%p,%08x),stub!\n", hcur, id);
1739 return TRUE;
1743 /**********************************************************************
1744 * LookupIconIdFromDirectoryEx (USER32.@)
1746 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1747 INT width, INT height, UINT cFlag )
1749 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1750 UINT retVal = 0;
1751 if( dir && !dir->idReserved && (dir->idType & 3) )
1753 const CURSORICONDIRENTRY* entry;
1755 const HDC hdc = GetDC(0);
1756 const int depth = (cFlag & LR_MONOCHROME) ?
1757 1 : GetDeviceCaps(hdc, BITSPIXEL);
1758 ReleaseDC(0, hdc);
1760 if( bIcon )
1761 entry = CURSORICON_FindBestIconRes( dir, width, height, depth, LR_DEFAULTSIZE );
1762 else
1763 entry = CURSORICON_FindBestCursorRes( dir, width, height, depth, LR_DEFAULTSIZE );
1765 if( entry ) retVal = entry->wResId;
1767 else WARN_(cursor)("invalid resource directory\n");
1768 return retVal;
1771 /**********************************************************************
1772 * LookupIconIdFromDirectory (USER32.@)
1774 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1776 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1779 /***********************************************************************
1780 * LoadCursorW (USER32.@)
1782 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1784 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1786 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1787 LR_SHARED | LR_DEFAULTSIZE );
1790 /***********************************************************************
1791 * LoadCursorA (USER32.@)
1793 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1795 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1797 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1798 LR_SHARED | LR_DEFAULTSIZE );
1801 /***********************************************************************
1802 * LoadCursorFromFileW (USER32.@)
1804 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1806 TRACE("%s\n", debugstr_w(name));
1808 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1809 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1812 /***********************************************************************
1813 * LoadCursorFromFileA (USER32.@)
1815 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1817 TRACE("%s\n", debugstr_a(name));
1819 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1820 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1823 /***********************************************************************
1824 * LoadIconW (USER32.@)
1826 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1828 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1830 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1831 LR_SHARED | LR_DEFAULTSIZE );
1834 /***********************************************************************
1835 * LoadIconA (USER32.@)
1837 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1839 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1841 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1842 LR_SHARED | LR_DEFAULTSIZE );
1845 /**********************************************************************
1846 * GetCursorFrameInfo (USER32.@)
1848 HCURSOR WINAPI GetCursorFrameInfo(HCURSOR hCursor, DWORD unk1, DWORD istep, DWORD *rate_jiffies, DWORD *num_steps)
1850 struct cursoricon_object *ptr;
1851 HCURSOR ret = 0;
1852 UINT icon_steps;
1854 if (rate_jiffies == NULL || num_steps == NULL) return 0;
1856 if (!(ptr = get_icon_ptr( hCursor ))) return 0;
1858 FIXME("semi-stub! %p => %d %d %p %p\n", hCursor, unk1, istep, rate_jiffies, num_steps);
1860 icon_steps = get_icon_steps(ptr);
1861 /* Important Note: Sequences are not currently supported, so this implementation
1862 * will not properly handle all cases. */
1863 if (istep < icon_steps || !ptr->is_ani)
1865 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) ptr;
1866 UINT icon_frames = 1;
1868 if (ptr->is_ani)
1869 icon_frames = ani_icon_data->num_frames;
1870 if (ptr->is_ani && icon_frames > 1)
1871 ret = ani_icon_data->frames[istep];
1872 else
1873 ret = hCursor;
1874 if (icon_frames == 1)
1876 *rate_jiffies = 0;
1877 *num_steps = 1;
1879 else if (icon_steps == 1)
1881 *num_steps = ~0;
1882 *rate_jiffies = ptr->delay;
1884 else if (istep < icon_steps)
1886 struct cursoricon_frame *frame;
1888 *num_steps = icon_steps;
1889 frame = get_icon_frame( ptr, istep );
1890 if (get_icon_steps(ptr) == 1)
1891 *num_steps = ~0;
1892 else
1893 *num_steps = get_icon_steps(ptr);
1894 /* If this specific frame does not have a delay then use the global delay */
1895 if (frame->delay == ~0)
1896 *rate_jiffies = ptr->delay;
1897 else
1898 *rate_jiffies = frame->delay;
1899 release_icon_frame( ptr, istep, frame );
1903 release_icon_ptr( hCursor, ptr );
1905 return ret;
1908 /**********************************************************************
1909 * GetIconInfo (USER32.@)
1911 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1913 ICONINFOEXW infoW;
1915 infoW.cbSize = sizeof(infoW);
1916 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
1917 iconinfo->fIcon = infoW.fIcon;
1918 iconinfo->xHotspot = infoW.xHotspot;
1919 iconinfo->yHotspot = infoW.yHotspot;
1920 iconinfo->hbmColor = infoW.hbmColor;
1921 iconinfo->hbmMask = infoW.hbmMask;
1922 return TRUE;
1925 /**********************************************************************
1926 * GetIconInfoExA (USER32.@)
1928 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
1930 ICONINFOEXW infoW;
1932 if (info->cbSize != sizeof(*info))
1934 SetLastError( ERROR_INVALID_PARAMETER );
1935 return FALSE;
1937 infoW.cbSize = sizeof(infoW);
1938 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
1939 info->fIcon = infoW.fIcon;
1940 info->xHotspot = infoW.xHotspot;
1941 info->yHotspot = infoW.yHotspot;
1942 info->hbmColor = infoW.hbmColor;
1943 info->hbmMask = infoW.hbmMask;
1944 info->wResID = infoW.wResID;
1945 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
1946 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
1947 return TRUE;
1950 /**********************************************************************
1951 * GetIconInfoExW (USER32.@)
1953 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
1955 struct cursoricon_frame *frame;
1956 struct cursoricon_object *ptr;
1957 HMODULE module;
1958 BOOL ret = TRUE;
1960 if (info->cbSize != sizeof(*info))
1962 SetLastError( ERROR_INVALID_PARAMETER );
1963 return FALSE;
1965 if (!(ptr = get_icon_ptr( icon )))
1967 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1968 return FALSE;
1971 frame = get_icon_frame( ptr, 0 );
1972 if (!frame)
1974 release_icon_ptr( icon, ptr );
1975 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1976 return FALSE;
1979 TRACE("%p => %dx%d\n", icon, frame->width, frame->height);
1981 info->fIcon = ptr->is_icon;
1982 info->xHotspot = ptr->hotspot.x;
1983 info->yHotspot = ptr->hotspot.y;
1984 info->hbmColor = copy_bitmap( frame->color );
1985 info->hbmMask = copy_bitmap( frame->mask );
1986 info->wResID = 0;
1987 info->szModName[0] = 0;
1988 info->szResName[0] = 0;
1989 if (ptr->module)
1991 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
1992 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
1994 if (!info->hbmMask || (!info->hbmColor && frame->color))
1996 DeleteObject( info->hbmMask );
1997 DeleteObject( info->hbmColor );
1998 ret = FALSE;
2000 module = ptr->module;
2001 release_icon_frame( ptr, 0, frame );
2002 release_icon_ptr( icon, ptr );
2003 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
2004 return ret;
2007 /* copy an icon bitmap, even when it can't be selected into a DC */
2008 /* helper for CreateIconIndirect */
2009 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
2010 HBITMAP src, int width, int height )
2012 HDC hdc = CreateCompatibleDC( 0 );
2014 if (!SelectObject( hdc, src )) /* do it the hard way */
2016 BITMAPINFO *info;
2017 void *bits;
2019 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
2020 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2021 info->bmiHeader.biWidth = width;
2022 info->bmiHeader.biHeight = height;
2023 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
2024 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
2025 info->bmiHeader.biCompression = BI_RGB;
2026 info->bmiHeader.biSizeImage = height * get_dib_width_bytes( width, info->bmiHeader.biBitCount );
2027 info->bmiHeader.biXPelsPerMeter = 0;
2028 info->bmiHeader.biYPelsPerMeter = 0;
2029 info->bmiHeader.biClrUsed = 0;
2030 info->bmiHeader.biClrImportant = 0;
2031 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
2032 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
2033 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
2034 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
2036 HeapFree( GetProcessHeap(), 0, bits );
2037 HeapFree( GetProcessHeap(), 0, info );
2039 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
2041 DeleteDC( hdc );
2044 /**********************************************************************
2045 * CreateIconIndirect (USER32.@)
2047 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2049 BITMAP bmpXor, bmpAnd;
2050 HICON hObj;
2051 HBITMAP color = 0, mask;
2052 int width, height;
2053 HDC hdc;
2055 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2056 iconinfo->hbmColor, iconinfo->hbmMask,
2057 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2059 if (!iconinfo->hbmMask) return 0;
2061 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2062 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2063 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2064 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2066 if (iconinfo->hbmColor)
2068 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2069 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2070 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2071 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2073 width = bmpXor.bmWidth;
2074 height = bmpXor.bmHeight;
2075 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1)
2077 color = CreateCompatibleBitmap( screen_dc, width, height );
2078 mask = CreateBitmap( width, height, 1, 1, NULL );
2080 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2082 else
2084 width = bmpAnd.bmWidth;
2085 height = bmpAnd.bmHeight;
2086 mask = CreateBitmap( width, height, 1, 1, NULL );
2089 hdc = CreateCompatibleDC( 0 );
2090 SelectObject( hdc, mask );
2091 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
2093 if (color)
2095 SelectObject( hdc, color );
2096 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
2098 else if (iconinfo->hbmColor)
2100 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
2102 else height /= 2;
2104 DeleteDC( hdc );
2106 hObj = alloc_icon_handle( FALSE, 1 );
2107 if (hObj)
2109 struct cursoricon_object *info = get_icon_ptr( hObj );
2110 struct cursoricon_frame *frame;
2112 info->is_icon = iconinfo->fIcon;
2113 frame = get_icon_frame( info, 0 );
2114 frame->delay = ~0;
2115 frame->width = width;
2116 frame->height = height;
2117 frame->color = color;
2118 frame->mask = mask;
2119 frame->alpha = create_alpha_bitmap( iconinfo->hbmColor, mask, NULL, NULL );
2120 release_icon_frame( info, 0, frame );
2121 if (info->is_icon)
2123 info->hotspot.x = width / 2;
2124 info->hotspot.y = height / 2;
2126 else
2128 info->hotspot.x = iconinfo->xHotspot;
2129 info->hotspot.y = iconinfo->yHotspot;
2132 release_icon_ptr( hObj, info );
2133 USER_Driver->pCreateCursorIcon( hObj );
2135 return hObj;
2138 /******************************************************************************
2139 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2141 * NOTES
2142 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2144 * PARAMS
2145 * hdc [I] Handle to device context
2146 * x0 [I] X coordinate of upper left corner
2147 * y0 [I] Y coordinate of upper left corner
2148 * hIcon [I] Handle to icon to draw
2149 * cxWidth [I] Width of icon
2150 * cyWidth [I] Height of icon
2151 * istep [I] Index of frame in animated cursor
2152 * hbr [I] Handle to background brush
2153 * flags [I] Icon-drawing flags
2155 * RETURNS
2156 * Success: TRUE
2157 * Failure: FALSE
2159 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2160 INT cxWidth, INT cyWidth, UINT istep,
2161 HBRUSH hbr, UINT flags )
2163 struct cursoricon_frame *frame;
2164 struct cursoricon_object *ptr;
2165 HDC hdc_dest, hMemDC;
2166 BOOL result = FALSE, DoOffscreen;
2167 HBITMAP hB_off = 0;
2168 COLORREF oldFg, oldBg;
2169 INT x, y, nStretchMode;
2171 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2172 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2174 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2175 if (istep >= get_icon_steps( ptr ))
2177 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
2178 release_icon_ptr( hIcon, ptr );
2179 return FALSE;
2181 if (!(frame = get_icon_frame( ptr, istep )))
2183 FIXME_(icon)("Error retrieving icon frame %d\n", istep);
2184 release_icon_ptr( hIcon, ptr );
2185 return FALSE;
2187 if (!(hMemDC = CreateCompatibleDC( hdc )))
2189 release_icon_frame( ptr, istep, frame );
2190 release_icon_ptr( hIcon, ptr );
2191 return FALSE;
2194 if (flags & DI_NOMIRROR)
2195 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2197 /* Calculate the size of the destination image. */
2198 if (cxWidth == 0)
2200 if (flags & DI_DEFAULTSIZE)
2201 cxWidth = GetSystemMetrics (SM_CXICON);
2202 else
2203 cxWidth = frame->width;
2205 if (cyWidth == 0)
2207 if (flags & DI_DEFAULTSIZE)
2208 cyWidth = GetSystemMetrics (SM_CYICON);
2209 else
2210 cyWidth = frame->height;
2213 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2215 if (DoOffscreen) {
2216 RECT r;
2218 r.left = 0;
2219 r.top = 0;
2220 r.right = cxWidth;
2221 r.bottom = cxWidth;
2223 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2224 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2226 DeleteDC( hdc_dest );
2227 goto failed;
2229 SelectObject(hdc_dest, hB_off);
2230 FillRect(hdc_dest, &r, hbr);
2231 x = y = 0;
2233 else
2235 hdc_dest = hdc;
2236 x = x0;
2237 y = y0;
2240 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2242 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2243 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2245 if (frame->alpha && (flags & DI_IMAGE))
2247 BOOL is_mono = FALSE;
2249 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2251 BITMAP bm;
2252 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2253 is_mono = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel == 1;
2255 if (!is_mono)
2257 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2258 SelectObject( hMemDC, frame->alpha );
2259 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2260 0, 0, frame->width, frame->height,
2261 pixelblend )) goto done;
2265 if (flags & DI_MASK)
2267 SelectObject( hMemDC, frame->mask );
2268 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2269 hMemDC, 0, 0, frame->width, frame->height, SRCAND );
2272 if (flags & DI_IMAGE)
2274 if (frame->color)
2276 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2277 SelectObject( hMemDC, frame->color );
2278 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2279 hMemDC, 0, 0, frame->width, frame->height, rop );
2281 else
2283 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2284 SelectObject( hMemDC, frame->mask );
2285 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2286 hMemDC, 0, frame->height, frame->width,
2287 frame->height, rop );
2291 done:
2292 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2294 SetTextColor( hdc, oldFg );
2295 SetBkColor( hdc, oldBg );
2296 SetStretchBltMode (hdc, nStretchMode);
2297 result = TRUE;
2298 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2299 if (hB_off) DeleteObject(hB_off);
2300 failed:
2301 DeleteDC( hMemDC );
2302 release_icon_frame( ptr, istep, frame );
2303 release_icon_ptr( hIcon, ptr );
2304 return result;
2307 /***********************************************************************
2308 * DIB_FixColorsToLoadflags
2310 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2311 * are in loadflags
2313 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2315 int colors;
2316 COLORREF c_W, c_S, c_F, c_L, c_C;
2317 int incr,i;
2318 RGBQUAD *ptr;
2319 int bitmap_type;
2320 LONG width;
2321 LONG height;
2322 WORD bpp;
2323 DWORD compr;
2325 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2327 WARN_(resource)("Invalid bitmap\n");
2328 return;
2331 if (bpp > 8) return;
2333 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2335 incr = 3;
2336 colors = 1 << bpp;
2338 else
2340 incr = 4;
2341 colors = bmi->bmiHeader.biClrUsed;
2342 if (colors > 256) colors = 256;
2343 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2346 c_W = GetSysColor(COLOR_WINDOW);
2347 c_S = GetSysColor(COLOR_3DSHADOW);
2348 c_F = GetSysColor(COLOR_3DFACE);
2349 c_L = GetSysColor(COLOR_3DLIGHT);
2351 if (loadflags & LR_LOADTRANSPARENT) {
2352 switch (bpp) {
2353 case 1: pix = pix >> 7; break;
2354 case 4: pix = pix >> 4; break;
2355 case 8: break;
2356 default:
2357 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2358 return;
2360 if (pix >= colors) {
2361 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2362 return;
2364 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2365 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2366 ptr->rgbBlue = GetBValue(c_W);
2367 ptr->rgbGreen = GetGValue(c_W);
2368 ptr->rgbRed = GetRValue(c_W);
2370 if (loadflags & LR_LOADMAP3DCOLORS)
2371 for (i=0; i<colors; i++) {
2372 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2373 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2374 if (c_C == RGB(128, 128, 128)) {
2375 ptr->rgbRed = GetRValue(c_S);
2376 ptr->rgbGreen = GetGValue(c_S);
2377 ptr->rgbBlue = GetBValue(c_S);
2378 } else if (c_C == RGB(192, 192, 192)) {
2379 ptr->rgbRed = GetRValue(c_F);
2380 ptr->rgbGreen = GetGValue(c_F);
2381 ptr->rgbBlue = GetBValue(c_F);
2382 } else if (c_C == RGB(223, 223, 223)) {
2383 ptr->rgbRed = GetRValue(c_L);
2384 ptr->rgbGreen = GetGValue(c_L);
2385 ptr->rgbBlue = GetBValue(c_L);
2391 /**********************************************************************
2392 * BITMAP_Load
2394 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2395 INT desiredx, INT desiredy, UINT loadflags )
2397 HBITMAP hbitmap = 0, orig_bm;
2398 HRSRC hRsrc;
2399 HGLOBAL handle;
2400 char *ptr = NULL;
2401 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2402 int size;
2403 BYTE pix;
2404 char *bits;
2405 LONG width, height, new_width, new_height;
2406 WORD bpp_dummy;
2407 DWORD compr_dummy, offbits = 0;
2408 INT bm_type;
2409 HDC screen_mem_dc = NULL;
2411 if (!(loadflags & LR_LOADFROMFILE))
2413 if (!instance)
2415 /* OEM bitmap: try to load the resource from user32.dll */
2416 instance = user32_module;
2419 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2420 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2422 if ((info = LockResource( handle )) == NULL) return 0;
2424 else
2426 BITMAPFILEHEADER * bmfh;
2428 if (!(ptr = map_fileW( name, NULL ))) return 0;
2429 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2430 bmfh = (BITMAPFILEHEADER *)ptr;
2431 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2433 WARN("Invalid/unsupported bitmap format!\n");
2434 goto end;
2436 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2439 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2440 &bpp_dummy, &compr_dummy);
2441 if (bm_type == -1)
2443 WARN("Invalid bitmap format!\n");
2444 goto end;
2447 size = bitmap_info_size(info, DIB_RGB_COLORS);
2448 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2449 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2451 if (!fix_info || !scaled_info) goto end;
2452 memcpy(fix_info, info, size);
2454 pix = *((LPBYTE)info + size);
2455 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2457 memcpy(scaled_info, fix_info, size);
2459 if(desiredx != 0)
2460 new_width = desiredx;
2461 else
2462 new_width = width;
2464 if(desiredy != 0)
2465 new_height = height > 0 ? desiredy : -desiredy;
2466 else
2467 new_height = height;
2469 if(bm_type == 0)
2471 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2472 core->bcWidth = new_width;
2473 core->bcHeight = new_height;
2475 else
2477 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2478 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2479 WARN("Broken BitmapInfoHeader!\n");
2480 goto end;
2483 scaled_info->bmiHeader.biWidth = new_width;
2484 scaled_info->bmiHeader.biHeight = new_height;
2487 if (new_height < 0) new_height = -new_height;
2489 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2490 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2492 bits = (char *)info + (offbits ? offbits : size);
2494 if (loadflags & LR_CREATEDIBSECTION)
2496 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2497 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2499 else
2501 if (is_dib_monochrome(fix_info))
2502 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2503 else
2504 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2507 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2508 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2509 SelectObject(screen_mem_dc, orig_bm);
2511 end:
2512 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2513 HeapFree(GetProcessHeap(), 0, scaled_info);
2514 HeapFree(GetProcessHeap(), 0, fix_info);
2515 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2517 return hbitmap;
2520 /**********************************************************************
2521 * LoadImageA (USER32.@)
2523 * See LoadImageW.
2525 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2526 INT desiredx, INT desiredy, UINT loadflags)
2528 HANDLE res;
2529 LPWSTR u_name;
2531 if (IS_INTRESOURCE(name))
2532 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2534 __TRY {
2535 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2536 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2537 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2539 __EXCEPT_PAGE_FAULT {
2540 SetLastError( ERROR_INVALID_PARAMETER );
2541 return 0;
2543 __ENDTRY
2544 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2545 HeapFree(GetProcessHeap(), 0, u_name);
2546 return res;
2550 /******************************************************************************
2551 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2553 * PARAMS
2554 * hinst [I] Handle of instance that contains image
2555 * name [I] Name of image
2556 * type [I] Type of image
2557 * desiredx [I] Desired width
2558 * desiredy [I] Desired height
2559 * loadflags [I] Load flags
2561 * RETURNS
2562 * Success: Handle to newly loaded image
2563 * Failure: NULL
2565 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2567 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2568 INT desiredx, INT desiredy, UINT loadflags )
2570 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2571 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2573 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2574 switch (type) {
2575 case IMAGE_BITMAP:
2576 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2578 case IMAGE_ICON:
2579 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2580 if (screen_dc)
2582 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2583 GetDeviceCaps(screen_dc, BITSPIXEL),
2584 FALSE, loadflags);
2586 break;
2588 case IMAGE_CURSOR:
2589 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2590 1, TRUE, loadflags);
2592 return 0;
2595 /******************************************************************************
2596 * CopyImage (USER32.@) Creates new image and copies attributes to it
2598 * PARAMS
2599 * hnd [I] Handle to image to copy
2600 * type [I] Type of image to copy
2601 * desiredx [I] Desired width of new image
2602 * desiredy [I] Desired height of new image
2603 * flags [I] Copy flags
2605 * RETURNS
2606 * Success: Handle to newly created image
2607 * Failure: NULL
2609 * BUGS
2610 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2611 * all other versions (95/2000/XP have been tested) ignore it.
2613 * NOTES
2614 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2615 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2616 * the copy will have the same depth as the screen.
2617 * The content of the image will only be copied if the bit depth of the
2618 * original image is compatible with the bit depth of the screen, or
2619 * if the source is a DIB section.
2620 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2622 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2623 INT desiredy, UINT flags )
2625 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2626 hnd, type, desiredx, desiredy, flags);
2628 switch (type)
2630 case IMAGE_BITMAP:
2632 HBITMAP res = NULL;
2633 DIBSECTION ds;
2634 int objSize;
2635 BITMAPINFO * bi;
2637 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2638 if (!objSize) return 0;
2639 if ((desiredx < 0) || (desiredy < 0)) return 0;
2641 if (flags & LR_COPYFROMRESOURCE)
2643 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2646 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2647 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2649 /* Allocate memory for a BITMAPINFOHEADER structure and a
2650 color table. The maximum number of colors in a color table
2651 is 256 which corresponds to a bitmap with depth 8.
2652 Bitmaps with higher depths don't have color tables. */
2653 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2654 if (!bi) return 0;
2656 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2657 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2658 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2659 bi->bmiHeader.biCompression = BI_RGB;
2661 if (flags & LR_CREATEDIBSECTION)
2663 /* Create a DIB section. LR_MONOCHROME is ignored */
2664 void * bits;
2665 HDC dc = CreateCompatibleDC(NULL);
2667 if (objSize == sizeof(DIBSECTION))
2669 /* The source bitmap is a DIB.
2670 Get its attributes to create an exact copy */
2671 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2674 /* Get the color table or the color masks */
2675 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2677 bi->bmiHeader.biWidth = desiredx;
2678 bi->bmiHeader.biHeight = desiredy;
2679 bi->bmiHeader.biSizeImage = 0;
2681 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2682 DeleteDC(dc);
2684 else
2686 /* Create a device-dependent bitmap */
2688 BOOL monochrome = (flags & LR_MONOCHROME);
2690 if (objSize == sizeof(DIBSECTION))
2692 /* The source bitmap is a DIB section.
2693 Get its attributes */
2694 HDC dc = CreateCompatibleDC(NULL);
2695 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2696 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2697 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2698 DeleteDC(dc);
2700 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2702 /* Look if the colors of the DIB are black and white */
2704 monochrome =
2705 (bi->bmiColors[0].rgbRed == 0xff
2706 && bi->bmiColors[0].rgbGreen == 0xff
2707 && bi->bmiColors[0].rgbBlue == 0xff
2708 && bi->bmiColors[0].rgbReserved == 0
2709 && bi->bmiColors[1].rgbRed == 0
2710 && bi->bmiColors[1].rgbGreen == 0
2711 && bi->bmiColors[1].rgbBlue == 0
2712 && bi->bmiColors[1].rgbReserved == 0)
2714 (bi->bmiColors[0].rgbRed == 0
2715 && bi->bmiColors[0].rgbGreen == 0
2716 && bi->bmiColors[0].rgbBlue == 0
2717 && bi->bmiColors[0].rgbReserved == 0
2718 && bi->bmiColors[1].rgbRed == 0xff
2719 && bi->bmiColors[1].rgbGreen == 0xff
2720 && bi->bmiColors[1].rgbBlue == 0xff
2721 && bi->bmiColors[1].rgbReserved == 0);
2724 else if (!monochrome)
2726 monochrome = ds.dsBm.bmBitsPixel == 1;
2729 if (monochrome)
2731 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2733 else
2735 HDC screenDC = GetDC(NULL);
2736 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2737 ReleaseDC(NULL, screenDC);
2741 if (res)
2743 /* Only copy the bitmap if it's a DIB section or if it's
2744 compatible to the screen */
2745 BOOL copyContents;
2747 if (objSize == sizeof(DIBSECTION))
2749 copyContents = TRUE;
2751 else
2753 HDC screenDC = GetDC(NULL);
2754 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2755 ReleaseDC(NULL, screenDC);
2757 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2760 if (copyContents)
2762 /* The source bitmap may already be selected in a device context,
2763 use GetDIBits/StretchDIBits and not StretchBlt */
2765 HDC dc;
2766 void * bits;
2768 dc = CreateCompatibleDC(NULL);
2770 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2771 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2772 bi->bmiHeader.biSizeImage = 0;
2773 bi->bmiHeader.biClrUsed = 0;
2774 bi->bmiHeader.biClrImportant = 0;
2776 /* Fill in biSizeImage */
2777 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2778 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2780 if (bits)
2782 HBITMAP oldBmp;
2784 /* Get the image bits of the source bitmap */
2785 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2787 /* Copy it to the destination bitmap */
2788 oldBmp = SelectObject(dc, res);
2789 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2790 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2791 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2792 SelectObject(dc, oldBmp);
2794 HeapFree(GetProcessHeap(), 0, bits);
2797 DeleteDC(dc);
2800 if (flags & LR_COPYDELETEORG)
2802 DeleteObject(hnd);
2805 HeapFree(GetProcessHeap(), 0, bi);
2806 return res;
2808 case IMAGE_ICON:
2809 case IMAGE_CURSOR:
2811 struct cursoricon_object *icon;
2812 HICON res = 0;
2813 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( screen_dc, BITSPIXEL );
2815 if (flags & LR_DEFAULTSIZE)
2817 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2818 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2821 if (!(icon = get_icon_ptr( hnd ))) return 0;
2823 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2824 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2825 type == IMAGE_CURSOR, flags );
2826 else
2827 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2828 release_icon_ptr( hnd, icon );
2830 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2831 return res;
2834 return 0;
2838 /******************************************************************************
2839 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2841 * RETURNS
2842 * Success: Handle to specified bitmap
2843 * Failure: NULL
2845 HBITMAP WINAPI LoadBitmapW(
2846 HINSTANCE instance, /* [in] Handle to application instance */
2847 LPCWSTR name) /* [in] Address of bitmap resource name */
2849 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2852 /**********************************************************************
2853 * LoadBitmapA (USER32.@)
2855 * See LoadBitmapW.
2857 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2859 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );