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 "wine/port.h"
26 #include <sys/types.h>
35 #include "wine/debug.h"
41 #include "shell32_main.h"
42 #include "undocshell.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(shell);
47 /********************** THE ICON CACHE ********************************/
49 #define INVALID_INDEX -1
53 LPWSTR sSourceFile; /* file (not path!) containing the icon */
54 DWORD dwSourceIndex; /* index within the file, if it is a resoure ID it will be negated */
55 DWORD dwListIndex; /* index within the iconlist */
56 DWORD dwFlags; /* GIL_* flags */
58 } SIC_ENTRY, * LPSIC_ENTRY;
60 static HDPA sic_hdpa = 0;
62 static CRITICAL_SECTION SHELL32_SicCS;
63 static CRITICAL_SECTION_DEBUG critsect_debug =
66 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
67 0, 0, { 0, (DWORD)(__FILE__ ": SHELL32_SicCS") }
69 static CRITICAL_SECTION SHELL32_SicCS = { &critsect_debug, -1, 0, 0, 0, 0 };
71 /*****************************************************************************
75 * Callback for DPA_Search
77 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
78 { TRACE("%p %p %8lx\n", p1, p2, lparam);
80 if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
83 if (strcmpiW(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
88 /*****************************************************************************
89 * SIC_IconAppend [internal]
92 * appends an icon pair to the end of the cache
94 static INT SIC_IconAppend (LPCWSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
96 INT ret, index, index1;
98 TRACE("%s %i %p %p\n", debugstr_w(sSourceFile), dwSourceIndex, hSmallIcon ,hBigIcon);
100 lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
102 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
103 lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, (strlenW(path)+1)*sizeof(WCHAR) );
104 strcpyW( lpsice->sSourceFile, path );
106 lpsice->dwSourceIndex = dwSourceIndex;
108 EnterCriticalSection(&SHELL32_SicCS);
110 index = DPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
111 if ( INVALID_INDEX == index )
113 HeapFree(GetProcessHeap(), 0, lpsice->sSourceFile);
119 index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
120 index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
124 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
126 lpsice->dwListIndex = index;
127 ret = lpsice->dwListIndex;
130 LeaveCriticalSection(&SHELL32_SicCS);
133 /****************************************************************************
134 * SIC_LoadIcon [internal]
137 * gets small/big icon by number from a file
139 static INT SIC_LoadIcon (LPCWSTR sSourceFile, INT dwSourceIndex)
140 { HICON hiconLarge=0;
143 PrivateExtractIconsW( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 );
144 PrivateExtractIconsW( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
146 if ( !hiconLarge || !hiconSmall)
148 WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, debugstr_w(sSourceFile), hiconLarge, hiconSmall);
151 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
153 /*****************************************************************************
154 * SIC_GetIconIndex [internal]
157 * sSourceFile [IN] filename of file containing the icon
158 * index [IN] index/resID (negated) in this file
161 * look in the cache for a proper icon. if not available the icon is taken
162 * from the file and cached
164 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex )
167 INT ret, index = INVALID_INDEX;
168 WCHAR path[MAX_PATH];
170 TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
172 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
173 sice.sSourceFile = path;
174 sice.dwSourceIndex = dwSourceIndex;
176 EnterCriticalSection(&SHELL32_SicCS);
178 if (NULL != DPA_GetPtr (sic_hdpa, 0))
180 /* search linear from position 0*/
181 index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
184 if ( INVALID_INDEX == index )
186 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
191 ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
194 LeaveCriticalSection(&SHELL32_SicCS);
197 /*****************************************************************************
198 * SIC_Initialize [internal]
201 * hack to load the resources from the shell32.dll under a different dll name
202 * will be removed when the resource-compiler is ready
204 BOOL SIC_Initialize(void)
211 if (sic_hdpa) /* already initialized?*/
214 sic_hdpa = DPA_Create(16);
221 ShellSmallIconList = ImageList_Create(16,16,ILC_COLOR32|ILC_MASK,0,0x20);
222 ShellBigIconList = ImageList_Create(32,32,ILC_COLOR32|ILC_MASK,0,0x20);
224 ImageList_SetBkColor(ShellSmallIconList, CLR_NONE);
225 ImageList_SetBkColor(ShellBigIconList, CLR_NONE);
227 for (index=1; index<39; index++)
229 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 16, 16,LR_SHARED);
230 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 32, 32,LR_SHARED);
234 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 16, 16,LR_SHARED);
235 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 32, 32,LR_SHARED);
237 SIC_IconAppend (swShell32Name, index, hSm, hLg);
240 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
244 /*************************************************************************
249 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
251 HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
256 void SIC_Destroy(void)
260 EnterCriticalSection(&SHELL32_SicCS);
262 if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
265 ImageList_Destroy(ShellSmallIconList);
266 ShellSmallIconList = 0;
267 ImageList_Destroy(ShellBigIconList);
268 ShellBigIconList = 0;
270 LeaveCriticalSection(&SHELL32_SicCS);
271 DeleteCriticalSection(&SHELL32_SicCS);
274 /*************************************************************************
275 * Shell_GetImageList [SHELL32.71]
278 * imglist[1|2] [OUT] pointer which receives imagelist handles
281 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
282 { TRACE("(%p,%p)\n",lpBigList,lpSmallList);
284 { *lpBigList = ShellBigIconList;
287 { *lpSmallList = ShellSmallIconList;
292 /*************************************************************************
293 * PidlToSicIndex [INTERNAL]
296 * sh [IN] IShellFolder
300 * pIndex [OUT] index within the SIC
303 BOOL PidlToSicIndex (
311 WCHAR szIconFile[MAX_PATH]; /* file containing the icon */
312 INT iSourceIndex; /* index or resID(negated) in this file */
316 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
318 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconW, 0, (void **)&ei)))
320 if (SUCCEEDED(IExtractIconW_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
322 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
325 IExtractIconW_Release(ei);
328 if (INVALID_INDEX == *pIndex) /* default icon when failed */
335 /*************************************************************************
336 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
339 * sh [IN] pointer to an instance of IShellFolder
341 * pIndex [OUT][OPTIONAL] SIC index for big icon
344 int WINAPI SHMapPIDLToSystemImageListIndex(
351 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
355 PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
356 PidlToSicIndex ( sh, pidl, 0, 0, &Index);
360 /*************************************************************************
361 * Shell_GetCachedImageIndex [SHELL32.72]
364 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
369 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
371 len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
372 szTemp = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
373 MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
375 ret = SIC_GetIconIndex( szTemp, nIndex );
377 HeapFree( GetProcessHeap(), 0, szTemp );
382 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
384 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
386 return SIC_GetIconIndex(szPath, nIndex);
389 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
390 { if( SHELL_OsIsUnicode())
391 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
392 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
395 /*************************************************************************
396 * ExtractIconEx [SHELL32.@]
398 UINT WINAPI ExtractIconExAW(LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
399 { if (SHELL_OsIsUnicode())
400 return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
401 return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
404 /*************************************************************************
405 * ExtractIconExW [SHELL32.@]
408 * -1 file is not valid
409 * or number of icons extracted
411 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
413 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
415 return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
418 /*************************************************************************
419 * ExtractIconExA [SHELL32.@]
421 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
424 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
425 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
427 TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
429 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
430 ret = ExtractIconExW (lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
431 HeapFree(GetProcessHeap(), 0, lpwstrFile);
435 /*************************************************************************
436 * ExtractAssociatedIconA (SHELL32.@)
438 * Return icon for given file (either from file itself or from associated
439 * executable) and patch parameters if needed.
441 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
449 lpiIcon = &wDummyIcon;
451 hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
453 if( hIcon < (HICON)2 )
454 { if( hIcon == (HICON)1 ) /* no icons found in given file */
455 { char tempPath[0x80];
456 HINSTANCE uRet = FindExecutableA(lpIconPath,NULL,tempPath);
458 if( uRet > (HINSTANCE)32 && tempPath[0] )
459 { strcpy(lpIconPath,tempPath);
460 hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
461 if( hIcon > (HICON)2 )
467 if( hIcon == (HICON)1 )
468 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
470 *lpiIcon = 6; /* generic icon - found nothing */
472 if (GetModuleFileNameA(hInst, lpIconPath, 0x80))
474 /* terminate string (GetModuleFileName doesn't do if buffer is too small) */
475 lpIconPath[0x80 - 1] = '\0';
476 hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
482 /*************************************************************************
483 * ExtractAssociatedIconExW (SHELL32.@)
485 * Return icon for given file (either from file itself or from associated
486 * executable) and patch parameters if needed.
488 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
490 FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
494 /*************************************************************************
495 * ExtractAssociatedIconExA (SHELL32.@)
497 * Return icon for given file (either from file itself or from associated
498 * executable) and patch parameters if needed.
500 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
503 INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
504 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
506 TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
508 MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
509 ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
510 HeapFree(GetProcessHeap(), 0, lpwstrFile);
515 /****************************************************************************
516 * SHDefExtractIconW [SHELL32.@]
518 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
519 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
523 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
525 ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
526 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
527 if (ret == 0xFFFFFFFF)
530 *phiconLarge = hIcons[0];
531 *phiconSmall = hIcons[1];
537 /****************************************************************************
538 * SHDefExtractIconA [SHELL32.@]
540 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
541 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
544 INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
545 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
547 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
549 MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
550 ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
551 HeapFree(GetProcessHeap(), 0, lpwstrFile);