2 * shell icon cache (SIC)
4 * Copyright 1998, 1999 Juergen Schmied
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
34 #include "wine/winuser16.h"
35 #include "wine/winbase16.h"
37 #include "wine/debug.h"
43 #include "shell32_main.h"
44 #include "undocshell.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
49 /********************** THE ICON CACHE ********************************/
51 #define INVALID_INDEX -1
55 LPSTR sSourceFile
; /* file (not path!) containing the icon */
56 DWORD dwSourceIndex
; /* index within the file, if it is a resoure ID it will be negated */
57 DWORD dwListIndex
; /* index within the iconlist */
58 DWORD dwFlags
; /* GIL_* flags */
60 } SIC_ENTRY
, * LPSIC_ENTRY
;
62 static HDPA sic_hdpa
= 0;
64 static CRITICAL_SECTION SHELL32_SicCS
;
65 static CRITICAL_SECTION_DEBUG critsect_debug
=
68 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
69 0, 0, { 0, (DWORD
)(__FILE__
": SHELL32_SicCS") }
71 static CRITICAL_SECTION SHELL32_SicCS
= { &critsect_debug
, -1, 0, 0, 0, 0 };
73 /*****************************************************************************
77 * Callback for DPA_Search
79 static INT CALLBACK
SIC_CompareEntries( LPVOID p1
, LPVOID p2
, LPARAM lparam
)
80 { TRACE("%p %p %8lx\n", p1
, p2
, lparam
);
82 if (((LPSIC_ENTRY
)p1
)->dwSourceIndex
!= ((LPSIC_ENTRY
)p2
)->dwSourceIndex
) /* first the faster one*/
85 if (strcasecmp(((LPSIC_ENTRY
)p1
)->sSourceFile
,((LPSIC_ENTRY
)p2
)->sSourceFile
))
90 /*****************************************************************************
91 * SIC_IconAppend [internal]
94 * appends a icon pair to the end of the cache
96 static INT
SIC_IconAppend (LPCSTR sSourceFile
, INT dwSourceIndex
, HICON hSmallIcon
, HICON hBigIcon
)
98 INT ret
, index
, index1
;
100 TRACE("%s %i %p %p\n", sSourceFile
, dwSourceIndex
, hSmallIcon
,hBigIcon
);
102 lpsice
= (LPSIC_ENTRY
) SHAlloc (sizeof (SIC_ENTRY
));
104 path
= PathFindFileNameA(sSourceFile
);
105 lpsice
->sSourceFile
= HeapAlloc( GetProcessHeap(), 0, strlen(path
)+1 );
106 strcpy( lpsice
->sSourceFile
, path
);
108 lpsice
->dwSourceIndex
= dwSourceIndex
;
110 EnterCriticalSection(&SHELL32_SicCS
);
112 index
= DPA_InsertPtr(sic_hdpa
, 0x7fff, lpsice
);
113 if ( INVALID_INDEX
== index
)
120 index
= ImageList_AddIcon (ShellSmallIconList
, hSmallIcon
);
121 index1
= ImageList_AddIcon (ShellBigIconList
, hBigIcon
);
125 FIXME("iconlists out of sync 0x%x 0x%x\n", index
, index1
);
127 lpsice
->dwListIndex
= index
;
128 ret
= lpsice
->dwListIndex
;
131 LeaveCriticalSection(&SHELL32_SicCS
);
134 /****************************************************************************
135 * SIC_LoadIcon [internal]
138 * gets small/big icon by number from a file
140 static INT
SIC_LoadIcon (LPCSTR sSourceFile
, INT dwSourceIndex
)
141 { HICON hiconLarge
=0;
144 PrivateExtractIconsA( sSourceFile
, dwSourceIndex
, 32, 32, &hiconLarge
, 0, 1, 0 );
145 PrivateExtractIconsA( sSourceFile
, dwSourceIndex
, 16, 16, &hiconSmall
, 0, 1, 0 );
147 if ( !hiconLarge
|| !hiconSmall
)
149 WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex
, sSourceFile
, hiconLarge
, hiconSmall
);
152 return SIC_IconAppend (sSourceFile
, dwSourceIndex
, hiconSmall
, hiconLarge
);
154 /*****************************************************************************
155 * SIC_GetIconIndex [internal]
158 * sSourceFile [IN] filename of file containing the icon
159 * index [IN] index/resID (negated) in this file
162 * look in the cache for a proper icon. if not available the icon is taken
163 * from the file and cached
165 INT
SIC_GetIconIndex (LPCSTR sSourceFile
, INT dwSourceIndex
)
167 INT ret
, index
= INVALID_INDEX
;
169 TRACE("%s %i\n", sSourceFile
, dwSourceIndex
);
171 sice
.sSourceFile
= PathFindFileNameA(sSourceFile
);
172 sice
.dwSourceIndex
= dwSourceIndex
;
174 EnterCriticalSection(&SHELL32_SicCS
);
176 if (NULL
!= DPA_GetPtr (sic_hdpa
, 0))
178 /* search linear from position 0*/
179 index
= DPA_Search (sic_hdpa
, &sice
, 0, SIC_CompareEntries
, 0, 0);
182 if ( INVALID_INDEX
== index
)
184 ret
= SIC_LoadIcon (sSourceFile
, dwSourceIndex
);
189 ret
= ((LPSIC_ENTRY
)DPA_GetPtr(sic_hdpa
, index
))->dwListIndex
;
192 LeaveCriticalSection(&SHELL32_SicCS
);
195 /****************************************************************************
196 * SIC_GetIcon [internal]
199 * retrieves the specified icon from the iconcache. if not found tries to load the icon
201 static HICON WINE_UNUSED
SIC_GetIcon (LPCSTR sSourceFile
, INT dwSourceIndex
, BOOL bSmallIcon
)
204 TRACE("%s %i\n", sSourceFile
, dwSourceIndex
);
206 index
= SIC_GetIconIndex(sSourceFile
, dwSourceIndex
);
208 if (INVALID_INDEX
== index
)
210 return (HICON
)INVALID_INDEX
;
214 return ImageList_GetIcon(ShellSmallIconList
, index
, ILD_NORMAL
);
215 return ImageList_GetIcon(ShellBigIconList
, index
, ILD_NORMAL
);
218 /*****************************************************************************
219 * SIC_Initialize [internal]
222 * hack to load the resources from the shell32.dll under a different dll name
223 * will be removed when the resource-compiler is ready
225 BOOL
SIC_Initialize(void)
232 if (sic_hdpa
) /* already initialized?*/
235 sic_hdpa
= DPA_Create(16);
242 ShellSmallIconList
= ImageList_Create(16,16,ILC_COLORDDB
| ILC_MASK
,0,0x20);
243 ShellBigIconList
= ImageList_Create(32,32,ILC_COLORDDB
| ILC_MASK
,0,0x20);
245 ImageList_SetBkColor(ShellSmallIconList
, GetSysColor(COLOR_WINDOW
));
246 ImageList_SetBkColor(ShellBigIconList
, GetSysColor(COLOR_WINDOW
));
248 for (index
=1; index
<39; index
++)
250 hSm
= LoadImageA(shell32_hInstance
, MAKEINTRESOURCEA(index
), IMAGE_ICON
, 16, 16,LR_SHARED
);
251 hLg
= LoadImageA(shell32_hInstance
, MAKEINTRESOURCEA(index
), IMAGE_ICON
, 32, 32,LR_SHARED
);
255 hSm
= LoadImageA(shell32_hInstance
, MAKEINTRESOURCEA(0), IMAGE_ICON
, 16, 16,LR_SHARED
);
256 hLg
= LoadImageA(shell32_hInstance
, MAKEINTRESOURCEA(0), IMAGE_ICON
, 32, 32,LR_SHARED
);
258 SIC_IconAppend ("shell32.dll", index
, hSm
, hLg
);
261 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList
, ShellBigIconList
);
265 /*************************************************************************
270 static INT CALLBACK
sic_free( LPVOID ptr
, LPVOID lparam
)
276 void SIC_Destroy(void)
280 EnterCriticalSection(&SHELL32_SicCS
);
282 if (sic_hdpa
) DPA_DestroyCallback(sic_hdpa
, sic_free
, NULL
);
286 LeaveCriticalSection(&SHELL32_SicCS
);
287 DeleteCriticalSection(&SHELL32_SicCS
);
289 /*************************************************************************
290 * Shell_GetImageList [SHELL32.71]
293 * imglist[1|2] [OUT] pointer which recive imagelist handles
296 BOOL WINAPI
Shell_GetImageList(HIMAGELIST
* lpBigList
, HIMAGELIST
* lpSmallList
)
297 { TRACE("(%p,%p)\n",lpBigList
,lpSmallList
);
299 { *lpBigList
= ShellBigIconList
;
302 { *lpSmallList
= ShellSmallIconList
;
307 /*************************************************************************
308 * PidlToSicIndex [INTERNAL]
311 * sh [IN] IShellFolder
315 * pIndex [OUT] index within the SIC
318 BOOL
PidlToSicIndex (
326 char szIconFile
[MAX_PATH
]; /* file containing the icon */
327 INT iSourceIndex
; /* index or resID(negated) in this file */
331 TRACE("sf=%p pidl=%p %s\n", sh
, pidl
, bBigIcon
?"Big":"Small");
333 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh
, 0, 1, &pidl
, &IID_IExtractIconA
, 0, (void **)&ei
)))
335 if (SUCCEEDED(IExtractIconA_GetIconLocation(ei
, uFlags
, szIconFile
, MAX_PATH
, &iSourceIndex
, &dwFlags
)))
337 *pIndex
= SIC_GetIconIndex(szIconFile
, iSourceIndex
);
340 IExtractIconA_Release(ei
);
343 if (INVALID_INDEX
== *pIndex
) /* default icon when failed */
350 /*************************************************************************
351 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
354 * sh [IN] pointer to an instance of IShellFolder
356 * pIndex [OUT][OPTIONAL] SIC index for big icon
359 int WINAPI
SHMapPIDLToSystemImageListIndex(
366 TRACE("(SF=%p,pidl=%p,%p)\n",sh
,pidl
,pIndex
);
370 PidlToSicIndex ( sh
, pidl
, 1, 0, pIndex
);
371 PidlToSicIndex ( sh
, pidl
, 0, 0, &Index
);
375 /*************************************************************************
376 * Shell_GetCachedImageIndex [SHELL32.72]
379 INT WINAPI
Shell_GetCachedImageIndexA(LPCSTR szPath
, INT nIndex
, BOOL bSimulateDoc
)
381 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath
), nIndex
, bSimulateDoc
);
382 return SIC_GetIconIndex(szPath
, nIndex
);
385 INT WINAPI
Shell_GetCachedImageIndexW(LPCWSTR szPath
, INT nIndex
, BOOL bSimulateDoc
)
387 LPSTR sTemp
= HEAP_strdupWtoA (GetProcessHeap(),0,szPath
);
389 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath
), nIndex
, bSimulateDoc
);
391 ret
= SIC_GetIconIndex(sTemp
, nIndex
);
392 HeapFree(GetProcessHeap(),0,sTemp
);
396 INT WINAPI
Shell_GetCachedImageIndexAW(LPCVOID szPath
, INT nIndex
, BOOL bSimulateDoc
)
397 { if( SHELL_OsIsUnicode())
398 return Shell_GetCachedImageIndexW(szPath
, nIndex
, bSimulateDoc
);
399 return Shell_GetCachedImageIndexA(szPath
, nIndex
, bSimulateDoc
);
402 /*************************************************************************
403 * ExtractIconEx [SHELL32.@]
405 UINT WINAPI
ExtractIconExAW(LPCVOID lpszFile
, INT nIconIndex
, HICON
* phiconLarge
, HICON
* phiconSmall
, UINT nIcons
)
406 { if (SHELL_OsIsUnicode())
407 return ExtractIconExW ( lpszFile
, nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
408 return ExtractIconExA ( lpszFile
, nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
411 /*************************************************************************
412 * ExtractIconExW [SHELL32.@]
415 * -1 file is not valid
416 * or number of icons extracted
418 UINT WINAPI
ExtractIconExW(LPCWSTR lpszFile
, INT nIconIndex
, HICON
* phiconLarge
, HICON
* phiconSmall
, UINT nIcons
)
420 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile
), nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
422 return PrivateExtractIconExW(lpszFile
, nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
425 /*************************************************************************
426 * ExtractIconExA [SHELL32.@]
428 UINT WINAPI
ExtractIconExA(LPCSTR lpszFile
, INT nIconIndex
, HICON
* phiconLarge
, HICON
* phiconSmall
, UINT nIcons
)
431 INT len
= MultiByteToWideChar(CP_ACP
, 0, lpszFile
, -1, NULL
, 0);
432 LPWSTR lpwstrFile
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
434 TRACE("%s %i %p %p %i\n", lpszFile
, nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
436 MultiByteToWideChar(CP_ACP
, 0, lpszFile
, -1, lpwstrFile
, len
);
437 ret
= ExtractIconExW (lpwstrFile
, nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
438 HeapFree(GetProcessHeap(), 0, lpwstrFile
);
442 /*************************************************************************
443 * ExtractAssociatedIconA (SHELL32.@)
445 * Return icon for given file (either from file itself or from associated
446 * executable) and patch parameters if needed.
448 HICON WINAPI
ExtractAssociatedIconA(HINSTANCE hInst
, LPSTR lpIconPath
, LPWORD lpiIcon
)
456 lpiIcon
= &wDummyIcon
;
458 hIcon
= ExtractIconA(hInst
, lpIconPath
, *lpiIcon
);
460 if( hIcon
< (HICON
)2 )
461 { if( hIcon
== (HICON
)1 ) /* no icons found in given file */
462 { char tempPath
[0x80];
463 HINSTANCE uRet
= FindExecutableA(lpIconPath
,NULL
,tempPath
);
465 if( uRet
> (HINSTANCE
)32 && tempPath
[0] )
466 { strcpy(lpIconPath
,tempPath
);
467 hIcon
= ExtractIconA(hInst
, lpIconPath
, *lpiIcon
);
468 if( hIcon
> (HICON
)2 )
474 if( hIcon
== (HICON
)1 )
475 *lpiIcon
= 2; /* MSDOS icon - we found .exe but no icons in it */
477 *lpiIcon
= 6; /* generic icon - found nothing */
479 GetModuleFileNameA(hInst
, lpIconPath
, 0x80);
480 hIcon
= LoadIconA( hInst
, MAKEINTRESOURCEA(*lpiIcon
));
485 /*************************************************************************
486 * ExtractAssociatedIconExW (SHELL32.@)
488 * Return icon for given file (either from file itself or from associated
489 * executable) and patch parameters if needed.
491 HICON WINAPI
ExtractAssociatedIconExW(HINSTANCE hInst
, LPWSTR lpIconPath
, LPWORD lpiIconIdx
, LPWORD lpiIconId
)
493 FIXME("%p %s %p %p): stub\n", hInst
, debugstr_w(lpIconPath
), lpiIconIdx
, lpiIconId
);
497 /*************************************************************************
498 * ExtractAssociatedIconExA (SHELL32.@)
500 * Return icon for given file (either from file itself or from associated
501 * executable) and patch parameters if needed.
503 HICON WINAPI
ExtractAssociatedIconExA(HINSTANCE hInst
, LPSTR lpIconPath
, LPWORD lpiIconIdx
, LPWORD lpiIconId
)
506 INT len
= MultiByteToWideChar( CP_ACP
, 0, lpIconPath
, -1, NULL
, 0 );
507 LPWSTR lpwstrFile
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
509 TRACE("%p %s %p %p)\n", hInst
, lpIconPath
, lpiIconIdx
, lpiIconId
);
511 MultiByteToWideChar( CP_ACP
, 0, lpIconPath
, -1, lpwstrFile
, len
);
512 ret
= ExtractAssociatedIconExW(hInst
, lpwstrFile
, lpiIconIdx
, lpiIconId
);
513 HeapFree(GetProcessHeap(), 0, lpwstrFile
);
518 /****************************************************************************
519 * SHDefExtractIconW [SHELL32.@]
521 HRESULT WINAPI
SHDefExtractIconW(LPCWSTR pszIconFile
, int iIndex
, UINT uFlags
,
522 HICON
* phiconLarge
, HICON
* phiconSmall
, UINT nIconSize
)
526 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile
), iIndex
, uFlags
, phiconLarge
, phiconSmall
, nIconSize
);
528 ret
= PrivateExtractIconsW(pszIconFile
, iIndex
, nIconSize
, nIconSize
, hIcons
, NULL
, 2, LR_DEFAULTCOLOR
);
529 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
530 if (ret
== 0xFFFFFFFF)
533 *phiconLarge
= hIcons
[0];
534 *phiconSmall
= hIcons
[1];
540 /****************************************************************************
541 * SHDefExtractIconA [SHELL32.@]
543 HRESULT WINAPI
SHDefExtractIconA(LPCSTR pszIconFile
, int iIndex
, UINT uFlags
,
544 HICON
* phiconLarge
, HICON
* phiconSmall
, UINT nIconSize
)
547 INT len
= MultiByteToWideChar(CP_ACP
, 0, pszIconFile
, -1, NULL
, 0);
548 LPWSTR lpwstrFile
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
550 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile
, iIndex
, uFlags
, phiconLarge
, phiconSmall
, nIconSize
);
552 MultiByteToWideChar(CP_ACP
, 0, pszIconFile
, -1, lpwstrFile
, len
);
553 ret
= SHDefExtractIconW(lpwstrFile
, iIndex
, uFlags
, phiconLarge
, phiconSmall
, nIconSize
);
554 HeapFree(GetProcessHeap(), 0, lpwstrFile
);