Remove the winetest_ok() hack that adds a trailing '\n' if it is
[wine.git] / windows / cursoricon.c
blob9f0d7b3e3ddb45acf9763dc442b319f896863146
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Theory:
27 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_icons.asp
29 * Cursors and icons are stored in a global heap block, with the
30 * following layout:
32 * CURSORICONINFO info;
33 * BYTE[] ANDbits;
34 * BYTE[] XORbits;
36 * The bits structures are in the format of a device-dependent bitmap.
38 * This layout is very sub-optimal, as the bitmap bits are stored in
39 * the X client instead of in the server like other bitmaps; however,
40 * some programs (notably Paint Brush) expect to be able to manipulate
41 * the bits directly :-(
43 * FIXME: what are we going to do with animation and color (bpp > 1) cursors ?!
46 #include "config.h"
47 #include "wine/port.h"
49 #include <stdarg.h>
50 #include <string.h>
51 #include <stdlib.h>
53 #include "windef.h"
54 #include "winbase.h"
55 #include "wingdi.h"
56 #include "wownt32.h"
57 #include "winerror.h"
58 #include "ntstatus.h"
59 #include "excpt.h"
60 #include "wine/winbase16.h"
61 #include "wine/winuser16.h"
62 #include "wine/exception.h"
63 #include "cursoricon.h"
64 #include "module.h"
65 #include "wine/debug.h"
66 #include "user.h"
67 #include "message.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
70 WINE_DECLARE_DEBUG_CHANNEL(icon);
71 WINE_DECLARE_DEBUG_CHANNEL(resource);
74 static RECT CURSOR_ClipRect; /* Cursor clipping rect */
76 static HDC screen_dc;
78 static const WCHAR DISPLAYW[] = {'D','I','S','P','L','A','Y',0};
80 /**********************************************************************
81 * ICONCACHE for cursors/icons loaded with LR_SHARED.
83 * FIXME: This should not be allocated on the system heap, but on a
84 * subsystem-global heap (i.e. one for all Win16 processes,
85 * and one for each Win32 process).
87 typedef struct tagICONCACHE
89 struct tagICONCACHE *next;
91 HMODULE hModule;
92 HRSRC hRsrc;
93 HRSRC hGroupRsrc;
94 HICON hIcon;
96 INT count;
98 } ICONCACHE;
100 static ICONCACHE *IconAnchor = NULL;
102 static CRITICAL_SECTION IconCrst;
103 static CRITICAL_SECTION_DEBUG critsect_debug =
105 0, 0, &IconCrst,
106 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
107 0, 0, { 0, (DWORD)(__FILE__ ": IconCrst") }
109 static CRITICAL_SECTION IconCrst = { &critsect_debug, -1, 0, 0, 0, 0 };
111 static WORD ICON_HOTSPOT = 0x4242;
114 /***********************************************************************
115 * map_fileW
117 * Helper function to map a file to memory:
118 * name - file name
119 * [RETURN] ptr - pointer to mapped file
121 static void *map_fileW( LPCWSTR name )
123 HANDLE hFile, hMapping;
124 LPVOID ptr = NULL;
126 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
127 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0 );
128 if (hFile != INVALID_HANDLE_VALUE)
130 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
131 CloseHandle( hFile );
132 if (hMapping)
134 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
135 CloseHandle( hMapping );
138 return ptr;
142 /***********************************************************************
143 * get_bitmap_width_bytes
145 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
146 * data.
148 static int get_bitmap_width_bytes( int width, int bpp )
150 switch(bpp)
152 case 1:
153 return 2 * ((width+15) / 16);
154 case 4:
155 return 2 * ((width+3) / 4);
156 case 24:
157 width *= 3;
158 /* fall through */
159 case 8:
160 return width + (width & 1);
161 case 16:
162 case 15:
163 return width * 2;
164 case 32:
165 return width * 4;
166 default:
167 WARN("Unknown depth %d, please report.\n", bpp );
169 return -1;
173 /***********************************************************************
174 * get_dib_width_bytes
176 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
178 static int get_dib_width_bytes( int width, int depth )
180 int words;
182 switch(depth)
184 case 1: words = (width + 31) / 32; break;
185 case 4: words = (width + 7) / 8; break;
186 case 8: words = (width + 3) / 4; break;
187 case 15:
188 case 16: words = (width + 1) / 2; break;
189 case 24: words = (width * 3 + 3)/4; break;
190 default:
191 WARN("(%d): Unsupported depth\n", depth );
192 /* fall through */
193 case 32:
194 words = width;
196 return 4 * words;
200 /***********************************************************************
201 * bitmap_info_size
203 * Return the size of the bitmap info structure including color table.
205 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
207 int colors;
209 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
211 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
212 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
213 return sizeof(BITMAPCOREHEADER) + colors *
214 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
216 else /* assume BITMAPINFOHEADER */
218 colors = info->bmiHeader.biClrUsed;
219 if (!colors && (info->bmiHeader.biBitCount <= 8))
220 colors = 1 << info->bmiHeader.biBitCount;
221 return sizeof(BITMAPINFOHEADER) + colors *
222 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
227 /**********************************************************************
228 * CURSORICON_FindSharedIcon
230 static HICON CURSORICON_FindSharedIcon( HMODULE hModule, HRSRC hRsrc )
232 HICON hIcon = 0;
233 ICONCACHE *ptr;
235 EnterCriticalSection( &IconCrst );
237 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
238 if ( ptr->hModule == hModule && ptr->hRsrc == hRsrc )
240 ptr->count++;
241 hIcon = ptr->hIcon;
242 break;
245 LeaveCriticalSection( &IconCrst );
247 return hIcon;
250 /*************************************************************************
251 * CURSORICON_FindCache
253 * Given a handle, find the corresponding cache element
255 * PARAMS
256 * Handle [I] handle to an Image
258 * RETURNS
259 * Success: The cache entry
260 * Failure: NULL
263 static ICONCACHE* CURSORICON_FindCache(HICON hIcon)
265 ICONCACHE *ptr;
266 ICONCACHE *pRet=NULL;
267 BOOL IsFound = FALSE;
268 int count;
270 EnterCriticalSection( &IconCrst );
272 for (count = 0, ptr = IconAnchor; ptr != NULL && !IsFound; ptr = ptr->next, count++ )
274 if ( hIcon == ptr->hIcon )
276 IsFound = TRUE;
277 pRet = ptr;
281 LeaveCriticalSection( &IconCrst );
283 return pRet;
286 /**********************************************************************
287 * CURSORICON_AddSharedIcon
289 static void CURSORICON_AddSharedIcon( HMODULE hModule, HRSRC hRsrc, HRSRC hGroupRsrc, HICON hIcon )
291 ICONCACHE *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(ICONCACHE) );
292 if ( !ptr ) return;
294 ptr->hModule = hModule;
295 ptr->hRsrc = hRsrc;
296 ptr->hIcon = hIcon;
297 ptr->hGroupRsrc = hGroupRsrc;
298 ptr->count = 1;
300 EnterCriticalSection( &IconCrst );
301 ptr->next = IconAnchor;
302 IconAnchor = ptr;
303 LeaveCriticalSection( &IconCrst );
306 /**********************************************************************
307 * CURSORICON_DelSharedIcon
309 static INT CURSORICON_DelSharedIcon( HICON hIcon )
311 INT count = -1;
312 ICONCACHE *ptr;
314 EnterCriticalSection( &IconCrst );
316 for ( ptr = IconAnchor; ptr; ptr = ptr->next )
317 if ( ptr->hIcon == hIcon )
319 if ( ptr->count > 0 ) ptr->count--;
320 count = ptr->count;
321 break;
324 LeaveCriticalSection( &IconCrst );
326 return count;
329 /**********************************************************************
330 * CURSORICON_FreeModuleIcons
332 void CURSORICON_FreeModuleIcons( HMODULE16 hMod16 )
334 ICONCACHE **ptr = &IconAnchor;
335 HMODULE hModule = HMODULE_32(GetExePtr( hMod16 ));
337 EnterCriticalSection( &IconCrst );
339 while ( *ptr )
341 if ( (*ptr)->hModule == hModule )
343 ICONCACHE *freePtr = *ptr;
344 *ptr = freePtr->next;
346 GlobalFree16(HICON_16(freePtr->hIcon));
347 HeapFree( GetProcessHeap(), 0, freePtr );
348 continue;
350 ptr = &(*ptr)->next;
353 LeaveCriticalSection( &IconCrst );
356 /**********************************************************************
357 * CURSORICON_FindBestIcon
359 * Find the icon closest to the requested size and number of colors.
361 static CURSORICONDIRENTRY *CURSORICON_FindBestIcon( CURSORICONDIR *dir, int width,
362 int height, int colors )
364 int i;
365 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
366 UINT iTotalDiff, iXDiff=0, iYDiff=0, iColorDiff;
367 UINT iTempXDiff, iTempYDiff, iTempColorDiff;
369 if (dir->idCount < 1)
371 WARN_(icon)("Empty directory!\n" );
372 return NULL;
374 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
376 /* Find Best Fit */
377 iTotalDiff = 0xFFFFFFFF;
378 iColorDiff = 0xFFFFFFFF;
379 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
381 iTempXDiff = abs(width - entry->ResInfo.icon.bWidth);
382 iTempYDiff = abs(height - entry->ResInfo.icon.bHeight);
384 if(iTotalDiff > (iTempXDiff + iTempYDiff))
386 iXDiff = iTempXDiff;
387 iYDiff = iTempYDiff;
388 iTotalDiff = iXDiff + iYDiff;
392 /* Find Best Colors for Best Fit */
393 for (i = 0, entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
395 if(abs(width - entry->ResInfo.icon.bWidth) == iXDiff &&
396 abs(height - entry->ResInfo.icon.bHeight) == iYDiff)
398 iTempColorDiff = abs(colors - (1<<entry->wBitCount));
399 if(iColorDiff > iTempColorDiff)
401 bestEntry = entry;
402 iColorDiff = iTempColorDiff;
407 return bestEntry;
411 /**********************************************************************
412 * CURSORICON_FindBestCursor
414 * Find the cursor closest to the requested size.
415 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
416 * ignored too
418 static CURSORICONDIRENTRY *CURSORICON_FindBestCursor( CURSORICONDIR *dir,
419 int width, int height, int color)
421 int i, maxwidth, maxheight;
422 CURSORICONDIRENTRY *entry, *bestEntry = NULL;
424 if (dir->idCount < 1)
426 WARN_(cursor)("Empty directory!\n" );
427 return NULL;
429 if (dir->idCount == 1) return &dir->idEntries[0]; /* No choice... */
431 /* Double height to account for AND and XOR masks */
433 height *= 2;
435 /* First find the largest one smaller than or equal to the requested size*/
437 maxwidth = maxheight = 0;
438 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
439 if ((entry->ResInfo.cursor.wWidth <= width) && (entry->ResInfo.cursor.wHeight <= height) &&
440 (entry->ResInfo.cursor.wWidth > maxwidth) && (entry->ResInfo.cursor.wHeight > maxheight) &&
441 (entry->wBitCount == 1))
443 bestEntry = entry;
444 maxwidth = entry->ResInfo.cursor.wWidth;
445 maxheight = entry->ResInfo.cursor.wHeight;
447 if (bestEntry) return bestEntry;
449 /* Now find the smallest one larger than the requested size */
451 maxwidth = maxheight = 255;
452 for(i = 0,entry = &dir->idEntries[0]; i < dir->idCount; i++,entry++)
453 if ((entry->ResInfo.cursor.wWidth < maxwidth) && (entry->ResInfo.cursor.wHeight < maxheight) &&
454 (entry->wBitCount == 1))
456 bestEntry = entry;
457 maxwidth = entry->ResInfo.cursor.wWidth;
458 maxheight = entry->ResInfo.cursor.wHeight;
461 return bestEntry;
464 /*********************************************************************
465 * The main purpose of this function is to create fake resource directory
466 * and fake resource entries. There are several reasons for this:
467 * - CURSORICONDIR and CURSORICONFILEDIR differ in sizes and their
468 * fields
469 * There are some "bad" cursor files which do not have
470 * bColorCount initialized but instead one must read this info
471 * directly from corresponding DIB sections
472 * Note: wResId is index to array of pointer returned in ptrs (origin is 1)
474 static BOOL CURSORICON_SimulateLoadingFromResourceW( LPWSTR filename, BOOL fCursor,
475 CURSORICONDIR **res, LPBYTE **ptr)
477 LPBYTE _free;
478 CURSORICONFILEDIR *bits;
479 int entries, size, i;
481 *res = NULL;
482 *ptr = NULL;
483 if (!(bits = map_fileW( filename ))) return FALSE;
485 /* FIXME: test for inimated icons
486 * hack to load the first icon from the *.ani file
488 if ( *(LPDWORD)bits==0x46464952 ) /* "RIFF" */
489 { LPBYTE pos = (LPBYTE) bits;
490 FIXME_(cursor)("Animated icons not correctly implemented! %p \n", bits);
492 for (;;)
493 { if (*(LPDWORD)pos==0x6e6f6369) /* "icon" */
494 { FIXME_(cursor)("icon entry found! %p\n", bits);
495 pos+=4;
496 if ( !*(LPWORD) pos==0x2fe) /* iconsize */
497 { goto fail;
499 bits=(CURSORICONFILEDIR*)(pos+4);
500 FIXME_(cursor)("icon size ok. offset=%p \n", bits);
501 break;
503 pos+=2;
504 if (pos>=(LPBYTE)bits+766) goto fail;
507 if (!(entries = bits->idCount)) goto fail;
508 size = sizeof(CURSORICONDIR) + sizeof(CURSORICONDIRENTRY) * (entries - 1);
509 _free = (LPBYTE) size;
511 for (i=0; i < entries; i++)
512 size += bits->idEntries[i].dwDIBSize + (fCursor ? sizeof(POINT16): 0);
514 if (!(*ptr = HeapAlloc( GetProcessHeap(), 0,
515 entries * sizeof (CURSORICONDIRENTRY*)))) goto fail;
516 if (!(*res = HeapAlloc( GetProcessHeap(), 0, size))) goto fail;
518 _free = (LPBYTE)(*res) + (int)_free;
519 memcpy((*res), bits, 6);
520 for (i=0; i<entries; i++)
522 ((LPBYTE*)(*ptr))[i] = _free;
523 if (fCursor) {
524 (*res)->idEntries[i].ResInfo.cursor.wWidth=bits->idEntries[i].bWidth;
525 (*res)->idEntries[i].ResInfo.cursor.wHeight=bits->idEntries[i].bHeight;
526 ((LPPOINT16)_free)->x=bits->idEntries[i].xHotspot;
527 ((LPPOINT16)_free)->y=bits->idEntries[i].yHotspot;
528 _free+=sizeof(POINT16);
529 } else {
530 (*res)->idEntries[i].ResInfo.icon.bWidth=bits->idEntries[i].bWidth;
531 (*res)->idEntries[i].ResInfo.icon.bHeight=bits->idEntries[i].bHeight;
532 (*res)->idEntries[i].ResInfo.icon.bColorCount = bits->idEntries[i].bColorCount;
534 (*res)->idEntries[i].wPlanes=1;
535 (*res)->idEntries[i].wBitCount = ((LPBITMAPINFOHEADER)((LPBYTE)bits +
536 bits->idEntries[i].dwDIBOffset))->biBitCount;
537 (*res)->idEntries[i].dwBytesInRes = bits->idEntries[i].dwDIBSize;
538 (*res)->idEntries[i].wResId=i+1;
540 memcpy(_free,(LPBYTE)bits +bits->idEntries[i].dwDIBOffset,
541 (*res)->idEntries[i].dwBytesInRes);
542 _free += (*res)->idEntries[i].dwBytesInRes;
544 UnmapViewOfFile( bits );
545 return TRUE;
546 fail:
547 if (*res) HeapFree( GetProcessHeap(), 0, *res );
548 if (*ptr) HeapFree( GetProcessHeap(), 0, *ptr );
549 UnmapViewOfFile( bits );
550 return FALSE;
554 /**********************************************************************
555 * CURSORICON_CreateFromResource
557 * Create a cursor or icon from in-memory resource template.
559 * FIXME: Convert to mono when cFlag is LR_MONOCHROME. Do something
560 * with cbSize parameter as well.
562 static HICON CURSORICON_CreateFromResource( HMODULE16 hModule, HGLOBAL16 hObj, LPBYTE bits,
563 UINT cbSize, BOOL bIcon, DWORD dwVersion,
564 INT width, INT height, UINT loadflags )
566 static HDC hdcMem;
567 int sizeAnd, sizeXor;
568 HBITMAP hAndBits = 0, hXorBits = 0; /* error condition for later */
569 BITMAP bmpXor, bmpAnd;
570 POINT16 hotspot;
571 BITMAPINFO *bmi;
572 BOOL DoStretch;
573 INT size;
575 hotspot.x = ICON_HOTSPOT;
576 hotspot.y = ICON_HOTSPOT;
578 TRACE_(cursor)("%08x (%u bytes), ver %08x, %ix%i %s %s\n",
579 (unsigned)bits, cbSize, (unsigned)dwVersion, width, height,
580 bIcon ? "icon" : "cursor", (loadflags & LR_MONOCHROME) ? "mono" : "" );
581 if (dwVersion == 0x00020000)
583 FIXME_(cursor)("\t2.xx resources are not supported\n");
584 return 0;
587 if (bIcon)
588 bmi = (BITMAPINFO *)bits;
589 else /* get the hotspot */
591 POINT16 *pt = (POINT16 *)bits;
592 hotspot = *pt;
593 bmi = (BITMAPINFO *)(pt + 1);
595 size = bitmap_info_size( bmi, DIB_RGB_COLORS );
597 if (!width) width = bmi->bmiHeader.biWidth;
598 if (!height) height = bmi->bmiHeader.biHeight/2;
599 DoStretch = (bmi->bmiHeader.biHeight/2 != height) ||
600 (bmi->bmiHeader.biWidth != width);
602 /* Check bitmap header */
604 if ( (bmi->bmiHeader.biSize != sizeof(BITMAPCOREHEADER)) &&
605 (bmi->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) ||
606 bmi->bmiHeader.biCompression != BI_RGB) )
608 WARN_(cursor)("\tinvalid resource bitmap header.\n");
609 return 0;
612 if (!screen_dc) screen_dc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
613 if (screen_dc)
615 BITMAPINFO* pInfo;
617 /* Make sure we have room for the monochrome bitmap later on.
618 * Note that BITMAPINFOINFO and BITMAPCOREHEADER are the same
619 * up to and including the biBitCount. In-memory icon resource
620 * format is as follows:
622 * BITMAPINFOHEADER icHeader // DIB header
623 * RGBQUAD icColors[] // Color table
624 * BYTE icXOR[] // DIB bits for XOR mask
625 * BYTE icAND[] // DIB bits for AND mask
628 if ((pInfo = (BITMAPINFO *)HeapAlloc( GetProcessHeap(), 0,
629 max(size, sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD)))))
631 memcpy( pInfo, bmi, size );
632 pInfo->bmiHeader.biHeight /= 2;
634 /* Create the XOR bitmap */
636 if (DoStretch) {
637 if(bIcon)
639 hXorBits = CreateCompatibleBitmap(screen_dc, width, height);
641 else
643 hXorBits = CreateBitmap(width, height, 1, 1, NULL);
645 if(hXorBits)
647 HBITMAP hOld;
648 BOOL res = FALSE;
650 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
651 if (hdcMem) {
652 hOld = SelectObject(hdcMem, hXorBits);
653 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
654 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight/2,
655 (char*)bmi + size, pInfo, DIB_RGB_COLORS, SRCCOPY);
656 SelectObject(hdcMem, hOld);
658 if (!res) { DeleteObject(hXorBits); hXorBits = 0; }
660 } else hXorBits = CreateDIBitmap( screen_dc, &pInfo->bmiHeader,
661 CBM_INIT, (char*)bmi + size, pInfo, DIB_RGB_COLORS );
662 if( hXorBits )
664 char* xbits = (char *)bmi + size +
665 get_dib_width_bytes( bmi->bmiHeader.biWidth,
666 bmi->bmiHeader.biBitCount ) * abs( bmi->bmiHeader.biHeight ) / 2;
668 pInfo->bmiHeader.biBitCount = 1;
669 if (pInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
671 RGBQUAD *rgb = pInfo->bmiColors;
673 pInfo->bmiHeader.biClrUsed = pInfo->bmiHeader.biClrImportant = 2;
674 rgb[0].rgbBlue = rgb[0].rgbGreen = rgb[0].rgbRed = 0x00;
675 rgb[1].rgbBlue = rgb[1].rgbGreen = rgb[1].rgbRed = 0xff;
676 rgb[0].rgbReserved = rgb[1].rgbReserved = 0;
678 else
680 RGBTRIPLE *rgb = (RGBTRIPLE *)(((BITMAPCOREHEADER *)pInfo) + 1);
682 rgb[0].rgbtBlue = rgb[0].rgbtGreen = rgb[0].rgbtRed = 0x00;
683 rgb[1].rgbtBlue = rgb[1].rgbtGreen = rgb[1].rgbtRed = 0xff;
686 /* Create the AND bitmap */
688 if (DoStretch) {
689 if ((hAndBits = CreateBitmap(width, height, 1, 1, NULL))) {
690 HBITMAP hOld;
691 BOOL res = FALSE;
693 if (!hdcMem) hdcMem = CreateCompatibleDC(screen_dc);
694 if (hdcMem) {
695 hOld = SelectObject(hdcMem, hAndBits);
696 res = StretchDIBits(hdcMem, 0, 0, width, height, 0, 0,
697 pInfo->bmiHeader.biWidth, pInfo->bmiHeader.biHeight,
698 xbits, pInfo, DIB_RGB_COLORS, SRCCOPY);
699 SelectObject(hdcMem, hOld);
701 if (!res) { DeleteObject(hAndBits); hAndBits = 0; }
703 } else hAndBits = CreateDIBitmap( screen_dc, &pInfo->bmiHeader,
704 CBM_INIT, xbits, pInfo, DIB_RGB_COLORS );
706 if( !hAndBits ) DeleteObject( hXorBits );
708 HeapFree( GetProcessHeap(), 0, pInfo );
712 if( !hXorBits || !hAndBits )
714 WARN_(cursor)("\tunable to create an icon bitmap.\n");
715 return 0;
718 /* Now create the CURSORICONINFO structure */
719 GetObjectA( hXorBits, sizeof(bmpXor), &bmpXor );
720 GetObjectA( hAndBits, sizeof(bmpAnd), &bmpAnd );
721 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
722 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
724 if (hObj) hObj = GlobalReAlloc16( hObj,
725 sizeof(CURSORICONINFO) + sizeXor + sizeAnd, GMEM_MOVEABLE );
726 if (!hObj) hObj = GlobalAlloc16( GMEM_MOVEABLE,
727 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
728 if (hObj)
730 CURSORICONINFO *info;
732 /* Make it owned by the module */
733 if (hModule) hModule = GetExePtr(hModule);
734 FarSetOwner16( hObj, hModule );
736 info = (CURSORICONINFO *)GlobalLock16( hObj );
737 info->ptHotSpot.x = hotspot.x;
738 info->ptHotSpot.y = hotspot.y;
739 info->nWidth = bmpXor.bmWidth;
740 info->nHeight = bmpXor.bmHeight;
741 info->nWidthBytes = bmpXor.bmWidthBytes;
742 info->bPlanes = bmpXor.bmPlanes;
743 info->bBitsPerPixel = bmpXor.bmBitsPixel;
745 /* Transfer the bitmap bits to the CURSORICONINFO structure */
747 GetBitmapBits( hAndBits, sizeAnd, (char *)(info + 1) );
748 GetBitmapBits( hXorBits, sizeXor, (char *)(info + 1) + sizeAnd );
749 GlobalUnlock16( hObj );
752 DeleteObject( hAndBits );
753 DeleteObject( hXorBits );
754 return HICON_32((HICON16)hObj);
758 /**********************************************************************
759 * CreateIconFromResource (USER32.@)
761 HICON WINAPI CreateIconFromResource( LPBYTE bits, UINT cbSize,
762 BOOL bIcon, DWORD dwVersion)
764 return CreateIconFromResourceEx( bits, cbSize, bIcon, dwVersion, 0,0,0);
768 /**********************************************************************
769 * CreateIconFromResourceEx (USER32.@)
771 HICON WINAPI CreateIconFromResourceEx( LPBYTE bits, UINT cbSize,
772 BOOL bIcon, DWORD dwVersion,
773 INT width, INT height,
774 UINT cFlag )
776 return CURSORICON_CreateFromResource( 0, 0, bits, cbSize, bIcon, dwVersion,
777 width, height, cFlag );
780 /**********************************************************************
781 * CURSORICON_Load
783 * Load a cursor or icon from resource or file.
785 static HICON CURSORICON_Load(HINSTANCE hInstance, LPCWSTR name,
786 INT width, INT height, INT colors,
787 BOOL fCursor, UINT loadflags)
789 HANDLE handle = 0;
790 HICON hIcon = 0;
791 HRSRC hRsrc;
792 CURSORICONDIR *dir;
793 CURSORICONDIRENTRY *dirEntry;
794 LPBYTE bits;
796 if ( loadflags & LR_LOADFROMFILE ) /* Load from file */
798 LPBYTE *ptr;
799 if (!CURSORICON_SimulateLoadingFromResourceW((LPWSTR)name, fCursor, &dir, &ptr))
800 return 0;
801 if (fCursor)
802 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(dir, width, height, 1);
803 else
804 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(dir, width, height, colors);
805 bits = ptr[dirEntry->wResId-1];
806 hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dirEntry->dwBytesInRes,
807 !fCursor, 0x00030000, width, height, loadflags);
808 HeapFree( GetProcessHeap(), 0, dir );
809 HeapFree( GetProcessHeap(), 0, ptr );
811 else /* Load from resource */
813 HRSRC hGroupRsrc;
814 WORD wResId;
815 DWORD dwBytesInRes;
817 if (!hInstance) /* Load OEM cursor/icon */
819 if (!(hInstance = GetModuleHandleA( "user32.dll" ))) return 0;
822 /* Normalize hInstance (must be uniquely represented for icon cache) */
824 if (!HIWORD( hInstance ))
825 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
827 /* Get directory resource ID */
829 if (!(hRsrc = FindResourceW( hInstance, name,
830 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
831 return 0;
832 hGroupRsrc = hRsrc;
834 /* Find the best entry in the directory */
836 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
837 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
838 if (fCursor)
839 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
840 width, height, 1);
841 else
842 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon( dir,
843 width, height, colors );
844 if (!dirEntry) return 0;
845 wResId = dirEntry->wResId;
846 dwBytesInRes = dirEntry->dwBytesInRes;
847 FreeResource( handle );
849 /* Load the resource */
851 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
852 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
854 /* If shared icon, check whether it was already loaded */
855 if ( (loadflags & LR_SHARED)
856 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
857 return hIcon;
859 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
860 bits = (LPBYTE)LockResource( handle );
861 hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dwBytesInRes,
862 !fCursor, 0x00030000, width, height, loadflags);
863 FreeResource( handle );
865 /* If shared icon, add to icon cache */
867 if ( hIcon && (loadflags & LR_SHARED) )
868 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
871 return hIcon;
874 /***********************************************************************
875 * CURSORICON_Copy
877 * Make a copy of a cursor or icon.
879 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
881 char *ptrOld, *ptrNew;
882 int size;
883 HICON16 hOld = HICON_16(hIcon);
884 HICON16 hNew;
886 if (!(ptrOld = (char *)GlobalLock16( hOld ))) return 0;
887 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
888 size = GlobalSize16( hOld );
889 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
890 FarSetOwner16( hNew, hInst16 );
891 ptrNew = (char *)GlobalLock16( hNew );
892 memcpy( ptrNew, ptrOld, size );
893 GlobalUnlock16( hOld );
894 GlobalUnlock16( hNew );
895 return HICON_32(hNew);
898 /*************************************************************************
899 * CURSORICON_ExtCopy
901 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
903 * PARAMS
904 * Handle [I] handle to an Image
905 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
906 * iDesiredCX [I] The Desired width of the Image
907 * iDesiredCY [I] The desired height of the Image
908 * nFlags [I] The flags from CopyImage
910 * RETURNS
911 * Success: The new handle of the Image
913 * NOTES
914 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
915 * LR_MONOCHROME should be implemented by CURSORICON_CreateFromResource.
916 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
921 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
922 INT iDesiredCX, INT iDesiredCY,
923 UINT nFlags)
925 HICON hNew=0;
927 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
928 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
930 if(hIcon == 0)
932 return 0;
935 /* Best Fit or Monochrome */
936 if( (nFlags & LR_COPYFROMRESOURCE
937 && (iDesiredCX > 0 || iDesiredCY > 0))
938 || nFlags & LR_MONOCHROME)
940 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
942 /* Not Found in Cache, then do a straight copy
944 if(pIconCache == NULL)
946 hNew = CURSORICON_Copy(0, hIcon);
947 if(nFlags & LR_COPYFROMRESOURCE)
949 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
952 else
954 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
955 LPBYTE pBits;
956 HANDLE hMem;
957 HRSRC hRsrc;
958 DWORD dwBytesInRes;
959 WORD wResId;
960 CURSORICONDIR *pDir;
961 CURSORICONDIRENTRY *pDirEntry;
962 BOOL bIsIcon = (nType == IMAGE_ICON);
964 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
966 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
967 || (iDesiredCX == 0 && iDesiredCY == 0))
969 iDesiredCY = GetSystemMetrics(bIsIcon ?
970 SM_CYICON : SM_CYCURSOR);
971 iDesiredCX = GetSystemMetrics(bIsIcon ?
972 SM_CXICON : SM_CXCURSOR);
975 /* Retrieve the CURSORICONDIRENTRY
977 if (!(hMem = LoadResource( pIconCache->hModule ,
978 pIconCache->hGroupRsrc)))
980 return 0;
982 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
984 return 0;
987 /* Find Best Fit
989 if(bIsIcon)
991 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
992 pDir, iDesiredCX, iDesiredCY, 256);
994 else
996 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
997 pDir, iDesiredCX, iDesiredCY, 1);
1000 wResId = pDirEntry->wResId;
1001 dwBytesInRes = pDirEntry->dwBytesInRes;
1002 FreeResource(hMem);
1004 TRACE_(icon)("ResID %u, BytesInRes %lu, Width %d, Height %d DX %d, DY %d\n",
1005 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1006 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1008 /* Get the Best Fit
1010 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1011 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1013 return 0;
1015 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1017 return 0;
1020 pBits = (LPBYTE)LockResource( hMem );
1022 if(nFlags & LR_DEFAULTSIZE)
1024 iTargetCY = GetSystemMetrics(SM_CYICON);
1025 iTargetCX = GetSystemMetrics(SM_CXICON);
1028 /* Create a New Icon with the proper dimension
1030 hNew = CURSORICON_CreateFromResource( 0, 0, pBits, dwBytesInRes,
1031 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1032 FreeResource(hMem);
1035 else hNew = CURSORICON_Copy(0, hIcon);
1036 return hNew;
1040 /***********************************************************************
1041 * CreateCursor (USER32.@)
1043 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1044 INT xHotSpot, INT yHotSpot,
1045 INT nWidth, INT nHeight,
1046 LPCVOID lpANDbits, LPCVOID lpXORbits )
1048 CURSORICONINFO info;
1050 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1051 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1053 info.ptHotSpot.x = xHotSpot;
1054 info.ptHotSpot.y = yHotSpot;
1055 info.nWidth = nWidth;
1056 info.nHeight = nHeight;
1057 info.nWidthBytes = 0;
1058 info.bPlanes = 1;
1059 info.bBitsPerPixel = 1;
1061 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1065 /***********************************************************************
1066 * CreateIcon (USER.407)
1068 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1069 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1070 LPCVOID lpANDbits, LPCVOID lpXORbits )
1072 CURSORICONINFO info;
1074 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1075 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1077 info.ptHotSpot.x = ICON_HOTSPOT;
1078 info.ptHotSpot.y = ICON_HOTSPOT;
1079 info.nWidth = nWidth;
1080 info.nHeight = nHeight;
1081 info.nWidthBytes = 0;
1082 info.bPlanes = bPlanes;
1083 info.bBitsPerPixel = bBitsPixel;
1085 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1089 /***********************************************************************
1090 * CreateIcon (USER32.@)
1092 * Creates an icon based on the specified bitmaps. The bitmaps must be
1093 * provided in a device dependent format and will be resized to
1094 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1095 * depth. The provided bitmaps must be top-down bitmaps.
1096 * Although Windows does not support 15bpp(*) this API must support it
1097 * for Winelib applications.
1099 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1100 * format!
1102 * BUGS
1104 * - The provided bitmaps are not resized!
1105 * - The documentation says the lpXORbits bitmap must be in a device
1106 * dependent format. But we must still resize it and perform depth
1107 * conversions if necessary.
1108 * - I'm a bit unsure about the how the 'device dependent format' thing works.
1109 * I did some tests on windows and found that if you provide a 16bpp bitmap
1110 * in lpXORbits, then its format but be 565 RGB if the screen's bit depth
1111 * is 16bpp but it must be 555 RGB if the screen's bit depth is anything
1112 * else. I don't know if this is part of the GDI specs or if this is a
1113 * quirk of the graphics card driver.
1114 * - You may think that we check whether the bit depths match or not
1115 * as an optimization. But the truth is that the conversion using
1116 * CreateDIBitmap does not work for some bit depth (e.g. 8bpp) and I have
1117 * no idea why.
1118 * - I'm pretty sure that all the things we do in CreateIcon should
1119 * also be done in CreateIconIndirect...
1121 HICON WINAPI CreateIcon(
1122 HINSTANCE hInstance, /* [in] the application's hInstance */
1123 INT nWidth, /* [in] the width of the provided bitmaps */
1124 INT nHeight, /* [in] the height of the provided bitmaps */
1125 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1126 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1127 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1128 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1130 HICON hIcon;
1131 HDC hdc;
1133 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1134 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1136 hdc=GetDC(0);
1137 if (!hdc)
1138 return 0;
1140 if (GetDeviceCaps(hdc,BITSPIXEL)==bBitsPixel) {
1141 CURSORICONINFO info;
1143 info.ptHotSpot.x = ICON_HOTSPOT;
1144 info.ptHotSpot.y = ICON_HOTSPOT;
1145 info.nWidth = nWidth;
1146 info.nHeight = nHeight;
1147 info.nWidthBytes = 0;
1148 info.bPlanes = bPlanes;
1149 info.bBitsPerPixel = bBitsPixel;
1151 hIcon=HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1152 } else {
1153 ICONINFO iinfo;
1154 BITMAPINFO bmi;
1156 iinfo.fIcon=TRUE;
1157 iinfo.xHotspot=ICON_HOTSPOT;
1158 iinfo.yHotspot=ICON_HOTSPOT;
1159 iinfo.hbmMask=CreateBitmap(nWidth,nHeight,1,1,lpANDbits);
1161 bmi.bmiHeader.biSize=sizeof(bmi.bmiHeader);
1162 bmi.bmiHeader.biWidth=nWidth;
1163 bmi.bmiHeader.biHeight=-nHeight;
1164 bmi.bmiHeader.biPlanes=bPlanes;
1165 bmi.bmiHeader.biBitCount=bBitsPixel;
1166 bmi.bmiHeader.biCompression=BI_RGB;
1167 bmi.bmiHeader.biSizeImage=0;
1168 bmi.bmiHeader.biXPelsPerMeter=0;
1169 bmi.bmiHeader.biYPelsPerMeter=0;
1170 bmi.bmiHeader.biClrUsed=0;
1171 bmi.bmiHeader.biClrImportant=0;
1173 iinfo.hbmColor = CreateDIBitmap( hdc, &bmi.bmiHeader,
1174 CBM_INIT, lpXORbits,
1175 &bmi, DIB_RGB_COLORS );
1177 hIcon=CreateIconIndirect(&iinfo);
1178 DeleteObject(iinfo.hbmMask);
1179 DeleteObject(iinfo.hbmColor);
1181 ReleaseDC(0,hdc);
1182 return hIcon;
1186 /***********************************************************************
1187 * CreateCursorIconIndirect (USER.408)
1189 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1190 CURSORICONINFO *info,
1191 LPCVOID lpANDbits,
1192 LPCVOID lpXORbits )
1194 HGLOBAL16 handle;
1195 char *ptr;
1196 int sizeAnd, sizeXor;
1198 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1199 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1200 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1201 sizeXor = info->nHeight * info->nWidthBytes;
1202 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1203 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1204 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1205 return 0;
1206 FarSetOwner16( handle, hInstance );
1207 ptr = (char *)GlobalLock16( handle );
1208 memcpy( ptr, info, sizeof(*info) );
1209 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1210 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1211 GlobalUnlock16( handle );
1212 return handle;
1216 /***********************************************************************
1217 * CopyIcon (USER.368)
1219 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1221 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1222 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1226 /***********************************************************************
1227 * CopyIcon (USER32.@)
1229 HICON WINAPI CopyIcon( HICON hIcon )
1231 TRACE_(icon)("%p\n", hIcon );
1232 return CURSORICON_Copy( 0, hIcon );
1236 /***********************************************************************
1237 * CopyCursor (USER.369)
1239 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1241 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1242 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1245 /**********************************************************************
1246 * DestroyIcon32 (USER.610)
1248 * This routine is actually exported from Win95 USER under the name
1249 * DestroyIcon32 ... The behaviour implemented here should mimic
1250 * the Win95 one exactly, especially the return values, which
1251 * depend on the setting of various flags.
1253 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1255 WORD retv;
1257 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1259 /* Check whether destroying active cursor */
1261 if ( QUEUE_Current()->cursor == HICON_32(handle) )
1263 WARN_(cursor)("Destroying active cursor!\n" );
1264 SetCursor( 0 );
1267 /* Try shared cursor/icon first */
1269 if ( !(flags & CID_NONSHARED) )
1271 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1273 if ( count != -1 )
1274 return (flags & CID_WIN32)? TRUE : (count == 0);
1276 /* FIXME: OEM cursors/icons should be recognized */
1279 /* Now assume non-shared cursor/icon */
1281 retv = GlobalFree16( handle );
1282 return (flags & CID_RESOURCE)? retv : TRUE;
1285 /***********************************************************************
1286 * DestroyIcon (USER32.@)
1288 BOOL WINAPI DestroyIcon( HICON hIcon )
1290 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1294 /***********************************************************************
1295 * DestroyCursor (USER32.@)
1297 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1299 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1303 /***********************************************************************
1304 * DrawIcon (USER32.@)
1306 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1308 CURSORICONINFO *ptr;
1309 HDC hMemDC;
1310 HBITMAP hXorBits, hAndBits;
1311 COLORREF oldFg, oldBg;
1313 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
1314 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1315 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
1316 (char *)(ptr+1) );
1317 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1318 ptr->bBitsPerPixel, (char *)(ptr + 1)
1319 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
1320 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1321 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1323 if (hXorBits && hAndBits)
1325 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1326 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
1327 SelectObject( hMemDC, hXorBits );
1328 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
1329 SelectObject( hMemDC, hBitTemp );
1331 DeleteDC( hMemDC );
1332 if (hXorBits) DeleteObject( hXorBits );
1333 if (hAndBits) DeleteObject( hAndBits );
1334 GlobalUnlock16(HICON_16(hIcon));
1335 SetTextColor( hdc, oldFg );
1336 SetBkColor( hdc, oldBg );
1337 return TRUE;
1340 /***********************************************************************
1341 * DumpIcon (USER.459)
1343 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1344 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1346 CURSORICONINFO *info = MapSL( pInfo );
1347 int sizeAnd, sizeXor;
1349 if (!info) return 0;
1350 sizeXor = info->nHeight * info->nWidthBytes;
1351 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1352 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1353 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1354 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1355 return MAKELONG( sizeXor, sizeXor );
1359 /***********************************************************************
1360 * SetCursor (USER32.@)
1361 * RETURNS:
1362 * A handle to the previous cursor shape.
1364 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1366 MESSAGEQUEUE *queue = QUEUE_Current();
1367 HCURSOR hOldCursor;
1369 if (hCursor == queue->cursor) return hCursor; /* No change */
1370 TRACE_(cursor)("%p\n", hCursor );
1371 hOldCursor = queue->cursor;
1372 queue->cursor = hCursor;
1373 /* Change the cursor shape only if it is visible */
1374 if (queue->cursor_count >= 0)
1376 USER_Driver.pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
1377 GlobalUnlock16(HCURSOR_16(hCursor));
1379 return hOldCursor;
1382 /***********************************************************************
1383 * ShowCursor (USER32.@)
1385 INT WINAPI ShowCursor( BOOL bShow )
1387 MESSAGEQUEUE *queue = QUEUE_Current();
1389 TRACE_(cursor)("%d, count=%d\n", bShow, queue->cursor_count );
1391 if (bShow)
1393 if (++queue->cursor_count == 0) /* Show it */
1395 USER_Driver.pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(queue->cursor)));
1396 GlobalUnlock16(HCURSOR_16(queue->cursor));
1399 else
1401 if (--queue->cursor_count == -1) /* Hide it */
1402 USER_Driver.pSetCursor( NULL );
1404 return queue->cursor_count;
1407 /***********************************************************************
1408 * GetCursor (USER32.@)
1410 HCURSOR WINAPI GetCursor(void)
1412 return QUEUE_Current()->cursor;
1416 /***********************************************************************
1417 * ClipCursor (USER.16)
1419 BOOL16 WINAPI ClipCursor16( const RECT16 *rect )
1421 if (!rect) SetRectEmpty( &CURSOR_ClipRect );
1422 else CONV_RECT16TO32( rect, &CURSOR_ClipRect );
1423 return TRUE;
1427 /***********************************************************************
1428 * ClipCursor (USER32.@)
1430 BOOL WINAPI ClipCursor( const RECT *rect )
1432 if (!rect) SetRectEmpty( &CURSOR_ClipRect );
1433 else CopyRect( &CURSOR_ClipRect, rect );
1434 return TRUE;
1438 /***********************************************************************
1439 * GetClipCursor (USER.309)
1441 void WINAPI GetClipCursor16( RECT16 *rect )
1443 if (rect) CONV_RECT32TO16( &CURSOR_ClipRect, rect );
1447 /***********************************************************************
1448 * GetClipCursor (USER32.@)
1450 BOOL WINAPI GetClipCursor( RECT *rect )
1452 if (rect)
1454 CopyRect( rect, &CURSOR_ClipRect );
1455 return TRUE;
1457 return FALSE;
1460 /**********************************************************************
1461 * LookupIconIdFromDirectoryEx (USER.364)
1463 * FIXME: exact parameter sizes
1465 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1466 INT16 width, INT16 height, UINT16 cFlag )
1468 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1471 /**********************************************************************
1472 * LookupIconIdFromDirectoryEx (USER32.@)
1474 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1475 INT width, INT height, UINT cFlag )
1477 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1478 UINT retVal = 0;
1479 if( dir && !dir->idReserved && (dir->idType & 3) )
1481 CURSORICONDIRENTRY* entry;
1482 HDC hdc;
1483 UINT palEnts;
1484 int colors;
1485 hdc = GetDC(0);
1486 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1487 if (palEnts == 0)
1488 palEnts = 256;
1489 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1491 ReleaseDC(0, hdc);
1493 if( bIcon )
1494 entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1495 else
1496 entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1498 if( entry ) retVal = entry->wResId;
1500 else WARN_(cursor)("invalid resource directory\n");
1501 return retVal;
1504 /**********************************************************************
1505 * LookupIconIdFromDirectory (USER.?)
1507 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
1509 return LookupIconIdFromDirectoryEx16( dir, bIcon,
1510 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1511 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1514 /**********************************************************************
1515 * LookupIconIdFromDirectory (USER32.@)
1517 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1519 return LookupIconIdFromDirectoryEx( dir, bIcon,
1520 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1521 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1524 /**********************************************************************
1525 * GetIconID (USER.455)
1527 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1529 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
1531 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1532 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1534 switch(resType)
1536 case RT_CURSOR:
1537 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1538 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1539 case RT_ICON:
1540 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1541 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1542 default:
1543 WARN_(cursor)("invalid res type %ld\n", resType );
1545 return 0;
1548 /**********************************************************************
1549 * LoadCursorIconHandler (USER.336)
1551 * Supposed to load resources of Windows 2.x applications.
1553 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1555 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1556 hResource, hModule, hRsrc);
1557 return (HGLOBAL16)0;
1560 /**********************************************************************
1561 * LoadDIBIconHandler (USER.357)
1563 * RT_ICON resource loader, installed by USER_SignalProc when module
1564 * is initialized.
1566 HGLOBAL16 WINAPI LoadDIBIconHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1568 /* If hResource is zero we must allocate a new memory block, if it's
1569 * non-zero but GlobalLock() returns NULL then it was discarded and
1570 * we have to recommit some memory, otherwise we just need to check
1571 * the block size. See LoadProc() in 16-bit SDK for more.
1574 hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
1575 if( hMemObj )
1577 LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1578 hMemObj = HICON_16(CURSORICON_CreateFromResource(
1579 hModule, hMemObj, bits,
1580 SizeofResource16(hModule, hRsrc), TRUE, 0x00030000,
1581 GetSystemMetrics(SM_CXICON),
1582 GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR));
1584 return hMemObj;
1587 /**********************************************************************
1588 * LoadDIBCursorHandler (USER.356)
1590 * RT_CURSOR resource loader. Same as above.
1592 HGLOBAL16 WINAPI LoadDIBCursorHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1594 hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
1595 if( hMemObj )
1597 LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1598 hMemObj = HICON_16(CURSORICON_CreateFromResource(
1599 hModule, hMemObj, bits,
1600 SizeofResource16(hModule, hRsrc), FALSE, 0x00030000,
1601 GetSystemMetrics(SM_CXCURSOR),
1602 GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME));
1604 return hMemObj;
1607 /**********************************************************************
1608 * LoadIconHandler (USER.456)
1610 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
1612 LPBYTE bits = (LPBYTE)LockResource16( hResource );
1614 TRACE_(cursor)("hRes=%04x\n",hResource);
1616 return HICON_16(CURSORICON_CreateFromResource(0, 0, bits, 0, TRUE,
1617 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
1620 /***********************************************************************
1621 * LoadCursorW (USER32.@)
1623 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1625 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1626 LR_SHARED | LR_DEFAULTSIZE );
1629 /***********************************************************************
1630 * LoadCursorA (USER32.@)
1632 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1634 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1635 LR_SHARED | LR_DEFAULTSIZE );
1638 /***********************************************************************
1639 * LoadCursorFromFileW (USER32.@)
1641 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1643 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1644 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1647 /***********************************************************************
1648 * LoadCursorFromFileA (USER32.@)
1650 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1652 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1653 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1656 /***********************************************************************
1657 * LoadIconW (USER32.@)
1659 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1661 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1662 LR_SHARED | LR_DEFAULTSIZE );
1665 /***********************************************************************
1666 * LoadIconA (USER32.@)
1668 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1670 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1671 LR_SHARED | LR_DEFAULTSIZE );
1674 /**********************************************************************
1675 * GetIconInfo (USER32.@)
1677 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1679 CURSORICONINFO *ciconinfo;
1680 INT height;
1682 ciconinfo = GlobalLock16(HICON_16(hIcon));
1683 if (!ciconinfo)
1684 return FALSE;
1686 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1687 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1689 iconinfo->fIcon = TRUE;
1690 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1691 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1693 else
1695 iconinfo->fIcon = FALSE;
1696 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1697 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1700 if (ciconinfo->bBitsPerPixel > 1)
1702 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
1703 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1704 (char *)(ciconinfo + 1)
1705 + ciconinfo->nHeight *
1706 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1707 height = ciconinfo->nHeight;
1709 else
1711 iconinfo->hbmColor = 0;
1712 height = ciconinfo->nHeight * 2;
1715 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
1716 1, 1, (char *)(ciconinfo + 1));
1718 GlobalUnlock16(HICON_16(hIcon));
1720 return TRUE;
1723 /**********************************************************************
1724 * CreateIconIndirect (USER32.@)
1726 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1728 BITMAP bmpXor,bmpAnd;
1729 HICON16 hObj;
1730 int sizeXor,sizeAnd;
1732 GetObjectA( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1733 GetObjectA( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1735 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1736 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1738 hObj = GlobalAlloc16( GMEM_MOVEABLE,
1739 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1740 if (hObj)
1742 CURSORICONINFO *info;
1744 info = (CURSORICONINFO *)GlobalLock16( hObj );
1746 /* If we are creating an icon, the hotspot is unused */
1747 if (iconinfo->fIcon)
1749 info->ptHotSpot.x = ICON_HOTSPOT;
1750 info->ptHotSpot.y = ICON_HOTSPOT;
1752 else
1754 info->ptHotSpot.x = iconinfo->xHotspot;
1755 info->ptHotSpot.y = iconinfo->yHotspot;
1758 info->nWidth = bmpXor.bmWidth;
1759 info->nHeight = bmpXor.bmHeight;
1760 info->nWidthBytes = bmpXor.bmWidthBytes;
1761 info->bPlanes = bmpXor.bmPlanes;
1762 info->bBitsPerPixel = bmpXor.bmBitsPixel;
1764 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1766 GetBitmapBits( iconinfo->hbmMask ,sizeAnd,(char*)(info + 1) );
1767 GetBitmapBits( iconinfo->hbmColor,sizeXor,(char*)(info + 1) +sizeAnd);
1768 GlobalUnlock16( hObj );
1770 return HICON_32(hObj);
1773 /******************************************************************************
1774 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1776 * NOTES
1777 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1779 * PARAMS
1780 * hdc [I] Handle to device context
1781 * x0 [I] X coordinate of upper left corner
1782 * y0 [I] Y coordinate of upper left corner
1783 * hIcon [I] Handle to icon to draw
1784 * cxWidth [I] Width of icon
1785 * cyWidth [I] Height of icon
1786 * istep [I] Index of frame in animated cursor
1787 * hbr [I] Handle to background brush
1788 * flags [I] Icon-drawing flags
1790 * RETURNS
1791 * Success: TRUE
1792 * Failure: FALSE
1794 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1795 INT cxWidth, INT cyWidth, UINT istep,
1796 HBRUSH hbr, UINT flags )
1798 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
1799 HDC hDC_off = 0, hMemDC;
1800 BOOL result = FALSE, DoOffscreen;
1801 HBITMAP hB_off = 0, hOld = 0;
1803 if (!ptr) return FALSE;
1804 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1805 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1807 hMemDC = CreateCompatibleDC (hdc);
1808 if (istep)
1809 FIXME_(icon)("Ignoring istep=%d\n", istep);
1810 if (flags & DI_COMPAT)
1811 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
1813 if (!flags) {
1814 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
1815 flags = DI_NORMAL;
1818 /* Calculate the size of the destination image. */
1819 if (cxWidth == 0)
1821 if (flags & DI_DEFAULTSIZE)
1822 cxWidth = GetSystemMetrics (SM_CXICON);
1823 else
1824 cxWidth = ptr->nWidth;
1826 if (cyWidth == 0)
1828 if (flags & DI_DEFAULTSIZE)
1829 cyWidth = GetSystemMetrics (SM_CYICON);
1830 else
1831 cyWidth = ptr->nHeight;
1834 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1836 if (DoOffscreen) {
1837 RECT r;
1839 r.left = 0;
1840 r.top = 0;
1841 r.right = cxWidth;
1842 r.bottom = cxWidth;
1844 hDC_off = CreateCompatibleDC(hdc);
1845 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
1846 if (hDC_off && hB_off) {
1847 hOld = SelectObject(hDC_off, hB_off);
1848 FillRect(hDC_off, &r, hbr);
1852 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
1854 HBITMAP hXorBits, hAndBits;
1855 COLORREF oldFg, oldBg;
1856 INT nStretchMode;
1858 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
1860 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1861 ptr->bPlanes, ptr->bBitsPerPixel,
1862 (char *)(ptr + 1)
1863 + ptr->nHeight *
1864 get_bitmap_width_bytes(ptr->nWidth,1) );
1865 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1866 1, 1, (char *)(ptr+1) );
1867 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1868 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1870 if (hXorBits && hAndBits)
1872 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1873 if (flags & DI_MASK)
1875 if (DoOffscreen)
1876 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1877 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1878 else
1879 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1880 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1882 SelectObject( hMemDC, hXorBits );
1883 if (flags & DI_IMAGE)
1885 if (DoOffscreen)
1886 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1887 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1888 else
1889 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1890 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1892 SelectObject( hMemDC, hBitTemp );
1893 result = TRUE;
1896 SetTextColor( hdc, oldFg );
1897 SetBkColor( hdc, oldBg );
1898 if (hXorBits) DeleteObject( hXorBits );
1899 if (hAndBits) DeleteObject( hAndBits );
1900 SetStretchBltMode (hdc, nStretchMode);
1901 if (DoOffscreen) {
1902 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
1903 SelectObject(hDC_off, hOld);
1906 if (hMemDC) DeleteDC( hMemDC );
1907 if (hDC_off) DeleteDC(hDC_off);
1908 if (hB_off) DeleteObject(hB_off);
1909 GlobalUnlock16(HICON_16(hIcon));
1910 return result;
1913 /***********************************************************************
1914 * DIB_FixColorsToLoadflags
1916 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
1917 * are in loadflags
1919 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
1921 int colors;
1922 COLORREF c_W, c_S, c_F, c_L, c_C;
1923 int incr,i;
1924 RGBQUAD *ptr;
1926 if (bmi->bmiHeader.biBitCount > 8) return;
1927 if (bmi->bmiHeader.biSize == sizeof(BITMAPINFOHEADER)) incr = 4;
1928 else if (bmi->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) incr = 3;
1929 else {
1930 WARN_(resource)("Wrong bitmap header size!\n");
1931 return;
1933 colors = bmi->bmiHeader.biClrUsed;
1934 if (!colors && (bmi->bmiHeader.biBitCount <= 8))
1935 colors = 1 << bmi->bmiHeader.biBitCount;
1936 c_W = GetSysColor(COLOR_WINDOW);
1937 c_S = GetSysColor(COLOR_3DSHADOW);
1938 c_F = GetSysColor(COLOR_3DFACE);
1939 c_L = GetSysColor(COLOR_3DLIGHT);
1940 if (loadflags & LR_LOADTRANSPARENT) {
1941 switch (bmi->bmiHeader.biBitCount) {
1942 case 1: pix = pix >> 7; break;
1943 case 4: pix = pix >> 4; break;
1944 case 8: break;
1945 default:
1946 WARN_(resource)("(%d): Unsupported depth\n", bmi->bmiHeader.biBitCount);
1947 return;
1949 if (pix >= colors) {
1950 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
1951 return;
1953 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
1954 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
1955 ptr->rgbBlue = GetBValue(c_W);
1956 ptr->rgbGreen = GetGValue(c_W);
1957 ptr->rgbRed = GetRValue(c_W);
1959 if (loadflags & LR_LOADMAP3DCOLORS)
1960 for (i=0; i<colors; i++) {
1961 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
1962 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
1963 if (c_C == RGB(128, 128, 128)) {
1964 ptr->rgbRed = GetRValue(c_S);
1965 ptr->rgbGreen = GetGValue(c_S);
1966 ptr->rgbBlue = GetBValue(c_S);
1967 } else if (c_C == RGB(192, 192, 192)) {
1968 ptr->rgbRed = GetRValue(c_F);
1969 ptr->rgbGreen = GetGValue(c_F);
1970 ptr->rgbBlue = GetBValue(c_F);
1971 } else if (c_C == RGB(223, 223, 223)) {
1972 ptr->rgbRed = GetRValue(c_L);
1973 ptr->rgbGreen = GetGValue(c_L);
1974 ptr->rgbBlue = GetBValue(c_L);
1980 /**********************************************************************
1981 * BITMAP_Load
1983 static HBITMAP BITMAP_Load( HINSTANCE instance,LPCWSTR name, UINT loadflags )
1985 HBITMAP hbitmap = 0;
1986 HRSRC hRsrc;
1987 HGLOBAL handle;
1988 char *ptr = NULL;
1989 BITMAPINFO *info, *fix_info=NULL;
1990 HGLOBAL hFix;
1991 int size;
1993 if (!(loadflags & LR_LOADFROMFILE))
1995 if (!instance)
1997 /* OEM bitmap: try to load the resource from user32.dll */
1998 if (HIWORD(name)) return 0;
1999 if (!(instance = GetModuleHandleA("user32.dll"))) return 0;
2001 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
2002 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
2004 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
2006 else
2008 if (!(ptr = map_fileW( name ))) return 0;
2009 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
2011 size = bitmap_info_size(info, DIB_RGB_COLORS);
2012 if ((hFix = GlobalAlloc(0, size))) fix_info=GlobalLock(hFix);
2013 if (fix_info) {
2014 BYTE pix;
2016 memcpy(fix_info, info, size);
2017 pix = *((LPBYTE)info + size);
2018 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
2019 if (!screen_dc) screen_dc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2020 if (screen_dc)
2022 char *bits = (char *)info + size;
2023 if (loadflags & LR_CREATEDIBSECTION) {
2024 DIBSECTION dib;
2025 hbitmap = CreateDIBSection(screen_dc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
2026 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
2027 SetDIBits(screen_dc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
2028 DIB_RGB_COLORS);
2030 else {
2031 hbitmap = CreateDIBitmap( screen_dc, &fix_info->bmiHeader, CBM_INIT,
2032 bits, fix_info, DIB_RGB_COLORS );
2035 GlobalUnlock(hFix);
2036 GlobalFree(hFix);
2038 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2039 return hbitmap;
2042 /**********************************************************************
2043 * LoadImageA (USER32.@)
2045 * FIXME: implementation lacks some features, see LR_ defines in winuser.h
2048 /* filter for page-fault exceptions */
2049 static WINE_EXCEPTION_FILTER(page_fault)
2051 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
2052 return EXCEPTION_EXECUTE_HANDLER;
2053 return EXCEPTION_CONTINUE_SEARCH;
2056 /*********************************************************************/
2058 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2059 INT desiredx, INT desiredy, UINT loadflags)
2061 HANDLE res;
2062 LPWSTR u_name;
2064 if (!HIWORD(name))
2065 return LoadImageW(hinst, (LPWSTR)name, type, desiredx, desiredy, loadflags);
2067 __TRY {
2068 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2069 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2070 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2072 __EXCEPT(page_fault) {
2073 SetLastError( ERROR_INVALID_PARAMETER );
2074 return 0;
2076 __ENDTRY
2077 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2078 HeapFree(GetProcessHeap(), 0, u_name);
2079 return res;
2083 /******************************************************************************
2084 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2086 * PARAMS
2087 * hinst [I] Handle of instance that contains image
2088 * name [I] Name of image
2089 * type [I] Type of image
2090 * desiredx [I] Desired width
2091 * desiredy [I] Desired height
2092 * loadflags [I] Load flags
2094 * RETURNS
2095 * Success: Handle to newly loaded image
2096 * Failure: NULL
2098 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2100 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2101 INT desiredx, INT desiredy, UINT loadflags )
2103 if (HIWORD(name)) {
2104 TRACE_(resource)("(%p,%p,%d,%d,%d,0x%08x)\n",
2105 hinst,name,type,desiredx,desiredy,loadflags);
2106 } else {
2107 TRACE_(resource)("(%p,%p,%d,%d,%d,0x%08x)\n",
2108 hinst,name,type,desiredx,desiredy,loadflags);
2110 if (loadflags & LR_DEFAULTSIZE) {
2111 if (type == IMAGE_ICON) {
2112 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2113 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2114 } else if (type == IMAGE_CURSOR) {
2115 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2116 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2119 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2120 switch (type) {
2121 case IMAGE_BITMAP:
2122 return BITMAP_Load( hinst, name, loadflags );
2124 case IMAGE_ICON:
2125 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2126 if (screen_dc)
2128 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2129 if (palEnts == 0) palEnts = 256;
2130 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2131 palEnts, FALSE, loadflags);
2133 break;
2135 case IMAGE_CURSOR:
2136 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2137 1, TRUE, loadflags);
2139 return 0;
2142 /******************************************************************************
2143 * CopyImage (USER32.@) Creates new image and copies attributes to it
2145 * PARAMS
2146 * hnd [I] Handle to image to copy
2147 * type [I] Type of image to copy
2148 * desiredx [I] Desired width of new image
2149 * desiredy [I] Desired height of new image
2150 * flags [I] Copy flags
2152 * RETURNS
2153 * Success: Handle to newly created image
2154 * Failure: NULL
2156 * FIXME: implementation still lacks nearly all features, see LR_*
2157 * defines in winuser.h
2159 HICON WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2160 INT desiredy, UINT flags )
2162 switch (type)
2164 case IMAGE_BITMAP:
2166 HBITMAP res;
2167 BITMAP bm;
2169 if (!GetObjectW( hnd, sizeof(bm), &bm )) return 0;
2170 bm.bmBits = NULL;
2171 if ((res = CreateBitmapIndirect(&bm)))
2173 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight );
2174 GetBitmapBits( hnd, bm.bmWidthBytes * bm.bmHeight, buf );
2175 SetBitmapBits( res, bm.bmWidthBytes * bm.bmHeight, buf );
2176 HeapFree( GetProcessHeap(), 0, buf );
2178 return (HICON)res;
2180 case IMAGE_ICON:
2181 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2182 case IMAGE_CURSOR:
2183 /* Should call CURSORICON_ExtCopy but more testing
2184 * needs to be done before we change this
2186 return CopyCursor(hnd);
2188 return 0;
2192 /******************************************************************************
2193 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2195 * RETURNS
2196 * Success: Handle to specified bitmap
2197 * Failure: NULL
2199 HBITMAP WINAPI LoadBitmapW(
2200 HINSTANCE instance, /* [in] Handle to application instance */
2201 LPCWSTR name) /* [in] Address of bitmap resource name */
2203 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2206 /**********************************************************************
2207 * LoadBitmapA (USER32.@)
2209 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2211 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );