oleaut32: Use the typelib debug channel for debug messages output during loading...
[wine/gsoc_dplay.git] / dlls / user32 / cursoricon.c
bloba1ba570dc78d8d5d6062b2c8bf80af82a9efa9d5
1 /*
2 * Cursor and icon support
4 * Copyright 1995 Alexandre Julliard
5 * 1996 Martin Von Loewis
6 * 1997 Alex Korobka
7 * 1998 Turchanov Sergey
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * Theory:
27 * Cursors and icons are stored in a global heap block, with the
28 * following layout:
30 * CURSORICONINFO info;
31 * BYTE[] ANDbits;
32 * BYTE[] XORbits;
34 * The bits structures are in the format of a device-dependent bitmap.
36 * This layout is very sub-optimal, as the bitmap bits are stored in
37 * the X client instead of in the server like other bitmaps; however,
38 * some programs (notably Paint Brush) expect to be able to manipulate
39 * the bits directly :-(
42 #include "config.h"
43 #include "wine/port.h"
45 #include <stdarg.h>
46 #include <string.h>
47 #include <stdlib.h>
49 #include "windef.h"
50 #include "winbase.h"
51 #include "wingdi.h"
52 #include "winerror.h"
53 #include "wine/winbase16.h"
54 #include "wine/winuser16.h"
55 #include "wine/exception.h"
56 #include "wine/debug.h"
57 #include "user_private.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
60 WINE_DECLARE_DEBUG_CHANNEL(icon);
61 WINE_DECLARE_DEBUG_CHANNEL(resource);
63 #include "pshpack1.h"
65 typedef struct {
66 BYTE bWidth;
67 BYTE bHeight;
68 BYTE bColorCount;
69 BYTE bReserved;
70 WORD xHotspot;
71 WORD yHotspot;
72 DWORD dwDIBSize;
73 DWORD dwDIBOffset;
74 } CURSORICONFILEDIRENTRY;
76 typedef struct
78 WORD idReserved;
79 WORD idType;
80 WORD idCount;
81 CURSORICONFILEDIRENTRY idEntries[1];
82 } CURSORICONFILEDIR;
84 #include "poppack.h"
86 #define CID_RESOURCE 0x0001
87 #define CID_WIN32 0x0004
88 #define CID_NONSHARED 0x0008
90 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
92 static HDC screen_dc;
94 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
96 /**********************************************************************
97 * ICONCACHE for cursors/icons loaded with LR_SHARED.
99 * FIXME: This should not be allocated on the system heap, but on a
100 * subsystem-global heap (i.e. one for all Win16 processes,
101 * and one for each Win32 process).
103 typedef struct tagICONCACHE
105 struct tagICONCACHE *next;
107 HMODULE hModule;
108 HRSRC hRsrc;
109 HRSRC hGroupRsrc;
110 HICON hIcon;
112 INT count;
114 } ICONCACHE;
116 static ICONCACHE *IconAnchor = NULL;
118 static CRITICAL_SECTION IconCrst;
119 static CRITICAL_SECTION_DEBUG critsect_debug =
121 0, 0, &IconCrst,
122 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
123 0, 0, { (DWORD_PTR)(__FILE__ ": IconCrst") }
125 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
127 static const WORD ICON_HOTSPOT = 0x4242;
130 /***********************************************************************
131 * map_fileW
133 * Helper function to map a file to memory:
134 * name - file name
135 * [RETURN] ptr - pointer to mapped file
136 * [RETURN] filesize - pointer size of file to be stored if not NULL
138 static void *map_fileW( LPCWSTR name, LPDWORD filesize )
140 HANDLE hFile, hMapping;
141 LPVOID ptr = NULL;
143 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
144 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
145 if (hFile != INVALID_HANDLE_VALUE)
147 hMapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
148 if (hMapping)
150 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
151 CloseHandle( hMapping );
152 if (filesize)
153 *filesize = GetFileSize( hFile, NULL );
155 CloseHandle( hFile );
157 return ptr;
161 /***********************************************************************
162 * get_bitmap_width_bytes
164 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
165 * data.
167 static int get_bitmap_width_bytes( int width, int bpp )
169 switch(bpp)
171 case 1:
172 return 2 * ((width+15) / 16);
173 case 4:
174 return 2 * ((width+3) / 4);
175 case 24:
176 width *= 3;
177 /* fall through */
178 case 8:
179 return width + (width & 1);
180 case 16:
181 case 15:
182 return width * 2;
183 case 32:
184 return width * 4;
185 default:
186 WARN("Unknown depth %d, please report.\n", bpp );
188 return -1;
192 /***********************************************************************
193 * get_dib_width_bytes
195 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
197 static int get_dib_width_bytes( int width, int depth )
199 int words;
201 switch(depth)
203 case 1: words = (width + 31) / 32; break;
204 case 4: words = (width + 7) / 8; break;
205 case 8: words = (width + 3) / 4; break;
206 case 15:
207 case 16: words = (width + 1) / 2; break;
208 case 24: words = (width * 3 + 3)/4; break;
209 default:
210 WARN("(%d): Unsupported depth\n", depth );
211 /* fall through */
212 case 32:
213 words = width;
215 return 4 * words;
219 /***********************************************************************
220 * bitmap_info_size
222 * Return the size of the bitmap info structure including color table.
224 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
226 int colors, masks = 0;
228 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
230 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
231 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
232 return sizeof(BITMAPCOREHEADER) + colors *
233 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
235 else /* assume BITMAPINFOHEADER */
237 colors = info->bmiHeader.biClrUsed;
238 if (colors > 256) /* buffer overflow otherwise */
239 colors = 256;
240 if (!colors && (info->bmiHeader.biBitCount <= 8))
241 colors = 1 << info->bmiHeader.biBitCount;
242 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
243 return sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) + colors *
244 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
249 /***********************************************************************
250 * is_dib_monochrome
252 * Returns whether a DIB can be converted to a monochrome DDB.
254 * A DIB can be converted if its color table contains only black and
255 * white. Black must be the first color in the color table.
257 * Note : If the first color in the color table is white followed by
258 * black, we can't convert it to a monochrome DDB with
259 * SetDIBits, because black and white would be inverted.
261 static BOOL is_dib_monochrome( const BITMAPINFO* info )
263 if (info->bmiHeader.biBitCount != 1) return FALSE;
265 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
267 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO*)info)->bmciColors;
269 /* Check if the first color is black */
270 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
272 rgb++;
274 /* Check if the second color is white */
275 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
276 && (rgb->rgbtBlue == 0xff));
278 else return FALSE;
280 else /* assume BITMAPINFOHEADER */
282 const RGBQUAD *rgb = info->bmiColors;
284 /* Check if the first color is black */
285 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
286 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
288 rgb++;
290 /* Check if the second color is white */
291 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
292 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
294 else return FALSE;
298 /***********************************************************************
299 * DIB_GetBitmapInfo
301 * Get the info from a bitmap header.
302 * Return 1 for INFOHEADER, 0 for COREHEADER,
303 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
305 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
306 LONG *height, WORD *bpp, DWORD *compr )
308 if (header->biSize == sizeof(BITMAPINFOHEADER))
310 *width = header->biWidth;
311 *height = header->biHeight;
312 *bpp = header->biBitCount;
313 *compr = header->biCompression;
314 return 1;
316 if (header->biSize == sizeof(BITMAPCOREHEADER))
318 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
319 *width = core->bcWidth;
320 *height = core->bcHeight;
321 *bpp = core->bcBitCount;
322 *compr = 0;
323 return 0;
325 if (header->biSize == sizeof(BITMAPV4HEADER))
327 const BITMAPV4HEADER *v4hdr = (const BITMAPV4HEADER *)header;
328 *width = v4hdr->bV4Width;
329 *height = v4hdr->bV4Height;
330 *bpp = v4hdr->bV4BitCount;
331 *compr = v4hdr->bV4V4Compression;
332 return 4;
334 if (header->biSize == sizeof(BITMAPV5HEADER))
336 const BITMAPV5HEADER *v5hdr = (const BITMAPV5HEADER *)header;
337 *width = v5hdr->bV5Width;
338 *height = v5hdr->bV5Height;
339 *bpp = v5hdr->bV5BitCount;
340 *compr = v5hdr->bV5Compression;
341 return 5;
343 ERR("(%d): unknown/wrong size for header\n", header->biSize );
344 return -1;
347 /**********************************************************************
348 * CURSORICON_FindSharedIcon
350 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
352 HICON hIcon = 0;
353 ICONCACHE *ptr;
355 EnterCriticalSection( &IconCrst );
357 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
358 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
360 ptr->count++;
361 hIcon = ptr->hIcon;
362 break;
365 LeaveCriticalSection( &IconCrst );
367 return hIcon;
370 /*************************************************************************
371 * CURSORICON_FindCache
373 * Given a handle, find the corresponding cache element
375 * PARAMS
376 * Handle [I] handle to an Image
378 * RETURNS
379 * Success: The cache entry
380 * Failure: NULL
383 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
385 ICONCACHE *ptr;
386 ICONCACHE *pRet=NULL;
387 BOOL IsFound = FALSE;
389 EnterCriticalSection( &IconCrst );
391 for (ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next)
393 if ( hIcon == ptr->hIcon )
395 IsFound = TRUE;
396 pRet = ptr;
400 LeaveCriticalSection( &IconCrst );
402 return pRet;
405 /**********************************************************************
406 * CURSORICON_AddSharedIcon
408 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
410 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
411 if ( !ptr ) return;
413 ptr->hModule = hModule;
414 ptr->hRsrc = hRsrc;
415 ptr->hIcon = hIcon;
416 ptr->hGroupRsrc = hGroupRsrc;
417 ptr->count = 1;
419 EnterCriticalSection( &IconCrst );
420 ptr->next = IconAnchor;
421 IconAnchor = ptr;
422 LeaveCriticalSection( &IconCrst );
425 /**********************************************************************
426 * CURSORICON_DelSharedIcon
428 static INT CURSORICON_DelSharedIcon( HICON hIcon )
430 INT count = -1;
431 ICONCACHE *ptr;
433 EnterCriticalSection( &IconCrst );
435 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
436 if ( ptr->hIcon == hIcon )
438 if ( ptr->count > 0 ) ptr->count--;
439 count = ptr->count;
440 break;
443 LeaveCriticalSection( &IconCrst );
445 return count;
448 /**********************************************************************
449 * CURSORICON_FreeModuleIcons
451 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
453 ICONCACHE **ptr = &IconAnchor;
454 HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
456 EnterCriticalSection( &IconCrst );
458 while ( *ptr )
460 if ( (*ptr)->hModule == hModule )
462 ICONCACHE *freePtr = *ptr;
463 *ptr = freePtr->next;
465 GlobalFree16(HICON_16(freePtr->hIcon));
466 HeapFree( GetProcessHeap(), 0, freePtr );
467 continue;
469 ptr = &(*ptr)->next;
472 LeaveCriticalSection( &IconCrst );
476 * The following macro functions account for the irregularities of
477 * accessing cursor and icon resources in files and resource entries.
479 typedef BOOL (*fnGetCIEntry)( LPVOID dir, int n,
480 int *width, int *height, int *bits );
482 /**********************************************************************
483 * CURSORICON_FindBestIcon
485 * Find the icon closest to the requested size and number of colors.
487 static int CURSORICON_FindBestIcon( LPVOID dir, fnGetCIEntry get_entry,
488 int width, int height, int colors )
490 int i, cx, cy, bits, bestEntry = -1;
491 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
492 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
494 /* Find Best Fit */
495 iTotalDiff = 0xFFFFFFFF;
496 iColorDiff = 0xFFFFFFFF;
497 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
499 iTempXDiff = abs(width - cx);
500 iTempYDiff = abs(height - cy);
502 if(iTotalDiff > (iTempXDiff + iTempYDiff))
504 iXDiff = iTempXDiff;
505 iYDiff = iTempYDiff;
506 iTotalDiff = iXDiff + iYDiff;
510 /* Find Best Colors for Best Fit */
511 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
513 if(abs(width - cx) == iXDiff && abs(height - cy) == iYDiff)
515 iTempColorDiff = abs(colors - (1<<bits));
516 if(iColorDiff > iTempColorDiff)
518 bestEntry = i;
519 iColorDiff = iTempColorDiff;
524 return bestEntry;
527 static BOOL CURSORICON_GetResIconEntry( LPVOID dir, int n,
528 int *width, int *height, int *bits )
530 CURSORICONDIR *resdir = dir;
531 ICONRESDIR *icon;
533 if ( resdir->idCount <= n )
534 return FALSE;
535 icon = &resdir->idEntries[n].ResInfo.icon;
536 *width = icon->bWidth;
537 *height = icon->bHeight;
538 *bits = resdir->idEntries[n].wBitCount;
539 return TRUE;
542 /**********************************************************************
543 * CURSORICON_FindBestCursor
545 * Find the cursor closest to the requested size.
546 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
547 * ignored too
549 static int CURSORICON_FindBestCursor( LPVOID dir, fnGetCIEntry get_entry,
550 int width, int height, int color )
552 int i, maxwidth, maxheight, cx, cy, bits, bestEntry = -1;
554 /* Double height to account for AND and XOR masks */
556 height *= 2;
558 /* First find the largest one smaller than or equal to the requested size*/
560 maxwidth = maxheight = 0;
561 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
563 if ((cx <= width) && (cy <= height) &&
564 (cx > maxwidth) && (cy > maxheight) &&
565 (bits == 1))
567 bestEntry = i;
568 maxwidth = cx;
569 maxheight = cy;
572 if (bestEntry != -1) return bestEntry;
574 /* Now find the smallest one larger than the requested size */
576 maxwidth = maxheight = 255;
577 for ( i = 0; get_entry( dir, i, &cx, &cy, &bits ); i++ )
579 if (((cx < maxwidth) && (cy < maxheight) && (bits == 1)) ||
580 (bestEntry==-1))
582 bestEntry = i;
583 maxwidth = cx;
584 maxheight = cy;
588 return bestEntry;
591 static BOOL CURSORICON_GetResCursorEntry( LPVOID dir, int n,
592 int *width, int *height, int *bits )
594 CURSORICONDIR *resdir = dir;
595 CURSORDIR *cursor;
597 if ( resdir->idCount <= n )
598 return FALSE;
599 cursor = &resdir->idEntries[n].ResInfo.cursor;
600 *width = cursor->wWidth;
601 *height = cursor->wHeight;
602 *bits = resdir->idEntries[n].wBitCount;
603 return TRUE;
606 static CURSORICONDIRENTRY *CURSORICON_FindBestIconRes( CURSORICONDIR * dir,
607 int width, int height, int colors )
609 int n;
611 n = CURSORICON_FindBestIcon( dir, CURSORICON_GetResIconEntry,
612 width, height, colors );
613 if ( n < 0 )
614 return NULL;
615 return &dir->idEntries[n];
618 static CURSORICONDIRENTRY *CURSORICON_FindBestCursorRes( CURSORICONDIR *dir,
619 int width, int height, int color )
621 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetResCursorEntry,
622 width, height, color );
623 if ( n < 0 )
624 return NULL;
625 return &dir->idEntries[n];
628 static BOOL CURSORICON_GetFileEntry( LPVOID dir, int n,
629 int *width, int *height, int *bits )
631 CURSORICONFILEDIR *filedir = dir;
632 CURSORICONFILEDIRENTRY *entry;
634 if ( filedir->idCount <= n )
635 return FALSE;
636 entry = &filedir->idEntries[n];
637 *width = entry->bWidth;
638 *height = entry->bHeight;
639 *bits = entry->bColorCount;
640 return TRUE;
643 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestCursorFile( CURSORICONFILEDIR *dir,
644 int width, int height, int color )
646 int n = CURSORICON_FindBestCursor( dir, CURSORICON_GetFileEntry,
647 width, height, color );
648 if ( n < 0 )
649 return NULL;
650 return &dir->idEntries[n];
653 static CURSORICONFILEDIRENTRY *CURSORICON_FindBestIconFile( CURSORICONFILEDIR *dir,
654 int width, int height, int color )
656 int n = CURSORICON_FindBestIcon( dir, CURSORICON_GetFileEntry,
657 width, height, color );
658 if ( n < 0 )
659 return NULL;
660 return &dir->idEntries[n];
663 /**********************************************************************
664 * CreateIconFromResourceEx (USER32.@)
666 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
667 * with cbSize parameter as well.
669 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
670 BOOL bIcon, DWORD dwVersion,
671 INT width, INT height,
672 UINT cFlag )
674 HGLOBAL16 hObj;
675 static HDC hdcMem;
676 int sizeAnd, sizeXor;
677 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
678 BITMAP bmpXor, bmpAnd;
679 POINT16 hotspot;
680 BITMAPINFO *bmi;
681 BOOL DoStretch;
682 INT size;
684 hotspot.x = ICON_HOTSPOT;
685 hotspot.y = ICON_HOTSPOT;
687 TRACE_(cursor)("%p (%u bytes), ver %08x, %ix%i %s %s\n",
688 bits, cbSize, dwVersion, width, height,
689 bIcon ? "icon" : "cursor", (cFlag & LR_MONOCHROME) ? "mono" : "" );
690 if (dwVersion == 0x00020000)
692 FIXME_(cursor)("\t2.xx resources are not supported\n");
693 return 0;
696 if (bIcon)
697 bmi = (BITMAPINFO *)bits;
698 else /* get the hotspot */
700 POINT16 *pt = (POINT16 *)bits;
701 hotspot = *pt;
702 bmi = (BITMAPINFO *)(pt + 1);
705 /* Check bitmap header */
707 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
708 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
709 bmi->bmiHeader.biCompression != BI_RGB) )
711 WARN_(cursor)("\tinvalid resource bitmap header.\n");
712 return 0;
715 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
717 if (!width) width = bmi->bmiHeader.biWidth;
718 if (!height) height = bmi->bmiHeader.biHeight/2;
719 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
720 (bmi->bmiHeader.biWidth != width);
722 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
723 if (screen_dc)
725 BITMAPINFO* pInfo;
727 /* Make sure we have room for the monochrome bitmap later on.
728 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
729 * up to and including the biBitCount. In-memory icon resource
730 * format is as follows:
732 * BITMAPINFOHEADER icHeader // DIB header
733 * RGBQUAD icColors[] // Color table
734 * BYTE icXOR[] // DIB bits for XOR mask
735 * BYTE icAND[] // DIB bits for AND mask
738 if ((pInfo = HeapAlloc( GetProcessHeap(), 0,
739 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
741 memcpy( pInfo, bmi, size );
742 pInfo->bmiHeader.biHeight /= 2;
744 /* Create the XOR bitmap */
746 if (DoStretch) {
747 if(bIcon)
749 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
751 else
753 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
755 if(hXorBits)
757 HBITMAP hOld;
758 BOOL res = FALSE;
760 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
761 if (hdcMem) {
762 hOld = SelectObject(hdcMem, hXorBits);
763 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
764 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
765 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
766 SelectObject(hdcMem, hOld);
768 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
770 } else {
771 if (is_dib_monochrome(bmi)) {
772 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
773 SetDIBits(screen_dc, hXorBits, 0, height,
774 (char*)bmi + size, pInfo, DIB_RGB_COLORS);
776 else
777 hXorBits = CreateDIBitmap(screen_dc, &pInfo->bmiHeader,
778 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS);
781 if( hXorBits )
783 char* xbits = (char *)bmi + size +
784 get_dib_width_bytes( bmi->bmiHeader.biWidth,
785 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
787 pInfo->bmiHeader.biBitCount = 1;
788 if (pInfo->bmiHeader.biSize != sizeof(BITMAPCOREHEADER))
790 RGBQUAD *rgb = pInfo->bmiColors;
792 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
793 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
794 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
795 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
797 else
799 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
801 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
802 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
805 /* Create the AND bitmap */
807 if (DoStretch) {
808 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
809 HBITMAP hOld;
810 BOOL res = FALSE;
812 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
813 if (hdcMem) {
814 hOld = SelectObject(hdcMem, hAndBits);
815 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
816 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
817 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
818 SelectObject(hdcMem, hOld);
820 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
822 } else {
823 hAndBits = CreateBitmap(width, height, 1, 1, NULL);
825 if (hAndBits) SetDIBits(screen_dc, hAndBits, 0, height,
826 xbits, pInfo, DIB_RGB_COLORS);
829 if( !hAndBits ) DeleteObject( hXorBits );
831 HeapFree( GetProcessHeap(), 0, pInfo );
835 if( !hXorBits || !hAndBits )
837 WARN_(cursor)("\tunable to create an icon bitmap.\n");
838 return 0;
841 /* Now create the CURSORICONINFO structure */
842 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
843 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
844 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
845 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
847 hObj = GlobalAlloc16( GMEM_MOVEABLE,
848 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
849 if (hObj)
851 CURSORICONINFO *info;
853 info = (CURSORICONINFO *)GlobalLock16( hObj );
854 info->ptHotSpot.x = hotspot.x;
855 info->ptHotSpot.y = hotspot.y;
856 info->nWidth = bmpXor.bmWidth;
857 info->nHeight = bmpXor.bmHeight;
858 info->nWidthBytes = bmpXor.bmWidthBytes;
859 info->bPlanes = bmpXor.bmPlanes;
860 info->bBitsPerPixel = bmpXor.bmBitsPixel;
862 /* Transfer the bitmap bits to the CURSORICONINFO structure */
864 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
865 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
866 GlobalUnlock16( hObj );
869 DeleteObject( hAndBits );
870 DeleteObject( hXorBits );
871 return HICON_32(hObj);
875 /**********************************************************************
876 * CreateIconFromResource (USER32.@)
878 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
879 BOOL bIcon, DWORD dwVersion)
881 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
885 static HICON CURSORICON_LoadFromFile( LPCWSTR filename,
886 INT width, INT height, INT colors,
887 BOOL fCursor, UINT loadflags)
889 CURSORICONFILEDIRENTRY *entry;
890 CURSORICONFILEDIR *dir;
891 DWORD filesize = 0;
892 HICON hIcon = 0;
893 LPBYTE bits;
895 TRACE("loading %s\n", debugstr_w( filename ));
897 bits = map_fileW( filename, &filesize );
898 if (!bits)
899 return hIcon;
901 /* Check for .ani. */
902 if (memcmp( bits, "RIFF", 4 ) == 0)
904 FIXME("No support for .ani cursors.\n");
905 goto end;
908 dir = (CURSORICONFILEDIR*) bits;
909 if ( filesize < sizeof(*dir) )
910 goto end;
912 if ( filesize < (sizeof(*dir) + sizeof(dir->idEntries[0])*(dir->idCount-1)) )
913 goto end;
915 if ( fCursor )
916 entry = CURSORICON_FindBestCursorFile( dir, width, height, colors );
917 else
918 entry = CURSORICON_FindBestIconFile( dir, width, height, colors );
920 if ( !entry )
921 goto end;
923 /* check that we don't run off the end of the file */
924 if ( entry->dwDIBOffset > filesize )
925 goto end;
926 if ( entry->dwDIBOffset + entry->dwDIBSize > filesize )
927 goto end;
929 hIcon = CreateIconFromResourceEx( &bits[entry->dwDIBOffset], entry->dwDIBSize,
930 !fCursor, 0x00030000, width, height, loadflags );
931 end:
932 TRACE("loaded %s -> %p\n", debugstr_w( filename ), hIcon );
933 UnmapViewOfFile( bits );
934 return hIcon;
937 /**********************************************************************
938 * CURSORICON_Load
940 * Load a cursor or icon from resource or file.
942 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
943 INT width, INT height, INT colors,
944 BOOL fCursor, UINT loadflags)
946 HANDLE handle = 0;
947 HICON hIcon = 0;
948 HRSRC hRsrc, hGroupRsrc;
949 CURSORICONDIR *dir;
950 CURSORICONDIRENTRY *dirEntry;
951 LPBYTE bits;
952 WORD wResId;
953 DWORD dwBytesInRes;
955 TRACE("%p, %s, %dx%d, colors %d, fCursor %d, flags 0x%04x\n",
956 hInstance, debugstr_w(name), width, height, colors, fCursor, loadflags);
958 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
959 return CURSORICON_LoadFromFile( name, width, height, colors, fCursor, loadflags );
961 if (!hInstance) hInstance = user32_module; /* Load OEM cursor/icon */
963 /* Normalize hInstance (must be uniquely represented for icon cache) */
965 if (!HIWORD( hInstance ))
966 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
968 /* Get directory resource ID */
970 if (!(hRsrc = FindResourceW( hInstance, name,
971 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
972 return 0;
973 hGroupRsrc = hRsrc;
975 /* Find the best entry in the directory */
977 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
978 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
979 if (fCursor)
980 dirEntry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
981 else
982 dirEntry = CURSORICON_FindBestIconRes( dir, width, height, colors );
983 if (!dirEntry) return 0;
984 wResId = dirEntry->wResId;
985 dwBytesInRes = dirEntry->dwBytesInRes;
986 FreeResource( handle );
988 /* Load the resource */
990 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
991 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
993 /* If shared icon, check whether it was already loaded */
994 if ( (loadflags & LR_SHARED)
995 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
996 return hIcon;
998 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
999 bits = (LPBYTE)LockResource( handle );
1000 hIcon = CreateIconFromResourceEx( bits, dwBytesInRes,
1001 !fCursor, 0x00030000, width, height, loadflags);
1002 FreeResource( handle );
1004 /* If shared icon, add to icon cache */
1006 if ( hIcon && (loadflags & LR_SHARED) )
1007 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
1009 return hIcon;
1012 /***********************************************************************
1013 * CURSORICON_Copy
1015 * Make a copy of a cursor or icon.
1017 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
1019 char *ptrOld, *ptrNew;
1020 int size;
1021 HICON16 hOld = HICON_16(hIcon);
1022 HICON16 hNew;
1024 if (!(ptrOld = (char *)GlobalLock16( hOld ))) return 0;
1025 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
1026 size = GlobalSize16( hOld );
1027 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
1028 FarSetOwner16( hNew, hInst16 );
1029 ptrNew = (char *)GlobalLock16( hNew );
1030 memcpy( ptrNew, ptrOld, size );
1031 GlobalUnlock16( hOld );
1032 GlobalUnlock16( hNew );
1033 return HICON_32(hNew);
1036 /*************************************************************************
1037 * CURSORICON_ExtCopy
1039 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
1041 * PARAMS
1042 * Handle [I] handle to an Image
1043 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
1044 * iDesiredCX [I] The Desired width of the Image
1045 * iDesiredCY [I] The desired height of the Image
1046 * nFlags [I] The flags from CopyImage
1048 * RETURNS
1049 * Success: The new handle of the Image
1051 * NOTES
1052 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
1053 * LR_MONOCHROME should be implemented by CreateIconFromResourceEx.
1054 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
1059 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
1060 INT iDesiredCX, INT iDesiredCY,
1061 UINT nFlags)
1063 HICON hNew=0;
1065 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
1066 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
1068 if(hIcon == 0)
1070 return 0;
1073 /* Best Fit or Monochrome */
1074 if( (nFlags & LR_COPYFROMRESOURCE
1075 && (iDesiredCX > 0 || iDesiredCY > 0))
1076 || nFlags & LR_MONOCHROME)
1078 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
1080 /* Not Found in Cache, then do a straight copy
1082 if(pIconCache == NULL)
1084 hNew = CURSORICON_Copy(0, hIcon);
1085 if(nFlags & LR_COPYFROMRESOURCE)
1087 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
1090 else
1092 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
1093 LPBYTE pBits;
1094 HANDLE hMem;
1095 HRSRC hRsrc;
1096 DWORD dwBytesInRes;
1097 WORD wResId;
1098 CURSORICONDIR *pDir;
1099 CURSORICONDIRENTRY *pDirEntry;
1100 BOOL bIsIcon = (nType == IMAGE_ICON);
1102 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
1104 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
1105 || (iDesiredCX == 0 && iDesiredCY == 0))
1107 iDesiredCY = GetSystemMetrics(bIsIcon ?
1108 SM_CYICON : SM_CYCURSOR);
1109 iDesiredCX = GetSystemMetrics(bIsIcon ?
1110 SM_CXICON : SM_CXCURSOR);
1113 /* Retrieve the CURSORICONDIRENTRY
1115 if (!(hMem = LoadResource( pIconCache->hModule ,
1116 pIconCache->hGroupRsrc)))
1118 return 0;
1120 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
1122 return 0;
1125 /* Find Best Fit
1127 if(bIsIcon)
1129 pDirEntry = CURSORICON_FindBestIconRes(
1130 pDir, iDesiredCX, iDesiredCY, 256 );
1132 else
1134 pDirEntry = CURSORICON_FindBestCursorRes(
1135 pDir, iDesiredCX, iDesiredCY, 1);
1138 wResId = pDirEntry->wResId;
1139 dwBytesInRes = pDirEntry->dwBytesInRes;
1140 FreeResource(hMem);
1142 TRACE_(icon)("ResID %u, BytesInRes %u, Width %d, Height %d DX %d, DY %d\n",
1143 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1144 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1146 /* Get the Best Fit
1148 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1149 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1151 return 0;
1153 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1155 return 0;
1158 pBits = (LPBYTE)LockResource( hMem );
1160 if(nFlags & LR_DEFAULTSIZE)
1162 iTargetCY = GetSystemMetrics(SM_CYICON);
1163 iTargetCX = GetSystemMetrics(SM_CXICON);
1166 /* Create a New Icon with the proper dimension
1168 hNew = CreateIconFromResourceEx( pBits, dwBytesInRes,
1169 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1170 FreeResource(hMem);
1173 else hNew = CURSORICON_Copy(0, hIcon);
1174 return hNew;
1178 /***********************************************************************
1179 * CreateCursor (USER32.@)
1181 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1182 INT xHotSpot, INT yHotSpot,
1183 INT nWidth, INT nHeight,
1184 LPCVOID lpANDbits, LPCVOID lpXORbits )
1186 CURSORICONINFO info;
1188 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1189 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1191 info.ptHotSpot.x = xHotSpot;
1192 info.ptHotSpot.y = yHotSpot;
1193 info.nWidth = nWidth;
1194 info.nHeight = nHeight;
1195 info.nWidthBytes = 0;
1196 info.bPlanes = 1;
1197 info.bBitsPerPixel = 1;
1199 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1203 /***********************************************************************
1204 * CreateIcon (USER.407)
1206 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1207 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1208 LPCVOID lpANDbits, LPCVOID lpXORbits )
1210 CURSORICONINFO info;
1212 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1213 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1215 info.ptHotSpot.x = ICON_HOTSPOT;
1216 info.ptHotSpot.y = ICON_HOTSPOT;
1217 info.nWidth = nWidth;
1218 info.nHeight = nHeight;
1219 info.nWidthBytes = 0;
1220 info.bPlanes = bPlanes;
1221 info.bBitsPerPixel = bBitsPixel;
1223 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1227 /***********************************************************************
1228 * CreateIcon (USER32.@)
1230 * Creates an icon based on the specified bitmaps. The bitmaps must be
1231 * provided in a device dependent format and will be resized to
1232 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1233 * depth. The provided bitmaps must be top-down bitmaps.
1234 * Although Windows does not support 15bpp(*) this API must support it
1235 * for Winelib applications.
1237 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1238 * format!
1240 * RETURNS
1241 * Success: handle to an icon
1242 * Failure: NULL
1244 * FIXME: Do we need to resize the bitmaps?
1246 HICON WINAPI CreateIcon(
1247 HINSTANCE hInstance, /* [in] the application's hInstance */
1248 INT nWidth, /* [in] the width of the provided bitmaps */
1249 INT nHeight, /* [in] the height of the provided bitmaps */
1250 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1251 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1252 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1253 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1255 ICONINFO iinfo;
1256 HICON hIcon;
1258 TRACE_(icon)("%dx%d, planes %d, bpp %d, xor %p, and %p\n",
1259 nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits, lpANDbits);
1261 iinfo.fIcon = TRUE;
1262 iinfo.xHotspot = ICON_HOTSPOT;
1263 iinfo.yHotspot = ICON_HOTSPOT;
1264 iinfo.hbmMask = CreateBitmap( nWidth, nHeight, 1, 1, lpANDbits );
1265 iinfo.hbmColor = CreateBitmap( nWidth, nHeight, bPlanes, bBitsPixel, lpXORbits );
1267 hIcon = CreateIconIndirect( &iinfo );
1269 DeleteObject( iinfo.hbmMask );
1270 DeleteObject( iinfo.hbmColor );
1272 return hIcon;
1276 /***********************************************************************
1277 * CreateCursorIconIndirect (USER.408)
1279 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1280 CURSORICONINFO *info,
1281 LPCVOID lpANDbits,
1282 LPCVOID lpXORbits )
1284 HGLOBAL16 handle;
1285 char *ptr;
1286 int sizeAnd, sizeXor;
1288 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1289 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1290 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1291 sizeXor = info->nHeight * info->nWidthBytes;
1292 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1293 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1294 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1295 return 0;
1296 FarSetOwner16( handle, hInstance );
1297 ptr = (char *)GlobalLock16( handle );
1298 memcpy( ptr, info, sizeof(*info) );
1299 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1300 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1301 GlobalUnlock16( handle );
1302 return handle;
1306 /***********************************************************************
1307 * CopyIcon (USER.368)
1309 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1311 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1312 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1316 /***********************************************************************
1317 * CopyIcon (USER32.@)
1319 HICON WINAPI CopyIcon( HICON hIcon )
1321 TRACE_(icon)("%p\n", hIcon );
1322 return CURSORICON_Copy( 0, hIcon );
1326 /***********************************************************************
1327 * CopyCursor (USER.369)
1329 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1331 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1332 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1335 /**********************************************************************
1336 * DestroyIcon32 (USER.610)
1338 * This routine is actually exported from Win95 USER under the name
1339 * DestroyIcon32 ... The behaviour implemented here should mimic
1340 * the Win95 one exactly, especially the return values, which
1341 * depend on the setting of various flags.
1343 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1345 WORD retv;
1347 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1349 /* Check whether destroying active cursor */
1351 if ( get_user_thread_info()->cursor == HICON_32(handle) )
1353 WARN_(cursor)("Destroying active cursor!\n" );
1354 return FALSE;
1357 /* Try shared cursor/icon first */
1359 if ( !(flags & CID_NONSHARED) )
1361 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1363 if ( count != -1 )
1364 return (flags & CID_WIN32)? TRUE : (count == 0);
1366 /* FIXME: OEM cursors/icons should be recognized */
1369 /* Now assume non-shared cursor/icon */
1371 retv = GlobalFree16( handle );
1372 return (flags & CID_RESOURCE)? retv : TRUE;
1375 /***********************************************************************
1376 * DestroyIcon (USER32.@)
1378 BOOL WINAPI DestroyIcon( HICON hIcon )
1380 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1384 /***********************************************************************
1385 * DestroyCursor (USER32.@)
1387 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1389 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1393 /***********************************************************************
1394 * DrawIcon (USER32.@)
1396 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1398 CURSORICONINFO *ptr;
1399 HDC hMemDC;
1400 HBITMAP hXorBits, hAndBits;
1401 COLORREF oldFg, oldBg;
1403 TRACE("%p, (%d,%d), %p\n", hdc, x, y, hIcon);
1405 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
1406 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1407 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
1408 (char *)(ptr+1) );
1409 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1410 ptr->bBitsPerPixel, (char *)(ptr + 1)
1411 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
1412 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1413 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1415 if (hXorBits && hAndBits)
1417 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1418 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
1419 SelectObject( hMemDC, hXorBits );
1420 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
1421 SelectObject( hMemDC, hBitTemp );
1423 DeleteDC( hMemDC );
1424 if (hXorBits) DeleteObject( hXorBits );
1425 if (hAndBits) DeleteObject( hAndBits );
1426 GlobalUnlock16(HICON_16(hIcon));
1427 SetTextColor( hdc, oldFg );
1428 SetBkColor( hdc, oldBg );
1429 return TRUE;
1432 /***********************************************************************
1433 * DumpIcon (USER.459)
1435 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1436 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1438 CURSORICONINFO *info = MapSL( pInfo );
1439 int sizeAnd, sizeXor;
1441 if (!info) return 0;
1442 sizeXor = info->nHeight * info->nWidthBytes;
1443 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1444 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1445 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1446 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1447 return MAKELONG( sizeXor, sizeXor );
1451 /***********************************************************************
1452 * SetCursor (USER32.@)
1454 * Set the cursor shape.
1456 * RETURNS
1457 * A handle to the previous cursor shape.
1459 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1461 struct user_thread_info *thread_info = get_user_thread_info();
1462 HCURSOR hOldCursor;
1464 if (hCursor == thread_info->cursor) return hCursor; /* No change */
1465 TRACE("%p\n", hCursor);
1466 hOldCursor = thread_info->cursor;
1467 thread_info->cursor = hCursor;
1468 /* Change the cursor shape only if it is visible */
1469 if (thread_info->cursor_count >= 0)
1471 USER_Driver->pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
1472 GlobalUnlock16(HCURSOR_16(hCursor));
1474 return hOldCursor;
1477 /***********************************************************************
1478 * ShowCursor (USER32.@)
1480 INT WINAPI ShowCursor( BOOL bShow )
1482 struct user_thread_info *thread_info = get_user_thread_info();
1484 TRACE("%d, count=%d\n", bShow, thread_info->cursor_count );
1486 if (bShow)
1488 if (++thread_info->cursor_count == 0) /* Show it */
1490 USER_Driver->pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(thread_info->cursor)));
1491 GlobalUnlock16(HCURSOR_16(thread_info->cursor));
1494 else
1496 if (--thread_info->cursor_count == -1) /* Hide it */
1497 USER_Driver->pSetCursor( NULL );
1499 return thread_info->cursor_count;
1502 /***********************************************************************
1503 * GetCursor (USER32.@)
1505 HCURSOR WINAPI GetCursor(void)
1507 return get_user_thread_info()->cursor;
1511 /***********************************************************************
1512 * ClipCursor (USER32.@)
1514 BOOL WINAPI ClipCursor( const RECT *rect )
1516 RECT virt;
1518 SetRect( &virt, 0, 0, GetSystemMetrics( SM_CXVIRTUALSCREEN ),
1519 GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
1520 OffsetRect( &virt, GetSystemMetrics( SM_XVIRTUALSCREEN ),
1521 GetSystemMetrics( SM_YVIRTUALSCREEN ) );
1523 TRACE( "Clipping to: %s was: %s screen: %s\n", wine_dbgstr_rect(rect),
1524 wine_dbgstr_rect(&CURSOR_ClipRect), wine_dbgstr_rect(&virt) );
1526 if (!IntersectRect( &CURSOR_ClipRect, &virt, rect ))
1527 CURSOR_ClipRect = virt;
1529 USER_Driver->pClipCursor( rect );
1530 return TRUE;
1534 /***********************************************************************
1535 * GetClipCursor (USER32.@)
1537 BOOL WINAPI GetClipCursor( RECT *rect )
1539 /* If this is first time - initialize the rect */
1540 if (IsRectEmpty( &CURSOR_ClipRect )) ClipCursor( NULL );
1542 return CopyRect( rect, &CURSOR_ClipRect );
1546 /***********************************************************************
1547 * SetSystemCursor (USER32.@)
1549 BOOL WINAPI SetSystemCursor(HCURSOR hcur, DWORD id)
1551 FIXME("(%p,%08x),stub!\n", hcur, id);
1552 return TRUE;
1556 /**********************************************************************
1557 * LookupIconIdFromDirectoryEx (USER.364)
1559 * FIXME: exact parameter sizes
1561 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1562 INT16 width, INT16 height, UINT16 cFlag )
1564 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1567 /**********************************************************************
1568 * LookupIconIdFromDirectoryEx (USER32.@)
1570 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1571 INT width, INT height, UINT cFlag )
1573 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1574 UINT retVal = 0;
1575 if( dir && !dir->idReserved && (dir->idType & 3) )
1577 CURSORICONDIRENTRY* entry;
1578 HDC hdc;
1579 UINT palEnts;
1580 int colors;
1581 hdc = GetDC(0);
1582 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1583 if (palEnts == 0)
1584 palEnts = 256;
1585 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1587 ReleaseDC(0, hdc);
1589 if( bIcon )
1590 entry = CURSORICON_FindBestIconRes( dir, width, height, colors );
1591 else
1592 entry = CURSORICON_FindBestCursorRes( dir, width, height, 1);
1594 if( entry ) retVal = entry->wResId;
1596 else WARN_(cursor)("invalid resource directory\n");
1597 return retVal;
1600 /**********************************************************************
1601 * LookupIconIdFromDirectory (USER.?)
1603 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
1605 return LookupIconIdFromDirectoryEx16( dir, bIcon,
1606 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1607 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1610 /**********************************************************************
1611 * LookupIconIdFromDirectory (USER32.@)
1613 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1615 return LookupIconIdFromDirectoryEx( dir, bIcon,
1616 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1617 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1620 /**********************************************************************
1621 * GetIconID (USER.455)
1623 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1625 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
1627 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1628 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1630 switch(resType)
1632 case RT_CURSOR:
1633 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1634 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1635 case RT_ICON:
1636 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1637 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1638 default:
1639 WARN_(cursor)("invalid res type %d\n", resType );
1641 return 0;
1644 /**********************************************************************
1645 * LoadCursorIconHandler (USER.336)
1647 * Supposed to load resources of Windows 2.x applications.
1649 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1651 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1652 hResource, hModule, hRsrc);
1653 return (HGLOBAL16)0;
1656 /**********************************************************************
1657 * LoadIconHandler (USER.456)
1659 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
1661 LPBYTE bits = (LPBYTE)LockResource16( hResource );
1663 TRACE_(cursor)("hRes=%04x\n",hResource);
1665 return HICON_16(CreateIconFromResourceEx( bits, 0, TRUE,
1666 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
1669 /***********************************************************************
1670 * LoadCursorW (USER32.@)
1672 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1674 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1676 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1677 LR_SHARED | LR_DEFAULTSIZE );
1680 /***********************************************************************
1681 * LoadCursorA (USER32.@)
1683 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1685 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1687 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1688 LR_SHARED | LR_DEFAULTSIZE );
1691 /***********************************************************************
1692 * LoadCursorFromFileW (USER32.@)
1694 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1696 TRACE("%s\n", debugstr_w(name));
1698 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1699 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1702 /***********************************************************************
1703 * LoadCursorFromFileA (USER32.@)
1705 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1707 TRACE("%s\n", debugstr_a(name));
1709 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1710 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1713 /***********************************************************************
1714 * LoadIconW (USER32.@)
1716 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1718 TRACE("%p, %s\n", hInstance, debugstr_w(name));
1720 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1721 LR_SHARED | LR_DEFAULTSIZE );
1724 /***********************************************************************
1725 * LoadIconA (USER32.@)
1727 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1729 TRACE("%p, %s\n", hInstance, debugstr_a(name));
1731 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1732 LR_SHARED | LR_DEFAULTSIZE );
1735 /**********************************************************************
1736 * GetIconInfo (USER32.@)
1738 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1740 CURSORICONINFO *ciconinfo;
1741 INT height;
1743 ciconinfo = GlobalLock16(HICON_16(hIcon));
1744 if (!ciconinfo)
1745 return FALSE;
1747 TRACE("%p => %dx%d, %d bpp\n", hIcon,
1748 ciconinfo->nWidth, ciconinfo->nHeight, ciconinfo->bBitsPerPixel);
1750 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1751 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1753 iconinfo->fIcon = TRUE;
1754 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1755 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1757 else
1759 iconinfo->fIcon = FALSE;
1760 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1761 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1764 height = ciconinfo->nHeight;
1766 if (ciconinfo->bBitsPerPixel > 1)
1768 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
1769 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1770 (char *)(ciconinfo + 1)
1771 + ciconinfo->nHeight *
1772 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1774 else
1776 iconinfo->hbmColor = 0;
1777 height *= 2;
1780 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
1781 1, 1, (char *)(ciconinfo + 1));
1783 GlobalUnlock16(HICON_16(hIcon));
1785 return TRUE;
1788 /**********************************************************************
1789 * CreateIconIndirect (USER32.@)
1791 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1793 DIBSECTION bmpXor;
1794 BITMAP bmpAnd;
1795 HICON16 hObj;
1796 int xor_objsize = 0, sizeXor = 0, sizeAnd, planes, bpp;
1798 TRACE("color %p, mask %p, hotspot %ux%u, fIcon %d\n",
1799 iconinfo->hbmColor, iconinfo->hbmMask,
1800 iconinfo->xHotspot, iconinfo->yHotspot, iconinfo->fIcon);
1802 if (!iconinfo->hbmMask) return 0;
1804 planes = GetDeviceCaps( screen_dc, PLANES );
1805 bpp = GetDeviceCaps( screen_dc, BITSPIXEL );
1807 if (iconinfo->hbmColor)
1809 xor_objsize = GetObjectW( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1810 TRACE("color: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1811 bmpXor.dsBm.bmWidth, bmpXor.dsBm.bmHeight, bmpXor.dsBm.bmWidthBytes,
1812 bmpXor.dsBm.bmPlanes, bmpXor.dsBm.bmBitsPixel);
1813 /* we can use either depth 1 or screen depth for xor bitmap */
1814 if (bmpXor.dsBm.bmPlanes == 1 && bmpXor.dsBm.bmBitsPixel == 1) planes = bpp = 1;
1815 sizeXor = bmpXor.dsBm.bmHeight * planes * get_bitmap_width_bytes( bmpXor.dsBm.bmWidth, bpp );
1817 GetObjectW( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1818 TRACE("mask: width %d, height %d, width bytes %d, planes %u, bpp %u\n",
1819 bmpAnd.bmWidth, bmpAnd.bmHeight, bmpAnd.bmWidthBytes,
1820 bmpAnd.bmPlanes, bmpAnd.bmBitsPixel);
1822 sizeAnd = bmpAnd.bmHeight * get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
1824 hObj = GlobalAlloc16( GMEM_MOVEABLE,
1825 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1826 if (hObj)
1828 CURSORICONINFO *info;
1830 info = (CURSORICONINFO *)GlobalLock16( hObj );
1832 /* If we are creating an icon, the hotspot is unused */
1833 if (iconinfo->fIcon)
1835 info->ptHotSpot.x = ICON_HOTSPOT;
1836 info->ptHotSpot.y = ICON_HOTSPOT;
1838 else
1840 info->ptHotSpot.x = iconinfo->xHotspot;
1841 info->ptHotSpot.y = iconinfo->yHotspot;
1844 if (iconinfo->hbmColor)
1846 info->nWidth = bmpXor.dsBm.bmWidth;
1847 info->nHeight = bmpXor.dsBm.bmHeight;
1848 info->nWidthBytes = bmpXor.dsBm.bmWidthBytes;
1849 info->bPlanes = planes;
1850 info->bBitsPerPixel = bpp;
1852 else
1854 info->nWidth = bmpAnd.bmWidth;
1855 info->nHeight = bmpAnd.bmHeight / 2;
1856 info->nWidthBytes = get_bitmap_width_bytes(bmpAnd.bmWidth, 1);
1857 info->bPlanes = 1;
1858 info->bBitsPerPixel = 1;
1861 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1863 /* Some apps pass a color bitmap as a mask, convert it to b/w */
1864 if (bmpAnd.bmBitsPixel == 1)
1866 GetBitmapBits( iconinfo->hbmMask, sizeAnd, (char*)(info + 1) );
1868 else
1870 HDC hdc, hdc_mem;
1871 HBITMAP hbmp_old, hbmp_mem_old, hbmp_mono;
1873 hdc = GetDC( 0 );
1874 hdc_mem = CreateCompatibleDC( hdc );
1876 hbmp_mono = CreateBitmap( bmpAnd.bmWidth, bmpAnd.bmHeight, 1, 1, NULL );
1878 hbmp_old = SelectObject( hdc, iconinfo->hbmMask );
1879 hbmp_mem_old = SelectObject( hdc_mem, hbmp_mono );
1881 BitBlt( hdc_mem, 0, 0, bmpAnd.bmWidth, bmpAnd.bmHeight, hdc, 0, 0, SRCCOPY );
1883 SelectObject( hdc, hbmp_old );
1884 SelectObject( hdc_mem, hbmp_mem_old );
1886 DeleteDC( hdc_mem );
1887 ReleaseDC( 0, hdc );
1889 GetBitmapBits( hbmp_mono, sizeAnd, (char*)(info + 1) );
1890 DeleteObject( hbmp_mono );
1893 if (iconinfo->hbmColor)
1895 char *dst_bits = (char*)(info + 1) + sizeAnd;
1897 if (bmpXor.dsBm.bmPlanes == planes && bmpXor.dsBm.bmBitsPixel == bpp)
1898 GetBitmapBits( iconinfo->hbmColor, sizeXor, dst_bits );
1899 else
1901 BITMAPINFO bminfo;
1902 int dib_width = get_dib_width_bytes( info->nWidth, info->bBitsPerPixel );
1903 int bitmap_width = get_bitmap_width_bytes( info->nWidth, info->bBitsPerPixel );
1905 bminfo.bmiHeader.biSize = sizeof(bminfo);
1906 bminfo.bmiHeader.biWidth = info->nWidth;
1907 bminfo.bmiHeader.biHeight = info->nHeight;
1908 bminfo.bmiHeader.biPlanes = info->bPlanes;
1909 bminfo.bmiHeader.biBitCount = info->bBitsPerPixel;
1910 bminfo.bmiHeader.biCompression = BI_RGB;
1911 bminfo.bmiHeader.biSizeImage = info->nHeight * dib_width;
1912 bminfo.bmiHeader.biXPelsPerMeter = 0;
1913 bminfo.bmiHeader.biYPelsPerMeter = 0;
1914 bminfo.bmiHeader.biClrUsed = 0;
1915 bminfo.bmiHeader.biClrImportant = 0;
1917 /* swap lines for dib sections */
1918 if (xor_objsize == sizeof(DIBSECTION))
1919 bminfo.bmiHeader.biHeight = -bminfo.bmiHeader.biHeight;
1921 if (dib_width != bitmap_width) /* need to fixup alignment */
1923 char *src_bits = HeapAlloc( GetProcessHeap(), 0, bminfo.bmiHeader.biSizeImage );
1925 if (src_bits && GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
1926 src_bits, &bminfo, DIB_RGB_COLORS ))
1928 int y;
1929 for (y = 0; y < info->nHeight; y++)
1930 memcpy( dst_bits + y * bitmap_width, src_bits + y * dib_width, bitmap_width );
1932 HeapFree( GetProcessHeap(), 0, src_bits );
1934 else
1935 GetDIBits( screen_dc, iconinfo->hbmColor, 0, info->nHeight,
1936 dst_bits, &bminfo, DIB_RGB_COLORS );
1939 GlobalUnlock16( hObj );
1941 return HICON_32(hObj);
1944 /******************************************************************************
1945 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1947 * NOTES
1948 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1950 * PARAMS
1951 * hdc [I] Handle to device context
1952 * x0 [I] X coordinate of upper left corner
1953 * y0 [I] Y coordinate of upper left corner
1954 * hIcon [I] Handle to icon to draw
1955 * cxWidth [I] Width of icon
1956 * cyWidth [I] Height of icon
1957 * istep [I] Index of frame in animated cursor
1958 * hbr [I] Handle to background brush
1959 * flags [I] Icon-drawing flags
1961 * RETURNS
1962 * Success: TRUE
1963 * Failure: FALSE
1965 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1966 INT cxWidth, INT cyWidth, UINT istep,
1967 HBRUSH hbr, UINT flags )
1969 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
1970 HDC hDC_off = 0, hMemDC;
1971 BOOL result = FALSE, DoOffscreen;
1972 HBITMAP hB_off = 0, hOld = 0;
1974 if (!ptr) return FALSE;
1975 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1976 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1978 hMemDC = CreateCompatibleDC (hdc);
1979 if (istep)
1980 FIXME_(icon)("Ignoring istep=%d\n", istep);
1981 if (flags & DI_COMPAT)
1982 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
1984 if (!flags) {
1985 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
1986 flags = DI_NORMAL;
1989 /* Calculate the size of the destination image. */
1990 if (cxWidth == 0)
1992 if (flags & DI_DEFAULTSIZE)
1993 cxWidth = GetSystemMetrics (SM_CXICON);
1994 else
1995 cxWidth = ptr->nWidth;
1997 if (cyWidth == 0)
1999 if (flags & DI_DEFAULTSIZE)
2000 cyWidth = GetSystemMetrics (SM_CYICON);
2001 else
2002 cyWidth = ptr->nHeight;
2005 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
2007 if (DoOffscreen) {
2008 RECT r;
2010 r.left = 0;
2011 r.top = 0;
2012 r.right = cxWidth;
2013 r.bottom = cxWidth;
2015 hDC_off = CreateCompatibleDC(hdc);
2016 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
2017 if (hDC_off && hB_off) {
2018 hOld = SelectObject(hDC_off, hB_off);
2019 FillRect(hDC_off, &r, hbr);
2023 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
2025 HBITMAP hXorBits, hAndBits;
2026 COLORREF oldFg, oldBg;
2027 INT nStretchMode;
2029 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
2031 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
2032 ptr->bPlanes, ptr->bBitsPerPixel,
2033 (char *)(ptr + 1)
2034 + ptr->nHeight *
2035 get_bitmap_width_bytes(ptr->nWidth,1) );
2036 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
2037 1, 1, (char *)(ptr+1) );
2038 oldFg = SetTextColor( hdc, RGB(0,0,0) );
2039 oldBg = SetBkColor( hdc, RGB(255,255,255) );
2041 if (hXorBits && hAndBits)
2043 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
2044 if (flags & DI_MASK)
2046 if (DoOffscreen)
2047 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2048 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2049 else
2050 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2051 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
2053 SelectObject( hMemDC, hXorBits );
2054 if (flags & DI_IMAGE)
2056 if (DoOffscreen)
2057 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
2058 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2059 else
2060 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
2061 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
2063 SelectObject( hMemDC, hBitTemp );
2064 result = TRUE;
2067 SetTextColor( hdc, oldFg );
2068 SetBkColor( hdc, oldBg );
2069 if (hXorBits) DeleteObject( hXorBits );
2070 if (hAndBits) DeleteObject( hAndBits );
2071 SetStretchBltMode (hdc, nStretchMode);
2072 if (DoOffscreen) {
2073 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
2074 SelectObject(hDC_off, hOld);
2077 if (hMemDC) DeleteDC( hMemDC );
2078 if (hDC_off) DeleteDC(hDC_off);
2079 if (hB_off) DeleteObject(hB_off);
2080 GlobalUnlock16(HICON_16(hIcon));
2081 return result;
2084 /***********************************************************************
2085 * DIB_FixColorsToLoadflags
2087 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
2088 * are in loadflags
2090 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
2092 int colors;
2093 COLORREF c_W, c_S, c_F, c_L, c_C;
2094 int incr,i;
2095 RGBQUAD *ptr;
2096 int bitmap_type;
2097 LONG width;
2098 LONG height;
2099 WORD bpp;
2100 DWORD compr;
2102 if (((bitmap_type = DIB_GetBitmapInfo((BITMAPINFOHEADER*) bmi, &width, &height, &bpp, &compr)) == -1))
2104 WARN_(resource)("Invalid bitmap\n");
2105 return;
2108 if (bpp > 8) return;
2110 if (bitmap_type == 0) /* BITMAPCOREHEADER */
2112 incr = 3;
2113 colors = 1 << bpp;
2115 else
2117 incr = 4;
2118 colors = bmi->bmiHeader.biClrUsed;
2119 if (colors > 256) colors = 256;
2120 if (!colors && (bpp <= 8)) colors = 1 << bpp;
2123 c_W = GetSysColor(COLOR_WINDOW);
2124 c_S = GetSysColor(COLOR_3DSHADOW);
2125 c_F = GetSysColor(COLOR_3DFACE);
2126 c_L = GetSysColor(COLOR_3DLIGHT);
2128 if (loadflags & LR_LOADTRANSPARENT) {
2129 switch (bpp) {
2130 case 1: pix = pix >> 7; break;
2131 case 4: pix = pix >> 4; break;
2132 case 8: break;
2133 default:
2134 WARN_(resource)("(%d): Unsupported depth\n", bpp);
2135 return;
2137 if (pix >= colors) {
2138 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
2139 return;
2141 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
2142 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
2143 ptr->rgbBlue = GetBValue(c_W);
2144 ptr->rgbGreen = GetGValue(c_W);
2145 ptr->rgbRed = GetRValue(c_W);
2147 if (loadflags & LR_LOADMAP3DCOLORS)
2148 for (i=0; i<colors; i++) {
2149 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
2150 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
2151 if (c_C == RGB(128, 128, 128)) {
2152 ptr->rgbRed = GetRValue(c_S);
2153 ptr->rgbGreen = GetGValue(c_S);
2154 ptr->rgbBlue = GetBValue(c_S);
2155 } else if (c_C == RGB(192, 192, 192)) {
2156 ptr->rgbRed = GetRValue(c_F);
2157 ptr->rgbGreen = GetGValue(c_F);
2158 ptr->rgbBlue = GetBValue(c_F);
2159 } else if (c_C == RGB(223, 223, 223)) {
2160 ptr->rgbRed = GetRValue(c_L);
2161 ptr->rgbGreen = GetGValue(c_L);
2162 ptr->rgbBlue = GetBValue(c_L);
2168 /**********************************************************************
2169 * BITMAP_Load
2171 static HBITMAP BITMAP_Load( HINSTANCE instance, LPCWSTR name,
2172 INT desiredx, INT desiredy, UINT loadflags )
2174 HBITMAP hbitmap = 0, orig_bm;
2175 HRSRC hRsrc;
2176 HGLOBAL handle;
2177 char *ptr = NULL;
2178 BITMAPINFO *info, *fix_info = NULL, *scaled_info = NULL;
2179 int size;
2180 BYTE pix;
2181 char *bits;
2182 LONG width, height, new_width, new_height;
2183 WORD bpp_dummy;
2184 DWORD compr_dummy;
2185 INT bm_type;
2186 HDC screen_mem_dc = NULL;
2188 if (!(loadflags & LR_LOADFROMFILE))
2190 if (!instance)
2192 /* OEM bitmap: try to load the resource from user32.dll */
2193 instance = user32_module;
2196 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2197 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2199 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
2201 else
2203 BITMAPFILEHEADER * bmfh;
2205 if (!(ptr = map_fileW( name, NULL ))) return 0;
2206 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2207 bmfh = (BITMAPFILEHEADER *)ptr;
2208 if (!( bmfh->bfType == 0x4d42 /* 'BM' */ &&
2209 bmfh->bfReserved1 == 0 &&
2210 bmfh->bfReserved2 == 0))
2212 WARN("Invalid/unsupported bitmap format!\n");
2213 UnmapViewOfFile( ptr );
2214 return 0;
2218 size = bitmap_info_size(info, DIB_RGB_COLORS);
2219 fix_info = HeapAlloc(GetProcessHeap(), 0, size);
2220 scaled_info = HeapAlloc(GetProcessHeap(), 0, size);
2222 if (!fix_info || !scaled_info) goto end;
2223 memcpy(fix_info, info, size);
2225 pix = *((LPBYTE)info + size);
2226 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2228 memcpy(scaled_info, fix_info, size);
2229 bm_type = DIB_GetBitmapInfo( &fix_info->bmiHeader, &width, &height,
2230 &bpp_dummy, &compr_dummy);
2231 if(desiredx != 0)
2232 new_width = desiredx;
2233 else
2234 new_width = width;
2236 if(desiredy != 0)
2237 new_height = height > 0 ? desiredy : -desiredy;
2238 else
2239 new_height = height;
2241 if(bm_type == 0)
2243 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)&scaled_info->bmiHeader;
2244 core->bcWidth = new_width;
2245 core->bcHeight = new_height;
2247 else
2249 scaled_info->bmiHeader.biWidth = new_width;
2250 scaled_info->bmiHeader.biHeight = new_height;
2253 if (new_height < 0) new_height = -new_height;
2255 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2256 if (!(screen_mem_dc = CreateCompatibleDC( screen_dc ))) goto end;
2258 bits = (char *)info + size;
2260 if (loadflags & LR_CREATEDIBSECTION)
2262 scaled_info->bmiHeader.biCompression = 0; /* DIBSection can't be compressed */
2263 hbitmap = CreateDIBSection(screen_dc, scaled_info, DIB_RGB_COLORS, NULL, 0, 0);
2265 else
2267 if (is_dib_monochrome(fix_info))
2268 hbitmap = CreateBitmap(new_width, new_height, 1, 1, NULL);
2269 else
2270 hbitmap = CreateCompatibleBitmap(screen_dc, new_width, new_height);
2273 orig_bm = SelectObject(screen_mem_dc, hbitmap);
2274 StretchDIBits(screen_mem_dc, 0, 0, new_width, new_height, 0, 0, width, height, bits, fix_info, DIB_RGB_COLORS, SRCCOPY);
2275 SelectObject(screen_mem_dc, orig_bm);
2277 end:
2278 if (screen_mem_dc) DeleteDC(screen_mem_dc);
2279 HeapFree(GetProcessHeap(), 0, scaled_info);
2280 HeapFree(GetProcessHeap(), 0, fix_info);
2281 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2283 return hbitmap;
2286 /**********************************************************************
2287 * LoadImageA (USER32.@)
2289 * See LoadImageW.
2291 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2292 INT desiredx, INT desiredy, UINT loadflags)
2294 HANDLE res;
2295 LPWSTR u_name;
2297 if (!HIWORD(name))
2298 return LoadImageW(hinst, (LPCWSTR)name, type, desiredx, desiredy, loadflags);
2300 __TRY {
2301 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2302 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2303 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2305 __EXCEPT_PAGE_FAULT {
2306 SetLastError( ERROR_INVALID_PARAMETER );
2307 return 0;
2309 __ENDTRY
2310 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2311 HeapFree(GetProcessHeap(), 0, u_name);
2312 return res;
2316 /******************************************************************************
2317 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2319 * PARAMS
2320 * hinst [I] Handle of instance that contains image
2321 * name [I] Name of image
2322 * type [I] Type of image
2323 * desiredx [I] Desired width
2324 * desiredy [I] Desired height
2325 * loadflags [I] Load flags
2327 * RETURNS
2328 * Success: Handle to newly loaded image
2329 * Failure: NULL
2331 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2333 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2334 INT desiredx, INT desiredy, UINT loadflags )
2336 TRACE_(resource)("(%p,%s,%d,%d,%d,0x%08x)\n",
2337 hinst,debugstr_w(name),type,desiredx,desiredy,loadflags);
2339 if (loadflags & LR_DEFAULTSIZE) {
2340 if (type == IMAGE_ICON) {
2341 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2342 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2343 } else if (type == IMAGE_CURSOR) {
2344 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2345 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2348 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2349 switch (type) {
2350 case IMAGE_BITMAP:
2351 return BITMAP_Load( hinst, name, desiredx, desiredy, loadflags );
2353 case IMAGE_ICON:
2354 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2355 if (screen_dc)
2357 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2358 if (palEnts == 0) palEnts = 256;
2359 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2360 palEnts, FALSE, loadflags);
2362 break;
2364 case IMAGE_CURSOR:
2365 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2366 1, TRUE, loadflags);
2368 return 0;
2371 /******************************************************************************
2372 * CopyImage (USER32.@) Creates new image and copies attributes to it
2374 * PARAMS
2375 * hnd [I] Handle to image to copy
2376 * type [I] Type of image to copy
2377 * desiredx [I] Desired width of new image
2378 * desiredy [I] Desired height of new image
2379 * flags [I] Copy flags
2381 * RETURNS
2382 * Success: Handle to newly created image
2383 * Failure: NULL
2385 * BUGS
2386 * Only Windows NT 4.0 supports the LR_COPYRETURNORG flag for bitmaps,
2387 * all other versions (95/2000/XP have been tested) ignore it.
2389 * NOTES
2390 * If LR_CREATEDIBSECTION is absent, the copy will be monochrome for
2391 * a monochrome source bitmap or if LR_MONOCHROME is present, otherwise
2392 * the copy will have the same depth as the screen.
2393 * The content of the image will only be copied if the bit depth of the
2394 * original image is compatible with the bit depth of the screen, or
2395 * if the source is a DIB section.
2396 * The LR_MONOCHROME flag is ignored if LR_CREATEDIBSECTION is present.
2398 HANDLE WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2399 INT desiredy, UINT flags )
2401 TRACE("hnd=%p, type=%u, desiredx=%d, desiredy=%d, flags=%x\n",
2402 hnd, type, desiredx, desiredy, flags);
2404 switch (type)
2406 case IMAGE_BITMAP:
2408 HBITMAP res = NULL;
2409 DIBSECTION ds;
2410 int objSize;
2411 BITMAPINFO * bi;
2413 objSize = GetObjectW( hnd, sizeof(ds), &ds );
2414 if (!objSize) return 0;
2415 if ((desiredx < 0) || (desiredy < 0)) return 0;
2417 if (flags & LR_COPYFROMRESOURCE)
2419 FIXME("The flag LR_COPYFROMRESOURCE is not implemented for bitmaps\n");
2422 if (desiredx == 0) desiredx = ds.dsBm.bmWidth;
2423 if (desiredy == 0) desiredy = ds.dsBm.bmHeight;
2425 /* Allocate memory for a BITMAPINFOHEADER structure and a
2426 color table. The maximum number of colors in a color table
2427 is 256 which corresponds to a bitmap with depth 8.
2428 Bitmaps with higher depths don't have color tables. */
2429 bi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2430 if (!bi) return 0;
2432 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2433 bi->bmiHeader.biPlanes = ds.dsBm.bmPlanes;
2434 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2435 bi->bmiHeader.biCompression = BI_RGB;
2437 if (flags & LR_CREATEDIBSECTION)
2439 /* Create a DIB section. LR_MONOCHROME is ignored */
2440 void * bits;
2441 HDC dc = CreateCompatibleDC(NULL);
2443 if (objSize == sizeof(DIBSECTION))
2445 /* The source bitmap is a DIB.
2446 Get its attributes to create an exact copy */
2447 memcpy(bi, &ds.dsBmih, sizeof(BITMAPINFOHEADER));
2450 /* Get the color table or the color masks */
2451 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2453 bi->bmiHeader.biWidth = desiredx;
2454 bi->bmiHeader.biHeight = desiredy;
2455 bi->bmiHeader.biSizeImage = 0;
2457 res = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
2458 DeleteDC(dc);
2460 else
2462 /* Create a device-dependent bitmap */
2464 BOOL monochrome = (flags & LR_MONOCHROME);
2466 if (objSize == sizeof(DIBSECTION))
2468 /* The source bitmap is a DIB section.
2469 Get its attributes */
2470 HDC dc = CreateCompatibleDC(NULL);
2471 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
2472 bi->bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
2473 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2474 DeleteDC(dc);
2476 if (!monochrome && ds.dsBm.bmBitsPixel == 1)
2478 /* Look if the colors of the DIB are black and white */
2480 monochrome =
2481 (bi->bmiColors[0].rgbRed == 0xff
2482 && bi->bmiColors[0].rgbGreen == 0xff
2483 && bi->bmiColors[0].rgbBlue == 0xff
2484 && bi->bmiColors[0].rgbReserved == 0
2485 && bi->bmiColors[1].rgbRed == 0
2486 && bi->bmiColors[1].rgbGreen == 0
2487 && bi->bmiColors[1].rgbBlue == 0
2488 && bi->bmiColors[1].rgbReserved == 0)
2490 (bi->bmiColors[0].rgbRed == 0
2491 && bi->bmiColors[0].rgbGreen == 0
2492 && bi->bmiColors[0].rgbBlue == 0
2493 && bi->bmiColors[0].rgbReserved == 0
2494 && bi->bmiColors[1].rgbRed == 0xff
2495 && bi->bmiColors[1].rgbGreen == 0xff
2496 && bi->bmiColors[1].rgbBlue == 0xff
2497 && bi->bmiColors[1].rgbReserved == 0);
2500 else if (!monochrome)
2502 monochrome = ds.dsBm.bmBitsPixel == 1;
2505 if (monochrome)
2507 res = CreateBitmap(desiredx, desiredy, 1, 1, NULL);
2509 else
2511 HDC screenDC = GetDC(NULL);
2512 res = CreateCompatibleBitmap(screenDC, desiredx, desiredy);
2513 ReleaseDC(NULL, screenDC);
2517 if (res)
2519 /* Only copy the bitmap if it's a DIB section or if it's
2520 compatible to the screen */
2521 BOOL copyContents;
2523 if (objSize == sizeof(DIBSECTION))
2525 copyContents = TRUE;
2527 else
2529 HDC screenDC = GetDC(NULL);
2530 int screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
2531 ReleaseDC(NULL, screenDC);
2533 copyContents = (ds.dsBm.bmBitsPixel == 1 || ds.dsBm.bmBitsPixel == screen_depth);
2536 if (copyContents)
2538 /* The source bitmap may already be selected in a device context,
2539 use GetDIBits/StretchDIBits and not StretchBlt */
2541 HDC dc;
2542 void * bits;
2544 dc = CreateCompatibleDC(NULL);
2546 bi->bmiHeader.biWidth = ds.dsBm.bmWidth;
2547 bi->bmiHeader.biHeight = ds.dsBm.bmHeight;
2548 bi->bmiHeader.biSizeImage = 0;
2549 bi->bmiHeader.biClrUsed = 0;
2550 bi->bmiHeader.biClrImportant = 0;
2552 /* Fill in biSizeImage */
2553 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, NULL, bi, DIB_RGB_COLORS);
2554 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bi->bmiHeader.biSizeImage);
2556 if (bits)
2558 HBITMAP oldBmp;
2560 /* Get the image bits of the source bitmap */
2561 GetDIBits(dc, hnd, 0, ds.dsBm.bmHeight, bits, bi, DIB_RGB_COLORS);
2563 /* Copy it to the destination bitmap */
2564 oldBmp = SelectObject(dc, res);
2565 StretchDIBits(dc, 0, 0, desiredx, desiredy,
2566 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
2567 bits, bi, DIB_RGB_COLORS, SRCCOPY);
2568 SelectObject(dc, oldBmp);
2570 HeapFree(GetProcessHeap(), 0, bits);
2573 DeleteDC(dc);
2576 if (flags & LR_COPYDELETEORG)
2578 DeleteObject(hnd);
2581 HeapFree(GetProcessHeap(), 0, bi);
2582 return res;
2584 case IMAGE_ICON:
2585 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2586 case IMAGE_CURSOR:
2587 /* Should call CURSORICON_ExtCopy but more testing
2588 * needs to be done before we change this
2590 if (flags) FIXME("Flags are ignored\n");
2591 return CopyCursor(hnd);
2593 return 0;
2597 /******************************************************************************
2598 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2600 * RETURNS
2601 * Success: Handle to specified bitmap
2602 * Failure: NULL
2604 HBITMAP WINAPI LoadBitmapW(
2605 HINSTANCE instance, /* [in] Handle to application instance */
2606 LPCWSTR name) /* [in] Address of bitmap resource name */
2608 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2611 /**********************************************************************
2612 * LoadBitmapA (USER32.@)
2614 * See LoadBitmapW.
2616 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2618 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );