3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * PROJECT: ReactOS user32.dll
21 * FILE: dll/win32/user32/windows/icon.c
23 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
25 * 09-05-2001 CSH Created
28 /* INCLUDES ******************************************************************/
32 #include <wine/debug.h>
33 WINE_DEFAULT_DEBUG_CHANNEL(user32
);
35 /* FUNCTIONS *****************************************************************/
39 CreateCursorIconFromData(HDC hDC
, PVOID ImageData
, ICONIMAGE
* IconImage
, int cxDesired
, int cyDesired
, int xHotspot
, int yHotspot
, BOOL fIcon
)
41 BYTE BitmapInfoBuffer
[sizeof(BITMAPINFOHEADER
) + 2 * sizeof(RGBQUAD
)];
42 BITMAPINFO
*bwBIH
= (BITMAPINFO
*)BitmapInfoBuffer
;
45 IconInfo
.fIcon
= fIcon
;
46 IconInfo
.xHotspot
= xHotspot
;
47 IconInfo
.yHotspot
= yHotspot
;
49 if (IconImage
->icHeader
.biBitCount
== 1)
51 IconInfo
.hbmColor
= (HBITMAP
)0;
52 IconImage
->icHeader
.biHeight
*= 2;
53 IconInfo
.hbmMask
= CreateDIBitmap(hDC
, &IconImage
->icHeader
, CBM_INIT
,
54 ImageData
, (BITMAPINFO
*)IconImage
,
59 /* Create the XOR bitmap */
60 IconInfo
.hbmColor
= CreateDIBitmap(hDC
, &IconImage
->icHeader
, CBM_INIT
,
61 ImageData
, (BITMAPINFO
*)IconImage
,
64 /* Make ImageData point to the start of the AND image data. */
65 ImageData
= ((PBYTE
)ImageData
) + (((IconImage
->icHeader
.biWidth
*
66 IconImage
->icHeader
.biBitCount
+ 31) & ~31) >> 3) *
67 (IconImage
->icHeader
.biHeight
);
69 /* Create a BITMAPINFO header for the monochrome part of the icon. */
70 bwBIH
->bmiHeader
.biBitCount
= 1;
71 bwBIH
->bmiHeader
.biWidth
= IconImage
->icHeader
.biWidth
;
72 bwBIH
->bmiHeader
.biHeight
= IconImage
->icHeader
.biHeight
;
73 bwBIH
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
74 bwBIH
->bmiHeader
.biPlanes
= 1;
75 bwBIH
->bmiHeader
.biSizeImage
= 0;
76 bwBIH
->bmiHeader
.biCompression
= BI_RGB
;
77 bwBIH
->bmiHeader
.biClrImportant
= 2;
78 bwBIH
->bmiHeader
.biClrUsed
= 2;
79 bwBIH
->bmiHeader
.biXPelsPerMeter
= 0;
80 bwBIH
->bmiHeader
.biYPelsPerMeter
= 0;
82 bwBIH
->bmiColors
[0].rgbBlue
= 0;
83 bwBIH
->bmiColors
[0].rgbGreen
= 0;
84 bwBIH
->bmiColors
[0].rgbRed
= 0;
85 bwBIH
->bmiColors
[0].rgbReserved
= 0;
87 bwBIH
->bmiColors
[1].rgbBlue
= 0xff;
88 bwBIH
->bmiColors
[1].rgbGreen
= 0xff;
89 bwBIH
->bmiColors
[1].rgbRed
= 0xff;
90 bwBIH
->bmiColors
[1].rgbReserved
= 0;
92 /* Create the AND bitmap. */
93 IconInfo
.hbmMask
= CreateDIBitmap(hDC
, &bwBIH
->bmiHeader
, 0,
94 ImageData
, bwBIH
, DIB_RGB_COLORS
);
96 SetDIBits(hDC
, IconInfo
.hbmMask
, 0, IconImage
->icHeader
.biHeight
,
97 ImageData
, bwBIH
, DIB_RGB_COLORS
);
101 /* Create the icon based on everything we have so far */
102 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
110 CopyIcon(HICON hIcon
)
112 HICON hRetIcon
= NULL
;
115 if(GetIconInfo(hIcon
, &IconInfo
))
117 hRetIcon
= CreateIconIndirect(&IconInfo
);
118 DeleteObject(IconInfo
.hbmColor
);
119 DeleteObject(IconInfo
.hbmMask
);
142 IconInfo
.fIcon
= TRUE
;
148 IconInfo
.hbmMask
= CreateBitmap(nWidth
, nHeight
, 1, 1, ANDbits
);
149 if(!IconInfo
.hbmMask
)
156 IconInfo
.hbmColor
= (HBITMAP
)0;
160 IconInfo
.hbmColor
= CreateBitmap(nWidth
, nHeight
, cPlanes
, cBitsPixel
, XORbits
);
161 if(!IconInfo
.hbmColor
)
163 DeleteObject(IconInfo
.hbmMask
);
168 return NtUserCreateCursorIconHandle(&IconInfo
, FALSE
);
177 CreateIconFromResource(
183 return CreateIconFromResourceEx(presbits
, dwResSize
, fIcon
, dwVer
, 0, 0, LR_DEFAULTSIZE
|LR_SHARED
);
192 CreateIconFromResourceEx(
201 ICONIMAGE
* SafeIconImage
;
211 FIXME - does win support LR_SHARED? According to msdn it does but we don't
212 have useful information to identify the icon
213 if (uFlags & LR_SHARED)
215 DbgPrint("FIXME: need LR_SHARED support in CreateIconFromResourceEx()\n");
219 TRACE("dwVersion, cxDesired, cyDesired are all ignored in this implementation!\n");
223 wXHotspot
= *(WORD
*)pbIconBits
;
224 pbIconBits
+= sizeof(WORD
);
225 wYHotspot
= *(WORD
*)pbIconBits
;
226 pbIconBits
+= sizeof(WORD
);
227 cbIconBits
-= 2 * sizeof(WORD
);
231 wXHotspot
= cxDesired
/ 2;
232 wYHotspot
= cyDesired
/ 2;
235 /* get an safe copy of the icon data */
236 SafeIconImage
= RtlAllocateHeap(GetProcessHeap(), 0, cbIconBits
);
237 if (SafeIconImage
== NULL
)
241 memcpy(SafeIconImage
, pbIconBits
, cbIconBits
);
243 /* Take into acount the original height was for both the AND and XOR images */
244 SafeIconImage
->icHeader
.biHeight
/= 2;
246 if (SafeIconImage
->icHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
248 BITMAPCOREHEADER
* Core
= (BITMAPCOREHEADER
*)SafeIconImage
;
249 ColourCount
= (Core
->bcBitCount
<= 8) ? (1 << Core
->bcBitCount
) : 0;
250 HeaderSize
= sizeof(BITMAPCOREHEADER
) + ColourCount
* sizeof(RGBTRIPLE
);
254 ColourCount
= (SafeIconImage
->icHeader
.biBitCount
<= 8) ?
255 (1 << SafeIconImage
->icHeader
.biBitCount
) : 0;
256 HeaderSize
= sizeof(BITMAPINFOHEADER
) + ColourCount
* sizeof(RGBQUAD
);
259 /* make data point to the start of the XOR image data */
260 Data
= (PBYTE
)SafeIconImage
+ HeaderSize
;
262 /* get a handle to the screen dc, the icon we create is going to be compatable with this */
263 // FIXME!!! This is a victim of the Win32k Initialization BUG!!!!!
264 //hScreenDc = CreateDCW(NULL, NULL, NULL, NULL);
265 hScreenDc
= CreateCompatibleDC(NULL
);
266 if (hScreenDc
== NULL
)
268 RtlFreeHeap(GetProcessHeap(), 0, SafeIconImage
);
272 hIcon
= CreateCursorIconFromData(hScreenDc
, Data
, SafeIconImage
, cxDesired
, cyDesired
, wXHotspot
, wYHotspot
, fIcon
);
273 RtlFreeHeap(GetProcessHeap(), 0, SafeIconImage
);
285 CreateIconIndirect(PICONINFO IconInfo
)
293 SetLastError(ERROR_INVALID_PARAMETER
);
297 if(!GetObjectW(IconInfo
->hbmMask
, sizeof(BITMAP
), &MaskBitmap
))
302 /* Try to get color bitmap */
303 if (GetObjectW(IconInfo
->hbmColor
, sizeof(BITMAP
), &ColorBitmap
))
305 /* Compare size of color and mask bitmap*/
306 if (ColorBitmap
.bmWidth
!= MaskBitmap
.bmWidth
||
307 ColorBitmap
.bmHeight
!= MaskBitmap
.bmHeight
)
309 ERR("Color and mask size are different!");
310 SetLastError(ERROR_INVALID_PARAMETER
);
313 /* Check if color and mask are switched and switch them back */
314 if (MaskBitmap
.bmBitsPixel
!= 1 && ColorBitmap
.bmBitsPixel
== 1)
316 hbmTemp
= IconInfo
->hbmMask
;
317 IconInfo
->hbmMask
= IconInfo
->hbmColor
;
318 IconInfo
->hbmColor
= hbmTemp
;
321 return (HICON
)NtUserCreateCursorIconHandle(IconInfo
, TRUE
);
333 return (BOOL
)NtUserDestroyCursor((HANDLE
)hIcon
, 0);
348 return DrawIconEx(hDC
, X
, Y
, hIcon
, 0, 0, 0, NULL
, DI_NORMAL
|DI_DEFAULTSIZE
|DI_COMPAT
);
364 HBRUSH hbrFlickerFreeDraw
,
367 return (BOOL
)NtUserDrawIconEx(hdc
, xLeft
, yTop
, hIcon
, cxWidth
, cyWidth
,
368 istepIfAniCur
, hbrFlickerFreeDraw
, diFlags
,
382 return NtUserGetIconInfo((HANDLE
)hIcon
, IconInfo
, 0, 0, 0, 0);
395 return(LoadImageA(hInstance
, lpIconName
, IMAGE_ICON
, 0, 0, LR_SHARED
| LR_DEFAULTSIZE
));
408 return(LoadImageW(hInstance
, lpIconName
, IMAGE_ICON
, 0, 0, LR_SHARED
| LR_DEFAULTSIZE
));
417 LookupIconIdFromDirectory(
421 return LookupIconIdFromDirectoryEx(presbits
,
423 fIcon
? GetSystemMetrics(SM_CXICON
) : GetSystemMetrics(SM_CXCURSOR
),
424 fIcon
? GetSystemMetrics(SM_CYICON
) : GetSystemMetrics(SM_CYCURSOR
),
433 * The following macro function accounts for the irregularities of
434 * accessing cursor and icon resources in files and resource entries.
437 (*fnGetCIEntry
)(LPVOID dir
, int n
, int *width
, int *height
, int *bits
);
439 /**********************************************************************
440 * CURSORICON_FindBestIcon
442 * Find the icon closest to the requested size and number of colors.
445 CURSORICON_FindBestIcon(LPVOID dir
,
446 fnGetCIEntry get_entry
,
451 int i
, cx
, cy
, Bits
, BestBits
= 0, BestEntry
= -1;
452 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
453 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
456 iTotalDiff
= 0xFFFFFFFF;
457 iColorDiff
= 0xFFFFFFFF;
458 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
460 iTempXDiff
= abs(Width
- cx
);
461 iTempYDiff
= abs(Height
- cy
);
463 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
467 iTotalDiff
= iXDiff
+ iYDiff
;
471 /* Find Best Colors for Best Fit */
472 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
474 if(abs(Width
- cx
) == iXDiff
&& abs(Height
- cy
) == iYDiff
)
476 iTempColorDiff
= abs(ColorBits
- Bits
);
477 if(iColorDiff
> iTempColorDiff
)
481 iColorDiff
= iTempColorDiff
;
486 TRACE("Best Icon: ResId: %d, bits : %d\n", BestEntry
, BestBits
);
493 /**********************************************************************
494 * CURSORICON_FindBestCursor
496 * Find the cursor closest to the requested size.
497 * FIXME: parameter 'color' ignored and entries with more than 1 bpp
501 CURSORICON_FindBestCursor(LPVOID dir
,
502 fnGetCIEntry get_entry
,
507 int i
, cx
, cy
, Bits
, BestBits
= 0, BestEntry
= -1;
508 UINT iTotalDiff
, iXDiff
=0, iYDiff
=0, iColorDiff
;
509 UINT iTempXDiff
, iTempYDiff
, iTempColorDiff
;
512 iTotalDiff
= 0xFFFFFFFF;
513 iColorDiff
= 0xFFFFFFFF;
514 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
516 iTempXDiff
= abs(Width
- cx
);
517 iTempYDiff
= abs(Height
- cy
);
519 if(iTotalDiff
> (iTempXDiff
+ iTempYDiff
))
523 iTotalDiff
= iXDiff
+ iYDiff
;
527 /* Find Best Colors for Best Fit */
528 for (i
= 0; get_entry(dir
, i
, &cx
, &cy
, &Bits
); i
++ )
530 if(abs(Width
- cx
) == iXDiff
&& abs(Height
- cy
) == iYDiff
)
532 iTempColorDiff
= abs(ColorBits
- Bits
);
533 if(iColorDiff
> iTempColorDiff
)
537 iColorDiff
= iTempColorDiff
;
542 TRACE("Best Cursor: ResId: %d, bits : %d\n", BestEntry
, BestBits
);
549 CURSORICON_GetResIconEntry(LPVOID dir
,
555 GRPCURSORICONDIR
*ResDir
= dir
;
558 if (ResDir
->idCount
<= n
)
561 Icon
= &ResDir
->idEntries
[n
].ResInfo
.icon
;
562 *Width
= Icon
->bWidth
;
563 *Height
= Icon
->bHeight
;
564 *Bits
= ResDir
->idEntries
[n
].wBitCount
;
569 CURSORICON_GetResCursorEntry(LPVOID dir
,
575 GRPCURSORICONDIR
*ResDir
= dir
;
576 CURSORRESDIR
*Cursor
;
578 if (ResDir
->idCount
<= n
)
581 Cursor
= &ResDir
->idEntries
[n
].ResInfo
.cursor
;
582 *Width
= Cursor
->wWidth
;
583 *Height
= Cursor
->wHeight
;
584 *Bits
= ResDir
->idEntries
[n
].wBitCount
;
588 static GRPCURSORICONDIRENTRY
*
589 CURSORICON_FindBestIconRes(GRPCURSORICONDIR
* dir
,
595 n
= CURSORICON_FindBestIcon(dir
,
596 CURSORICON_GetResIconEntry
,
603 return &dir
->idEntries
[n
];
606 static GRPCURSORICONDIRENTRY
*
607 CURSORICON_FindBestCursorRes(GRPCURSORICONDIR
*dir
,
613 n
= CURSORICON_FindBestCursor(dir
,
614 CURSORICON_GetResCursorEntry
,
621 return &dir
->idEntries
[n
];
626 LookupIconIdFromDirectoryEx(PBYTE xdir
,
632 GRPCURSORICONDIR
*dir
= (GRPCURSORICONDIR
*)xdir
;
637 if(dir
&& !dir
->idReserved
&& (IMAGE_ICON
== dir
->idType
|| IMAGE_CURSOR
== dir
->idType
))
639 GRPCURSORICONDIRENTRY
*entry
= NULL
;
642 if (cFlag
& LR_MONOCHROME
)
646 else if (cFlag
& LR_VGACOLOR
)
652 ColorBits
= gpsi
->BitsPixel
;
656 entry
= CURSORICON_FindBestIconRes(dir
, width
, height
, ColorBits
);
658 entry
= CURSORICON_FindBestCursorRes(dir
, width
, height
, 1);
664 WARN("%s() : Invalid resource directory\n", __FUNCTION__
);