wineandroid: Implement wglCreateContextAttribsARB.
[wine.git] / dlls / user32 / cursoricon.c
blob33ce5cb2e4c3c4c6f64c9900c52453315826f3ec
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, 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 /* Double height to account for AND and XOR masks */
578 height *= 2;
580 /* First find the largest one smaller than or equal to the requested size*/
582 maxwidth = maxheight = 0;
583 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
585 if ((cx <= width) && (cy <= height) &&
586 (cx > maxwidth) && (cy > maxheight))
588 bestEntry = i;
589 maxwidth = cx;
590 maxheight = cy;
593 if (bestEntry != -1) return bestEntry;
595 /* Now find the smallest one larger than the requested size */
597 maxwidth = maxheight = 255;
598 for ( i = 0; get_entry( dir, size, i, &cx, &cy, &bits ); i++ )
600 if (((cx < maxwidth) && (cy < maxheight)) || (bestEntry == -1))
602 bestEntry = i;
603 maxwidth = cx;
604 maxheight = cy;
608 return bestEntry;
611 static BOOL CURSORICON_GetResCursorEntry( LPCVOID dir, DWORD size, int n,
612 int *width, int *height, int *bits )
614 const CURSORICONDIR *resdir = dir;
615 const CURSORDIR *cursor;
617 if ( resdir->idCount <= n )
618 return FALSE;
619 if ((const char *)&resdir->idEntries[n + 1] - (const char *)dir > size)
620 return FALSE;
621 cursor = &resdir->idEntries[n].ResInfo.cursor;
622 *width = cursor->wWidth;
623 *height = cursor->wHeight;
624 *bits = resdir->idEntries[n].wBitCount;
625 return TRUE;
628 static const CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( const CURSORICONDIR * dir, DWORD size,
629 int width, int height, int depth,
630 UINT loadflags )
632 int n;
634 n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetResIconEntry,
635 width, height, depth, loadflags );
636 if ( n < 0 )
637 return NULL;
638 return &dir->idEntries[n];
641 static const CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( const CURSORICONDIR *dir, DWORD size,
642 int width, int height, int depth,
643 UINT loadflags )
645 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetResCursorEntry,
646 width, height, depth, loadflags );
647 if ( n < 0 )
648 return NULL;
649 return &dir->idEntries[n];
652 static BOOL CURSORICON_GetFileEntry( LPCVOID dir, DWORD size, int n,
653 int *width, int *height, int *bits )
655 const CURSORICONFILEDIR *filedir = dir;
656 const CURSORICONFILEDIRENTRY *entry;
657 const BITMAPINFOHEADER *info;
659 if ( filedir->idCount <= n )
660 return FALSE;
661 if ((const char *)&filedir->idEntries[n + 1] - (const char *)dir > size)
662 return FALSE;
663 entry = &filedir->idEntries[n];
664 info = (const BITMAPINFOHEADER *)((const char *)dir + entry->dwDIBOffset);
665 if (info->biSize != sizeof(BITMAPCOREHEADER))
667 if ((const char *)(info + 1) - (const char *)dir > size) return FALSE;
668 *bits = info->biBitCount;
670 else
672 const BITMAPCOREHEADER *coreinfo = (const BITMAPCOREHEADER *)((const char *)dir + entry->dwDIBOffset);
673 if ((const char *)(coreinfo + 1) - (const char *)dir > size) return FALSE;
674 *bits = coreinfo->bcBitCount;
676 *width = entry->bWidth;
677 *height = entry->bHeight;
678 return TRUE;
681 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( const CURSORICONFILEDIR *dir, DWORD size,
682 int width, int height, int depth,
683 UINT loadflags )
685 int n = CURSORICON_FindBestCursor( dir, size, CURSORICON_GetFileEntry,
686 width, height, depth, loadflags );
687 if ( n < 0 )
688 return NULL;
689 return &dir->idEntries[n];
692 static const CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( const CURSORICONFILEDIR *dir, DWORD size,
693 int width, int height, int depth,
694 UINT loadflags )
696 int n = CURSORICON_FindBestIcon( dir, size, CURSORICON_GetFileEntry,
697 width, height, depth, loadflags );
698 if ( n < 0 )
699 return NULL;
700 return &dir->idEntries[n];
703 /***********************************************************************
704 * bmi_has_alpha
706 static BOOL bmi_has_alpha( const BITMAPINFO *info, const void *bits )
708 int i;
709 BOOL has_alpha = FALSE;
710 const unsigned char *ptr = bits;
712 if (info->bmiHeader.biBitCount != 32) return FALSE;
713 for (i = 0; i < info->bmiHeader.biWidth * abs(info->bmiHeader.biHeight); i++, ptr += 4)
714 if ((has_alpha = (ptr[3] != 0))) break;
715 return has_alpha;
718 /***********************************************************************
719 * create_alpha_bitmap
721 * Create the alpha bitmap for a 32-bpp icon that has an alpha channel.
723 static HBITMAP create_alpha_bitmap( HBITMAP color, const BITMAPINFO *src_info, const void *color_bits )
725 HBITMAP alpha = 0;
726 BITMAPINFO *info = NULL;
727 BITMAP bm;
728 HDC hdc;
729 void *bits;
730 unsigned char *ptr;
731 int i;
733 if (!GetObjectW( color, sizeof(bm), &bm )) return 0;
734 if (bm.bmBitsPixel != 32) return 0;
736 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
737 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
738 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
739 info->bmiHeader.biWidth = bm.bmWidth;
740 info->bmiHeader.biHeight = -bm.bmHeight;
741 info->bmiHeader.biPlanes = 1;
742 info->bmiHeader.biBitCount = 32;
743 info->bmiHeader.biCompression = BI_RGB;
744 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
745 info->bmiHeader.biXPelsPerMeter = 0;
746 info->bmiHeader.biYPelsPerMeter = 0;
747 info->bmiHeader.biClrUsed = 0;
748 info->bmiHeader.biClrImportant = 0;
749 if (!(alpha = CreateDIBSection( hdc, info, DIB_RGB_COLORS, &bits, NULL, 0 ))) goto done;
751 if (src_info)
753 SelectObject( hdc, alpha );
754 StretchDIBits( hdc, 0, 0, bm.bmWidth, bm.bmHeight,
755 0, 0, src_info->bmiHeader.biWidth, src_info->bmiHeader.biHeight,
756 color_bits, src_info, DIB_RGB_COLORS, SRCCOPY );
759 else
761 GetDIBits( hdc, color, 0, bm.bmHeight, bits, info, DIB_RGB_COLORS );
762 if (!bmi_has_alpha( info, bits ))
764 DeleteObject( alpha );
765 alpha = 0;
766 goto done;
770 /* pre-multiply by alpha */
771 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
773 unsigned int alpha = ptr[3];
774 ptr[0] = ptr[0] * alpha / 255;
775 ptr[1] = ptr[1] * alpha / 255;
776 ptr[2] = ptr[2] * alpha / 255;
779 done:
780 DeleteDC( hdc );
781 HeapFree( GetProcessHeap(), 0, info );
782 return alpha;
786 /***********************************************************************
787 * create_icon_from_bmi
789 * Create an icon from its BITMAPINFO.
791 static HICON create_icon_from_bmi( const BITMAPINFO *bmi, DWORD maxsize, HMODULE module, LPCWSTR resname,
792 HRSRC rsrc, POINT hotspot, BOOL bIcon, INT width, INT height,
793 UINT cFlag )
795 DWORD size, color_size, mask_size;
796 HBITMAP color = 0, mask = 0, alpha = 0;
797 const void *color_bits, *mask_bits;
798 BITMAPINFO *bmi_copy;
799 BOOL ret = FALSE;
800 BOOL do_stretch;
801 HICON hObj = 0;
802 HDC screen_dc;
803 HDC hdc = 0;
804 LONG bmi_width, bmi_height;
805 WORD bpp;
806 DWORD compr;
808 /* Check bitmap header */
810 if (maxsize < sizeof(BITMAPCOREHEADER))
812 WARN( "invalid size %u\n", maxsize );
813 return 0;
815 if (maxsize < bmi->bmiHeader.biSize)
817 WARN( "invalid header size %u\n", bmi->bmiHeader.biSize );
818 return 0;
820 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
821 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
822 (bmi->bmiHeader.biCompression != BI_RGB &&
823 bmi->bmiHeader.biCompression != BI_BITFIELDS)) )
825 WARN( "invalid bitmap header %u\n", bmi->bmiHeader.biSize );
826 return 0;
829 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
830 DIB_GetBitmapInfo(&bmi->bmiHeader, &bmi_width, &bmi_height, &bpp, &compr);
831 color_size = get_dib_image_size( bmi_width, bmi_height / 2,
832 bpp );
833 mask_size = get_dib_image_size( bmi_width, bmi_height / 2, 1 );
834 if (size > maxsize || color_size > maxsize - size)
836 WARN( "truncated file %u < %u+%u+%u\n", maxsize, size, color_size, mask_size );
837 return 0;
839 if (mask_size > maxsize - size - color_size) mask_size = 0; /* no mask */
841 if (cFlag & LR_DEFAULTSIZE)
843 if (!width) width = GetSystemMetrics( bIcon ? SM_CXICON : SM_CXCURSOR );
844 if (!height) height = GetSystemMetrics( bIcon ? SM_CYICON : SM_CYCURSOR );
846 else
848 if (!width) width = bmi_width;
849 if (!height) height = bmi_height/2;
851 do_stretch = (bmi_height/2 != height) ||
852 (bmi_width != width);
854 /* Scale the hotspot */
855 if (bIcon)
857 hotspot.x = width / 2;
858 hotspot.y = height / 2;
860 else if (do_stretch)
862 hotspot.x = (hotspot.x * width) / bmi_width;
863 hotspot.y = (hotspot.y * height) / (bmi_height / 2);
866 if (!(screen_dc = get_screen_dc())) return 0;
868 if (!(bmi_copy = HeapAlloc( GetProcessHeap(), 0, max( size, FIELD_OFFSET( BITMAPINFO, bmiColors[2] )))))
869 return 0;
870 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
872 memcpy( bmi_copy, bmi, size );
873 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
874 bmi_copy->bmiHeader.biHeight /= 2;
875 else
876 ((BITMAPCOREINFO *)bmi_copy)->bmciHeader.bcHeight /= 2;
877 bmi_height /= 2;
879 color_bits = (const char*)bmi + size;
880 mask_bits = (const char*)color_bits + color_size;
882 alpha = 0;
883 if (is_dib_monochrome( bmi ))
885 if (!(mask = CreateBitmap( width, height * 2, 1, 1, NULL ))) goto done;
886 color = 0;
888 /* copy color data into second half of mask bitmap */
889 SelectObject( hdc, mask );
890 StretchDIBits( hdc, 0, height, width, height,
891 0, 0, bmi_width, bmi_height,
892 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
894 else
896 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
897 if (!(color = CreateBitmap( width, height, GetDeviceCaps( screen_dc, PLANES ),
898 GetDeviceCaps( screen_dc, BITSPIXEL ), NULL )))
900 DeleteObject( mask );
901 goto done;
903 SelectObject( hdc, color );
904 StretchDIBits( hdc, 0, 0, width, height,
905 0, 0, bmi_width, bmi_height,
906 color_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
908 if (bmi_has_alpha( bmi_copy, color_bits ))
909 alpha = create_alpha_bitmap( color, bmi_copy, color_bits );
911 /* convert info to monochrome to copy the mask */
912 if (bmi_copy->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
914 RGBQUAD *rgb = bmi_copy->bmiColors;
916 bmi_copy->bmiHeader.biBitCount = 1;
917 bmi_copy->bmiHeader.biClrUsed = bmi_copy->bmiHeader.biClrImportant = 2;
918 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
919 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
920 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
922 else
924 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)bmi_copy) + 1);
926 ((BITMAPCOREINFO *)bmi_copy)->bmciHeader.bcBitCount = 1;
927 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
928 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
932 if (mask_size)
934 SelectObject( hdc, mask );
935 StretchDIBits( hdc, 0, 0, width, height,
936 0, 0, bmi_width, bmi_height,
937 mask_bits, bmi_copy, DIB_RGB_COLORS, SRCCOPY );
939 ret = TRUE;
941 done:
942 DeleteDC( hdc );
943 HeapFree( GetProcessHeap(), 0, bmi_copy );
945 if (ret)
946 hObj = alloc_icon_handle( FALSE, 0 );
947 if (hObj)
949 struct cursoricon_object *info = get_icon_ptr( hObj );
950 struct cursoricon_frame *frame;
952 info->is_icon = bIcon;
953 info->module = module;
954 info->hotspot = hotspot;
955 frame = get_icon_frame( info, 0 );
956 frame->delay = ~0;
957 frame->width = width;
958 frame->height = height;
959 frame->color = color;
960 frame->mask = mask;
961 frame->alpha = alpha;
962 release_icon_frame( info, frame );
963 if (!IS_INTRESOURCE(resname))
965 info->resname = HeapAlloc( GetProcessHeap(), 0, (strlenW(resname) + 1) * sizeof(WCHAR) );
966 if (info->resname) strcpyW( info->resname, resname );
968 else info->resname = MAKEINTRESOURCEW( LOWORD(resname) );
970 if (module && (cFlag & LR_SHARED))
972 info->rsrc = rsrc;
973 list_add_head( &icon_cache, &info->entry );
975 release_user_handle_ptr( info );
977 else
979 DeleteObject( color );
980 DeleteObject( alpha );
981 DeleteObject( mask );
983 return hObj;
987 /**********************************************************************
988 * .ANI cursor support
990 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
991 ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
992 ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
994 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
995 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
996 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
997 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
998 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
999 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
1000 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
1002 #define ANI_FLAG_ICON 0x1
1003 #define ANI_FLAG_SEQUENCE 0x2
1005 typedef struct {
1006 DWORD header_size;
1007 DWORD num_frames;
1008 DWORD num_steps;
1009 DWORD width;
1010 DWORD height;
1011 DWORD bpp;
1012 DWORD num_planes;
1013 DWORD display_rate;
1014 DWORD flags;
1015 } ani_header;
1017 typedef struct {
1018 DWORD data_size;
1019 const unsigned char *data;
1020 } riff_chunk_t;
1022 static void dump_ani_header( const ani_header *header )
1024 TRACE(" header size: %d\n", header->header_size);
1025 TRACE(" frames: %d\n", header->num_frames);
1026 TRACE(" steps: %d\n", header->num_steps);
1027 TRACE(" width: %d\n", header->width);
1028 TRACE(" height: %d\n", header->height);
1029 TRACE(" bpp: %d\n", header->bpp);
1030 TRACE(" planes: %d\n", header->num_planes);
1031 TRACE(" display rate: %d\n", header->display_rate);
1032 TRACE(" flags: 0x%08x\n", header->flags);
1037 * RIFF:
1038 * DWORD "RIFF"
1039 * DWORD size
1040 * DWORD riff_id
1041 * BYTE[] data
1043 * LIST:
1044 * DWORD "LIST"
1045 * DWORD size
1046 * DWORD list_id
1047 * BYTE[] data
1049 * CHUNK:
1050 * DWORD chunk_id
1051 * DWORD size
1052 * BYTE[] data
1054 static void riff_find_chunk( DWORD chunk_id, DWORD chunk_type, const riff_chunk_t *parent_chunk, riff_chunk_t *chunk )
1056 const unsigned char *ptr = parent_chunk->data;
1057 const unsigned char *end = parent_chunk->data + (parent_chunk->data_size - (2 * sizeof(DWORD)));
1059 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) end -= sizeof(DWORD);
1061 while (ptr < end)
1063 if ((!chunk_type && *(const DWORD *)ptr == chunk_id )
1064 || (chunk_type && *(const DWORD *)ptr == chunk_type && *((const DWORD *)ptr + 2) == chunk_id ))
1066 ptr += sizeof(DWORD);
1067 chunk->data_size = (*(const DWORD *)ptr + 1) & ~1;
1068 ptr += sizeof(DWORD);
1069 if (chunk_type == ANI_LIST_ID || chunk_type == ANI_RIFF_ID) ptr += sizeof(DWORD);
1070 chunk->data = ptr;
1072 return;
1075 ptr += sizeof(DWORD);
1076 ptr += (*(const DWORD *)ptr + 1) & ~1;
1077 ptr += sizeof(DWORD);
1083 * .ANI layout:
1085 * RIFF:'ACON' RIFF chunk
1086 * |- CHUNK:'anih' Header
1087 * |- CHUNK:'seq ' Sequence information (optional)
1088 * \- LIST:'fram' Frame list
1089 * |- CHUNK:icon Cursor frames
1090 * |- CHUNK:icon
1091 * |- ...
1092 * \- CHUNK:icon
1094 static HCURSOR CURSORICON_CreateIconFromANI( const BYTE *bits, DWORD bits_size, INT width, INT height,
1095 INT depth, BOOL is_icon, UINT loadflags )
1097 struct animated_cursoricon_object *ani_icon_data;
1098 struct cursoricon_object *info;
1099 DWORD *frame_rates = NULL;
1100 DWORD *frame_seq = NULL;
1101 ani_header header;
1102 BOOL use_seq = FALSE;
1103 HCURSOR cursor;
1104 UINT i;
1105 BOOL error = FALSE;
1106 HICON *frames;
1108 riff_chunk_t root_chunk = { bits_size, bits };
1109 riff_chunk_t ACON_chunk = {0};
1110 riff_chunk_t anih_chunk = {0};
1111 riff_chunk_t fram_chunk = {0};
1112 riff_chunk_t rate_chunk = {0};
1113 riff_chunk_t seq_chunk = {0};
1114 const unsigned char *icon_chunk;
1115 const unsigned char *icon_data;
1117 TRACE("bits %p, bits_size %d\n", bits, bits_size);
1119 riff_find_chunk( ANI_ACON_ID, ANI_RIFF_ID, &root_chunk, &ACON_chunk );
1120 if (!ACON_chunk.data)
1122 ERR("Failed to get root chunk.\n");
1123 return 0;
1126 riff_find_chunk( ANI_anih_ID, 0, &ACON_chunk, &anih_chunk );
1127 if (!anih_chunk.data)
1129 ERR("Failed to get 'anih' chunk.\n");
1130 return 0;
1132 memcpy( &header, anih_chunk.data, sizeof(header) );
1133 dump_ani_header( &header );
1135 if (!(header.flags & ANI_FLAG_ICON))
1137 FIXME("Raw animated icon/cursor data is not currently supported.\n");
1138 return 0;
1141 if (header.flags & ANI_FLAG_SEQUENCE)
1143 riff_find_chunk( ANI_seq__ID, 0, &ACON_chunk, &seq_chunk );
1144 if (seq_chunk.data)
1146 frame_seq = (DWORD *) seq_chunk.data;
1147 use_seq = TRUE;
1149 else
1151 FIXME("Sequence data expected but not found, assuming steps == frames.\n");
1152 header.num_steps = header.num_frames;
1156 riff_find_chunk( ANI_rate_ID, 0, &ACON_chunk, &rate_chunk );
1157 if (rate_chunk.data)
1158 frame_rates = (DWORD *) rate_chunk.data;
1160 riff_find_chunk( ANI_fram_ID, ANI_LIST_ID, &ACON_chunk, &fram_chunk );
1161 if (!fram_chunk.data)
1163 ERR("Failed to get icon list.\n");
1164 return 0;
1167 cursor = alloc_icon_handle( TRUE, header.num_steps );
1168 if (!cursor) return 0;
1169 frames = HeapAlloc( GetProcessHeap(), 0, sizeof(*frames) * header.num_frames );
1170 if (!frames)
1172 free_icon_handle( cursor );
1173 return 0;
1176 info = get_icon_ptr( cursor );
1177 ani_icon_data = (struct animated_cursoricon_object *) info;
1178 info->is_icon = is_icon;
1179 ani_icon_data->num_frames = header.num_frames;
1181 /* The .ANI stores the display rate in jiffies (1/60s) */
1182 info->delay = header.display_rate;
1184 icon_chunk = fram_chunk.data;
1185 icon_data = fram_chunk.data + (2 * sizeof(DWORD));
1186 for (i=0; i<header.num_frames; i++)
1188 const DWORD chunk_size = *(const DWORD *)(icon_chunk + sizeof(DWORD));
1189 const CURSORICONFILEDIRENTRY *entry;
1190 INT frameWidth, frameHeight;
1191 const BITMAPINFO *bmi;
1193 entry = CURSORICON_FindBestIconFile((const CURSORICONFILEDIR *) icon_data,
1194 bits + bits_size - icon_data,
1195 width, height, depth, loadflags );
1197 info->hotspot.x = entry->xHotspot;
1198 info->hotspot.y = entry->yHotspot;
1199 if (!header.width || !header.height)
1201 frameWidth = entry->bWidth;
1202 frameHeight = entry->bHeight;
1204 else
1206 frameWidth = header.width;
1207 frameHeight = header.height;
1210 frames[i] = NULL;
1211 if (entry->dwDIBOffset < bits + bits_size - icon_data)
1213 bmi = (const BITMAPINFO *) (icon_data + entry->dwDIBOffset);
1214 /* Grab a frame from the animation */
1215 frames[i] = create_icon_from_bmi( bmi, bits + bits_size - (const BYTE *)bmi,
1216 NULL, NULL, NULL, info->hotspot,
1217 is_icon, frameWidth, frameHeight, loadflags );
1220 if (!frames[i])
1222 FIXME_(cursor)("failed to convert animated cursor frame.\n");
1223 error = TRUE;
1224 if (i == 0)
1226 FIXME_(cursor)("Completely failed to create animated cursor!\n");
1227 ani_icon_data->num_frames = 0;
1228 release_user_handle_ptr( info );
1229 free_icon_handle( cursor );
1230 HeapFree( GetProcessHeap(), 0, frames );
1231 return 0;
1233 break;
1236 /* Advance to the next chunk */
1237 icon_chunk += chunk_size + (2 * sizeof(DWORD));
1238 icon_data = icon_chunk + (2 * sizeof(DWORD));
1241 /* There was an error but we at least decoded the first frame, so just use that frame */
1242 if (error)
1244 FIXME_(cursor)("Error creating animated cursor, only using first frame!\n");
1245 for (i=1; i<ani_icon_data->num_frames; i++)
1246 free_icon_handle( ani_icon_data->frames[i] );
1247 use_seq = FALSE;
1248 info->delay = 0;
1249 ani_icon_data->num_steps = 1;
1250 ani_icon_data->num_frames = 1;
1253 /* Setup the animated frames in the correct sequence */
1254 for (i=0; i<ani_icon_data->num_steps; i++)
1256 DWORD frame_id = use_seq ? frame_seq[i] : i;
1257 struct cursoricon_frame *frame;
1259 if (frame_id >= ani_icon_data->num_frames)
1261 frame_id = ani_icon_data->num_frames-1;
1262 ERR_(cursor)("Sequence indicates frame past end of list, corrupt?\n");
1264 ani_icon_data->frames[i] = frames[frame_id];
1265 frame = get_icon_frame( info, i );
1266 if (frame_rates)
1267 frame->delay = frame_rates[i];
1268 else
1269 frame->delay = ~0;
1270 release_icon_frame( info, frame );
1273 HeapFree( GetProcessHeap(), 0, frames );
1274 release_user_handle_ptr( info );
1276 return cursor;
1280 /**********************************************************************
1281 * CreateIconFromResourceEx (USER32.@)
1283 * FIXME: Convert to mono when cFlag is LR_MONOCHROME.
1285 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
1286 BOOL bIcon, DWORD dwVersion,
1287 INT width, INT height,
1288 UINT cFlag )
1290 POINT hotspot;
1291 const BITMAPINFO *bmi;
1293 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
1294 bits, cbSize, dwVersion, width, height,
1295 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
1297 if (!bits) return 0;
1299 if (dwVersion == 0x00020000)
1301 FIXME_(cursor)("\t2.xx resources are not supported\n");
1302 return 0;
1305 /* Check if the resource is an animated icon/cursor */
1306 if (!memcmp(bits, "RIFF", 4))
1307 return CURSORICON_CreateIconFromANI( bits, cbSize, width, height,
1308 0 /* default depth */, bIcon, cFlag );
1310 if (bIcon)
1312 hotspot.x = width / 2;
1313 hotspot.y = height / 2;
1314 bmi = (BITMAPINFO *)bits;
1316 else /* get the hotspot */
1318 const SHORT *pt = (const SHORT *)bits;
1319 hotspot.x = pt[0];
1320 hotspot.y = pt[1];
1321 bmi = (const BITMAPINFO *)(pt + 2);
1322 cbSize -= 2 * sizeof(*pt);
1325 return create_icon_from_bmi( bmi, cbSize, NULL, NULL, NULL, hotspot, bIcon, width, height, cFlag );
1329 /**********************************************************************
1330 * CreateIconFromResource (USER32.@)
1332 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
1333 BOOL bIcon, DWORD dwVersion)
1335 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
1339 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
1340 INT width, INT height, INT depth,
1341 BOOL fCursor, UINT loadflags)
1343 const CURSORICONFILEDIRENTRY *entry;
1344 const CURSORICONFILEDIR *dir;
1345 DWORD filesize = 0;
1346 HICON hIcon = 0;
1347 const BYTE *bits;
1348 POINT hotspot;
1350 TRACE("loading %s\n", debugstr_w( filename ));
1352 bits = map_fileW( filename, &filesize );
1353 if (!bits)
1354 return hIcon;
1356 /* Check for .ani. */
1357 if (memcmp( bits, "RIFF", 4 ) == 0)
1359 hIcon = CURSORICON_CreateIconFromANI( bits, filesize, width, height, depth, !fCursor, loadflags );
1360 goto end;
1363 dir = (const CURSORICONFILEDIR*) bits;
1364 if ( filesize < FIELD_OFFSET( CURSORICONFILEDIR, idEntries[dir->idCount] ))
1365 goto end;
1367 if ( fCursor )
1368 entry = CURSORICON_FindBestCursorFile( dir, filesize, width, height, depth, loadflags );
1369 else
1370 entry = CURSORICON_FindBestIconFile( dir, filesize, width, height, depth, loadflags );
1372 if ( !entry )
1373 goto end;
1375 /* check that we don't run off the end of the file */
1376 if ( entry->dwDIBOffset > filesize )
1377 goto end;
1378 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
1379 goto end;
1381 hotspot.x = entry->xHotspot;
1382 hotspot.y = entry->yHotspot;
1383 hIcon = create_icon_from_bmi( (const BITMAPINFO *)&bits[entry->dwDIBOffset], filesize - entry->dwDIBOffset,
1384 NULL, NULL, NULL, hotspot, !fCursor, width, height, loadflags );
1385 end:
1386 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
1387 UnmapViewOfFile( bits );
1388 return hIcon;
1391 /**********************************************************************
1392 * CURSORICON_Load
1394 * Load a cursor or icon from resource or file.
1396 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
1397 INT width, INT height, INT depth,
1398 BOOL fCursor, UINT loadflags)
1400 HANDLE handle = 0;
1401 HICON hIcon = 0;
1402 HRSRC hRsrc;
1403 DWORD size;
1404 const CURSORICONDIR *dir;
1405 const CURSORICONDIRENTRY *dirEntry;
1406 const BYTE *bits;
1407 WORD wResId;
1408 POINT hotspot;
1410 TRACE("%p, %s, %dx%d, depth %d, fCursor %d, flags 0x%04x\n",
1411 hInstance, debugstr_w(name), width, height, depth, fCursor, loadflags);
1413 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
1414 return CURSORICON_LoadFromFile( name, width, height, depth, fCursor, loadflags );
1416 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
1418 /* don't cache 16-bit instances (FIXME: should never get 16-bit instances in the first place) */
1419 if ((ULONG_PTR)hInstance >> 16 == 0) loadflags &= ~LR_SHARED;
1421 /* Get directory resource ID */
1423 if (!(hRsrc = FindResourceW( hInstance, name,
1424 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
1426 /* try animated resource */
1427 if (!(hRsrc = FindResourceW( hInstance, name,
1428 (LPWSTR)(fCursor ? RT_ANICURSOR : RT_ANIICON) ))) return 0;
1429 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1430 bits = LockResource( handle );
1431 return CURSORICON_CreateIconFromANI( bits, SizeofResource( hInstance, handle ),
1432 width, height, depth, !fCursor, loadflags );
1435 /* Find the best entry in the directory */
1437 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1438 if (!(dir = LockResource( handle ))) return 0;
1439 size = SizeofResource( hInstance, hRsrc );
1440 if (fCursor)
1441 dirEntry = CURSORICON_FindBestCursorRes( dir, size, width, height, depth, loadflags );
1442 else
1443 dirEntry = CURSORICON_FindBestIconRes( dir, size, width, height, depth, loadflags );
1444 if (!dirEntry) return 0;
1445 wResId = dirEntry->wResId;
1446 FreeResource( handle );
1448 /* Load the resource */
1450 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
1451 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
1453 /* If shared icon, check whether it was already loaded */
1454 if (loadflags & LR_SHARED)
1456 struct cursoricon_object *ptr;
1458 USER_Lock();
1459 LIST_FOR_EACH_ENTRY( ptr, &icon_cache, struct cursoricon_object, entry )
1461 if (ptr->module != hInstance) continue;
1462 if (ptr->rsrc != hRsrc) continue;
1463 hIcon = ptr->obj.handle;
1464 break;
1466 USER_Unlock();
1467 if (hIcon) return hIcon;
1470 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
1471 size = SizeofResource( hInstance, hRsrc );
1472 bits = LockResource( handle );
1474 if (!fCursor)
1476 hotspot.x = width / 2;
1477 hotspot.y = height / 2;
1479 else /* get the hotspot */
1481 const SHORT *pt = (const SHORT *)bits;
1482 hotspot.x = pt[0];
1483 hotspot.y = pt[1];
1484 bits += 2 * sizeof(SHORT);
1485 size -= 2 * sizeof(SHORT);
1487 hIcon = create_icon_from_bmi( (const BITMAPINFO *)bits, size, hInstance, name, hRsrc,
1488 hotspot, !fCursor, width, height, loadflags );
1489 FreeResource( handle );
1490 return hIcon;
1494 /***********************************************************************
1495 * CreateCursor (USER32.@)
1497 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1498 INT xHotSpot, INT yHotSpot,
1499 INT nWidth, INT nHeight,
1500 LPCVOID lpANDbits, LPCVOID lpXORbits )
1502 ICONINFO info;
1503 HCURSOR hCursor;
1505 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1506 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1508 info.fIcon = FALSE;
1509 info.xHotspot = xHotSpot;
1510 info.yHotspot = yHotSpot;
1511 info.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1512 info.hbmColor = CreateBitmap( nWidth, nHeight, 1, 1, lpXORbits );
1513 hCursor = CreateIconIndirect( &info );
1514 DeleteObject( info.hbmMask );
1515 DeleteObject( info.hbmColor );
1516 return hCursor;
1520 /***********************************************************************
1521 * CreateIcon (USER32.@)
1523 * Creates an icon based on the specified bitmaps. The bitmaps must be
1524 * provided in a device dependent format and will be resized to
1525 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1526 * depth. The provided bitmaps must be top-down bitmaps.
1527 * Although Windows does not support 15bpp(*) this API must support it
1528 * for Winelib applications.
1530 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1531 * format!
1533 * RETURNS
1534 * Success: handle to an icon
1535 * Failure: NULL
1537 * FIXME: Do we need to resize the bitmaps?
1539 HICON WINAPI CreateIcon(
1540 HINSTANCE hInstance, /* [in] the application's hInstance */
1541 INT nWidth, /* [in] the width of the provided bitmaps */
1542 INT nHeight, /* [in] the height of the provided bitmaps */
1543 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1544 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1545 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1546 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1548 ICONINFO iinfo;
1549 HICON hIcon;
1551 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1552 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1554 iinfo.fIcon = TRUE;
1555 iinfo.xHotspot = nWidth / 2;
1556 iinfo.yHotspot = nHeight / 2;
1557 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1558 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1560 hIcon = CreateIconIndirect( &iinfo );
1562 DeleteObject( iinfo.hbmMask );
1563 DeleteObject( iinfo.hbmColor );
1565 return hIcon;
1569 /***********************************************************************
1570 * CopyIcon (USER32.@)
1572 HICON WINAPI CopyIcon( HICON hIcon )
1574 struct cursoricon_object *ptrOld, *ptrNew;
1575 HICON hNew;
1577 if (!(ptrOld = get_icon_ptr( hIcon )))
1579 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1580 return 0;
1582 if ((hNew = alloc_icon_handle( FALSE, 0 )))
1584 struct cursoricon_frame *frameOld, *frameNew;
1586 ptrNew = get_icon_ptr( hNew );
1587 ptrNew->is_icon = ptrOld->is_icon;
1588 ptrNew->hotspot = ptrOld->hotspot;
1589 if (!(frameOld = get_icon_frame( ptrOld, 0 )))
1591 release_user_handle_ptr( ptrOld );
1592 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1593 return 0;
1595 if (!(frameNew = get_icon_frame( ptrNew, 0 )))
1597 release_icon_frame( ptrOld, frameOld );
1598 release_user_handle_ptr( ptrOld );
1599 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
1600 return 0;
1602 frameNew->delay = 0;
1603 frameNew->width = frameOld->width;
1604 frameNew->height = frameOld->height;
1605 frameNew->mask = copy_bitmap( frameOld->mask );
1606 frameNew->color = copy_bitmap( frameOld->color );
1607 frameNew->alpha = copy_bitmap( frameOld->alpha );
1608 release_icon_frame( ptrOld, frameOld );
1609 release_icon_frame( ptrNew, frameNew );
1610 release_user_handle_ptr( ptrNew );
1612 release_user_handle_ptr( ptrOld );
1613 return hNew;
1617 /***********************************************************************
1618 * DestroyIcon (USER32.@)
1620 BOOL WINAPI DestroyIcon( HICON hIcon )
1622 BOOL ret = FALSE;
1623 struct cursoricon_object *obj = get_icon_ptr( hIcon );
1625 TRACE_(icon)("%p\n", hIcon );
1627 if (obj)
1629 BOOL shared = (obj->rsrc != NULL);
1630 release_user_handle_ptr( obj );
1631 ret = (GetCursor() != hIcon);
1632 if (!shared) free_icon_handle( hIcon );
1634 return ret;
1638 /***********************************************************************
1639 * DestroyCursor (USER32.@)
1641 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1643 return DestroyIcon( hCursor );
1646 /***********************************************************************
1647 * DrawIcon (USER32.@)
1649 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1651 return DrawIconEx( hdc, x, y, hIcon, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE );
1654 /***********************************************************************
1655 * SetCursor (USER32.@)
1657 * Set the cursor shape.
1659 * RETURNS
1660 * A handle to the previous cursor shape.
1662 HCURSOR WINAPI DECLSPEC_HOTPATCH SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1664 struct cursoricon_object *obj;
1665 HCURSOR hOldCursor;
1666 int show_count;
1667 BOOL ret;
1669 TRACE("%p\n", hCursor);
1671 SERVER_START_REQ( set_cursor )
1673 req->flags = SET_CURSOR_HANDLE;
1674 req->handle = wine_server_user_handle( hCursor );
1675 if ((ret = !wine_server_call_err( req )))
1677 hOldCursor = wine_server_ptr_handle( reply->prev_handle );
1678 show_count = reply->prev_count;
1681 SERVER_END_REQ;
1683 if (!ret) return 0;
1684 USER_Driver->pSetCursor( show_count >= 0 ? hCursor : 0 );
1686 if (!(obj = get_icon_ptr( hOldCursor ))) return 0;
1687 release_user_handle_ptr( obj );
1688 return hOldCursor;
1691 /***********************************************************************
1692 * ShowCursor (USER32.@)
1694 INT WINAPI DECLSPEC_HOTPATCH ShowCursor( BOOL bShow )
1696 HCURSOR cursor;
1697 int increment = bShow ? 1 : -1;
1698 int count;
1700 SERVER_START_REQ( set_cursor )
1702 req->flags = SET_CURSOR_COUNT;
1703 req->show_count = increment;
1704 wine_server_call( req );
1705 cursor = wine_server_ptr_handle( reply->prev_handle );
1706 count = reply->prev_count + increment;
1708 SERVER_END_REQ;
1710 TRACE("%d, count=%d\n", bShow, count );
1712 if (bShow && !count) USER_Driver->pSetCursor( cursor );
1713 else if (!bShow && count == -1) USER_Driver->pSetCursor( 0 );
1715 return count;
1718 /***********************************************************************
1719 * GetCursor (USER32.@)
1721 HCURSOR WINAPI GetCursor(void)
1723 HCURSOR ret;
1725 SERVER_START_REQ( set_cursor )
1727 req->flags = 0;
1728 wine_server_call( req );
1729 ret = wine_server_ptr_handle( reply->prev_handle );
1731 SERVER_END_REQ;
1732 return ret;
1736 /***********************************************************************
1737 * ClipCursor (USER32.@)
1739 BOOL WINAPI DECLSPEC_HOTPATCH ClipCursor( const RECT *rect )
1741 BOOL ret;
1742 RECT new_rect;
1744 TRACE( "Clipping to %s\n", wine_dbgstr_rect(rect) );
1746 if (rect && (rect->left > rect->right || rect->top > rect->bottom)) return FALSE;
1748 SERVER_START_REQ( set_cursor )
1750 req->clip_msg = WM_WINE_CLIPCURSOR;
1751 if (rect)
1753 req->flags = SET_CURSOR_CLIP;
1754 req->clip.left = rect->left;
1755 req->clip.top = rect->top;
1756 req->clip.right = rect->right;
1757 req->clip.bottom = rect->bottom;
1759 else req->flags = SET_CURSOR_NOCLIP;
1761 if ((ret = !wine_server_call( req )))
1763 new_rect.left = reply->new_clip.left;
1764 new_rect.top = reply->new_clip.top;
1765 new_rect.right = reply->new_clip.right;
1766 new_rect.bottom = reply->new_clip.bottom;
1769 SERVER_END_REQ;
1770 if (ret) USER_Driver->pClipCursor( &new_rect );
1771 return ret;
1775 /***********************************************************************
1776 * GetClipCursor (USER32.@)
1778 BOOL WINAPI DECLSPEC_HOTPATCH GetClipCursor( RECT *rect )
1780 BOOL ret;
1782 if (!rect) return FALSE;
1784 SERVER_START_REQ( set_cursor )
1786 req->flags = 0;
1787 if ((ret = !wine_server_call( req )))
1789 rect->left = reply->new_clip.left;
1790 rect->top = reply->new_clip.top;
1791 rect->right = reply->new_clip.right;
1792 rect->bottom = reply->new_clip.bottom;
1795 SERVER_END_REQ;
1796 return ret;
1800 /***********************************************************************
1801 * SetSystemCursor (USER32.@)
1803 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1805 FIXME("(%p,%08x),stub!\n", hcur, id);
1806 return TRUE;
1810 /**********************************************************************
1811 * LookupIconIdFromDirectoryEx (USER32.@)
1813 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1814 INT width, INT height, UINT cFlag )
1816 const CURSORICONDIR *dir = (const CURSORICONDIR*)xdir;
1817 UINT retVal = 0;
1818 if( dir && !dir->idReserved && (dir->idType & 3) )
1820 const CURSORICONDIRENTRY* entry;
1822 const HDC hdc = GetDC(0);
1823 const int depth = (cFlag & LR_MONOCHROME) ?
1824 1 : GetDeviceCaps(hdc, BITSPIXEL);
1825 ReleaseDC(0, hdc);
1827 if( bIcon )
1828 entry = CURSORICON_FindBestIconRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1829 else
1830 entry = CURSORICON_FindBestCursorRes( dir, ~0u, width, height, depth, LR_DEFAULTSIZE );
1832 if( entry ) retVal = entry->wResId;
1834 else WARN_(cursor)("invalid resource directory\n");
1835 return retVal;
1838 /**********************************************************************
1839 * LookupIconIdFromDirectory (USER32.@)
1841 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1843 return LookupIconIdFromDirectoryEx( dir, bIcon, 0, 0, bIcon ? 0 : LR_MONOCHROME );
1846 /***********************************************************************
1847 * LoadCursorW (USER32.@)
1849 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1851 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1853 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1854 LR_SHARED | LR_DEFAULTSIZE );
1857 /***********************************************************************
1858 * LoadCursorA (USER32.@)
1860 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1862 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1864 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1865 LR_SHARED | LR_DEFAULTSIZE );
1868 /***********************************************************************
1869 * LoadCursorFromFileW (USER32.@)
1871 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1873 TRACE("%s\n", debugstr_w(name));
1875 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1876 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1879 /***********************************************************************
1880 * LoadCursorFromFileA (USER32.@)
1882 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1884 TRACE("%s\n", debugstr_a(name));
1886 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1887 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1890 /***********************************************************************
1891 * LoadIconW (USER32.@)
1893 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1895 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1897 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1898 LR_SHARED | LR_DEFAULTSIZE );
1901 /***********************************************************************
1902 * LoadIconA (USER32.@)
1904 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1906 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1908 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1909 LR_SHARED | LR_DEFAULTSIZE );
1912 /**********************************************************************
1913 * GetCursorFrameInfo (USER32.@)
1915 * NOTES
1916 * So far no use has been found for the second parameter, it is currently presumed
1917 * that this parameter is reserved for future use.
1919 * PARAMS
1920 * hCursor [I] Handle to cursor for which to retrieve information
1921 * reserved [I] No purpose has been found for this parameter (may be NULL)
1922 * istep [I] The step of the cursor for which to retrieve information
1923 * rate_jiffies [O] Pointer to DWORD that receives the frame-specific delay (cannot be NULL)
1924 * num_steps [O] Pointer to DWORD that receives the number of steps in the cursor (cannot be NULL)
1926 * RETURNS
1927 * Success: Handle to a frame of the cursor (specified by istep)
1928 * Failure: NULL cursor (0)
1930 HCURSOR WINAPI GetCursorFrameInfo(HCURSOR hCursor, DWORD reserved, DWORD istep, DWORD *rate_jiffies, DWORD *num_steps)
1932 struct cursoricon_object *ptr;
1933 HCURSOR ret = 0;
1934 UINT icon_steps;
1936 if (rate_jiffies == NULL || num_steps == NULL) return 0;
1938 if (!(ptr = get_icon_ptr( hCursor ))) return 0;
1940 TRACE("%p => %d %d %p %p\n", hCursor, reserved, istep, rate_jiffies, num_steps);
1941 if (reserved != 0)
1942 FIXME("Second parameter non-zero (%d), please report this!\n", reserved);
1944 icon_steps = get_icon_steps(ptr);
1945 if (istep < icon_steps || !ptr->is_ani)
1947 struct animated_cursoricon_object *ani_icon_data = (struct animated_cursoricon_object *) ptr;
1948 UINT icon_frames = 1;
1950 if (ptr->is_ani)
1951 icon_frames = ani_icon_data->num_frames;
1952 if (ptr->is_ani && icon_frames > 1)
1953 ret = ani_icon_data->frames[istep];
1954 else
1955 ret = hCursor;
1956 if (icon_frames == 1)
1958 *rate_jiffies = 0;
1959 *num_steps = 1;
1961 else if (icon_steps == 1)
1963 *num_steps = ~0;
1964 *rate_jiffies = ptr->delay;
1966 else if (istep < icon_steps)
1968 struct cursoricon_frame *frame;
1970 *num_steps = icon_steps;
1971 frame = get_icon_frame( ptr, istep );
1972 if (get_icon_steps(ptr) == 1)
1973 *num_steps = ~0;
1974 else
1975 *num_steps = get_icon_steps(ptr);
1976 /* If this specific frame does not have a delay then use the global delay */
1977 if (frame->delay == ~0)
1978 *rate_jiffies = ptr->delay;
1979 else
1980 *rate_jiffies = frame->delay;
1981 release_icon_frame( ptr, frame );
1985 release_user_handle_ptr( ptr );
1987 return ret;
1990 /**********************************************************************
1991 * GetIconInfo (USER32.@)
1993 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1995 ICONINFOEXW infoW;
1997 infoW.cbSize = sizeof(infoW);
1998 if (!GetIconInfoExW( hIcon, &infoW )) return FALSE;
1999 iconinfo->fIcon = infoW.fIcon;
2000 iconinfo->xHotspot = infoW.xHotspot;
2001 iconinfo->yHotspot = infoW.yHotspot;
2002 iconinfo->hbmColor = infoW.hbmColor;
2003 iconinfo->hbmMask = infoW.hbmMask;
2004 return TRUE;
2007 /**********************************************************************
2008 * GetIconInfoExA (USER32.@)
2010 BOOL WINAPI GetIconInfoExA( HICON icon, ICONINFOEXA *info )
2012 ICONINFOEXW infoW;
2014 if (info->cbSize != sizeof(*info))
2016 SetLastError( ERROR_INVALID_PARAMETER );
2017 return FALSE;
2019 infoW.cbSize = sizeof(infoW);
2020 if (!GetIconInfoExW( icon, &infoW )) return FALSE;
2021 info->fIcon = infoW.fIcon;
2022 info->xHotspot = infoW.xHotspot;
2023 info->yHotspot = infoW.yHotspot;
2024 info->hbmColor = infoW.hbmColor;
2025 info->hbmMask = infoW.hbmMask;
2026 info->wResID = infoW.wResID;
2027 WideCharToMultiByte( CP_ACP, 0, infoW.szModName, -1, info->szModName, MAX_PATH, NULL, NULL );
2028 WideCharToMultiByte( CP_ACP, 0, infoW.szResName, -1, info->szResName, MAX_PATH, NULL, NULL );
2029 return TRUE;
2032 /**********************************************************************
2033 * GetIconInfoExW (USER32.@)
2035 BOOL WINAPI GetIconInfoExW( HICON icon, ICONINFOEXW *info )
2037 struct cursoricon_frame *frame;
2038 struct cursoricon_object *ptr;
2039 HMODULE module;
2040 BOOL ret = TRUE;
2042 if (info->cbSize != sizeof(*info))
2044 SetLastError( ERROR_INVALID_PARAMETER );
2045 return FALSE;
2047 if (!(ptr = get_icon_ptr( icon )))
2049 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2050 return FALSE;
2053 frame = get_icon_frame( ptr, 0 );
2054 if (!frame)
2056 release_user_handle_ptr( ptr );
2057 SetLastError( ERROR_INVALID_CURSOR_HANDLE );
2058 return FALSE;
2061 TRACE("%p => %dx%d\n", icon, frame->width, frame->height);
2063 info->fIcon = ptr->is_icon;
2064 info->xHotspot = ptr->hotspot.x;
2065 info->yHotspot = ptr->hotspot.y;
2066 info->hbmColor = copy_bitmap( frame->color );
2067 info->hbmMask = copy_bitmap( frame->mask );
2068 info->wResID = 0;
2069 info->szModName[0] = 0;
2070 info->szResName[0] = 0;
2071 if (ptr->module)
2073 if (IS_INTRESOURCE( ptr->resname )) info->wResID = LOWORD( ptr->resname );
2074 else lstrcpynW( info->szResName, ptr->resname, MAX_PATH );
2076 if (!info->hbmMask || (!info->hbmColor && frame->color))
2078 DeleteObject( info->hbmMask );
2079 DeleteObject( info->hbmColor );
2080 ret = FALSE;
2082 module = ptr->module;
2083 release_icon_frame( ptr, frame );
2084 release_user_handle_ptr( ptr );
2085 if (ret && module) GetModuleFileNameW( module, info->szModName, MAX_PATH );
2086 return ret;
2089 /* copy an icon bitmap, even when it can't be selected into a DC */
2090 /* helper for CreateIconIndirect */
2091 static void stretch_blt_icon( HDC hdc_dst, int dst_x, int dst_y, int dst_width, int dst_height,
2092 HBITMAP src, int width, int height )
2094 HDC hdc = CreateCompatibleDC( 0 );
2096 if (!SelectObject( hdc, src )) /* do it the hard way */
2098 BITMAPINFO *info;
2099 void *bits;
2101 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return;
2102 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2103 info->bmiHeader.biWidth = width;
2104 info->bmiHeader.biHeight = height;
2105 info->bmiHeader.biPlanes = GetDeviceCaps( hdc_dst, PLANES );
2106 info->bmiHeader.biBitCount = GetDeviceCaps( hdc_dst, BITSPIXEL );
2107 info->bmiHeader.biCompression = BI_RGB;
2108 info->bmiHeader.biSizeImage = get_dib_image_size( width, height, info->bmiHeader.biBitCount );
2109 info->bmiHeader.biXPelsPerMeter = 0;
2110 info->bmiHeader.biYPelsPerMeter = 0;
2111 info->bmiHeader.biClrUsed = 0;
2112 info->bmiHeader.biClrImportant = 0;
2113 bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
2114 if (bits && GetDIBits( hdc, src, 0, height, bits, info, DIB_RGB_COLORS ))
2115 StretchDIBits( hdc_dst, dst_x, dst_y, dst_width, dst_height,
2116 0, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
2118 HeapFree( GetProcessHeap(), 0, bits );
2119 HeapFree( GetProcessHeap(), 0, info );
2121 else StretchBlt( hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc, 0, 0, width, height, SRCCOPY );
2123 DeleteDC( hdc );
2126 /**********************************************************************
2127 * CreateIconIndirect (USER32.@)
2129 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
2131 BITMAP bmpXor, bmpAnd;
2132 HICON hObj;
2133 HBITMAP color = 0, mask;
2134 int width, height;
2135 HDC hdc;
2137 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
2138 iconinfo->hbmColor, iconinfo->hbmMask,
2139 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
2141 if (!iconinfo->hbmMask) return 0;
2143 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
2144 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2145 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
2146 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
2148 if (iconinfo->hbmColor)
2150 GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
2151 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
2152 bmpXor.bmWidth, bmpXor.bmHeight, bmpXor.bmWidthBytes,
2153 bmpXor.bmPlanes, bmpXor.bmBitsPixel);
2155 width = bmpXor.bmWidth;
2156 height = bmpXor.bmHeight;
2157 if (bmpXor.bmPlanes * bmpXor.bmBitsPixel != 1 || bmpAnd.bmPlanes * bmpAnd.bmBitsPixel != 1)
2159 color = CreateCompatibleBitmap( get_screen_dc(), width, height );
2160 mask = CreateBitmap( width, height, 1, 1, NULL );
2162 else mask = CreateBitmap( width, height * 2, 1, 1, NULL );
2164 else
2166 width = bmpAnd.bmWidth;
2167 height = bmpAnd.bmHeight;
2168 mask = CreateBitmap( width, height, 1, 1, NULL );
2171 hdc = CreateCompatibleDC( 0 );
2172 SelectObject( hdc, mask );
2173 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmMask, bmpAnd.bmWidth, bmpAnd.bmHeight );
2175 if (color)
2177 SelectObject( hdc, color );
2178 stretch_blt_icon( hdc, 0, 0, width, height, iconinfo->hbmColor, width, height );
2180 else if (iconinfo->hbmColor)
2182 stretch_blt_icon( hdc, 0, height, width, height, iconinfo->hbmColor, width, height );
2184 else height /= 2;
2186 DeleteDC( hdc );
2188 hObj = alloc_icon_handle( FALSE, 0 );
2189 if (hObj)
2191 struct cursoricon_object *info = get_icon_ptr( hObj );
2192 struct cursoricon_frame *frame;
2194 info->is_icon = iconinfo->fIcon;
2195 frame = get_icon_frame( info, 0 );
2196 frame->delay = ~0;
2197 frame->width = width;
2198 frame->height = height;
2199 frame->color = color;
2200 frame->mask = mask;
2201 frame->alpha = create_alpha_bitmap( iconinfo->hbmColor, NULL, NULL );
2202 release_icon_frame( info, frame );
2203 if (info->is_icon)
2205 info->hotspot.x = width / 2;
2206 info->hotspot.y = height / 2;
2208 else
2210 info->hotspot.x = iconinfo->xHotspot;
2211 info->hotspot.y = iconinfo->yHotspot;
2214 release_user_handle_ptr( info );
2216 return hObj;
2219 /******************************************************************************
2220 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
2222 * NOTES
2223 * Why is this using SM_CXICON instead of SM_CXCURSOR?
2225 * PARAMS
2226 * hdc [I] Handle to device context
2227 * x0 [I] X coordinate of upper left corner
2228 * y0 [I] Y coordinate of upper left corner
2229 * hIcon [I] Handle to icon to draw
2230 * cxWidth [I] Width of icon
2231 * cyWidth [I] Height of icon
2232 * istep [I] Index of frame in animated cursor
2233 * hbr [I] Handle to background brush
2234 * flags [I] Icon-drawing flags
2236 * RETURNS
2237 * Success: TRUE
2238 * Failure: FALSE
2240 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
2241 INT cxWidth, INT cyWidth, UINT istep,
2242 HBRUSH hbr, UINT flags )
2244 struct cursoricon_frame *frame;
2245 struct cursoricon_object *ptr;
2246 HDC hdc_dest, hMemDC;
2247 BOOL result = FALSE, DoOffscreen;
2248 HBITMAP hB_off = 0;
2249 COLORREF oldFg, oldBg;
2250 INT x, y, nStretchMode;
2252 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
2253 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
2255 if (!(ptr = get_icon_ptr( hIcon ))) return FALSE;
2256 if (istep >= get_icon_steps( ptr ))
2258 TRACE_(icon)("Stepped past end of animated frames=%d\n", istep);
2259 release_user_handle_ptr( ptr );
2260 return FALSE;
2262 if (!(frame = get_icon_frame( ptr, istep )))
2264 FIXME_(icon)("Error retrieving icon frame %d\n", istep);
2265 release_user_handle_ptr( ptr );
2266 return FALSE;
2268 if (!(hMemDC = CreateCompatibleDC( hdc )))
2270 release_icon_frame( ptr, frame );
2271 release_user_handle_ptr( ptr );
2272 return FALSE;
2275 if (flags & DI_NOMIRROR)
2276 FIXME_(icon)("Ignoring flag DI_NOMIRROR\n");
2278 /* Calculate the size of the destination image. */
2279 if (cxWidth == 0)
2281 if (flags & DI_DEFAULTSIZE)
2282 cxWidth = GetSystemMetrics (SM_CXICON);
2283 else
2284 cxWidth = frame->width;
2286 if (cyWidth == 0)
2288 if (flags & DI_DEFAULTSIZE)
2289 cyWidth = GetSystemMetrics (SM_CYICON);
2290 else
2291 cyWidth = frame->height;
2294 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2296 if (DoOffscreen) {
2297 RECT r;
2299 SetRect(&r, 0, 0, cxWidth, cxWidth);
2301 if (!(hdc_dest = CreateCompatibleDC(hdc))) goto failed;
2302 if (!(hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth)))
2304 DeleteDC( hdc_dest );
2305 goto failed;
2307 SelectObject(hdc_dest, hB_off);
2308 FillRect(hdc_dest, &r, hbr);
2309 x = y = 0;
2311 else
2313 hdc_dest = hdc;
2314 x = x0;
2315 y = y0;
2318 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2320 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2321 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2323 if (frame->alpha && (flags & DI_IMAGE))
2325 BOOL alpha_blend = TRUE;
2327 if (GetObjectType( hdc_dest ) == OBJ_MEMDC)
2329 BITMAP bm;
2330 HBITMAP bmp = GetCurrentObject( hdc_dest, OBJ_BITMAP );
2331 alpha_blend = GetObjectW( bmp, sizeof(bm), &bm ) && bm.bmBitsPixel > 8;
2333 if (alpha_blend)
2335 BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2336 SelectObject( hMemDC, frame->alpha );
2337 if (GdiAlphaBlend( hdc_dest, x, y, cxWidth, cyWidth, hMemDC,
2338 0, 0, frame->width, frame->height,
2339 pixelblend )) goto done;
2343 if (flags & DI_MASK)
2345 DWORD rop = (flags & DI_IMAGE) ? SRCAND : SRCCOPY;
2346 SelectObject( hMemDC, frame->mask );
2347 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2348 hMemDC, 0, 0, frame->width, frame->height, rop );
2351 if (flags & DI_IMAGE)
2353 if (frame->color)
2355 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2356 SelectObject( hMemDC, frame->color );
2357 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2358 hMemDC, 0, 0, frame->width, frame->height, rop );
2360 else
2362 DWORD rop = (flags & DI_MASK) ? SRCINVERT : SRCCOPY;
2363 SelectObject( hMemDC, frame->mask );
2364 StretchBlt( hdc_dest, x, y, cxWidth, cyWidth,
2365 hMemDC, 0, frame->height, frame->width,
2366 frame->height, rop );
2370 done:
2371 if (DoOffscreen) BitBlt( hdc, x0, y0, cxWidth, cyWidth, hdc_dest, 0, 0, SRCCOPY );
2373 SetTextColor( hdc, oldFg );
2374 SetBkColor( hdc, oldBg );
2375 SetStretchBltMode (hdc, nStretchMode);
2376 result = TRUE;
2377 if (hdc_dest != hdc) DeleteDC( hdc_dest );
2378 if (hB_off) DeleteObject(hB_off);
2379 failed:
2380 DeleteDC( hMemDC );
2381 release_icon_frame( ptr, frame );
2382 release_user_handle_ptr( ptr );
2383 return result;
2386 /***********************************************************************
2387 * DIB_FixColorsToLoadflags
2389 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2390 * are in loadflags
2392 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2394 int colors;
2395 COLORREF c_W, c_S, c_F, c_L, c_C;
2396 int incr,i;
2397 RGBQUAD *ptr;
2398 int bitmap_type;
2399 LONG width;
2400 LONG height;
2401 WORD bpp;
2402 DWORD compr;
2404 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2406 WARN_(resource)("Invalid bitmap\n");
2407 return;
2410 if (bpp > 8) return;
2412 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2414 incr = 3;
2415 colors = 1 << bpp;
2417 else
2419 incr = 4;
2420 colors = bmi->bmiHeader.biClrUsed;
2421 if (colors > 256) colors = 256;
2422 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2425 c_W = GetSysColor(COLOR_WINDOW);
2426 c_S = GetSysColor(COLOR_3DSHADOW);
2427 c_F = GetSysColor(COLOR_3DFACE);
2428 c_L = GetSysColor(COLOR_3DLIGHT);
2430 if (loadflags & LR_LOADTRANSPARENT) {
2431 switch (bpp) {
2432 case 1: pix = pix >> 7; break;
2433 case 4: pix = pix >> 4; break;
2434 case 8: break;
2435 default:
2436 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2437 return;
2439 if (pix >= colors) {
2440 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2441 return;
2443 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2444 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2445 ptr->rgbBlue = GetBValue(c_W);
2446 ptr->rgbGreen = GetGValue(c_W);
2447 ptr->rgbRed = GetRValue(c_W);
2449 if (loadflags & LR_LOADMAP3DCOLORS)
2450 for (i=0; i<colors; i++) {
2451 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2452 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2453 if (c_C == RGB(128, 128, 128)) {
2454 ptr->rgbRed = GetRValue(c_S);
2455 ptr->rgbGreen = GetGValue(c_S);
2456 ptr->rgbBlue = GetBValue(c_S);
2457 } else if (c_C == RGB(192, 192, 192)) {
2458 ptr->rgbRed = GetRValue(c_F);
2459 ptr->rgbGreen = GetGValue(c_F);
2460 ptr->rgbBlue = GetBValue(c_F);
2461 } else if (c_C == RGB(223, 223, 223)) {
2462 ptr->rgbRed = GetRValue(c_L);
2463 ptr->rgbGreen = GetGValue(c_L);
2464 ptr->rgbBlue = GetBValue(c_L);
2470 /**********************************************************************
2471 * BITMAP_Load
2473 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2474 INT desiredx, INT desiredy, UINT loadflags )
2476 HBITMAP hbitmap = 0, orig_bm;
2477 HRSRC hRsrc;
2478 HGLOBAL handle;
2479 const char *ptr = NULL;
2480 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2481 int size;
2482 BYTE pix;
2483 char *bits;
2484 LONG width, height, new_width, new_height;
2485 WORD bpp_dummy;
2486 DWORD compr_dummy, offbits = 0;
2487 INT bm_type;
2488 HDC screen_mem_dc = NULL;
2489 HDC screen_dc;
2491 if (!(loadflags & LR_LOADFROMFILE))
2493 if (!instance)
2495 /* OEM bitmap: try to load the resource from user32.dll */
2496 instance = user32_module;
2499 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2500 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2502 if ((info = LockResource( handle )) == NULL) return 0;
2504 else
2506 BITMAPFILEHEADER * bmfh;
2508 if (!(ptr = map_fileW( name, NULL ))) return 0;
2509 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2510 bmfh = (BITMAPFILEHEADER *)ptr;
2511 if (bmfh->bfType != 0x4d42 /* 'BM' */)
2513 WARN("Invalid/unsupported bitmap format!\n");
2514 goto end;
2516 if (bmfh->bfOffBits) offbits = bmfh->bfOffBits - sizeof(BITMAPFILEHEADER);
2519 bm_type = DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
2520 &bpp_dummy, &compr_dummy);
2521 if (bm_type == -1)
2523 WARN("Invalid bitmap format!\n");
2524 goto end;
2527 size = bitmap_info_size(info, DIB_RGB_COLORS);
2528 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2529 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2531 if (!fix_info || !scaled_info) goto end;
2532 memcpy(fix_info, info, size);
2534 pix = *((LPBYTE)info + size);
2535 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2537 memcpy(scaled_info, fix_info, size);
2539 if(desiredx != 0)
2540 new_width = desiredx;
2541 else
2542 new_width = width;
2544 if(desiredy != 0)
2545 new_height = height > 0 ? desiredy : -desiredy;
2546 else
2547 new_height = height;
2549 if(bm_type == 0)
2551 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2552 core->bcWidth = new_width;
2553 core->bcHeight = new_height;
2555 else
2557 /* Some sanity checks for BITMAPINFO (not applicable to BITMAPCOREINFO) */
2558 if (info->bmiHeader.biHeight > 65535 || info->bmiHeader.biWidth > 65535) {
2559 WARN("Broken BitmapInfoHeader!\n");
2560 goto end;
2563 scaled_info->bmiHeader.biWidth = new_width;
2564 scaled_info->bmiHeader.biHeight = new_height;
2567 if (new_height < 0) new_height = -new_height;
2569 screen_dc = get_screen_dc();
2570 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2572 bits = (char *)info + (offbits ? offbits : size);
2574 if (loadflags & LR_CREATEDIBSECTION)
2576 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2577 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2579 else
2581 if (is_dib_monochrome(fix_info))
2582 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2583 else
2584 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2587 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2588 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2589 SelectObject(screen_mem_dc, orig_bm);
2591 end:
2592 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2593 HeapFree(GetProcessHeap(), 0, scaled_info);
2594 HeapFree(GetProcessHeap(), 0, fix_info);
2595 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2597 return hbitmap;
2600 /**********************************************************************
2601 * LoadImageA (USER32.@)
2603 * See LoadImageW.
2605 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2606 INT desiredx, INT desiredy, UINT loadflags)
2608 HANDLE res;
2609 LPWSTR u_name;
2611 if (IS_INTRESOURCE(name))
2612 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2614 __TRY {
2615 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2616 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2617 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2619 __EXCEPT_PAGE_FAULT {
2620 SetLastError( ERROR_INVALID_PARAMETER );
2621 return 0;
2623 __ENDTRY
2624 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2625 HeapFree(GetProcessHeap(), 0, u_name);
2626 return res;
2630 /******************************************************************************
2631 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2633 * PARAMS
2634 * hinst [I] Handle of instance that contains image
2635 * name [I] Name of image
2636 * type [I] Type of image
2637 * desiredx [I] Desired width
2638 * desiredy [I] Desired height
2639 * loadflags [I] Load flags
2641 * RETURNS
2642 * Success: Handle to newly loaded image
2643 * Failure: NULL
2645 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2647 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2648 INT desiredx, INT desiredy, UINT loadflags )
2650 int depth;
2652 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2653 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2655 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2656 switch (type) {
2657 case IMAGE_BITMAP:
2658 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2660 case IMAGE_ICON:
2661 case IMAGE_CURSOR:
2662 depth = 1;
2663 if (!(loadflags & LR_MONOCHROME))
2665 HDC screen_dc;
2667 if ((screen_dc = get_screen_dc()))
2668 depth = GetDeviceCaps( screen_dc, BITSPIXEL );
2670 return CURSORICON_Load(hinst, name, desiredx, desiredy, depth, (type == IMAGE_CURSOR), loadflags);
2672 return 0;
2675 /******************************************************************************
2676 * CopyImage (USER32.@) Creates new image and copies attributes to it
2678 * PARAMS
2679 * hnd [I] Handle to image to copy
2680 * type [I] Type of image to copy
2681 * desiredx [I] Desired width of new image
2682 * desiredy [I] Desired height of new image
2683 * flags [I] Copy flags
2685 * RETURNS
2686 * Success: Handle to newly created image
2687 * Failure: NULL
2689 * BUGS
2690 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2691 * all other versions (95/2000/XP have been tested) ignore it.
2693 * NOTES
2694 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2695 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2696 * the copy will have the same depth as the screen.
2697 * The content of the image will only be copied if the bit depth of the
2698 * original image is compatible with the bit depth of the screen, or
2699 * if the source is a DIB section.
2700 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2702 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2703 INT desiredy, UINT flags )
2705 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2706 hnd, type, desiredx, desiredy, flags);
2708 switch (type)
2710 case IMAGE_BITMAP:
2712 HBITMAP res = NULL;
2713 DIBSECTION ds;
2714 int objSize;
2715 BITMAPINFO * bi;
2717 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2718 if (!objSize) return 0;
2719 if ((desiredx < 0) || (desiredy < 0)) return 0;
2721 if (flags & LR_COPYFROMRESOURCE)
2723 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2726 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2727 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2729 /* Allocate memory for a BITMAPINFOHEADER structure and a
2730 color table. The maximum number of colors in a color table
2731 is 256 which corresponds to a bitmap with depth 8.
2732 Bitmaps with higher depths don't have color tables. */
2733 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2734 if (!bi) return 0;
2736 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2737 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2738 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2739 bi->bmiHeader.biCompression = BI_RGB;
2741 if (flags & LR_CREATEDIBSECTION)
2743 /* Create a DIB section. LR_MONOCHROME is ignored */
2744 void * bits;
2745 HDC dc = CreateCompatibleDC(NULL);
2747 if (objSize == sizeof(DIBSECTION))
2749 /* The source bitmap is a DIB.
2750 Get its attributes to create an exact copy */
2751 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2754 bi->bmiHeader.biWidth = desiredx;
2755 bi->bmiHeader.biHeight = desiredy;
2757 /* Get the color table or the color masks */
2758 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2760 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2761 DeleteDC(dc);
2763 else
2765 /* Create a device-dependent bitmap */
2767 BOOL monochrome = (flags & LR_MONOCHROME);
2769 if (objSize == sizeof(DIBSECTION))
2771 /* The source bitmap is a DIB section.
2772 Get its attributes */
2773 HDC dc = CreateCompatibleDC(NULL);
2774 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2775 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2776 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2777 DeleteDC(dc);
2779 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2781 /* Look if the colors of the DIB are black and white */
2783 monochrome =
2784 (bi->bmiColors[0].rgbRed == 0xff
2785 && bi->bmiColors[0].rgbGreen == 0xff
2786 && bi->bmiColors[0].rgbBlue == 0xff
2787 && bi->bmiColors[0].rgbReserved == 0
2788 && bi->bmiColors[1].rgbRed == 0
2789 && bi->bmiColors[1].rgbGreen == 0
2790 && bi->bmiColors[1].rgbBlue == 0
2791 && bi->bmiColors[1].rgbReserved == 0)
2793 (bi->bmiColors[0].rgbRed == 0
2794 && bi->bmiColors[0].rgbGreen == 0
2795 && bi->bmiColors[0].rgbBlue == 0
2796 && bi->bmiColors[0].rgbReserved == 0
2797 && bi->bmiColors[1].rgbRed == 0xff
2798 && bi->bmiColors[1].rgbGreen == 0xff
2799 && bi->bmiColors[1].rgbBlue == 0xff
2800 && bi->bmiColors[1].rgbReserved == 0);
2803 else if (!monochrome)
2805 monochrome = ds.dsBm.bmBitsPixel == 1;
2808 if (monochrome)
2810 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2812 else
2814 HDC screenDC = GetDC(NULL);
2815 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2816 ReleaseDC(NULL, screenDC);
2820 if (res)
2822 /* Only copy the bitmap if it's a DIB section or if it's
2823 compatible to the screen */
2824 BOOL copyContents;
2826 if (objSize == sizeof(DIBSECTION))
2828 copyContents = TRUE;
2830 else
2832 HDC screenDC = GetDC(NULL);
2833 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2834 ReleaseDC(NULL, screenDC);
2836 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2839 if (copyContents)
2841 /* The source bitmap may already be selected in a device context,
2842 use GetDIBits/StretchDIBits and not StretchBlt */
2844 HDC dc;
2845 void * bits;
2847 dc = CreateCompatibleDC(NULL);
2849 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2850 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2851 bi->bmiHeader.biSizeImage = 0;
2852 bi->bmiHeader.biClrUsed = 0;
2853 bi->bmiHeader.biClrImportant = 0;
2855 /* Fill in biSizeImage */
2856 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2857 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2859 if (bits)
2861 HBITMAP oldBmp;
2863 /* Get the image bits of the source bitmap */
2864 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2866 /* Copy it to the destination bitmap */
2867 oldBmp = SelectObject(dc, res);
2868 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2869 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2870 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2871 SelectObject(dc, oldBmp);
2873 HeapFree(GetProcessHeap(), 0, bits);
2876 DeleteDC(dc);
2879 if (flags & LR_COPYDELETEORG)
2881 DeleteObject(hnd);
2884 HeapFree(GetProcessHeap(), 0, bi);
2885 return res;
2887 case IMAGE_ICON:
2888 case IMAGE_CURSOR:
2890 struct cursoricon_object *icon;
2891 HICON res = 0;
2892 int depth = (flags & LR_MONOCHROME) ? 1 : GetDeviceCaps( get_screen_dc(), BITSPIXEL );
2894 if (flags & LR_DEFAULTSIZE)
2896 if (!desiredx) desiredx = GetSystemMetrics( type == IMAGE_ICON ? SM_CXICON : SM_CXCURSOR );
2897 if (!desiredy) desiredy = GetSystemMetrics( type == IMAGE_ICON ? SM_CYICON : SM_CYCURSOR );
2900 if (!(icon = get_icon_ptr( hnd ))) return 0;
2902 if (icon->rsrc && (flags & LR_COPYFROMRESOURCE))
2903 res = CURSORICON_Load( icon->module, icon->resname, desiredx, desiredy, depth,
2904 !icon->is_icon, flags );
2905 else
2906 res = CopyIcon( hnd ); /* FIXME: change size if necessary */
2907 release_user_handle_ptr( icon );
2909 if (res && (flags & LR_COPYDELETEORG)) DeleteObject( hnd );
2910 return res;
2913 return 0;
2917 /******************************************************************************
2918 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2920 * RETURNS
2921 * Success: Handle to specified bitmap
2922 * Failure: NULL
2924 HBITMAP WINAPI LoadBitmapW(
2925 HINSTANCE instance, /* [in] Handle to application instance */
2926 LPCWSTR name) /* [in] Address of bitmap resource name */
2928 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2931 /**********************************************************************
2932 * LoadBitmapA (USER32.@)
2934 * See LoadBitmapW.
2936 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2938 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );