ddraw/tests: Rewrite LimitTest().
[wine.git] / dlls / user32 / cursoricon.c
blob1fbec2a5afe9f1a908add6c9b4101f73dff5777a
1 /*
2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
6 * 1997 Alex Korobka
7 * 1998 Turchanov Sergey
8 * 2007 Henri Verbeet
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <stdlib.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "winerror.h"
37 #include "winnls.h"
38 #include "wine/exception.h"
39 #include "wine/server.h"
40 #include "controls.h"
41 #include "win.h"
42 #include "user_private.h"
43 #include "wine/list.h"
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
48 WINE_DECLARE_DEBUG_CHANNEL(icon);
49 WINE_DECLARE_DEBUG_CHANNEL(resource);
51 static struct list icon_cache = LIST_INIT( icon_cache );
53 /**********************************************************************
54 * User objects management
57 struct cursoricon_frame
59 UINT width; /* frame-specific width */
60 UINT height; /* frame-specific height */
61 UINT delay; /* frame-specific delay between this frame and the next (in jiffies) */
62 HBITMAP color; /* color bitmap */
63 HBITMAP alpha; /* pre-multiplied alpha bitmap for 32-bpp icons */
64 HBITMAP mask; /* mask bitmap (followed by color for 1-bpp icons) */
67 struct cursoricon_object
69 struct user_object obj; /* object header */
70 struct list entry; /* entry in shared icons list */
71 ULONG_PTR param; /* opaque param used by 16-bit code */
72 HMODULE module; /* module for icons loaded from resources */
73 LPWSTR resname; /* resource name for icons loaded from resources */
74 HRSRC rsrc; /* resource for shared icons */
75 BOOL is_icon; /* whether icon or cursor */
76 BOOL is_ani; /* whether this object is a static cursor or an animated cursor */
77 UINT delay; /* delay between this frame and the next (in jiffies) */
78 POINT hotspot;
81 struct static_cursoricon_object
83 struct cursoricon_object shared;
84 struct cursoricon_frame frame; /* frame-specific icon data */
87 struct animated_cursoricon_object
89 struct cursoricon_object shared;
90 UINT num_frames; /* number of frames in the icon/cursor */
91 UINT num_steps; /* number of sequence steps in the icon/cursor */
92 HICON frames[1]; /* list of animated cursor frames */
95 static HDC get_screen_dc(void)
97 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
98 static HDC screen_dc;
100 if (!screen_dc)
101 screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
103 return screen_dc;
106 static HICON alloc_icon_handle( BOOL is_ani, UINT num_steps )
108 struct cursoricon_object *obj;
109 int icon_size;
110 HICON handle;
112 if (is_ani)
113 icon_size = FIELD_OFFSET( struct animated_cursoricon_object, frames[num_steps] );
114 else
115 icon_size = sizeof( struct static_cursoricon_object );
116 obj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, icon_size );
117 if (!obj) return NULL;
119 obj->delay = 0;
120 obj->is_ani = is_ani;
121 if (is_ani)
123 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
125 ani_icon_data->num_steps = num_steps;
126 ani_icon_data->num_frames = num_steps; /* changed later for some animated cursors */
129 if (!(handle = alloc_user_handle( &obj->obj, USER_ICON )))
130 HeapFree( GetProcessHeap(), 0, obj );
131 return handle;
134 static struct cursoricon_object *get_icon_ptr( HICON handle )
136 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
137 if (obj == OBJ_OTHER_PROCESS)
139 WARN( "icon handle %p from other process\n", handle );
140 obj = NULL;
142 return obj;
145 static struct cursoricon_frame *get_icon_frame( struct cursoricon_object *obj, int istep )
147 struct static_cursoricon_object *req_frame;
149 if (obj->is_ani)
151 struct animated_cursoricon_object *ani_icon_data;
152 struct cursoricon_object *frameobj;
154 ani_icon_data = (struct animated_cursoricon_object *) obj;
155 if (!(frameobj = get_icon_ptr( ani_icon_data->frames[istep] )))
156 return 0;
157 req_frame = (struct static_cursoricon_object *) frameobj;
159 else
160 req_frame = (struct static_cursoricon_object *) obj;
162 return &req_frame->frame;
165 static void release_icon_frame( struct cursoricon_object *obj, struct cursoricon_frame *frame )
167 if (obj->is_ani)
169 struct cursoricon_object *frameobj;
171 frameobj = (struct cursoricon_object *) (((char *)frame) - FIELD_OFFSET(struct static_cursoricon_object, frame));
172 release_user_handle_ptr( frameobj );
176 static UINT get_icon_steps( struct cursoricon_object *obj )
178 if (obj->is_ani)
180 struct animated_cursoricon_object *ani_icon_data;
182 ani_icon_data = (struct animated_cursoricon_object *) obj;
183 return ani_icon_data->num_steps;
185 return 1;
188 static BOOL free_icon_handle( HICON handle )
190 struct cursoricon_object *obj = free_user_handle( handle, USER_ICON );
192 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
193 else if (obj)
195 ULONG_PTR param = obj->param;
196 UINT i;
198 assert( !obj->rsrc ); /* shared icons can't be freed */
200 if (!obj->is_ani)
202 struct cursoricon_frame *frame = get_icon_frame( obj, 0 );
204 if (frame->alpha) DeleteObject( frame->alpha );
205 if (frame->color) DeleteObject( frame->color );
206 DeleteObject( frame->mask );
207 release_icon_frame( obj, frame );
209 else
211 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) obj;
213 for (i=0; i<ani_icon_data->num_steps; i++)
215 HICON hFrame = ani_icon_data->frames[i];
217 if (hFrame)
219 UINT j;
221 free_icon_handle( ani_icon_data->frames[i] );
222 for (j=0; j<ani_icon_data->num_steps; j++)
224 if (ani_icon_data->frames[j] == hFrame)
225 ani_icon_data->frames[j] = 0;
230 if (!IS_INTRESOURCE( obj->resname )) HeapFree( GetProcessHeap(), 0, obj->resname );
231 HeapFree( GetProcessHeap(), 0, obj );
232 if (wow_handlers.free_icon_param && param) wow_handlers.free_icon_param( param );
233 USER_Driver->pDestroyCursorIcon( handle );
234 return TRUE;
236 return FALSE;
239 ULONG_PTR get_icon_param( HICON handle )
241 ULONG_PTR ret = 0;
242 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
244 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
245 else if (obj)
247 ret = obj->param;
248 release_user_handle_ptr( obj );
250 return ret;
253 ULONG_PTR set_icon_param( HICON handle, ULONG_PTR param )
255 ULONG_PTR ret = 0;
256 struct cursoricon_object *obj = get_user_handle_ptr( handle, USER_ICON );
258 if (obj == OBJ_OTHER_PROCESS) WARN( "icon handle %p from other process\n", handle );
259 else if (obj)
261 ret = obj->param;
262 obj->param = param;
263 release_user_handle_ptr( obj );
265 return ret;
269 /***********************************************************************
270 * map_fileW
272 * Helper function to map a file to memory:
273 * name - file name
274 * [RETURN] ptr - pointer to mapped file
275 * [RETURN] filesize - pointer size of file to be stored if not NULL
277 static const void *map_fileW( LPCWSTR name, LPDWORD filesize )
279 HANDLE hFile, hMapping;
280 LPVOID ptr = NULL;
282 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
283 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
284 if (hFile != INVALID_HANDLE_VALUE)
286 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
287 if (hMapping)
289 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
290 CloseHandle( hMapping );
291 if (filesize)
292 *filesize = GetFileSize( hFile, NULL );
294 CloseHandle( hFile );
296 return ptr;
300 /***********************************************************************
301 * get_dib_image_size
303 * Return the size of a DIB bitmap in bytes.
305 static int get_dib_image_size( int width, int height, int depth )
307 return (((width * depth + 31) / 8) & ~3) * abs( height );
311 /***********************************************************************
312 * bitmap_info_size
314 * Return the size of the bitmap info structure including color table.
316 int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
318 unsigned int colors, size, masks = 0;
320 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
322 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
323 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
324 return sizeof(BITMAPCOREHEADER) + colors *
325 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
327 else /* assume BITMAPINFOHEADER */
329 colors = info->bmiHeader.biClrUsed;
330 if (colors > 256) /* buffer overflow otherwise */
331 colors = 256;
332 if (!colors && (info->bmiHeader.biBitCount <= 8))
333 colors = 1 << info->bmiHeader.biBitCount;
334 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
335 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
336 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
341 /***********************************************************************
342 * copy_bitmap
344 * Helper function to duplicate a bitmap.
346 static HBITMAP copy_bitmap( HBITMAP bitmap )
348 HDC src, dst = 0;
349 HBITMAP new_bitmap = 0;
350 BITMAP bmp;
352 if (!bitmap) return 0;
353 if (!GetObjectW( bitmap, sizeof(bmp), &bmp )) return 0;
355 if ((src = CreateCompatibleDC( 0 )) && (dst = CreateCompatibleDC( 0 )))
357 SelectObject( src, bitmap );
358 if ((new_bitmap = CreateCompatibleBitmap( src, bmp.bmWidth, bmp.bmHeight )))
360 SelectObject( dst, new_bitmap );
361 BitBlt( dst, 0, 0, bmp.bmWidth, bmp.bmHeight, src, 0, 0, SRCCOPY );
364 DeleteDC( dst );
365 DeleteDC( src );
366 return new_bitmap;
370 /***********************************************************************
371 * is_dib_monochrome
373 * Returns whether a DIB can be converted to a monochrome DDB.
375 * A DIB can be converted if its color table contains only black and
376 * white. Black must be the first color in the color table.
378 * Note : If the first color in the color table is white followed by
379 * black, we can't convert it to a monochrome DDB with
380 * SetDIBits, because black and white would be inverted.
382 static BOOL is_dib_monochrome( const BITMAPINFO* info )
384 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
386 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
388 if (((const BITMAPCOREINFO*)info)->bmciHeader.bcBitCount != 1) return FALSE;
390 /* Check if the first color is black */
391 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
393 rgb++;
395 /* Check if the second color is white */
396 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
397 && (rgb->rgbtBlue == 0xff));
399 else return FALSE;
401 else /* assume BITMAPINFOHEADER */
403 const RGBQUAD *rgb = info->bmiColors;
405 if (info->bmiHeader.biBitCount != 1) return FALSE;
407 /* Check if the first color is black */
408 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
409 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
411 rgb++;
413 /* Check if the second color is white */
414 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
415 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
417 else return FALSE;
421 /***********************************************************************
422 * DIB_GetBitmapInfo
424 * Get the info from a bitmap header.
425 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 in case of failure.
427 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
428 LONG *height, WORD *bpp, DWORD *compr )
430 if (header->biSize == sizeof(BITMAPCOREHEADER))
432 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
433 *width = core->bcWidth;
434 *height = core->bcHeight;
435 *bpp = core->bcBitCount;
436 *compr = 0;
437 return 0;
439 else if (header->biSize == sizeof(BITMAPINFOHEADER) ||
440 header->biSize == sizeof(BITMAPV4HEADER) ||
441 header->biSize == sizeof(BITMAPV5HEADER))
443 *width = header->biWidth;
444 *height = header->biHeight;
445 *bpp = header->biBitCount;
446 *compr = header->biCompression;
447 return 1;
449 WARN("unknown/wrong size (%u) for header\n", header->biSize);
450 return -1;
453 /**********************************************************************
454 * get_icon_size
456 BOOL get_icon_size( HICON handle, SIZE *size )
458 struct cursoricon_object *info;
459 struct cursoricon_frame *frame;
461 if (!(info = get_icon_ptr( handle ))) return FALSE;
462 frame = get_icon_frame( info, 0 );
463 size->cx = frame->width;
464 size->cy = frame->height;
465 release_icon_frame( info, frame);
466 release_user_handle_ptr( info );
467 return TRUE;
471 * The following macro functions account for the irregularities of
472 * accessing cursor and icon resources in files and resource entries.
474 typedef BOOL (*fnGetCIEntry)( LPCVOID dir, DWORD size, int n,
475 int *width, int *height, int *bits );
477 /**********************************************************************
478 * CURSORICON_FindBestIcon
480 * Find the icon closest to the requested size and bit depth.
482 static int CURSORICON_FindBestIcon( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
483 int width, int height, int depth, UINT loadflags )
485 int i, cx, cy, bits, bestEntry = -1;
486 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
487 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
489 /* Find Best Fit */
490 iTotalDiff = 0xFFFFFFFF;
491 iColorDiff = 0xFFFFFFFF;
493 if (loadflags & LR_DEFAULTSIZE)
495 if (!width) width = GetSystemMetrics( SM_CXICON );
496 if (!height) height = GetSystemMetrics( SM_CYICON );
498 else if (!width && !height)
500 /* use the size of the first entry */
501 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
502 iTotalDiff = 0;
505 for ( i = 0; iTotalDiff && get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
507 iTempXDiff = abs(width - cx);
508 iTempYDiff = abs(height - cy);
510 if(iTotalDiff > (iTempXDiff + iTempYDiff))
512 iXDiff = iTempXDiff;
513 iYDiff = iTempYDiff;
514 iTotalDiff = iXDiff + iYDiff;
518 /* Find Best Colors for Best Fit */
519 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
521 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
523 iTempColorDiff = abs(depth - bits);
524 if(iColorDiff > iTempColorDiff)
526 bestEntry = i;
527 iColorDiff = iTempColorDiff;
532 return bestEntry;
535 static BOOL CURSORICON_GetResIconEntry( LPCVOID dir, DWORD size, int n,
536 int *width, int *height, int *bits )
538 const CURSORICONDIR *resdir = dir;
539 const ICONRESDIR *icon;
541 if ( resdir->idCount <= n )
542 return FALSE;
543 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
544 return FALSE;
545 icon = &resdir->idEntries[n].ResInfo.icon;
546 *width = icon->bWidth;
547 *height = icon->bHeight;
548 *bits = resdir->idEntries[n].wBitCount;
549 return TRUE;
552 /**********************************************************************
553 * CURSORICON_FindBestCursor
555 * Find the cursor closest to the requested size.
557 * FIXME: parameter 'color' ignored.
559 static int CURSORICON_FindBestCursor( LPCVOID dir, DWORD size, fnGetCIEntry get_entry,
560 int width, int height, int depth, UINT loadflags )
562 int i, maxwidth, maxheight, maxbits, cx, cy, bits, bestEntry = -1;
564 if (loadflags & LR_DEFAULTSIZE)
566 if (!width) width = GetSystemMetrics( SM_CXCURSOR );
567 if (!height) height = GetSystemMetrics( SM_CYCURSOR );
569 else if (!width && !height)
571 /* use the first entry */
572 if (!get_entry( dir, size, 0, &width, &height, &bits )) return -1;
573 return 0;
576 /* First find the largest one smaller than or equal to the requested size*/
578 maxwidth = maxheight = maxbits = 0;
579 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
581 if (cx > width || cy > height) continue;
582 if (cx < maxwidth || cy < maxheight) continue;
583 if (cx == maxwidth && cy == maxheight)
585 if (loadflags & LR_MONOCHROME)
587 if (maxbits && bits >= maxbits) continue;
589 else if (bits <= maxbits) continue;
591 bestEntry = i;
592 maxwidth = cx;
593 maxheight = cy;
594 maxbits = bits;
596 if (bestEntry != -1) return bestEntry;
598 /* Now find the smallest one larger than the requested size */
600 maxwidth = maxheight = 255;
601 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
603 if (cx > maxwidth || cy > maxheight) continue;
604 if (cx == maxwidth && cy == maxheight)
606 if (loadflags & LR_MONOCHROME)
608 if (maxbits && bits >= maxbits) continue;
610 else if (bits <= maxbits) continue;
612 bestEntry = i;
613 maxwidth = cx;
614 maxheight = cy;
615 maxbits = bits;
617 if (bestEntry == -1) bestEntry = 0;
619 return bestEntry;
622 static BOOL CURSORICON_GetResCursorEntry( LPCVOID dir, DWORD size, int n,
623 int *width, int *height, int *bits )
625 const CURSORICONDIR *resdir = dir;
626 const CURSORDIR *cursor;
628 if ( resdir->idCount <= n )
629 return FALSE;
630 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
631 return FALSE;
632 cursor = &resdir->idEntries[n].ResInfo.cursor;
633 *width = cursor->wWidth;
634 *height = cursor->wHeight;
635 *bits = resdir->idEntries[n].wBitCount;
636 if (*height == *width * 2) *height /= 2;
637 return TRUE;
640 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir, DWORD size,
641 int width, int height, int depth,
642 UINT loadflags )
644 int n;
646 n = CURSORICON_FindBestIcon( dir, size, 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, DWORD size,
654 int width, int height, int depth,
655 UINT loadflags )
657 int n = CURSORICON_FindBestCursor( dir, size, 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, DWORD size, 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 if ((const char *)&filedir->idEntries[n + 1] - (const char *)dir > size)
674 return FALSE;
675 entry = &filedir->idEntries[n];
676 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
677 if (info->biSize != sizeof(BITMAPCOREHEADER))
679 if ((const char *)(info + 1) - (const char *)dir > size) return FALSE;
680 *bits = info->biBitCount;
682 else
684 const BITMAPCOREHEADER *coreinfo = (const BITMAPCOREHEADER *)((const char *)dir + entry->dwDIBOffset);
685 if ((const char *)(coreinfo + 1) - (const char *)dir > size) return FALSE;
686 *bits = coreinfo->bcBitCount;
688 *width = entry->bWidth;
689 *height = entry->bHeight;
690 return TRUE;
693 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir, DWORD size,
694 int width, int height, int depth,
695 UINT loadflags )
697 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetFileEntry,
698 width, height, depth, loadflags );
699 if ( n < 0 )
700 return NULL;
701 return &dir->idEntries[n];
704 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir, DWORD size,
705 int width, int height, int depth,
706 UINT loadflags )
708 int n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetFileEntry,
709 width, height, depth, loadflags );
710 if ( n < 0 )
711 return NULL;
712 return &dir->idEntries[n];
715 /***********************************************************************
716 * bmi_has_alpha
718 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
720 int i;
721 BOOL has_alpha = FALSE;
722 const unsigned char *ptr = bits;
724 if (info->bmiHeader.biBitCount != 32) return FALSE;
725 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
726 if ((has_alpha = (ptr[3] != 0))) break;
727 return has_alpha;
730 /***********************************************************************
731 * create_alpha_bitmap
733 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
735 static HBITMAP create_alpha_bitmap( HBITMAP color, const BITMAPINFO *src_info, const void *color_bits )
737 HBITMAP alpha = 0;
738 BITMAPINFO *info = NULL;
739 BITMAP bm;
740 HDC hdc;
741 void *bits;
742 unsigned char *ptr;
743 int i;
745 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
746 if (bm.bmBitsPixel != 32) return 0;
748 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
749 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
750 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
751 info->bmiHeader.biWidth = bm.bmWidth;
752 info->bmiHeader.biHeight = -bm.bmHeight;
753 info->bmiHeader.biPlanes = 1;
754 info->bmiHeader.biBitCount = 32;
755 info->bmiHeader.biCompression = BI_RGB;
756 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
757 info->bmiHeader.biXPelsPerMeter = 0;
758 info->bmiHeader.biYPelsPerMeter = 0;
759 info->bmiHeader.biClrUsed = 0;
760 info->bmiHeader.biClrImportant = 0;
761 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
763 if (src_info)
765 SelectObject( hdc, alpha );
766 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
767 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
768 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
771 else
773 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
774 if (!bmi_has_alpha( info, bits ))
776 DeleteObject( alpha );
777 alpha = 0;
778 goto done;
782 /* pre-multiply by alpha */
783 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
785 unsigned int alpha = ptr[3];
786 ptr[0] = ptr[0] * alpha / 255;
787 ptr[1] = ptr[1] * alpha / 255;
788 ptr[2] = ptr[2] * alpha / 255;
791 done:
792 DeleteDC( hdc );
793 HeapFree( GetProcessHeap(), 0, info );
794 return alpha;
798 /***********************************************************************
799 * create_icon_from_bmi
801 * Create an icon from its BITMAPINFO.
803 static HICON create_icon_from_bmi( const BITMAPINFO *bmi, DWORD maxsize, HMODULE module, LPCWSTR resname,
804 HRSRC rsrc, POINT hotspot, BOOL bIcon, INT width, INT height,
805 UINT cFlag )
807 DWORD size, color_size, mask_size;
808 HBITMAP color = 0, mask = 0, alpha = 0;
809 const void *color_bits, *mask_bits;
810 BITMAPINFO *bmi_copy;
811 BOOL ret = FALSE;
812 BOOL do_stretch;
813 HICON hObj = 0;
814 HDC screen_dc;
815 HDC hdc = 0;
816 LONG bmi_width, bmi_height;
817 WORD bpp;
818 DWORD compr;
820 /* Check bitmap header */
822 if (maxsize < sizeof(BITMAPCOREHEADER))
824 WARN( "invalid size %u\n", maxsize );
825 return 0;
827 if (maxsize < bmi->bmiHeader.biSize)
829 WARN( "invalid header size %u\n", bmi->bmiHeader.biSize );
830 return 0;
832 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
833 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
834 (bmi->bmiHeader.biCompression != BI_RGB &&
835 bmi->bmiHeader.biCompression != BI_BITFIELDS)) )
837 WARN( "invalid bitmap header %u\n", bmi->bmiHeader.biSize );
838 return 0;
841 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
842 DIB_GetBitmapInfo(&bmi->bmiHeader, &bmi_width, &bmi_height, &bpp, &compr);
843 color_size = get_dib_image_size( bmi_width, bmi_height / 2,
844 bpp );
845 mask_size = get_dib_image_size( bmi_width, bmi_height / 2, 1 );
846 if (size > maxsize || color_size > maxsize - size)
848 WARN( "truncated file %u < %u+%u+%u\n", maxsize, size, color_size, mask_size );
849 return 0;
851 if (mask_size > maxsize - size - color_size) mask_size = 0; /* no mask */
853 if (cFlag & LR_DEFAULTSIZE)
855 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
856 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
858 else
860 if (!width) width = bmi_width;
861 if (!height) height = bmi_height/2;
863 do_stretch = (bmi_height/2 != height) ||
864 (bmi_width != width);
866 /* Scale the hotspot */
867 if (bIcon)
869 hotspot.x = width / 2;
870 hotspot.y = height / 2;
872 else if (do_stretch)
874 hotspot.x = (hotspot.x * width) / bmi_width;
875 hotspot.y = (hotspot.y * height) / (bmi_height / 2);
878 if (!(screen_dc = get_screen_dc())) return 0;
880 if (!(bmi_copy = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
881 return 0;
882 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
884 memcpy( bmi_copy, bmi, size );
885 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
886 bmi_copy->bmiHeader.biHeight /= 2;
887 else
888 ((BITMAPCOREINFO *)bmi_copy)->bmciHeader.bcHeight /= 2;
889 bmi_height /= 2;
891 color_bits = (const char*)bmi + size;
892 mask_bits = (const char*)color_bits + color_size;
894 alpha = 0;
895 if (is_dib_monochrome( bmi ))
897 if (!(mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
898 color = 0;
900 /* copy color data into second half of mask bitmap */
901 SelectObject( hdc, mask );
902 StretchDIBits( hdc, 0, height, width, height,
903 0, 0, bmi_width, bmi_height,
904 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
906 else
908 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
909 if (!(color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
910 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
912 DeleteObject( mask );
913 goto done;
915 SelectObject( hdc, color );
916 StretchDIBits( hdc, 0, 0, width, height,
917 0, 0, bmi_width, bmi_height,
918 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
920 if (bmi_has_alpha( bmi_copy, color_bits ))
921 alpha = create_alpha_bitmap( color, bmi_copy, color_bits );
923 /* convert info to monochrome to copy the mask */
924 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
926 RGBQUAD *rgb = bmi_copy->bmiColors;
928 bmi_copy->bmiHeader.biBitCount = 1;
929 bmi_copy->bmiHeader.biClrUsed = bmi_copy->bmiHeader.biClrImportant = 2;
930 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
931 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
932 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
934 else
936 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)bmi_copy) + 1);
938 ((BITMAPCOREINFO *)bmi_copy)->bmciHeader.bcBitCount = 1;
939 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
940 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
944 if (mask_size)
946 SelectObject( hdc, mask );
947 StretchDIBits( hdc, 0, 0, width, height,
948 0, 0, bmi_width, bmi_height,
949 mask_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
951 ret = TRUE;
953 done:
954 DeleteDC( hdc );
955 HeapFree( GetProcessHeap(), 0, bmi_copy );
957 if (ret)
958 hObj = alloc_icon_handle( FALSE, 0 );
959 if (hObj)
961 struct cursoricon_object *info = get_icon_ptr( hObj );
962 struct cursoricon_frame *frame;
964 info->is_icon = bIcon;
965 info->module = module;
966 info->hotspot = hotspot;
967 frame = get_icon_frame( info, 0 );
968 frame->delay = ~0;
969 frame->width = width;
970 frame->height = height;
971 frame->color = color;
972 frame->mask = mask;
973 frame->alpha = alpha;
974 release_icon_frame( info, frame );
975 if (!IS_INTRESOURCE(resname))
977 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
978 if (info->resname) strcpyW( info->resname, resname );
980 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
982 if (module && (cFlag & LR_SHARED))
984 info->rsrc = rsrc;
985 list_add_head( &icon_cache, &info->entry );
987 release_user_handle_ptr( info );
989 else
991 DeleteObject( color );
992 DeleteObject( alpha );
993 DeleteObject( mask );
995 return hObj;
999 /**********************************************************************
1000 * .ANI cursor support
1002 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
1003 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
1004 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
1006 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
1007 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
1008 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
1009 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
1010 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
1011 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
1012 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
1014 #define ANI_FLAG_ICON 0x1
1015 #define ANI_FLAG_SEQUENCE 0x2
1017 typedef struct {
1018 DWORD header_size;
1019 DWORD num_frames;
1020 DWORD num_steps;
1021 DWORD width;
1022 DWORD height;
1023 DWORD bpp;
1024 DWORD num_planes;
1025 DWORD display_rate;
1026 DWORD flags;
1027 } ani_header;
1029 typedef struct {
1030 DWORD data_size;
1031 const unsigned char *data;
1032 } riff_chunk_t;
1034 static void dump_ani_header( const ani_header *header )
1036 TRACE(" header size: %d\n", header->header_size);
1037 TRACE(" frames: %d\n", header->num_frames);
1038 TRACE(" steps: %d\n", header->num_steps);
1039 TRACE(" width: %d\n", header->width);
1040 TRACE(" height: %d\n", header->height);
1041 TRACE(" bpp: %d\n", header->bpp);
1042 TRACE(" planes: %d\n", header->num_planes);
1043 TRACE(" display rate: %d\n", header->display_rate);
1044 TRACE(" flags: 0x%08x\n", header->flags);
1049 * RIFF:
1050 * DWORD "RIFF"
1051 * DWORD size
1052 * DWORD riff_id
1053 * BYTE[] data
1055 * LIST:
1056 * DWORD "LIST"
1057 * DWORD size
1058 * DWORD list_id
1059 * BYTE[] data
1061 * CHUNK:
1062 * DWORD chunk_id
1063 * DWORD size
1064 * BYTE[] data
1066 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1068 const unsigned char *ptr = parent_chunk->data;
1069 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1071 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1073 while (ptr < end)
1075 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1076 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1078 ptr += sizeof(DWORD);
1079 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1080 ptr += sizeof(DWORD);
1081 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1082 chunk->data = ptr;
1084 return;
1087 ptr += sizeof(DWORD);
1088 ptr += (*(const DWORD *)ptr + 1) & ~1;
1089 ptr += sizeof(DWORD);
1095 * .ANI layout:
1097 * RIFF:'ACON' RIFF chunk
1098 * |- CHUNK:'anih' Header
1099 * |- CHUNK:'seq ' Sequence information (optional)
1100 * \- LIST:'fram' Frame list
1101 * |- CHUNK:icon Cursor frames
1102 * |- CHUNK:icon
1103 * |- ...
1104 * \- CHUNK:icon
1106 static HCURSOR CURSORICON_CreateIconFromANI( const BYTE *bits, DWORD bits_size, INT width, INT height,
1107 INT depth, BOOL is_icon, UINT loadflags )
1109 struct animated_cursoricon_object *ani_icon_data;
1110 struct cursoricon_object *info;
1111 DWORD *frame_rates = NULL;
1112 DWORD *frame_seq = NULL;
1113 ani_header header;
1114 BOOL use_seq = FALSE;
1115 HCURSOR cursor;
1116 UINT i;
1117 BOOL error = FALSE;
1118 HICON *frames;
1120 riff_chunk_t root_chunk = { bits_size, bits };
1121 riff_chunk_t ACON_chunk = {0};
1122 riff_chunk_t anih_chunk = {0};
1123 riff_chunk_t fram_chunk = {0};
1124 riff_chunk_t rate_chunk = {0};
1125 riff_chunk_t seq_chunk = {0};
1126 const unsigned char *icon_chunk;
1127 const unsigned char *icon_data;
1129 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1131 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1132 if (!ACON_chunk.data)
1134 ERR("Failed to get root chunk.\n");
1135 return 0;
1138 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1139 if (!anih_chunk.data)
1141 ERR("Failed to get 'anih' chunk.\n");
1142 return 0;
1144 memcpy( &header, anih_chunk.data, sizeof(header) );
1145 dump_ani_header( &header );
1147 if (!(header.flags & ANI_FLAG_ICON))
1149 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1150 return 0;
1153 if (header.flags & ANI_FLAG_SEQUENCE)
1155 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1156 if (seq_chunk.data)
1158 frame_seq = (DWORD *) seq_chunk.data;
1159 use_seq = TRUE;
1161 else
1163 FIXME("Sequence data expected but not found, assuming steps == frames.\n");
1164 header.num_steps = header.num_frames;
1168 riff_find_chunk( ANI_rate_ID, 0, &ACON_chunk, &rate_chunk );
1169 if (rate_chunk.data)
1170 frame_rates = (DWORD *) rate_chunk.data;
1172 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1173 if (!fram_chunk.data)
1175 ERR("Failed to get icon list.\n");
1176 return 0;
1179 cursor = alloc_icon_handle( TRUE, header.num_steps );
1180 if (!cursor) return 0;
1181 frames = HeapAlloc( GetProcessHeap(), 0, sizeof(*frames) * header.num_frames );
1182 if (!frames)
1184 free_icon_handle( cursor );
1185 return 0;
1188 info = get_icon_ptr( cursor );
1189 ani_icon_data = (struct animated_cursoricon_object *) info;
1190 info->is_icon = is_icon;
1191 ani_icon_data->num_frames = header.num_frames;
1193 /* The .ANI stores the display rate in jiffies (1/60s) */
1194 info->delay = header.display_rate;
1196 icon_chunk = fram_chunk.data;
1197 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1198 for (i=0; i<header.num_frames; i++)
1200 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1201 const CURSORICONFILEDIRENTRY *entry;
1202 INT frameWidth, frameHeight;
1203 const BITMAPINFO *bmi;
1205 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1206 bits + bits_size - icon_data,
1207 width, height, depth, loadflags );
1209 info->hotspot.x = entry->xHotspot;
1210 info->hotspot.y = entry->yHotspot;
1211 if (!header.width || !header.height)
1213 frameWidth = entry->bWidth;
1214 frameHeight = entry->bHeight;
1216 else
1218 frameWidth = header.width;
1219 frameHeight = header.height;
1222 frames[i] = NULL;
1223 if (entry->dwDIBOffset < bits + bits_size - icon_data)
1225 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1226 /* Grab a frame from the animation */
1227 frames[i] = create_icon_from_bmi( bmi, bits + bits_size - (const BYTE *)bmi,
1228 NULL, NULL, NULL, info->hotspot,
1229 is_icon, frameWidth, frameHeight, loadflags );
1232 if (!frames[i])
1234 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1235 error = TRUE;
1236 if (i == 0)
1238 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1239 ani_icon_data->num_frames = 0;
1240 release_user_handle_ptr( info );
1241 free_icon_handle( cursor );
1242 HeapFree( GetProcessHeap(), 0, frames );
1243 return 0;
1245 break;
1248 /* Advance to the next chunk */
1249 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1250 icon_data = icon_chunk + (2 * sizeof(DWORD));
1253 /* There was an error but we at least decoded the first frame, so just use that frame */
1254 if (error)
1256 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1257 for (i=1; i<ani_icon_data->num_frames; i++)
1258 free_icon_handle( ani_icon_data->frames[i] );
1259 use_seq = FALSE;
1260 info->delay = 0;
1261 ani_icon_data->num_steps = 1;
1262 ani_icon_data->num_frames = 1;
1265 /* Setup the animated frames in the correct sequence */
1266 for (i=0; i<ani_icon_data->num_steps; i++)
1268 DWORD frame_id = use_seq ? frame_seq[i] : i;
1269 struct cursoricon_frame *frame;
1271 if (frame_id >= ani_icon_data->num_frames)
1273 frame_id = ani_icon_data->num_frames-1;
1274 ERR_(cursor)("Sequence indicates frame past end of list, corrupt?\n");
1276 ani_icon_data->frames[i] = frames[frame_id];
1277 frame = get_icon_frame( info, i );
1278 if (frame_rates)
1279 frame->delay = frame_rates[i];
1280 else
1281 frame->delay = ~0;
1282 release_icon_frame( info, frame );
1285 HeapFree( GetProcessHeap(), 0, frames );
1286 release_user_handle_ptr( info );
1288 return cursor;
1292 /**********************************************************************
1293 * CreateIconFromResourceEx (USER32.@)
1295 * FIXME: Convert to mono when cFlag is LR_MONOCHROME.
1297 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1298 BOOL bIcon, DWORD dwVersion,
1299 INT width, INT height,
1300 UINT cFlag )
1302 POINT hotspot;
1303 const BITMAPINFO *bmi;
1305 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1306 bits, cbSize, dwVersion, width, height,
1307 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1309 if (!bits) return 0;
1311 if (dwVersion == 0x00020000)
1313 FIXME_(cursor)("\t2.xx resources are not supported\n");
1314 return 0;
1317 /* Check if the resource is an animated icon/cursor */
1318 if (!memcmp(bits, "RIFF", 4))
1319 return CURSORICON_CreateIconFromANI( bits, cbSize, width, height,
1320 0 /* default depth */, bIcon, cFlag );
1322 if (bIcon)
1324 hotspot.x = width / 2;
1325 hotspot.y = height / 2;
1326 bmi = (BITMAPINFO *)bits;
1328 else /* get the hotspot */
1330 const SHORT *pt = (const SHORT *)bits;
1331 hotspot.x = pt[0];
1332 hotspot.y = pt[1];
1333 bmi = (const BITMAPINFO *)(pt + 2);
1334 cbSize -= 2 * sizeof(*pt);
1337 return create_icon_from_bmi( bmi, cbSize, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1341 /**********************************************************************
1342 * CreateIconFromResource (USER32.@)
1344 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1345 BOOL bIcon, DWORD dwVersion)
1347 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1351 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1352 INT width, INT height, INT depth,
1353 BOOL fCursor, UINT loadflags)
1355 const CURSORICONFILEDIRENTRY *entry;
1356 const CURSORICONFILEDIR *dir;
1357 DWORD filesize = 0;
1358 HICON hIcon = 0;
1359 const BYTE *bits;
1360 POINT hotspot;
1362 TRACE("loading %s\n", debugstr_w( filename ));
1364 bits = map_fileW( filename, &filesize );
1365 if (!bits)
1366 return hIcon;
1368 /* Check for .ani. */
1369 if (memcmp( bits, "RIFF", 4 ) == 0)
1371 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, !fCursor, loadflags );
1372 goto end;
1375 dir = (const CURSORICONFILEDIR*) bits;
1376 if ( filesize < FIELD_OFFSET( CURSORICONFILEDIR, idEntries[dir->idCount] ))
1377 goto end;
1379 if ( fCursor )
1380 entry = CURSORICON_FindBestCursorFile( dir, filesize, width, height, depth, loadflags );
1381 else
1382 entry = CURSORICON_FindBestIconFile( dir, filesize, width, height, depth, loadflags );
1384 if ( !entry )
1385 goto end;
1387 /* check that we don't run off the end of the file */
1388 if ( entry->dwDIBOffset > filesize )
1389 goto end;
1390 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1391 goto end;
1393 hotspot.x = entry->xHotspot;
1394 hotspot.y = entry->yHotspot;
1395 hIcon = create_icon_from_bmi( (const BITMAPINFO *)&bits[entry->dwDIBOffset], filesize - entry->dwDIBOffset,
1396 NULL, NULL, NULL, hotspot, !fCursor, width, height, loadflags );
1397 end:
1398 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1399 UnmapViewOfFile( bits );
1400 return hIcon;
1403 /**********************************************************************
1404 * CURSORICON_Load
1406 * Load a cursor or icon from resource or file.
1408 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1409 INT width, INT height, INT depth,
1410 BOOL fCursor, UINT loadflags)
1412 HANDLE handle = 0;
1413 HICON hIcon = 0;
1414 HRSRC hRsrc;
1415 DWORD size;
1416 const CURSORICONDIR *dir;
1417 const CURSORICONDIRENTRY *dirEntry;
1418 const BYTE *bits;
1419 WORD wResId;
1420 POINT hotspot;
1422 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1423 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1425 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1426 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1428 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1430 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1431 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1433 /* Get directory resource ID */
1435 if (!(hRsrc = FindResourceW( hInstance, name,
1436 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1438 /* try animated resource */
1439 if (!(hRsrc = FindResourceW( hInstance, name,
1440 (LPWSTR)(fCursor ? RT_ANICURSOR : RT_ANIICON) ))) return 0;
1441 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1442 bits = LockResource( handle );
1443 return CURSORICON_CreateIconFromANI( bits, SizeofResource( hInstance, handle ),
1444 width, height, depth, !fCursor, loadflags );
1447 /* Find the best entry in the directory */
1449 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1450 if (!(dir = LockResource( handle ))) return 0;
1451 size = SizeofResource( hInstance, hRsrc );
1452 if (fCursor)
1453 dirEntry = CURSORICON_FindBestCursorRes( dir, size, width, height, depth, loadflags );
1454 else
1455 dirEntry = CURSORICON_FindBestIconRes( dir, size, width, height, depth, loadflags );
1456 if (!dirEntry) return 0;
1457 wResId = dirEntry->wResId;
1458 FreeResource( handle );
1460 /* Load the resource */
1462 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1463 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1465 /* If shared icon, check whether it was already loaded */
1466 if (loadflags & LR_SHARED)
1468 struct cursoricon_object *ptr;
1470 USER_Lock();
1471 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1473 if (ptr->module != hInstance) continue;
1474 if (ptr->rsrc != hRsrc) continue;
1475 hIcon = ptr->obj.handle;
1476 break;
1478 USER_Unlock();
1479 if (hIcon) return hIcon;
1482 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1483 size = SizeofResource( hInstance, hRsrc );
1484 bits = LockResource( handle );
1486 if (!fCursor)
1488 hotspot.x = width / 2;
1489 hotspot.y = height / 2;
1491 else /* get the hotspot */
1493 const SHORT *pt = (const SHORT *)bits;
1494 hotspot.x = pt[0];
1495 hotspot.y = pt[1];
1496 bits += 2 * sizeof(SHORT);
1497 size -= 2 * sizeof(SHORT);
1499 hIcon = create_icon_from_bmi( (const BITMAPINFO *)bits, size, hInstance, name, hRsrc,
1500 hotspot, !fCursor, width, height, loadflags );
1501 FreeResource( handle );
1502 return hIcon;
1506 /***********************************************************************
1507 * CreateCursor (USER32.@)
1509 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1510 INT xHotSpot, INT yHotSpot,
1511 INT nWidth, INT nHeight,
1512 LPCVOID lpANDbits, LPCVOID lpXORbits )
1514 ICONINFO info;
1515 HCURSOR hCursor;
1517 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1518 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1520 info.fIcon = FALSE;
1521 info.xHotspot = xHotSpot;
1522 info.yHotspot = yHotSpot;
1523 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1524 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1525 hCursor = CreateIconIndirect( &info );
1526 DeleteObject( info.hbmMask );
1527 DeleteObject( info.hbmColor );
1528 return hCursor;
1532 /***********************************************************************
1533 * CreateIcon (USER32.@)
1535 * Creates an icon based on the specified bitmaps. The bitmaps must be
1536 * provided in a device dependent format and will be resized to
1537 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1538 * depth. The provided bitmaps must be top-down bitmaps.
1539 * Although Windows does not support 15bpp(*) this API must support it
1540 * for Winelib applications.
1542 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1543 * format!
1545 * RETURNS
1546 * Success: handle to an icon
1547 * Failure: NULL
1549 * FIXME: Do we need to resize the bitmaps?
1551 HICON WINAPI CreateIcon(
1552 HINSTANCE hInstance, /* [in] the application's hInstance */
1553 INT nWidth, /* [in] the width of the provided bitmaps */
1554 INT nHeight, /* [in] the height of the provided bitmaps */
1555 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1556 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1557 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1558 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1560 ICONINFO iinfo;
1561 HICON hIcon;
1563 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1564 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1566 iinfo.fIcon = TRUE;
1567 iinfo.xHotspot = nWidth / 2;
1568 iinfo.yHotspot = nHeight / 2;
1569 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1570 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1572 hIcon = CreateIconIndirect( &iinfo );
1574 DeleteObject( iinfo.hbmMask );
1575 DeleteObject( iinfo.hbmColor );
1577 return hIcon;
1581 /***********************************************************************
1582 * CopyIcon (USER32.@)
1584 HICON WINAPI CopyIcon( HICON hIcon )
1586 struct cursoricon_object *ptrOld, *ptrNew;
1587 HICON hNew;
1589 if (!(ptrOld = get_icon_ptr( hIcon )))
1591 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1592 return 0;
1594 if ((hNew = alloc_icon_handle( FALSE, 0 )))
1596 struct cursoricon_frame *frameOld, *frameNew;
1598 ptrNew = get_icon_ptr( hNew );
1599 ptrNew->is_icon = ptrOld->is_icon;
1600 ptrNew->hotspot = ptrOld->hotspot;
1601 if (!(frameOld = get_icon_frame( ptrOld, 0 )))
1603 release_user_handle_ptr( ptrOld );
1604 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1605 return 0;
1607 if (!(frameNew = get_icon_frame( ptrNew, 0 )))
1609 release_icon_frame( ptrOld, frameOld );
1610 release_user_handle_ptr( ptrOld );
1611 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1612 return 0;
1614 frameNew->delay = 0;
1615 frameNew->width = frameOld->width;
1616 frameNew->height = frameOld->height;
1617 frameNew->mask = copy_bitmap( frameOld->mask );
1618 frameNew->color = copy_bitmap( frameOld->color );
1619 frameNew->alpha = copy_bitmap( frameOld->alpha );
1620 release_icon_frame( ptrOld, frameOld );
1621 release_icon_frame( ptrNew, frameNew );
1622 release_user_handle_ptr( ptrNew );
1624 release_user_handle_ptr( ptrOld );
1625 return hNew;
1629 /***********************************************************************
1630 * DestroyIcon (USER32.@)
1632 BOOL WINAPI DestroyIcon( HICON hIcon )
1634 BOOL ret = FALSE;
1635 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1637 TRACE_(icon)("%p\n", hIcon );
1639 if (obj)
1641 BOOL shared = (obj->rsrc != NULL);
1642 release_user_handle_ptr( obj );
1643 ret = (GetCursor() != hIcon);
1644 if (!shared) free_icon_handle( hIcon );
1646 return ret;
1650 /***********************************************************************
1651 * DestroyCursor (USER32.@)
1653 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1655 return DestroyIcon( hCursor );
1658 /***********************************************************************
1659 * DrawIcon (USER32.@)
1661 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1663 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1666 /***********************************************************************
1667 * SetCursor (USER32.@)
1669 * Set the cursor shape.
1671 * RETURNS
1672 * A handle to the previous cursor shape.
1674 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1676 struct cursoricon_object *obj;
1677 HCURSOR hOldCursor;
1678 int show_count;
1679 BOOL ret;
1681 TRACE("%p\n", hCursor);
1683 SERVER_START_REQ( set_cursor )
1685 req->flags = SET_CURSOR_HANDLE;
1686 req->handle = wine_server_user_handle( hCursor );
1687 if ((ret = !wine_server_call_err( req )))
1689 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1690 show_count = reply->prev_count;
1693 SERVER_END_REQ;
1695 if (!ret) return 0;
1696 USER_Driver->pSetCursor( show_count >= 0 ? hCursor : 0 );
1698 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1699 release_user_handle_ptr( obj );
1700 return hOldCursor;
1703 /***********************************************************************
1704 * ShowCursor (USER32.@)
1706 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1708 HCURSOR cursor;
1709 int increment = bShow ? 1 : -1;
1710 int count;
1712 SERVER_START_REQ( set_cursor )
1714 req->flags = SET_CURSOR_COUNT;
1715 req->show_count = increment;
1716 wine_server_call( req );
1717 cursor = wine_server_ptr_handle( reply->prev_handle );
1718 count = reply->prev_count + increment;
1720 SERVER_END_REQ;
1722 TRACE("%d, count=%d\n", bShow, count );
1724 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1725 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1727 return count;
1730 /***********************************************************************
1731 * GetCursor (USER32.@)
1733 HCURSOR WINAPI GetCursor(void)
1735 HCURSOR ret;
1737 SERVER_START_REQ( set_cursor )
1739 req->flags = 0;
1740 wine_server_call( req );
1741 ret = wine_server_ptr_handle( reply->prev_handle );
1743 SERVER_END_REQ;
1744 return ret;
1748 /***********************************************************************
1749 * ClipCursor (USER32.@)
1751 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1753 BOOL ret;
1754 RECT new_rect;
1756 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1758 if (rect && (rect->left > rect->right || rect->top > rect->bottom)) return FALSE;
1760 SERVER_START_REQ( set_cursor )
1762 req->clip_msg = WM_WINE_CLIPCURSOR;
1763 if (rect)
1765 req->flags = SET_CURSOR_CLIP;
1766 req->clip.left = rect->left;
1767 req->clip.top = rect->top;
1768 req->clip.right = rect->right;
1769 req->clip.bottom = rect->bottom;
1771 else req->flags = SET_CURSOR_NOCLIP;
1773 if ((ret = !wine_server_call( req )))
1775 new_rect.left = reply->new_clip.left;
1776 new_rect.top = reply->new_clip.top;
1777 new_rect.right = reply->new_clip.right;
1778 new_rect.bottom = reply->new_clip.bottom;
1781 SERVER_END_REQ;
1782 if (ret) USER_Driver->pClipCursor( &new_rect );
1783 return ret;
1787 /***********************************************************************
1788 * GetClipCursor (USER32.@)
1790 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1792 BOOL ret;
1794 if (!rect) return FALSE;
1796 SERVER_START_REQ( set_cursor )
1798 req->flags = 0;
1799 if ((ret = !wine_server_call( req )))
1801 rect->left = reply->new_clip.left;
1802 rect->top = reply->new_clip.top;
1803 rect->right = reply->new_clip.right;
1804 rect->bottom = reply->new_clip.bottom;
1807 SERVER_END_REQ;
1808 return ret;
1812 /***********************************************************************
1813 * SetSystemCursor (USER32.@)
1815 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1817 FIXME("(%p,%08x),stub!\n", hcur, id);
1818 return TRUE;
1822 /**********************************************************************
1823 * LookupIconIdFromDirectoryEx (USER32.@)
1825 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1826 INT width, INT height, UINT cFlag )
1828 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1829 UINT retVal = 0;
1830 if( dir && !dir->idReserved && (dir->idType & 3) )
1832 const CURSORICONDIRENTRY* entry;
1834 const HDC hdc = GetDC(0);
1835 const int depth = (cFlag & LR_MONOCHROME) ?
1836 1 : GetDeviceCaps(hdc, BITSPIXEL);
1837 ReleaseDC(0, hdc);
1839 if( bIcon )
1840 entry = CURSORICON_FindBestIconRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1841 else
1842 entry = CURSORICON_FindBestCursorRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1844 if( entry ) retVal = entry->wResId;
1846 else WARN_(cursor)("invalid resource directory\n");
1847 return retVal;
1850 /**********************************************************************
1851 * LookupIconIdFromDirectory (USER32.@)
1853 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1855 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1858 /***********************************************************************
1859 * LoadCursorW (USER32.@)
1861 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1863 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1865 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1866 LR_SHARED | LR_DEFAULTSIZE );
1869 /***********************************************************************
1870 * LoadCursorA (USER32.@)
1872 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1874 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1876 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1877 LR_SHARED | LR_DEFAULTSIZE );
1880 /***********************************************************************
1881 * LoadCursorFromFileW (USER32.@)
1883 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1885 TRACE("%s\n", debugstr_w(name));
1887 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1888 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1891 /***********************************************************************
1892 * LoadCursorFromFileA (USER32.@)
1894 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1896 TRACE("%s\n", debugstr_a(name));
1898 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1899 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1902 /***********************************************************************
1903 * LoadIconW (USER32.@)
1905 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1907 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1909 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1910 LR_SHARED | LR_DEFAULTSIZE );
1913 /***********************************************************************
1914 * LoadIconA (USER32.@)
1916 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1918 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1920 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1921 LR_SHARED | LR_DEFAULTSIZE );
1924 /**********************************************************************
1925 * GetCursorFrameInfo (USER32.@)
1927 * NOTES
1928 * So far no use has been found for the second parameter, it is currently presumed
1929 * that this parameter is reserved for future use.
1931 * PARAMS
1932 * hCursor [I] Handle to cursor for which to retrieve information
1933 * reserved [I] No purpose has been found for this parameter (may be NULL)
1934 * istep [I] The step of the cursor for which to retrieve information
1935 * rate_jiffies [O] Pointer to DWORD that receives the frame-specific delay (cannot be NULL)
1936 * num_steps [O] Pointer to DWORD that receives the number of steps in the cursor (cannot be NULL)
1938 * RETURNS
1939 * Success: Handle to a frame of the cursor (specified by istep)
1940 * Failure: NULL cursor (0)
1942 HCURSOR WINAPI GetCursorFrameInfo(HCURSOR hCursor, DWORD reserved, DWORD istep, DWORD *rate_jiffies, DWORD *num_steps)
1944 struct cursoricon_object *ptr;
1945 HCURSOR ret = 0;
1946 UINT icon_steps;
1948 if (rate_jiffies == NULL || num_steps == NULL) return 0;
1950 if (!(ptr = get_icon_ptr( hCursor ))) return 0;
1952 TRACE("%p => %d %d %p %p\n", hCursor, reserved, istep, rate_jiffies, num_steps);
1953 if (reserved != 0)
1954 FIXME("Second parameter non-zero (%d), please report this!\n", reserved);
1956 icon_steps = get_icon_steps(ptr);
1957 if (istep < icon_steps || !ptr->is_ani)
1959 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) ptr;
1960 UINT icon_frames = 1;
1962 if (ptr->is_ani)
1963 icon_frames = ani_icon_data->num_frames;
1964 if (ptr->is_ani && icon_frames > 1)
1965 ret = ani_icon_data->frames[istep];
1966 else
1967 ret = hCursor;
1968 if (icon_frames == 1)
1970 *rate_jiffies = 0;
1971 *num_steps = 1;
1973 else if (icon_steps == 1)
1975 *num_steps = ~0;
1976 *rate_jiffies = ptr->delay;
1978 else if (istep < icon_steps)
1980 struct cursoricon_frame *frame;
1982 *num_steps = icon_steps;
1983 frame = get_icon_frame( ptr, istep );
1984 if (get_icon_steps(ptr) == 1)
1985 *num_steps = ~0;
1986 else
1987 *num_steps = get_icon_steps(ptr);
1988 /* If this specific frame does not have a delay then use the global delay */
1989 if (frame->delay == ~0)
1990 *rate_jiffies = ptr->delay;
1991 else
1992 *rate_jiffies = frame->delay;
1993 release_icon_frame( ptr, frame );
1997 release_user_handle_ptr( ptr );
1999 return ret;
2002 /**********************************************************************
2003 * GetIconInfo (USER32.@)
2005 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
2007 ICONINFOEXW infoW;
2009 infoW.cbSize = sizeof(infoW);
2010 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
2011 iconinfo->fIcon = infoW.fIcon;
2012 iconinfo->xHotspot = infoW.xHotspot;
2013 iconinfo->yHotspot = infoW.yHotspot;
2014 iconinfo->hbmColor = infoW.hbmColor;
2015 iconinfo->hbmMask = infoW.hbmMask;
2016 return TRUE;
2019 /**********************************************************************
2020 * GetIconInfoExA (USER32.@)
2022 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
2024 ICONINFOEXW infoW;
2026 if (info->cbSize != sizeof(*info))
2028 SetLastError( ERROR_INVALID_PARAMETER );
2029 return FALSE;
2031 infoW.cbSize = sizeof(infoW);
2032 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
2033 info->fIcon = infoW.fIcon;
2034 info->xHotspot = infoW.xHotspot;
2035 info->yHotspot = infoW.yHotspot;
2036 info->hbmColor = infoW.hbmColor;
2037 info->hbmMask = infoW.hbmMask;
2038 info->wResID = infoW.wResID;
2039 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
2040 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
2041 return TRUE;
2044 /**********************************************************************
2045 * GetIconInfoExW (USER32.@)
2047 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
2049 struct cursoricon_frame *frame;
2050 struct cursoricon_object *ptr;
2051 HMODULE module;
2052 BOOL ret = TRUE;
2054 if (info->cbSize != sizeof(*info))
2056 SetLastError( ERROR_INVALID_PARAMETER );
2057 return FALSE;
2059 if (!(ptr = get_icon_ptr( icon )))
2061 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2062 return FALSE;
2065 frame = get_icon_frame( ptr, 0 );
2066 if (!frame)
2068 release_user_handle_ptr( ptr );
2069 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2070 return FALSE;
2073 TRACE("%p => %dx%d\n", icon, frame->width, frame->height);
2075 info->fIcon = ptr->is_icon;
2076 info->xHotspot = ptr->hotspot.x;
2077 info->yHotspot = ptr->hotspot.y;
2078 info->hbmColor = copy_bitmap( frame->color );
2079 info->hbmMask = copy_bitmap( frame->mask );
2080 info->wResID = 0;
2081 info->szModName[0] = 0;
2082 info->szResName[0] = 0;
2083 if (ptr->module)
2085 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
2086 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
2088 if (!info->hbmMask || (!info->hbmColor && frame->color))
2090 DeleteObject( info->hbmMask );
2091 DeleteObject( info->hbmColor );
2092 ret = FALSE;
2094 module = ptr->module;
2095 release_icon_frame( ptr, frame );
2096 release_user_handle_ptr( ptr );
2097 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
2098 return ret;
2101 /* copy an icon bitmap, even when it can't be selected into a DC */
2102 /* helper for CreateIconIndirect */
2103 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
2104 HBITMAP src, int width, int height )
2106 HDC hdc = CreateCompatibleDC( 0 );
2108 if (!SelectObject( hdc, src )) /* do it the hard way */
2110 BITMAPINFO *info;
2111 void *bits;
2113 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
2114 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2115 info->bmiHeader.biWidth = width;
2116 info->bmiHeader.biHeight = height;
2117 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
2118 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
2119 info->bmiHeader.biCompression = BI_RGB;
2120 info->bmiHeader.biSizeImage = get_dib_image_size( width, height, info->bmiHeader.biBitCount );
2121 info->bmiHeader.biXPelsPerMeter = 0;
2122 info->bmiHeader.biYPelsPerMeter = 0;
2123 info->bmiHeader.biClrUsed = 0;
2124 info->bmiHeader.biClrImportant = 0;
2125 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
2126 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
2127 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
2128 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
2130 HeapFree( GetProcessHeap(), 0, bits );
2131 HeapFree( GetProcessHeap(), 0, info );
2133 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
2135 DeleteDC( hdc );
2138 /**********************************************************************
2139 * CreateIconIndirect (USER32.@)
2141 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2143 BITMAP bmpXor, bmpAnd;
2144 HICON hObj;
2145 HBITMAP color = 0, mask;
2146 int width, height;
2147 HDC hdc;
2149 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2150 iconinfo->hbmColor, iconinfo->hbmMask,
2151 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2153 if (!iconinfo->hbmMask) return 0;
2155 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2156 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2157 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2158 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2160 if (iconinfo->hbmColor)
2162 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2163 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2164 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2165 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2167 width = bmpXor.bmWidth;
2168 height = bmpXor.bmHeight;
2169 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1 || bmpAnd.bmPlanes * bmpAnd.bmBitsPixel != 1)
2171 color = CreateCompatibleBitmap( get_screen_dc(), width, height );
2172 mask = CreateBitmap( width, height, 1, 1, NULL );
2174 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2176 else
2178 width = bmpAnd.bmWidth;
2179 height = bmpAnd.bmHeight;
2180 mask = CreateBitmap( width, height, 1, 1, NULL );
2183 hdc = CreateCompatibleDC( 0 );
2184 SelectObject( hdc, mask );
2185 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
2187 if (color)
2189 SelectObject( hdc, color );
2190 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
2192 else if (iconinfo->hbmColor)
2194 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
2196 else height /= 2;
2198 DeleteDC( hdc );
2200 hObj = alloc_icon_handle( FALSE, 0 );
2201 if (hObj)
2203 struct cursoricon_object *info = get_icon_ptr( hObj );
2204 struct cursoricon_frame *frame;
2206 info->is_icon = iconinfo->fIcon;
2207 frame = get_icon_frame( info, 0 );
2208 frame->delay = ~0;
2209 frame->width = width;
2210 frame->height = height;
2211 frame->color = color;
2212 frame->mask = mask;
2213 frame->alpha = create_alpha_bitmap( iconinfo->hbmColor, NULL, NULL );
2214 release_icon_frame( info, frame );
2215 if (info->is_icon)
2217 info->hotspot.x = width / 2;
2218 info->hotspot.y = height / 2;
2220 else
2222 info->hotspot.x = iconinfo->xHotspot;
2223 info->hotspot.y = iconinfo->yHotspot;
2226 release_user_handle_ptr( info );
2228 return hObj;
2231 /******************************************************************************
2232 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2234 * NOTES
2235 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2237 * PARAMS
2238 * hdc [I] Handle to device context
2239 * x0 [I] X coordinate of upper left corner
2240 * y0 [I] Y coordinate of upper left corner
2241 * hIcon [I] Handle to icon to draw
2242 * cxWidth [I] Width of icon
2243 * cyWidth [I] Height of icon
2244 * istep [I] Index of frame in animated cursor
2245 * hbr [I] Handle to background brush
2246 * flags [I] Icon-drawing flags
2248 * RETURNS
2249 * Success: TRUE
2250 * Failure: FALSE
2252 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2253 INT cxWidth, INT cyWidth, UINT istep,
2254 HBRUSH hbr, UINT flags )
2256 struct cursoricon_frame *frame;
2257 struct cursoricon_object *ptr;
2258 HDC hdc_dest, hMemDC;
2259 BOOL result = FALSE, DoOffscreen;
2260 HBITMAP hB_off = 0;
2261 COLORREF oldFg, oldBg;
2262 INT x, y, nStretchMode;
2264 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2265 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2267 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2268 if (istep >= get_icon_steps( ptr ))
2270 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
2271 release_user_handle_ptr( ptr );
2272 return FALSE;
2274 if (!(frame = get_icon_frame( ptr, istep )))
2276 FIXME_(icon)("Error retrieving icon frame %d\n", istep);
2277 release_user_handle_ptr( ptr );
2278 return FALSE;
2280 if (!(hMemDC = CreateCompatibleDC( hdc )))
2282 release_icon_frame( ptr, frame );
2283 release_user_handle_ptr( ptr );
2284 return FALSE;
2287 if (flags & DI_NOMIRROR)
2288 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2290 /* Calculate the size of the destination image. */
2291 if (cxWidth == 0)
2293 if (flags & DI_DEFAULTSIZE)
2294 cxWidth = GetSystemMetrics (SM_CXICON);
2295 else
2296 cxWidth = frame->width;
2298 if (cyWidth == 0)
2300 if (flags & DI_DEFAULTSIZE)
2301 cyWidth = GetSystemMetrics (SM_CYICON);
2302 else
2303 cyWidth = frame->height;
2306 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2308 if (DoOffscreen) {
2309 RECT r;
2311 SetRect(&r, 0, 0, cxWidth, cxWidth);
2313 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2314 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2316 DeleteDC( hdc_dest );
2317 goto failed;
2319 SelectObject(hdc_dest, hB_off);
2320 FillRect(hdc_dest, &r, hbr);
2321 x = y = 0;
2323 else
2325 hdc_dest = hdc;
2326 x = x0;
2327 y = y0;
2330 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2332 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2333 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2335 if (frame->alpha && (flags & DI_IMAGE))
2337 BOOL alpha_blend = TRUE;
2339 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2341 BITMAP bm;
2342 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2343 alpha_blend = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel > 8;
2345 if (alpha_blend)
2347 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2348 SelectObject( hMemDC, frame->alpha );
2349 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2350 0, 0, frame->width, frame->height,
2351 pixelblend )) goto done;
2355 if (flags & DI_MASK)
2357 DWORD rop = (flags & DI_IMAGE) ? SRCAND : SRCCOPY;
2358 SelectObject( hMemDC, frame->mask );
2359 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2360 hMemDC, 0, 0, frame->width, frame->height, rop );
2363 if (flags & DI_IMAGE)
2365 if (frame->color)
2367 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2368 SelectObject( hMemDC, frame->color );
2369 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2370 hMemDC, 0, 0, frame->width, frame->height, rop );
2372 else
2374 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2375 SelectObject( hMemDC, frame->mask );
2376 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2377 hMemDC, 0, frame->height, frame->width,
2378 frame->height, rop );
2382 done:
2383 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2385 SetTextColor( hdc, oldFg );
2386 SetBkColor( hdc, oldBg );
2387 SetStretchBltMode (hdc, nStretchMode);
2388 result = TRUE;
2389 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2390 if (hB_off) DeleteObject(hB_off);
2391 failed:
2392 DeleteDC( hMemDC );
2393 release_icon_frame( ptr, frame );
2394 release_user_handle_ptr( ptr );
2395 return result;
2398 /***********************************************************************
2399 * DIB_FixColorsToLoadflags
2401 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2402 * are in loadflags
2404 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2406 int colors;
2407 COLORREF c_W, c_S, c_F, c_L, c_C;
2408 int incr,i;
2409 RGBQUAD *ptr;
2410 int bitmap_type;
2411 LONG width;
2412 LONG height;
2413 WORD bpp;
2414 DWORD compr;
2416 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2418 WARN_(resource)("Invalid bitmap\n");
2419 return;
2422 if (bpp > 8) return;
2424 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2426 incr = 3;
2427 colors = 1 << bpp;
2429 else
2431 incr = 4;
2432 colors = bmi->bmiHeader.biClrUsed;
2433 if (colors > 256) colors = 256;
2434 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2437 c_W = GetSysColor(COLOR_WINDOW);
2438 c_S = GetSysColor(COLOR_3DSHADOW);
2439 c_F = GetSysColor(COLOR_3DFACE);
2440 c_L = GetSysColor(COLOR_3DLIGHT);
2442 if (loadflags & LR_LOADTRANSPARENT) {
2443 switch (bpp) {
2444 case 1: pix = pix >> 7; break;
2445 case 4: pix = pix >> 4; break;
2446 case 8: break;
2447 default:
2448 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2449 return;
2451 if (pix >= colors) {
2452 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2453 return;
2455 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2456 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2457 ptr->rgbBlue = GetBValue(c_W);
2458 ptr->rgbGreen = GetGValue(c_W);
2459 ptr->rgbRed = GetRValue(c_W);
2461 if (loadflags & LR_LOADMAP3DCOLORS)
2462 for (i=0; i<colors; i++) {
2463 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2464 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2465 if (c_C == RGB(128, 128, 128)) {
2466 ptr->rgbRed = GetRValue(c_S);
2467 ptr->rgbGreen = GetGValue(c_S);
2468 ptr->rgbBlue = GetBValue(c_S);
2469 } else if (c_C == RGB(192, 192, 192)) {
2470 ptr->rgbRed = GetRValue(c_F);
2471 ptr->rgbGreen = GetGValue(c_F);
2472 ptr->rgbBlue = GetBValue(c_F);
2473 } else if (c_C == RGB(223, 223, 223)) {
2474 ptr->rgbRed = GetRValue(c_L);
2475 ptr->rgbGreen = GetGValue(c_L);
2476 ptr->rgbBlue = GetBValue(c_L);
2482 /**********************************************************************
2483 * BITMAP_Load
2485 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2486 INT desiredx, INT desiredy, UINT loadflags )
2488 HBITMAP hbitmap = 0, orig_bm;
2489 HRSRC hRsrc;
2490 HGLOBAL handle;
2491 const char *ptr = NULL;
2492 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2493 int size;
2494 BYTE pix;
2495 char *bits;
2496 LONG width, height, new_width, new_height;
2497 WORD bpp_dummy;
2498 DWORD compr_dummy, offbits = 0;
2499 INT bm_type;
2500 HDC screen_mem_dc = NULL;
2501 HDC screen_dc;
2503 if (!(loadflags & LR_LOADFROMFILE))
2505 if (!instance)
2507 /* OEM bitmap: try to load the resource from user32.dll */
2508 instance = user32_module;
2511 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2512 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2514 if ((info = LockResource( handle )) == NULL) return 0;
2516 else
2518 BITMAPFILEHEADER * bmfh;
2520 if (!(ptr = map_fileW( name, NULL ))) return 0;
2521 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2522 bmfh = (BITMAPFILEHEADER *)ptr;
2523 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2525 WARN("Invalid/unsupported bitmap format!\n");
2526 goto end;
2528 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2531 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2532 &bpp_dummy, &compr_dummy);
2533 if (bm_type == -1)
2535 WARN("Invalid bitmap format!\n");
2536 goto end;
2539 size = bitmap_info_size(info, DIB_RGB_COLORS);
2540 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2541 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2543 if (!fix_info || !scaled_info) goto end;
2544 memcpy(fix_info, info, size);
2546 pix = *((LPBYTE)info + size);
2547 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2549 memcpy(scaled_info, fix_info, size);
2551 if(desiredx != 0)
2552 new_width = desiredx;
2553 else
2554 new_width = width;
2556 if(desiredy != 0)
2557 new_height = height > 0 ? desiredy : -desiredy;
2558 else
2559 new_height = height;
2561 if(bm_type == 0)
2563 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2564 core->bcWidth = new_width;
2565 core->bcHeight = new_height;
2567 else
2569 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2570 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2571 WARN("Broken BitmapInfoHeader!\n");
2572 goto end;
2575 scaled_info->bmiHeader.biWidth = new_width;
2576 scaled_info->bmiHeader.biHeight = new_height;
2579 if (new_height < 0) new_height = -new_height;
2581 screen_dc = get_screen_dc();
2582 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2584 bits = (char *)info + (offbits ? offbits : size);
2586 if (loadflags & LR_CREATEDIBSECTION)
2588 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2589 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2591 else
2593 if (is_dib_monochrome(fix_info))
2594 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2595 else
2596 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2599 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2600 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2601 SelectObject(screen_mem_dc, orig_bm);
2603 end:
2604 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2605 HeapFree(GetProcessHeap(), 0, scaled_info);
2606 HeapFree(GetProcessHeap(), 0, fix_info);
2607 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2609 return hbitmap;
2612 /**********************************************************************
2613 * LoadImageA (USER32.@)
2615 * See LoadImageW.
2617 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2618 INT desiredx, INT desiredy, UINT loadflags)
2620 HANDLE res;
2621 LPWSTR u_name;
2623 if (IS_INTRESOURCE(name))
2624 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2626 __TRY {
2627 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2628 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2629 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2631 __EXCEPT_PAGE_FAULT {
2632 SetLastError( ERROR_INVALID_PARAMETER );
2633 return 0;
2635 __ENDTRY
2636 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2637 HeapFree(GetProcessHeap(), 0, u_name);
2638 return res;
2642 /******************************************************************************
2643 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2645 * PARAMS
2646 * hinst [I] Handle of instance that contains image
2647 * name [I] Name of image
2648 * type [I] Type of image
2649 * desiredx [I] Desired width
2650 * desiredy [I] Desired height
2651 * loadflags [I] Load flags
2653 * RETURNS
2654 * Success: Handle to newly loaded image
2655 * Failure: NULL
2657 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2659 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2660 INT desiredx, INT desiredy, UINT loadflags )
2662 int depth;
2664 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2665 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2667 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2668 switch (type) {
2669 case IMAGE_BITMAP:
2670 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2672 case IMAGE_ICON:
2673 case IMAGE_CURSOR:
2674 depth = 1;
2675 if (!(loadflags & LR_MONOCHROME))
2677 HDC screen_dc;
2679 if ((screen_dc = get_screen_dc()))
2680 depth = GetDeviceCaps( screen_dc, BITSPIXEL );
2682 return CURSORICON_Load(hinst, name, desiredx, desiredy, depth, (type == IMAGE_CURSOR), loadflags);
2684 return 0;
2687 /******************************************************************************
2688 * CopyImage (USER32.@) Creates new image and copies attributes to it
2690 * PARAMS
2691 * hnd [I] Handle to image to copy
2692 * type [I] Type of image to copy
2693 * desiredx [I] Desired width of new image
2694 * desiredy [I] Desired height of new image
2695 * flags [I] Copy flags
2697 * RETURNS
2698 * Success: Handle to newly created image
2699 * Failure: NULL
2701 * BUGS
2702 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2703 * all other versions (95/2000/XP have been tested) ignore it.
2705 * NOTES
2706 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2707 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2708 * the copy will have the same depth as the screen.
2709 * The content of the image will only be copied if the bit depth of the
2710 * original image is compatible with the bit depth of the screen, or
2711 * if the source is a DIB section.
2712 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2714 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2715 INT desiredy, UINT flags )
2717 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2718 hnd, type, desiredx, desiredy, flags);
2720 switch (type)
2722 case IMAGE_BITMAP:
2724 HBITMAP res = NULL;
2725 DIBSECTION ds;
2726 int objSize;
2727 BITMAPINFO * bi;
2729 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2730 if (!objSize) return 0;
2731 if ((desiredx < 0) || (desiredy < 0)) return 0;
2733 if (flags & LR_COPYFROMRESOURCE)
2735 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2738 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2739 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2741 /* Allocate memory for a BITMAPINFOHEADER structure and a
2742 color table. The maximum number of colors in a color table
2743 is 256 which corresponds to a bitmap with depth 8.
2744 Bitmaps with higher depths don't have color tables. */
2745 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2746 if (!bi) return 0;
2748 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2749 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2750 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2751 bi->bmiHeader.biCompression = BI_RGB;
2753 if (flags & LR_CREATEDIBSECTION)
2755 /* Create a DIB section. LR_MONOCHROME is ignored */
2756 void * bits;
2757 HDC dc = CreateCompatibleDC(NULL);
2759 if (objSize == sizeof(DIBSECTION))
2761 /* The source bitmap is a DIB.
2762 Get its attributes to create an exact copy */
2763 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2766 bi->bmiHeader.biWidth = desiredx;
2767 bi->bmiHeader.biHeight = desiredy;
2769 /* Get the color table or the color masks */
2770 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2772 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2773 DeleteDC(dc);
2775 else
2777 /* Create a device-dependent bitmap */
2779 BOOL monochrome = (flags & LR_MONOCHROME);
2781 if (objSize == sizeof(DIBSECTION))
2783 /* The source bitmap is a DIB section.
2784 Get its attributes */
2785 HDC dc = CreateCompatibleDC(NULL);
2786 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2787 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2788 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2789 DeleteDC(dc);
2791 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2793 /* Look if the colors of the DIB are black and white */
2795 monochrome =
2796 (bi->bmiColors[0].rgbRed == 0xff
2797 && bi->bmiColors[0].rgbGreen == 0xff
2798 && bi->bmiColors[0].rgbBlue == 0xff
2799 && bi->bmiColors[0].rgbReserved == 0
2800 && bi->bmiColors[1].rgbRed == 0
2801 && bi->bmiColors[1].rgbGreen == 0
2802 && bi->bmiColors[1].rgbBlue == 0
2803 && bi->bmiColors[1].rgbReserved == 0)
2805 (bi->bmiColors[0].rgbRed == 0
2806 && bi->bmiColors[0].rgbGreen == 0
2807 && bi->bmiColors[0].rgbBlue == 0
2808 && bi->bmiColors[0].rgbReserved == 0
2809 && bi->bmiColors[1].rgbRed == 0xff
2810 && bi->bmiColors[1].rgbGreen == 0xff
2811 && bi->bmiColors[1].rgbBlue == 0xff
2812 && bi->bmiColors[1].rgbReserved == 0);
2815 else if (!monochrome)
2817 monochrome = ds.dsBm.bmBitsPixel == 1;
2820 if (monochrome)
2822 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2824 else
2826 HDC screenDC = GetDC(NULL);
2827 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2828 ReleaseDC(NULL, screenDC);
2832 if (res)
2834 /* Only copy the bitmap if it's a DIB section or if it's
2835 compatible to the screen */
2836 BOOL copyContents;
2838 if (objSize == sizeof(DIBSECTION))
2840 copyContents = TRUE;
2842 else
2844 HDC screenDC = GetDC(NULL);
2845 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2846 ReleaseDC(NULL, screenDC);
2848 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2851 if (copyContents)
2853 /* The source bitmap may already be selected in a device context,
2854 use GetDIBits/StretchDIBits and not StretchBlt */
2856 HDC dc;
2857 void * bits;
2859 dc = CreateCompatibleDC(NULL);
2861 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2862 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2863 bi->bmiHeader.biSizeImage = 0;
2864 bi->bmiHeader.biClrUsed = 0;
2865 bi->bmiHeader.biClrImportant = 0;
2867 /* Fill in biSizeImage */
2868 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2869 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2871 if (bits)
2873 HBITMAP oldBmp;
2875 /* Get the image bits of the source bitmap */
2876 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2878 /* Copy it to the destination bitmap */
2879 oldBmp = SelectObject(dc, res);
2880 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2881 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2882 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2883 SelectObject(dc, oldBmp);
2885 HeapFree(GetProcessHeap(), 0, bits);
2888 DeleteDC(dc);
2891 if (flags & LR_COPYDELETEORG)
2893 DeleteObject(hnd);
2896 HeapFree(GetProcessHeap(), 0, bi);
2897 return res;
2899 case IMAGE_ICON:
2900 case IMAGE_CURSOR:
2902 struct cursoricon_object *icon;
2903 HICON res = 0;
2904 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( get_screen_dc(), BITSPIXEL );
2906 if (flags & LR_DEFAULTSIZE)
2908 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2909 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2912 if (!(icon = get_icon_ptr( hnd ))) return 0;
2914 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2915 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2916 !icon->is_icon, flags );
2917 else
2918 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2919 release_user_handle_ptr( icon );
2921 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2922 return res;
2925 return 0;
2929 /******************************************************************************
2930 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2932 * RETURNS
2933 * Success: Handle to specified bitmap
2934 * Failure: NULL
2936 HBITMAP WINAPI LoadBitmapW(
2937 HINSTANCE instance, /* [in] Handle to application instance */
2938 LPCWSTR name) /* [in] Address of bitmap resource name */
2940 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2943 /**********************************************************************
2944 * LoadBitmapA (USER32.@)
2946 * See LoadBitmapW.
2948 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2950 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );