2 * shell icon cache (SIC)
13 #include "wine/winuser16.h"
14 #include "wine/winbase16.h"
16 #include "debugtools.h"
21 #include "shell32_main.h"
22 #include "undocshell.h"
25 DEFAULT_DEBUG_CHANNEL(shell);
27 /********************** THE ICON CACHE ********************************/
29 #define INVALID_INDEX -1
33 LPSTR sSourceFile; /* file (not path!) containing the icon */
34 DWORD dwSourceIndex; /* index within the file, if it is a resoure ID it will be negated */
35 DWORD dwListIndex; /* index within the iconlist */
36 DWORD dwFlags; /* GIL_* flags */
38 } SIC_ENTRY, * LPSIC_ENTRY;
40 static HDPA sic_hdpa = 0;
41 static CRITICAL_SECTION SHELL32_SicCS = CRITICAL_SECTION_INIT("SHELL32_SicCS");
43 /*****************************************************************************
47 * Callback for DPA_Search
49 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
50 { TRACE("%p %p\n", p1, p2);
52 if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
55 if (strcasecmp(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
60 /*****************************************************************************
61 * SIC_IconAppend [internal]
64 * appends a icon pair to the end of the cache
66 static INT SIC_IconAppend (LPCSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
68 INT ret, index, index1;
70 TRACE("%s %i %x %x\n", sSourceFile, dwSourceIndex, hSmallIcon ,hBigIcon);
72 lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
74 path = PathFindFileNameA(sSourceFile);
75 lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, strlen(path)+1 );
76 strcpy( lpsice->sSourceFile, path );
78 lpsice->dwSourceIndex = dwSourceIndex;
80 EnterCriticalSection(&SHELL32_SicCS);
82 index = pDPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
83 if ( INVALID_INDEX == index )
90 index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
91 index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
95 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
97 lpsice->dwListIndex = index;
98 ret = lpsice->dwListIndex;
101 LeaveCriticalSection(&SHELL32_SicCS);
104 /****************************************************************************
105 * SIC_LoadIcon [internal]
108 * gets small/big icon by number from a file
110 static INT SIC_LoadIcon (LPCSTR sSourceFile, INT dwSourceIndex)
111 { HICON hiconLarge=0;
114 PrivateExtractIconsA( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 );
115 PrivateExtractIconsA( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
117 if ( !hiconLarge || !hiconSmall)
119 WARN("failure loading icon %i from %s (%x %x)\n", dwSourceIndex, sSourceFile, hiconLarge, hiconSmall);
122 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
124 /*****************************************************************************
125 * SIC_GetIconIndex [internal]
128 * sSourceFile [IN] filename of file containing the icon
129 * index [IN] index/resID (negated) in this file
132 * look in the cache for a proper icon. if not available the icon is taken
133 * from the file and cached
135 INT SIC_GetIconIndex (LPCSTR sSourceFile, INT dwSourceIndex )
137 INT ret, index = INVALID_INDEX;
139 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
141 sice.sSourceFile = PathFindFileNameA(sSourceFile);
142 sice.dwSourceIndex = dwSourceIndex;
144 EnterCriticalSection(&SHELL32_SicCS);
146 if (NULL != pDPA_GetPtr (sic_hdpa, 0))
148 index = pDPA_Search (sic_hdpa, &sice, -1L, SIC_CompareEntries, 0, 0);
151 if ( INVALID_INDEX == index )
153 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
158 ret = ((LPSIC_ENTRY)pDPA_GetPtr(sic_hdpa, index))->dwListIndex;
161 LeaveCriticalSection(&SHELL32_SicCS);
164 /****************************************************************************
165 * SIC_GetIcon [internal]
168 * retrieves the specified icon from the iconcache. if not found tries to load the icon
170 static HICON WINE_UNUSED SIC_GetIcon (LPCSTR sSourceFile, INT dwSourceIndex, BOOL bSmallIcon )
173 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
175 index = SIC_GetIconIndex(sSourceFile, dwSourceIndex);
177 if (INVALID_INDEX == index)
179 return INVALID_INDEX;
183 return ImageList_GetIcon(ShellSmallIconList, index, ILD_NORMAL);
184 return ImageList_GetIcon(ShellBigIconList, index, ILD_NORMAL);
187 /*****************************************************************************
188 * SIC_Initialize [internal]
191 * hack to load the resources from the shell32.dll under a different dll name
192 * will be removed when the resource-compiler is ready
194 BOOL SIC_Initialize(void)
201 if (sic_hdpa) /* already initialized?*/
204 sic_hdpa = pDPA_Create(16);
211 ShellSmallIconList = ImageList_Create(16,16,ILC_COLORDDB | ILC_MASK,0,0x20);
212 ShellBigIconList = ImageList_Create(32,32,ILC_COLORDDB | ILC_MASK,0,0x20);
214 ImageList_SetBkColor(ShellSmallIconList, GetSysColor(COLOR_WINDOW));
215 ImageList_SetBkColor(ShellBigIconList, GetSysColor(COLOR_WINDOW));
217 for (index=1; index<39; index++)
219 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 16, 16,LR_SHARED);
220 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 32, 32,LR_SHARED);
224 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 16, 16,LR_SHARED);
225 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 32, 32,LR_SHARED);
227 SIC_IconAppend ("shell32.dll", index, hSm, hLg);
230 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
234 /*************************************************************************
239 void SIC_Destroy(void)
246 EnterCriticalSection(&SHELL32_SicCS);
248 if (sic_hdpa && NULL != pDPA_GetPtr (sic_hdpa, 0))
250 for (i=0; i < pDPA_GetPtrCount(sic_hdpa); ++i)
252 lpsice = pDPA_GetPtr(sic_hdpa, i);
255 pDPA_Destroy(sic_hdpa);
260 LeaveCriticalSection(&SHELL32_SicCS);
261 DeleteCriticalSection(&SHELL32_SicCS);
263 /*************************************************************************
264 * Shell_GetImageList [SHELL32.71]
267 * imglist[1|2] [OUT] pointer which recive imagelist handles
270 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
271 { TRACE("(%p,%p)\n",lpBigList,lpSmallList);
273 { *lpBigList = ShellBigIconList;
276 { *lpSmallList = ShellSmallIconList;
281 /*************************************************************************
282 * PidlToSicIndex [INTERNAL]
285 * sh [IN] IShellFolder
289 * pIndex [OUT] index within the SIC
292 BOOL PidlToSicIndex (
300 char szIconFile[MAX_PATH]; /* file containing the icon */
301 INT iSourceIndex; /* index or resID(negated) in this file */
305 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
307 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconA, 0, (void **)&ei)))
309 if (SUCCEEDED(IExtractIconA_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
311 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
314 IExtractIconA_Release(ei);
317 if (INVALID_INDEX == *pIndex) /* default icon when failed */
324 /*************************************************************************
325 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
328 * sh [IN] pointer to an instance of IShellFolder
330 * pIndex [OUT][OPTIONAL] SIC index for big icon
333 int WINAPI SHMapPIDLToSystemImageListIndex(
340 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
344 PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
345 PidlToSicIndex ( sh, pidl, 0, 0, &Index);
349 /*************************************************************************
350 * Shell_GetCachedImageIndex [SHELL32.72]
353 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
355 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
356 return SIC_GetIconIndex(szPath, nIndex);
359 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
361 LPSTR sTemp = HEAP_strdupWtoA (GetProcessHeap(),0,szPath);
363 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
365 ret = SIC_GetIconIndex(sTemp, nIndex);
366 HeapFree(GetProcessHeap(),0,sTemp);
370 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
371 { if( SHELL_OsIsUnicode())
372 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
373 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
376 /*************************************************************************
377 * ExtractIconEx [SHELL32.@]
379 HICON WINAPI ExtractIconExAW ( LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
380 { if (SHELL_OsIsUnicode())
381 return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
382 return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
384 /*************************************************************************
385 * ExtractIconExA [SHELL32.@]
388 * 1 file is not valid
389 * HICON handle of a icon (phiconLarge/Small == NULL)
391 HICON WINAPI ExtractIconExA ( LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
394 TRACE("file=%s idx=%i %p %p num=%i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons );
396 if (nIconIndex==-1) /* Number of icons requested */
397 return PrivateExtractIconsA( lpszFile, -1, 0, 0, NULL, 0, 0, 0 );
401 ret = PrivateExtractIconsA( lpszFile, nIconIndex, 32, 32, phiconLarge, 0, nIcons, 0 );
403 { ret = phiconLarge[0];
407 /* if no pointers given and one icon expected, return the handle directly*/
408 if (!phiconLarge && !phiconSmall && nIcons==1 )
413 ret = PrivateExtractIconsA( lpszFile, nIconIndex, 16, 16, phiconSmall, 0, nIcons, 0 );
415 { ret = phiconSmall[0];
421 /*************************************************************************
422 * ExtractIconExW [SHELL32.@]
424 HICON WINAPI ExtractIconExW ( LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
428 TRACE("file=%s idx=%i %p %p num=%i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons );
430 sFile = HEAP_strdupWtoA (GetProcessHeap(),0,lpszFile);
431 ret = ExtractIconExA ( sFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
432 HeapFree(GetProcessHeap(),0,sFile);