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
22 #include <sys/types.h>
29 #include "wine/winuser16.h"
30 #include "wine/winbase16.h"
32 #include "wine/debug.h"
37 #include "shell32_main.h"
38 #include "undocshell.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(shell);
43 /********************** THE ICON CACHE ********************************/
45 #define INVALID_INDEX -1
49 LPSTR sSourceFile; /* file (not path!) containing the icon */
50 DWORD dwSourceIndex; /* index within the file, if it is a resoure ID it will be negated */
51 DWORD dwListIndex; /* index within the iconlist */
52 DWORD dwFlags; /* GIL_* flags */
54 } SIC_ENTRY, * LPSIC_ENTRY;
56 static HDPA sic_hdpa = 0;
57 static CRITICAL_SECTION SHELL32_SicCS = CRITICAL_SECTION_INIT("SHELL32_SicCS");
59 /*****************************************************************************
63 * Callback for DPA_Search
65 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
66 { TRACE("%p %p %8lx\n", p1, p2, lparam);
68 if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
71 if (strcasecmp(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
76 /*****************************************************************************
77 * SIC_IconAppend [internal]
80 * appends a icon pair to the end of the cache
82 static INT SIC_IconAppend (LPCSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
84 INT ret, index, index1;
86 TRACE("%s %i %x %x\n", sSourceFile, dwSourceIndex, hSmallIcon ,hBigIcon);
88 lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
90 path = PathFindFileNameA(sSourceFile);
91 lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, strlen(path)+1 );
92 strcpy( lpsice->sSourceFile, path );
94 lpsice->dwSourceIndex = dwSourceIndex;
96 EnterCriticalSection(&SHELL32_SicCS);
98 index = pDPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
99 if ( INVALID_INDEX == index )
106 index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
107 index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
111 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
113 lpsice->dwListIndex = index;
114 ret = lpsice->dwListIndex;
117 LeaveCriticalSection(&SHELL32_SicCS);
120 /****************************************************************************
121 * SIC_LoadIcon [internal]
124 * gets small/big icon by number from a file
126 static INT SIC_LoadIcon (LPCSTR sSourceFile, INT dwSourceIndex)
127 { HICON hiconLarge=0;
130 PrivateExtractIconsA( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 );
131 PrivateExtractIconsA( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
133 if ( !hiconLarge || !hiconSmall)
135 WARN("failure loading icon %i from %s (%x %x)\n", dwSourceIndex, sSourceFile, hiconLarge, hiconSmall);
138 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
140 /*****************************************************************************
141 * SIC_GetIconIndex [internal]
144 * sSourceFile [IN] filename of file containing the icon
145 * index [IN] index/resID (negated) in this file
148 * look in the cache for a proper icon. if not available the icon is taken
149 * from the file and cached
151 INT SIC_GetIconIndex (LPCSTR sSourceFile, INT dwSourceIndex )
153 INT ret, index = INVALID_INDEX;
155 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
157 sice.sSourceFile = PathFindFileNameA(sSourceFile);
158 sice.dwSourceIndex = dwSourceIndex;
160 EnterCriticalSection(&SHELL32_SicCS);
162 if (NULL != pDPA_GetPtr (sic_hdpa, 0))
164 /* search linear from position 0*/
165 index = pDPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
168 if ( INVALID_INDEX == index )
170 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
175 ret = ((LPSIC_ENTRY)pDPA_GetPtr(sic_hdpa, index))->dwListIndex;
178 LeaveCriticalSection(&SHELL32_SicCS);
181 /****************************************************************************
182 * SIC_GetIcon [internal]
185 * retrieves the specified icon from the iconcache. if not found tries to load the icon
187 static HICON WINE_UNUSED SIC_GetIcon (LPCSTR sSourceFile, INT dwSourceIndex, BOOL bSmallIcon )
190 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
192 index = SIC_GetIconIndex(sSourceFile, dwSourceIndex);
194 if (INVALID_INDEX == index)
196 return INVALID_INDEX;
200 return ImageList_GetIcon(ShellSmallIconList, index, ILD_NORMAL);
201 return ImageList_GetIcon(ShellBigIconList, index, ILD_NORMAL);
204 /*****************************************************************************
205 * SIC_Initialize [internal]
208 * hack to load the resources from the shell32.dll under a different dll name
209 * will be removed when the resource-compiler is ready
211 BOOL SIC_Initialize(void)
218 if (sic_hdpa) /* already initialized?*/
221 sic_hdpa = pDPA_Create(16);
228 ShellSmallIconList = ImageList_Create(16,16,ILC_COLORDDB | ILC_MASK,0,0x20);
229 ShellBigIconList = ImageList_Create(32,32,ILC_COLORDDB | ILC_MASK,0,0x20);
231 ImageList_SetBkColor(ShellSmallIconList, GetSysColor(COLOR_WINDOW));
232 ImageList_SetBkColor(ShellBigIconList, GetSysColor(COLOR_WINDOW));
234 for (index=1; index<39; index++)
236 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 16, 16,LR_SHARED);
237 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 32, 32,LR_SHARED);
241 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 16, 16,LR_SHARED);
242 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 32, 32,LR_SHARED);
244 SIC_IconAppend ("shell32.dll", index, hSm, hLg);
247 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
251 /*************************************************************************
256 void SIC_Destroy(void)
263 EnterCriticalSection(&SHELL32_SicCS);
265 if (sic_hdpa && NULL != pDPA_GetPtr (sic_hdpa, 0))
267 for (i=0; i < pDPA_GetPtrCount(sic_hdpa); ++i)
269 lpsice = pDPA_GetPtr(sic_hdpa, i);
272 pDPA_Destroy(sic_hdpa);
277 LeaveCriticalSection(&SHELL32_SicCS);
278 DeleteCriticalSection(&SHELL32_SicCS);
280 /*************************************************************************
281 * Shell_GetImageList [SHELL32.71]
284 * imglist[1|2] [OUT] pointer which recive imagelist handles
287 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
288 { TRACE("(%p,%p)\n",lpBigList,lpSmallList);
290 { *lpBigList = ShellBigIconList;
293 { *lpSmallList = ShellSmallIconList;
298 /*************************************************************************
299 * PidlToSicIndex [INTERNAL]
302 * sh [IN] IShellFolder
306 * pIndex [OUT] index within the SIC
309 BOOL PidlToSicIndex (
317 char szIconFile[MAX_PATH]; /* file containing the icon */
318 INT iSourceIndex; /* index or resID(negated) in this file */
322 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
324 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconA, 0, (void **)&ei)))
326 if (SUCCEEDED(IExtractIconA_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
328 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
331 IExtractIconA_Release(ei);
334 if (INVALID_INDEX == *pIndex) /* default icon when failed */
341 /*************************************************************************
342 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
345 * sh [IN] pointer to an instance of IShellFolder
347 * pIndex [OUT][OPTIONAL] SIC index for big icon
350 int WINAPI SHMapPIDLToSystemImageListIndex(
357 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
361 PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
362 PidlToSicIndex ( sh, pidl, 0, 0, &Index);
366 /*************************************************************************
367 * Shell_GetCachedImageIndex [SHELL32.72]
370 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
372 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
373 return SIC_GetIconIndex(szPath, nIndex);
376 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
378 LPSTR sTemp = HEAP_strdupWtoA (GetProcessHeap(),0,szPath);
380 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
382 ret = SIC_GetIconIndex(sTemp, nIndex);
383 HeapFree(GetProcessHeap(),0,sTemp);
387 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
388 { if( SHELL_OsIsUnicode())
389 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
390 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
393 /*************************************************************************
394 * ExtractIconEx [SHELL32.@]
396 HICON WINAPI ExtractIconExAW ( LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
397 { if (SHELL_OsIsUnicode())
398 return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
399 return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
401 /*************************************************************************
402 * ExtractIconExA [SHELL32.@]
405 * 1 file is not valid
406 * HICON handle of a icon (phiconLarge/Small == NULL)
408 HICON WINAPI ExtractIconExA ( LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
411 TRACE("file=%s idx=%i %p %p num=%i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons );
413 if (nIconIndex==-1) /* Number of icons requested */
414 return PrivateExtractIconsA( lpszFile, -1, 0, 0, NULL, 0, 0, 0 );
418 ret = PrivateExtractIconsA( lpszFile, nIconIndex, 32, 32, phiconLarge, 0, nIcons, 0 );
420 { ret = phiconLarge[0];
424 /* if no pointers given and one icon expected, return the handle directly*/
425 if (!phiconLarge && !phiconSmall && nIcons==1 )
430 ret = PrivateExtractIconsA( lpszFile, nIconIndex, 16, 16, phiconSmall, 0, nIcons, 0 );
432 { ret = phiconSmall[0];
438 /*************************************************************************
439 * ExtractIconExW [SHELL32.@]
441 HICON WINAPI ExtractIconExW ( LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
445 TRACE("file=%s idx=%i %p %p num=%i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons );
447 sFile = HEAP_strdupWtoA (GetProcessHeap(),0,lpszFile);
448 ret = ExtractIconExA ( sFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
449 HeapFree(GetProcessHeap(),0,sFile);