offsets array is the size of the wine data format so there is no need
[wine.git] / windows / cursoricon.c
blobc5bc5f89d4791ceda82a0920a2722c9571cd6386
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) hInstance = user32_module; /* Load OEM cursor/icon */
819 /* Normalize hInstance (must be uniquely represented for icon cache) */
821 if (!HIWORD( hInstance ))
822 hInstance = HINSTANCE_32(GetExePtr( HINSTANCE_16(hInstance) ));
824 /* Get directory resource ID */
826 if (!(hRsrc = FindResourceW( hInstance, name,
827 (LPWSTR)(fCursor ? RT_GROUP_CURSOR : RT_GROUP_ICON) )))
828 return 0;
829 hGroupRsrc = hRsrc;
831 /* Find the best entry in the directory */
833 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
834 if (!(dir = (CURSORICONDIR*)LockResource( handle ))) return 0;
835 if (fCursor)
836 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor( dir,
837 width, height, 1);
838 else
839 dirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon( dir,
840 width, height, colors );
841 if (!dirEntry) return 0;
842 wResId = dirEntry->wResId;
843 dwBytesInRes = dirEntry->dwBytesInRes;
844 FreeResource( handle );
846 /* Load the resource */
848 if (!(hRsrc = FindResourceW(hInstance,MAKEINTRESOURCEW(wResId),
849 (LPWSTR)(fCursor ? RT_CURSOR : RT_ICON) ))) return 0;
851 /* If shared icon, check whether it was already loaded */
852 if ( (loadflags & LR_SHARED)
853 && (hIcon = CURSORICON_FindSharedIcon( hInstance, hRsrc ) ) != 0 )
854 return hIcon;
856 if (!(handle = LoadResource( hInstance, hRsrc ))) return 0;
857 bits = (LPBYTE)LockResource( handle );
858 hIcon = CURSORICON_CreateFromResource( 0, 0, bits, dwBytesInRes,
859 !fCursor, 0x00030000, width, height, loadflags);
860 FreeResource( handle );
862 /* If shared icon, add to icon cache */
864 if ( hIcon && (loadflags & LR_SHARED) )
865 CURSORICON_AddSharedIcon( hInstance, hRsrc, hGroupRsrc, hIcon );
868 return hIcon;
871 /***********************************************************************
872 * CURSORICON_Copy
874 * Make a copy of a cursor or icon.
876 static HICON CURSORICON_Copy( HINSTANCE16 hInst16, HICON hIcon )
878 char *ptrOld, *ptrNew;
879 int size;
880 HICON16 hOld = HICON_16(hIcon);
881 HICON16 hNew;
883 if (!(ptrOld = (char *)GlobalLock16( hOld ))) return 0;
884 if (hInst16 && !(hInst16 = GetExePtr( hInst16 ))) return 0;
885 size = GlobalSize16( hOld );
886 hNew = GlobalAlloc16( GMEM_MOVEABLE, size );
887 FarSetOwner16( hNew, hInst16 );
888 ptrNew = (char *)GlobalLock16( hNew );
889 memcpy( ptrNew, ptrOld, size );
890 GlobalUnlock16( hOld );
891 GlobalUnlock16( hNew );
892 return HICON_32(hNew);
895 /*************************************************************************
896 * CURSORICON_ExtCopy
898 * Copies an Image from the Cache if LR_COPYFROMRESOURCE is specified
900 * PARAMS
901 * Handle [I] handle to an Image
902 * nType [I] Type of Handle (IMAGE_CURSOR | IMAGE_ICON)
903 * iDesiredCX [I] The Desired width of the Image
904 * iDesiredCY [I] The desired height of the Image
905 * nFlags [I] The flags from CopyImage
907 * RETURNS
908 * Success: The new handle of the Image
910 * NOTES
911 * LR_COPYDELETEORG and LR_MONOCHROME are currently not implemented.
912 * LR_MONOCHROME should be implemented by CURSORICON_CreateFromResource.
913 * LR_COPYFROMRESOURCE will only work if the Image is in the Cache.
918 static HICON CURSORICON_ExtCopy(HICON hIcon, UINT nType,
919 INT iDesiredCX, INT iDesiredCY,
920 UINT nFlags)
922 HICON hNew=0;
924 TRACE_(icon)("hIcon %p, nType %u, iDesiredCX %i, iDesiredCY %i, nFlags %u\n",
925 hIcon, nType, iDesiredCX, iDesiredCY, nFlags);
927 if(hIcon == 0)
929 return 0;
932 /* Best Fit or Monochrome */
933 if( (nFlags & LR_COPYFROMRESOURCE
934 && (iDesiredCX > 0 || iDesiredCY > 0))
935 || nFlags & LR_MONOCHROME)
937 ICONCACHE* pIconCache = CURSORICON_FindCache(hIcon);
939 /* Not Found in Cache, then do a straight copy
941 if(pIconCache == NULL)
943 hNew = CURSORICON_Copy(0, hIcon);
944 if(nFlags & LR_COPYFROMRESOURCE)
946 TRACE_(icon)("LR_COPYFROMRESOURCE: Failed to load from cache\n");
949 else
951 int iTargetCY = iDesiredCY, iTargetCX = iDesiredCX;
952 LPBYTE pBits;
953 HANDLE hMem;
954 HRSRC hRsrc;
955 DWORD dwBytesInRes;
956 WORD wResId;
957 CURSORICONDIR *pDir;
958 CURSORICONDIRENTRY *pDirEntry;
959 BOOL bIsIcon = (nType == IMAGE_ICON);
961 /* Completing iDesiredCX CY for Monochrome Bitmaps if needed
963 if(((nFlags & LR_MONOCHROME) && !(nFlags & LR_COPYFROMRESOURCE))
964 || (iDesiredCX == 0 && iDesiredCY == 0))
966 iDesiredCY = GetSystemMetrics(bIsIcon ?
967 SM_CYICON : SM_CYCURSOR);
968 iDesiredCX = GetSystemMetrics(bIsIcon ?
969 SM_CXICON : SM_CXCURSOR);
972 /* Retrieve the CURSORICONDIRENTRY
974 if (!(hMem = LoadResource( pIconCache->hModule ,
975 pIconCache->hGroupRsrc)))
977 return 0;
979 if (!(pDir = (CURSORICONDIR*)LockResource( hMem )))
981 return 0;
984 /* Find Best Fit
986 if(bIsIcon)
988 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestIcon(
989 pDir, iDesiredCX, iDesiredCY, 256);
991 else
993 pDirEntry = (CURSORICONDIRENTRY *)CURSORICON_FindBestCursor(
994 pDir, iDesiredCX, iDesiredCY, 1);
997 wResId = pDirEntry->wResId;
998 dwBytesInRes = pDirEntry->dwBytesInRes;
999 FreeResource(hMem);
1001 TRACE_(icon)("ResID %u, BytesInRes %lu, Width %d, Height %d DX %d, DY %d\n",
1002 wResId, dwBytesInRes, pDirEntry->ResInfo.icon.bWidth,
1003 pDirEntry->ResInfo.icon.bHeight, iDesiredCX, iDesiredCY);
1005 /* Get the Best Fit
1007 if (!(hRsrc = FindResourceW(pIconCache->hModule ,
1008 MAKEINTRESOURCEW(wResId), (LPWSTR)(bIsIcon ? RT_ICON : RT_CURSOR))))
1010 return 0;
1012 if (!(hMem = LoadResource( pIconCache->hModule , hRsrc )))
1014 return 0;
1017 pBits = (LPBYTE)LockResource( hMem );
1019 if(nFlags & LR_DEFAULTSIZE)
1021 iTargetCY = GetSystemMetrics(SM_CYICON);
1022 iTargetCX = GetSystemMetrics(SM_CXICON);
1025 /* Create a New Icon with the proper dimension
1027 hNew = CURSORICON_CreateFromResource( 0, 0, pBits, dwBytesInRes,
1028 bIsIcon, 0x00030000, iTargetCX, iTargetCY, nFlags);
1029 FreeResource(hMem);
1032 else hNew = CURSORICON_Copy(0, hIcon);
1033 return hNew;
1037 /***********************************************************************
1038 * CreateCursor (USER32.@)
1040 HCURSOR WINAPI CreateCursor( HINSTANCE hInstance,
1041 INT xHotSpot, INT yHotSpot,
1042 INT nWidth, INT nHeight,
1043 LPCVOID lpANDbits, LPCVOID lpXORbits )
1045 CURSORICONINFO info;
1047 TRACE_(cursor)("%dx%d spot=%d,%d xor=%p and=%p\n",
1048 nWidth, nHeight, xHotSpot, yHotSpot, lpXORbits, lpANDbits);
1050 info.ptHotSpot.x = xHotSpot;
1051 info.ptHotSpot.y = yHotSpot;
1052 info.nWidth = nWidth;
1053 info.nHeight = nHeight;
1054 info.nWidthBytes = 0;
1055 info.bPlanes = 1;
1056 info.bBitsPerPixel = 1;
1058 return HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1062 /***********************************************************************
1063 * CreateIcon (USER.407)
1065 HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
1066 INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
1067 LPCVOID lpANDbits, LPCVOID lpXORbits )
1069 CURSORICONINFO info;
1071 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1072 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1074 info.ptHotSpot.x = ICON_HOTSPOT;
1075 info.ptHotSpot.y = ICON_HOTSPOT;
1076 info.nWidth = nWidth;
1077 info.nHeight = nHeight;
1078 info.nWidthBytes = 0;
1079 info.bPlanes = bPlanes;
1080 info.bBitsPerPixel = bBitsPixel;
1082 return CreateCursorIconIndirect16( hInstance, &info, lpANDbits, lpXORbits );
1086 /***********************************************************************
1087 * CreateIcon (USER32.@)
1089 * Creates an icon based on the specified bitmaps. The bitmaps must be
1090 * provided in a device dependent format and will be resized to
1091 * (SM_CXICON,SM_CYICON) and depth converted to match the screen's color
1092 * depth. The provided bitmaps must be top-down bitmaps.
1093 * Although Windows does not support 15bpp(*) this API must support it
1094 * for Winelib applications.
1096 * (*) Windows does not support 15bpp but it supports the 555 RGB 16bpp
1097 * format!
1099 * BUGS
1101 * - The provided bitmaps are not resized!
1102 * - The documentation says the lpXORbits bitmap must be in a device
1103 * dependent format. But we must still resize it and perform depth
1104 * conversions if necessary.
1105 * - I'm a bit unsure about the how the 'device dependent format' thing works.
1106 * I did some tests on windows and found that if you provide a 16bpp bitmap
1107 * in lpXORbits, then its format but be 565 RGB if the screen's bit depth
1108 * is 16bpp but it must be 555 RGB if the screen's bit depth is anything
1109 * else. I don't know if this is part of the GDI specs or if this is a
1110 * quirk of the graphics card driver.
1111 * - You may think that we check whether the bit depths match or not
1112 * as an optimization. But the truth is that the conversion using
1113 * CreateDIBitmap does not work for some bit depth (e.g. 8bpp) and I have
1114 * no idea why.
1115 * - I'm pretty sure that all the things we do in CreateIcon should
1116 * also be done in CreateIconIndirect...
1118 HICON WINAPI CreateIcon(
1119 HINSTANCE hInstance, /* [in] the application's hInstance */
1120 INT nWidth, /* [in] the width of the provided bitmaps */
1121 INT nHeight, /* [in] the height of the provided bitmaps */
1122 BYTE bPlanes, /* [in] the number of planes in the provided bitmaps */
1123 BYTE bBitsPixel, /* [in] the number of bits per pixel of the lpXORbits bitmap */
1124 LPCVOID lpANDbits, /* [in] a monochrome bitmap representing the icon's mask */
1125 LPCVOID lpXORbits) /* [in] the icon's 'color' bitmap */
1127 HICON hIcon;
1128 HDC hdc;
1130 TRACE_(icon)("%dx%dx%d, xor=%p, and=%p\n",
1131 nWidth, nHeight, bPlanes * bBitsPixel, lpXORbits, lpANDbits);
1133 hdc=GetDC(0);
1134 if (!hdc)
1135 return 0;
1137 if (GetDeviceCaps(hdc,BITSPIXEL)==bBitsPixel) {
1138 CURSORICONINFO info;
1140 info.ptHotSpot.x = ICON_HOTSPOT;
1141 info.ptHotSpot.y = ICON_HOTSPOT;
1142 info.nWidth = nWidth;
1143 info.nHeight = nHeight;
1144 info.nWidthBytes = 0;
1145 info.bPlanes = bPlanes;
1146 info.bBitsPerPixel = bBitsPixel;
1148 hIcon=HICON_32(CreateCursorIconIndirect16(0, &info, lpANDbits, lpXORbits));
1149 } else {
1150 ICONINFO iinfo;
1151 BITMAPINFO bmi;
1153 iinfo.fIcon=TRUE;
1154 iinfo.xHotspot=ICON_HOTSPOT;
1155 iinfo.yHotspot=ICON_HOTSPOT;
1156 iinfo.hbmMask=CreateBitmap(nWidth,nHeight,1,1,lpANDbits);
1158 bmi.bmiHeader.biSize=sizeof(bmi.bmiHeader);
1159 bmi.bmiHeader.biWidth=nWidth;
1160 bmi.bmiHeader.biHeight=-nHeight;
1161 bmi.bmiHeader.biPlanes=bPlanes;
1162 bmi.bmiHeader.biBitCount=bBitsPixel;
1163 bmi.bmiHeader.biCompression=BI_RGB;
1164 bmi.bmiHeader.biSizeImage=0;
1165 bmi.bmiHeader.biXPelsPerMeter=0;
1166 bmi.bmiHeader.biYPelsPerMeter=0;
1167 bmi.bmiHeader.biClrUsed=0;
1168 bmi.bmiHeader.biClrImportant=0;
1170 iinfo.hbmColor = CreateDIBitmap( hdc, &bmi.bmiHeader,
1171 CBM_INIT, lpXORbits,
1172 &bmi, DIB_RGB_COLORS );
1174 hIcon=CreateIconIndirect(&iinfo);
1175 DeleteObject(iinfo.hbmMask);
1176 DeleteObject(iinfo.hbmColor);
1178 ReleaseDC(0,hdc);
1179 return hIcon;
1183 /***********************************************************************
1184 * CreateCursorIconIndirect (USER.408)
1186 HGLOBAL16 WINAPI CreateCursorIconIndirect16( HINSTANCE16 hInstance,
1187 CURSORICONINFO *info,
1188 LPCVOID lpANDbits,
1189 LPCVOID lpXORbits )
1191 HGLOBAL16 handle;
1192 char *ptr;
1193 int sizeAnd, sizeXor;
1195 hInstance = GetExePtr( hInstance ); /* Make it a module handle */
1196 if (!lpXORbits || !lpANDbits || info->bPlanes != 1) return 0;
1197 info->nWidthBytes = get_bitmap_width_bytes(info->nWidth,info->bBitsPerPixel);
1198 sizeXor = info->nHeight * info->nWidthBytes;
1199 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1200 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
1201 sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
1202 return 0;
1203 FarSetOwner16( handle, hInstance );
1204 ptr = (char *)GlobalLock16( handle );
1205 memcpy( ptr, info, sizeof(*info) );
1206 memcpy( ptr + sizeof(CURSORICONINFO), lpANDbits, sizeAnd );
1207 memcpy( ptr + sizeof(CURSORICONINFO) + sizeAnd, lpXORbits, sizeXor );
1208 GlobalUnlock16( handle );
1209 return handle;
1213 /***********************************************************************
1214 * CopyIcon (USER.368)
1216 HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
1218 TRACE_(icon)("%04x %04x\n", hInstance, hIcon );
1219 return HICON_16(CURSORICON_Copy(hInstance, HICON_32(hIcon)));
1223 /***********************************************************************
1224 * CopyIcon (USER32.@)
1226 HICON WINAPI CopyIcon( HICON hIcon )
1228 TRACE_(icon)("%p\n", hIcon );
1229 return CURSORICON_Copy( 0, hIcon );
1233 /***********************************************************************
1234 * CopyCursor (USER.369)
1236 HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
1238 TRACE_(cursor)("%04x %04x\n", hInstance, hCursor );
1239 return HICON_16(CURSORICON_Copy(hInstance, HCURSOR_32(hCursor)));
1242 /**********************************************************************
1243 * DestroyIcon32 (USER.610)
1245 * This routine is actually exported from Win95 USER under the name
1246 * DestroyIcon32 ... The behaviour implemented here should mimic
1247 * the Win95 one exactly, especially the return values, which
1248 * depend on the setting of various flags.
1250 WORD WINAPI DestroyIcon32( HGLOBAL16 handle, UINT16 flags )
1252 WORD retv;
1254 TRACE_(icon)("(%04x, %04x)\n", handle, flags );
1256 /* Check whether destroying active cursor */
1258 if ( QUEUE_Current()->cursor == HICON_32(handle) )
1260 WARN_(cursor)("Destroying active cursor!\n" );
1261 SetCursor( 0 );
1264 /* Try shared cursor/icon first */
1266 if ( !(flags & CID_NONSHARED) )
1268 INT count = CURSORICON_DelSharedIcon(HICON_32(handle));
1270 if ( count != -1 )
1271 return (flags & CID_WIN32)? TRUE : (count == 0);
1273 /* FIXME: OEM cursors/icons should be recognized */
1276 /* Now assume non-shared cursor/icon */
1278 retv = GlobalFree16( handle );
1279 return (flags & CID_RESOURCE)? retv : TRUE;
1282 /***********************************************************************
1283 * DestroyIcon (USER32.@)
1285 BOOL WINAPI DestroyIcon( HICON hIcon )
1287 return DestroyIcon32(HICON_16(hIcon), CID_WIN32);
1291 /***********************************************************************
1292 * DestroyCursor (USER32.@)
1294 BOOL WINAPI DestroyCursor( HCURSOR hCursor )
1296 return DestroyIcon32(HCURSOR_16(hCursor), CID_WIN32);
1300 /***********************************************************************
1301 * DrawIcon (USER32.@)
1303 BOOL WINAPI DrawIcon( HDC hdc, INT x, INT y, HICON hIcon )
1305 CURSORICONINFO *ptr;
1306 HDC hMemDC;
1307 HBITMAP hXorBits, hAndBits;
1308 COLORREF oldFg, oldBg;
1310 if (!(ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)))) return FALSE;
1311 if (!(hMemDC = CreateCompatibleDC( hdc ))) return FALSE;
1312 hAndBits = CreateBitmap( ptr->nWidth, ptr->nHeight, 1, 1,
1313 (char *)(ptr+1) );
1314 hXorBits = CreateBitmap( ptr->nWidth, ptr->nHeight, ptr->bPlanes,
1315 ptr->bBitsPerPixel, (char *)(ptr + 1)
1316 + ptr->nHeight * get_bitmap_width_bytes(ptr->nWidth,1) );
1317 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1318 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1320 if (hXorBits && hAndBits)
1322 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1323 BitBlt( hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0, SRCAND );
1324 SelectObject( hMemDC, hXorBits );
1325 BitBlt(hdc, x, y, ptr->nWidth, ptr->nHeight, hMemDC, 0, 0,SRCINVERT);
1326 SelectObject( hMemDC, hBitTemp );
1328 DeleteDC( hMemDC );
1329 if (hXorBits) DeleteObject( hXorBits );
1330 if (hAndBits) DeleteObject( hAndBits );
1331 GlobalUnlock16(HICON_16(hIcon));
1332 SetTextColor( hdc, oldFg );
1333 SetBkColor( hdc, oldBg );
1334 return TRUE;
1337 /***********************************************************************
1338 * DumpIcon (USER.459)
1340 DWORD WINAPI DumpIcon16( SEGPTR pInfo, WORD *lpLen,
1341 SEGPTR *lpXorBits, SEGPTR *lpAndBits )
1343 CURSORICONINFO *info = MapSL( pInfo );
1344 int sizeAnd, sizeXor;
1346 if (!info) return 0;
1347 sizeXor = info->nHeight * info->nWidthBytes;
1348 sizeAnd = info->nHeight * get_bitmap_width_bytes( info->nWidth, 1 );
1349 if (lpAndBits) *lpAndBits = pInfo + sizeof(CURSORICONINFO);
1350 if (lpXorBits) *lpXorBits = pInfo + sizeof(CURSORICONINFO) + sizeAnd;
1351 if (lpLen) *lpLen = sizeof(CURSORICONINFO) + sizeAnd + sizeXor;
1352 return MAKELONG( sizeXor, sizeXor );
1356 /***********************************************************************
1357 * SetCursor (USER32.@)
1358 * RETURNS:
1359 * A handle to the previous cursor shape.
1361 HCURSOR WINAPI SetCursor( HCURSOR hCursor /* [in] Handle of cursor to show */ )
1363 MESSAGEQUEUE *queue = QUEUE_Current();
1364 HCURSOR hOldCursor;
1366 if (hCursor == queue->cursor) return hCursor; /* No change */
1367 TRACE_(cursor)("%p\n", hCursor );
1368 hOldCursor = queue->cursor;
1369 queue->cursor = hCursor;
1370 /* Change the cursor shape only if it is visible */
1371 if (queue->cursor_count >= 0)
1373 USER_Driver.pSetCursor( (CURSORICONINFO*)GlobalLock16(HCURSOR_16(hCursor)) );
1374 GlobalUnlock16(HCURSOR_16(hCursor));
1376 return hOldCursor;
1379 /***********************************************************************
1380 * ShowCursor (USER32.@)
1382 INT WINAPI ShowCursor( BOOL bShow )
1384 MESSAGEQUEUE *queue = QUEUE_Current();
1386 TRACE_(cursor)("%d, count=%d\n", bShow, queue->cursor_count );
1388 if (bShow)
1390 if (++queue->cursor_count == 0) /* Show it */
1392 USER_Driver.pSetCursor((CURSORICONINFO*)GlobalLock16(HCURSOR_16(queue->cursor)));
1393 GlobalUnlock16(HCURSOR_16(queue->cursor));
1396 else
1398 if (--queue->cursor_count == -1) /* Hide it */
1399 USER_Driver.pSetCursor( NULL );
1401 return queue->cursor_count;
1404 /***********************************************************************
1405 * GetCursor (USER32.@)
1407 HCURSOR WINAPI GetCursor(void)
1409 return QUEUE_Current()->cursor;
1413 /***********************************************************************
1414 * ClipCursor (USER32.@)
1416 BOOL WINAPI ClipCursor( const RECT *rect )
1418 if (!rect) SetRectEmpty( &CURSOR_ClipRect );
1419 else CopyRect( &CURSOR_ClipRect, rect );
1420 return TRUE;
1424 /***********************************************************************
1425 * GetClipCursor (USER32.@)
1427 BOOL WINAPI GetClipCursor( RECT *rect )
1429 if (rect)
1431 CopyRect( rect, &CURSOR_ClipRect );
1432 return TRUE;
1434 return FALSE;
1437 /**********************************************************************
1438 * LookupIconIdFromDirectoryEx (USER.364)
1440 * FIXME: exact parameter sizes
1442 INT16 WINAPI LookupIconIdFromDirectoryEx16( LPBYTE dir, BOOL16 bIcon,
1443 INT16 width, INT16 height, UINT16 cFlag )
1445 return LookupIconIdFromDirectoryEx( dir, bIcon, width, height, cFlag );
1448 /**********************************************************************
1449 * LookupIconIdFromDirectoryEx (USER32.@)
1451 INT WINAPI LookupIconIdFromDirectoryEx( LPBYTE xdir, BOOL bIcon,
1452 INT width, INT height, UINT cFlag )
1454 CURSORICONDIR *dir = (CURSORICONDIR*)xdir;
1455 UINT retVal = 0;
1456 if( dir && !dir->idReserved && (dir->idType & 3) )
1458 CURSORICONDIRENTRY* entry;
1459 HDC hdc;
1460 UINT palEnts;
1461 int colors;
1462 hdc = GetDC(0);
1463 palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
1464 if (palEnts == 0)
1465 palEnts = 256;
1466 colors = (cFlag & LR_MONOCHROME) ? 2 : palEnts;
1468 ReleaseDC(0, hdc);
1470 if( bIcon )
1471 entry = CURSORICON_FindBestIcon( dir, width, height, colors );
1472 else
1473 entry = CURSORICON_FindBestCursor( dir, width, height, 1);
1475 if( entry ) retVal = entry->wResId;
1477 else WARN_(cursor)("invalid resource directory\n");
1478 return retVal;
1481 /**********************************************************************
1482 * LookupIconIdFromDirectory (USER.?)
1484 INT16 WINAPI LookupIconIdFromDirectory16( LPBYTE dir, BOOL16 bIcon )
1486 return LookupIconIdFromDirectoryEx16( dir, bIcon,
1487 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1488 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1491 /**********************************************************************
1492 * LookupIconIdFromDirectory (USER32.@)
1494 INT WINAPI LookupIconIdFromDirectory( LPBYTE dir, BOOL bIcon )
1496 return LookupIconIdFromDirectoryEx( dir, bIcon,
1497 bIcon ? GetSystemMetrics(SM_CXICON) : GetSystemMetrics(SM_CXCURSOR),
1498 bIcon ? GetSystemMetrics(SM_CYICON) : GetSystemMetrics(SM_CYCURSOR), bIcon ? 0 : LR_MONOCHROME );
1501 /**********************************************************************
1502 * GetIconID (USER.455)
1504 WORD WINAPI GetIconID16( HGLOBAL16 hResource, DWORD resType )
1506 LPBYTE lpDir = (LPBYTE)GlobalLock16(hResource);
1508 TRACE_(cursor)("hRes=%04x, entries=%i\n",
1509 hResource, lpDir ? ((CURSORICONDIR*)lpDir)->idCount : 0);
1511 switch(resType)
1513 case RT_CURSOR:
1514 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, FALSE,
1515 GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME );
1516 case RT_ICON:
1517 return (WORD)LookupIconIdFromDirectoryEx16( lpDir, TRUE,
1518 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
1519 default:
1520 WARN_(cursor)("invalid res type %ld\n", resType );
1522 return 0;
1525 /**********************************************************************
1526 * LoadCursorIconHandler (USER.336)
1528 * Supposed to load resources of Windows 2.x applications.
1530 HGLOBAL16 WINAPI LoadCursorIconHandler16( HGLOBAL16 hResource, HMODULE16 hModule, HRSRC16 hRsrc )
1532 FIXME_(cursor)("(%04x,%04x,%04x): old 2.x resources are not supported!\n",
1533 hResource, hModule, hRsrc);
1534 return (HGLOBAL16)0;
1537 /**********************************************************************
1538 * LoadDIBIconHandler (USER.357)
1540 * RT_ICON resource loader, installed by USER_SignalProc when module
1541 * is initialized.
1543 HGLOBAL16 WINAPI LoadDIBIconHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1545 /* If hResource is zero we must allocate a new memory block, if it's
1546 * non-zero but GlobalLock() returns NULL then it was discarded and
1547 * we have to recommit some memory, otherwise we just need to check
1548 * the block size. See LoadProc() in 16-bit SDK for more.
1551 hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
1552 if( hMemObj )
1554 LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1555 hMemObj = HICON_16(CURSORICON_CreateFromResource(
1556 hModule, hMemObj, bits,
1557 SizeofResource16(hModule, hRsrc), TRUE, 0x00030000,
1558 GetSystemMetrics(SM_CXICON),
1559 GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR));
1561 return hMemObj;
1564 /**********************************************************************
1565 * LoadDIBCursorHandler (USER.356)
1567 * RT_CURSOR resource loader. Same as above.
1569 HGLOBAL16 WINAPI LoadDIBCursorHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1571 hMemObj = NE_DefResourceHandler( hMemObj, hModule, hRsrc );
1572 if( hMemObj )
1574 LPBYTE bits = (LPBYTE)GlobalLock16( hMemObj );
1575 hMemObj = HICON_16(CURSORICON_CreateFromResource(
1576 hModule, hMemObj, bits,
1577 SizeofResource16(hModule, hRsrc), FALSE, 0x00030000,
1578 GetSystemMetrics(SM_CXCURSOR),
1579 GetSystemMetrics(SM_CYCURSOR), LR_MONOCHROME));
1581 return hMemObj;
1584 /**********************************************************************
1585 * LoadIconHandler (USER.456)
1587 HICON16 WINAPI LoadIconHandler16( HGLOBAL16 hResource, BOOL16 bNew )
1589 LPBYTE bits = (LPBYTE)LockResource16( hResource );
1591 TRACE_(cursor)("hRes=%04x\n",hResource);
1593 return HICON_16(CURSORICON_CreateFromResource(0, 0, bits, 0, TRUE,
1594 bNew ? 0x00030000 : 0x00020000, 0, 0, LR_DEFAULTCOLOR));
1597 /***********************************************************************
1598 * LoadCursorW (USER32.@)
1600 HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance, LPCWSTR name)
1602 return LoadImageW( hInstance, name, IMAGE_CURSOR, 0, 0,
1603 LR_SHARED | LR_DEFAULTSIZE );
1606 /***********************************************************************
1607 * LoadCursorA (USER32.@)
1609 HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance, LPCSTR name)
1611 return LoadImageA( hInstance, name, IMAGE_CURSOR, 0, 0,
1612 LR_SHARED | LR_DEFAULTSIZE );
1615 /***********************************************************************
1616 * LoadCursorFromFileW (USER32.@)
1618 HCURSOR WINAPI LoadCursorFromFileW (LPCWSTR name)
1620 return LoadImageW( 0, name, IMAGE_CURSOR, 0, 0,
1621 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1624 /***********************************************************************
1625 * LoadCursorFromFileA (USER32.@)
1627 HCURSOR WINAPI LoadCursorFromFileA (LPCSTR name)
1629 return LoadImageA( 0, name, IMAGE_CURSOR, 0, 0,
1630 LR_LOADFROMFILE | LR_DEFAULTSIZE );
1633 /***********************************************************************
1634 * LoadIconW (USER32.@)
1636 HICON WINAPI LoadIconW(HINSTANCE hInstance, LPCWSTR name)
1638 return LoadImageW( hInstance, name, IMAGE_ICON, 0, 0,
1639 LR_SHARED | LR_DEFAULTSIZE );
1642 /***********************************************************************
1643 * LoadIconA (USER32.@)
1645 HICON WINAPI LoadIconA(HINSTANCE hInstance, LPCSTR name)
1647 return LoadImageA( hInstance, name, IMAGE_ICON, 0, 0,
1648 LR_SHARED | LR_DEFAULTSIZE );
1651 /**********************************************************************
1652 * GetIconInfo (USER32.@)
1654 BOOL WINAPI GetIconInfo(HICON hIcon, PICONINFO iconinfo)
1656 CURSORICONINFO *ciconinfo;
1657 INT height;
1659 ciconinfo = GlobalLock16(HICON_16(hIcon));
1660 if (!ciconinfo)
1661 return FALSE;
1663 if ( (ciconinfo->ptHotSpot.x == ICON_HOTSPOT) &&
1664 (ciconinfo->ptHotSpot.y == ICON_HOTSPOT) )
1666 iconinfo->fIcon = TRUE;
1667 iconinfo->xHotspot = ciconinfo->nWidth / 2;
1668 iconinfo->yHotspot = ciconinfo->nHeight / 2;
1670 else
1672 iconinfo->fIcon = FALSE;
1673 iconinfo->xHotspot = ciconinfo->ptHotSpot.x;
1674 iconinfo->yHotspot = ciconinfo->ptHotSpot.y;
1677 if (ciconinfo->bBitsPerPixel > 1)
1679 iconinfo->hbmColor = CreateBitmap( ciconinfo->nWidth, ciconinfo->nHeight,
1680 ciconinfo->bPlanes, ciconinfo->bBitsPerPixel,
1681 (char *)(ciconinfo + 1)
1682 + ciconinfo->nHeight *
1683 get_bitmap_width_bytes (ciconinfo->nWidth,1) );
1684 height = ciconinfo->nHeight;
1686 else
1688 iconinfo->hbmColor = 0;
1689 height = ciconinfo->nHeight * 2;
1692 iconinfo->hbmMask = CreateBitmap ( ciconinfo->nWidth, height,
1693 1, 1, (char *)(ciconinfo + 1));
1695 GlobalUnlock16(HICON_16(hIcon));
1697 return TRUE;
1700 /**********************************************************************
1701 * CreateIconIndirect (USER32.@)
1703 HICON WINAPI CreateIconIndirect(PICONINFO iconinfo)
1705 BITMAP bmpXor,bmpAnd;
1706 HICON16 hObj;
1707 int sizeXor,sizeAnd;
1709 GetObjectA( iconinfo->hbmColor, sizeof(bmpXor), &bmpXor );
1710 GetObjectA( iconinfo->hbmMask, sizeof(bmpAnd), &bmpAnd );
1712 sizeXor = bmpXor.bmHeight * bmpXor.bmWidthBytes;
1713 sizeAnd = bmpAnd.bmHeight * bmpAnd.bmWidthBytes;
1715 hObj = GlobalAlloc16( GMEM_MOVEABLE,
1716 sizeof(CURSORICONINFO) + sizeXor + sizeAnd );
1717 if (hObj)
1719 CURSORICONINFO *info;
1721 info = (CURSORICONINFO *)GlobalLock16( hObj );
1723 /* If we are creating an icon, the hotspot is unused */
1724 if (iconinfo->fIcon)
1726 info->ptHotSpot.x = ICON_HOTSPOT;
1727 info->ptHotSpot.y = ICON_HOTSPOT;
1729 else
1731 info->ptHotSpot.x = iconinfo->xHotspot;
1732 info->ptHotSpot.y = iconinfo->yHotspot;
1735 info->nWidth = bmpXor.bmWidth;
1736 info->nHeight = bmpXor.bmHeight;
1737 info->nWidthBytes = bmpXor.bmWidthBytes;
1738 info->bPlanes = bmpXor.bmPlanes;
1739 info->bBitsPerPixel = bmpXor.bmBitsPixel;
1741 /* Transfer the bitmap bits to the CURSORICONINFO structure */
1743 GetBitmapBits( iconinfo->hbmMask ,sizeAnd,(char*)(info + 1) );
1744 GetBitmapBits( iconinfo->hbmColor,sizeXor,(char*)(info + 1) +sizeAnd);
1745 GlobalUnlock16( hObj );
1747 return HICON_32(hObj);
1750 /******************************************************************************
1751 * DrawIconEx (USER32.@) Draws an icon or cursor on device context
1753 * NOTES
1754 * Why is this using SM_CXICON instead of SM_CXCURSOR?
1756 * PARAMS
1757 * hdc [I] Handle to device context
1758 * x0 [I] X coordinate of upper left corner
1759 * y0 [I] Y coordinate of upper left corner
1760 * hIcon [I] Handle to icon to draw
1761 * cxWidth [I] Width of icon
1762 * cyWidth [I] Height of icon
1763 * istep [I] Index of frame in animated cursor
1764 * hbr [I] Handle to background brush
1765 * flags [I] Icon-drawing flags
1767 * RETURNS
1768 * Success: TRUE
1769 * Failure: FALSE
1771 BOOL WINAPI DrawIconEx( HDC hdc, INT x0, INT y0, HICON hIcon,
1772 INT cxWidth, INT cyWidth, UINT istep,
1773 HBRUSH hbr, UINT flags )
1775 CURSORICONINFO *ptr = (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon));
1776 HDC hDC_off = 0, hMemDC;
1777 BOOL result = FALSE, DoOffscreen;
1778 HBITMAP hB_off = 0, hOld = 0;
1780 if (!ptr) return FALSE;
1781 TRACE_(icon)("(hdc=%p,pos=%d.%d,hicon=%p,extend=%d.%d,istep=%d,br=%p,flags=0x%08x)\n",
1782 hdc,x0,y0,hIcon,cxWidth,cyWidth,istep,hbr,flags );
1784 hMemDC = CreateCompatibleDC (hdc);
1785 if (istep)
1786 FIXME_(icon)("Ignoring istep=%d\n", istep);
1787 if (flags & DI_COMPAT)
1788 FIXME_(icon)("Ignoring flag DI_COMPAT\n");
1790 if (!flags) {
1791 FIXME_(icon)("no flags set? setting to DI_NORMAL\n");
1792 flags = DI_NORMAL;
1795 /* Calculate the size of the destination image. */
1796 if (cxWidth == 0)
1798 if (flags & DI_DEFAULTSIZE)
1799 cxWidth = GetSystemMetrics (SM_CXICON);
1800 else
1801 cxWidth = ptr->nWidth;
1803 if (cyWidth == 0)
1805 if (flags & DI_DEFAULTSIZE)
1806 cyWidth = GetSystemMetrics (SM_CYICON);
1807 else
1808 cyWidth = ptr->nHeight;
1811 DoOffscreen = (GetObjectType( hbr ) == OBJ_BRUSH);
1813 if (DoOffscreen) {
1814 RECT r;
1816 r.left = 0;
1817 r.top = 0;
1818 r.right = cxWidth;
1819 r.bottom = cxWidth;
1821 hDC_off = CreateCompatibleDC(hdc);
1822 hB_off = CreateCompatibleBitmap(hdc, cxWidth, cyWidth);
1823 if (hDC_off && hB_off) {
1824 hOld = SelectObject(hDC_off, hB_off);
1825 FillRect(hDC_off, &r, hbr);
1829 if (hMemDC && (!DoOffscreen || (hDC_off && hB_off)))
1831 HBITMAP hXorBits, hAndBits;
1832 COLORREF oldFg, oldBg;
1833 INT nStretchMode;
1835 nStretchMode = SetStretchBltMode (hdc, STRETCH_DELETESCANS);
1837 hXorBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1838 ptr->bPlanes, ptr->bBitsPerPixel,
1839 (char *)(ptr + 1)
1840 + ptr->nHeight *
1841 get_bitmap_width_bytes(ptr->nWidth,1) );
1842 hAndBits = CreateBitmap ( ptr->nWidth, ptr->nHeight,
1843 1, 1, (char *)(ptr+1) );
1844 oldFg = SetTextColor( hdc, RGB(0,0,0) );
1845 oldBg = SetBkColor( hdc, RGB(255,255,255) );
1847 if (hXorBits && hAndBits)
1849 HBITMAP hBitTemp = SelectObject( hMemDC, hAndBits );
1850 if (flags & DI_MASK)
1852 if (DoOffscreen)
1853 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1854 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1855 else
1856 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1857 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCAND);
1859 SelectObject( hMemDC, hXorBits );
1860 if (flags & DI_IMAGE)
1862 if (DoOffscreen)
1863 StretchBlt (hDC_off, 0, 0, cxWidth, cyWidth,
1864 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1865 else
1866 StretchBlt (hdc, x0, y0, cxWidth, cyWidth,
1867 hMemDC, 0, 0, ptr->nWidth, ptr->nHeight, SRCPAINT);
1869 SelectObject( hMemDC, hBitTemp );
1870 result = TRUE;
1873 SetTextColor( hdc, oldFg );
1874 SetBkColor( hdc, oldBg );
1875 if (hXorBits) DeleteObject( hXorBits );
1876 if (hAndBits) DeleteObject( hAndBits );
1877 SetStretchBltMode (hdc, nStretchMode);
1878 if (DoOffscreen) {
1879 BitBlt(hdc, x0, y0, cxWidth, cyWidth, hDC_off, 0, 0, SRCCOPY);
1880 SelectObject(hDC_off, hOld);
1883 if (hMemDC) DeleteDC( hMemDC );
1884 if (hDC_off) DeleteDC(hDC_off);
1885 if (hB_off) DeleteObject(hB_off);
1886 GlobalUnlock16(HICON_16(hIcon));
1887 return result;
1890 /***********************************************************************
1891 * DIB_FixColorsToLoadflags
1893 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
1894 * are in loadflags
1896 static void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
1898 int colors;
1899 COLORREF c_W, c_S, c_F, c_L, c_C;
1900 int incr,i;
1901 RGBQUAD *ptr;
1903 if (bmi->bmiHeader.biBitCount > 8) return;
1904 if (bmi->bmiHeader.biSize == sizeof(BITMAPINFOHEADER)) incr = 4;
1905 else if (bmi->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) incr = 3;
1906 else {
1907 WARN_(resource)("Wrong bitmap header size!\n");
1908 return;
1910 colors = bmi->bmiHeader.biClrUsed;
1911 if (!colors && (bmi->bmiHeader.biBitCount <= 8))
1912 colors = 1 << bmi->bmiHeader.biBitCount;
1913 c_W = GetSysColor(COLOR_WINDOW);
1914 c_S = GetSysColor(COLOR_3DSHADOW);
1915 c_F = GetSysColor(COLOR_3DFACE);
1916 c_L = GetSysColor(COLOR_3DLIGHT);
1917 if (loadflags & LR_LOADTRANSPARENT) {
1918 switch (bmi->bmiHeader.biBitCount) {
1919 case 1: pix = pix >> 7; break;
1920 case 4: pix = pix >> 4; break;
1921 case 8: break;
1922 default:
1923 WARN_(resource)("(%d): Unsupported depth\n", bmi->bmiHeader.biBitCount);
1924 return;
1926 if (pix >= colors) {
1927 WARN_(resource)("pixel has color index greater than biClrUsed!\n");
1928 return;
1930 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
1931 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
1932 ptr->rgbBlue = GetBValue(c_W);
1933 ptr->rgbGreen = GetGValue(c_W);
1934 ptr->rgbRed = GetRValue(c_W);
1936 if (loadflags & LR_LOADMAP3DCOLORS)
1937 for (i=0; i<colors; i++) {
1938 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
1939 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
1940 if (c_C == RGB(128, 128, 128)) {
1941 ptr->rgbRed = GetRValue(c_S);
1942 ptr->rgbGreen = GetGValue(c_S);
1943 ptr->rgbBlue = GetBValue(c_S);
1944 } else if (c_C == RGB(192, 192, 192)) {
1945 ptr->rgbRed = GetRValue(c_F);
1946 ptr->rgbGreen = GetGValue(c_F);
1947 ptr->rgbBlue = GetBValue(c_F);
1948 } else if (c_C == RGB(223, 223, 223)) {
1949 ptr->rgbRed = GetRValue(c_L);
1950 ptr->rgbGreen = GetGValue(c_L);
1951 ptr->rgbBlue = GetBValue(c_L);
1957 /**********************************************************************
1958 * BITMAP_Load
1960 static HBITMAP BITMAP_Load( HINSTANCE instance,LPCWSTR name, UINT loadflags )
1962 HBITMAP hbitmap = 0;
1963 HRSRC hRsrc;
1964 HGLOBAL handle;
1965 char *ptr = NULL;
1966 BITMAPINFO *info, *fix_info=NULL;
1967 HGLOBAL hFix;
1968 int size;
1970 if (!(loadflags & LR_LOADFROMFILE))
1972 if (!instance)
1974 /* OEM bitmap: try to load the resource from user32.dll */
1975 if (HIWORD(name)) return 0;
1976 instance = user32_module;
1978 if (!(hRsrc = FindResourceW( instance, name, (LPWSTR)RT_BITMAP ))) return 0;
1979 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
1981 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
1983 else
1985 if (!(ptr = map_fileW( name ))) return 0;
1986 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
1988 size = bitmap_info_size(info, DIB_RGB_COLORS);
1989 if ((hFix = GlobalAlloc(0, size))) fix_info=GlobalLock(hFix);
1990 if (fix_info) {
1991 BYTE pix;
1993 memcpy(fix_info, info, size);
1994 pix = *((LPBYTE)info + size);
1995 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
1996 if (!screen_dc) screen_dc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
1997 if (screen_dc)
1999 char *bits = (char *)info + size;
2000 if (loadflags & LR_CREATEDIBSECTION) {
2001 DIBSECTION dib;
2002 hbitmap = CreateDIBSection(screen_dc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
2003 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
2004 SetDIBits(screen_dc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
2005 DIB_RGB_COLORS);
2007 else {
2008 hbitmap = CreateDIBitmap( screen_dc, &fix_info->bmiHeader, CBM_INIT,
2009 bits, fix_info, DIB_RGB_COLORS );
2012 GlobalUnlock(hFix);
2013 GlobalFree(hFix);
2015 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
2016 return hbitmap;
2019 /**********************************************************************
2020 * LoadImageA (USER32.@)
2022 * FIXME: implementation lacks some features, see LR_ defines in winuser.h
2025 /* filter for page-fault exceptions */
2026 static WINE_EXCEPTION_FILTER(page_fault)
2028 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
2029 return EXCEPTION_EXECUTE_HANDLER;
2030 return EXCEPTION_CONTINUE_SEARCH;
2033 /*********************************************************************/
2035 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
2036 INT desiredx, INT desiredy, UINT loadflags)
2038 HANDLE res;
2039 LPWSTR u_name;
2041 if (!HIWORD(name))
2042 return LoadImageW(hinst, (LPWSTR)name, type, desiredx, desiredy, loadflags);
2044 __TRY {
2045 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
2046 u_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2047 MultiByteToWideChar( CP_ACP, 0, name, -1, u_name, len );
2049 __EXCEPT(page_fault) {
2050 SetLastError( ERROR_INVALID_PARAMETER );
2051 return 0;
2053 __ENDTRY
2054 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
2055 HeapFree(GetProcessHeap(), 0, u_name);
2056 return res;
2060 /******************************************************************************
2061 * LoadImageW (USER32.@) Loads an icon, cursor, or bitmap
2063 * PARAMS
2064 * hinst [I] Handle of instance that contains image
2065 * name [I] Name of image
2066 * type [I] Type of image
2067 * desiredx [I] Desired width
2068 * desiredy [I] Desired height
2069 * loadflags [I] Load flags
2071 * RETURNS
2072 * Success: Handle to newly loaded image
2073 * Failure: NULL
2075 * FIXME: Implementation lacks some features, see LR_ defines in winuser.h
2077 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
2078 INT desiredx, INT desiredy, UINT loadflags )
2080 if (HIWORD(name)) {
2081 TRACE_(resource)("(%p,%p,%d,%d,%d,0x%08x)\n",
2082 hinst,name,type,desiredx,desiredy,loadflags);
2083 } else {
2084 TRACE_(resource)("(%p,%p,%d,%d,%d,0x%08x)\n",
2085 hinst,name,type,desiredx,desiredy,loadflags);
2087 if (loadflags & LR_DEFAULTSIZE) {
2088 if (type == IMAGE_ICON) {
2089 if (!desiredx) desiredx = GetSystemMetrics(SM_CXICON);
2090 if (!desiredy) desiredy = GetSystemMetrics(SM_CYICON);
2091 } else if (type == IMAGE_CURSOR) {
2092 if (!desiredx) desiredx = GetSystemMetrics(SM_CXCURSOR);
2093 if (!desiredy) desiredy = GetSystemMetrics(SM_CYCURSOR);
2096 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
2097 switch (type) {
2098 case IMAGE_BITMAP:
2099 return BITMAP_Load( hinst, name, loadflags );
2101 case IMAGE_ICON:
2102 if (!screen_dc) screen_dc = CreateDCW( DISPLAYW, NULL, NULL, NULL );
2103 if (screen_dc)
2105 UINT palEnts = GetSystemPaletteEntries(screen_dc, 0, 0, NULL);
2106 if (palEnts == 0) palEnts = 256;
2107 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2108 palEnts, FALSE, loadflags);
2110 break;
2112 case IMAGE_CURSOR:
2113 return CURSORICON_Load(hinst, name, desiredx, desiredy,
2114 1, TRUE, loadflags);
2116 return 0;
2119 /******************************************************************************
2120 * CopyImage (USER32.@) Creates new image and copies attributes to it
2122 * PARAMS
2123 * hnd [I] Handle to image to copy
2124 * type [I] Type of image to copy
2125 * desiredx [I] Desired width of new image
2126 * desiredy [I] Desired height of new image
2127 * flags [I] Copy flags
2129 * RETURNS
2130 * Success: Handle to newly created image
2131 * Failure: NULL
2133 * FIXME: implementation still lacks nearly all features, see LR_*
2134 * defines in winuser.h
2136 HICON WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
2137 INT desiredy, UINT flags )
2139 switch (type)
2141 case IMAGE_BITMAP:
2143 HBITMAP res;
2144 BITMAP bm;
2146 if (!GetObjectW( hnd, sizeof(bm), &bm )) return 0;
2147 bm.bmBits = NULL;
2148 if ((res = CreateBitmapIndirect(&bm)))
2150 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight );
2151 GetBitmapBits( hnd, bm.bmWidthBytes * bm.bmHeight, buf );
2152 SetBitmapBits( res, bm.bmWidthBytes * bm.bmHeight, buf );
2153 HeapFree( GetProcessHeap(), 0, buf );
2155 return (HICON)res;
2157 case IMAGE_ICON:
2158 return CURSORICON_ExtCopy(hnd,type, desiredx, desiredy, flags);
2159 case IMAGE_CURSOR:
2160 /* Should call CURSORICON_ExtCopy but more testing
2161 * needs to be done before we change this
2163 return CopyCursor(hnd);
2165 return 0;
2169 /******************************************************************************
2170 * LoadBitmapW (USER32.@) Loads bitmap from the executable file
2172 * RETURNS
2173 * Success: Handle to specified bitmap
2174 * Failure: NULL
2176 HBITMAP WINAPI LoadBitmapW(
2177 HINSTANCE instance, /* [in] Handle to application instance */
2178 LPCWSTR name) /* [in] Address of bitmap resource name */
2180 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
2183 /**********************************************************************
2184 * LoadBitmapA (USER32.@)
2186 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
2188 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );