Remove two ancient FAQ entries that did more harm than good.
[wine.git] / dlls / shell32 / iconcache.c
blobaf4b2f519a90e0a0432b12b05a61a3a61f2f3750
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
31 #define COBJMACROS
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "winuser.h"
37 #include "winreg.h"
38 #include "wine/debug.h"
40 #include "shellapi.h"
41 #include "objbase.h"
42 #include "shlguid.h"
43 #include "pidl.h"
44 #include "shell32_main.h"
45 #include "undocshell.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(shell);
49 /********************** THE ICON CACHE ********************************/
51 #define INVALID_INDEX -1
53 typedef struct
55 LPWSTR 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 */
59 DWORD dwAccessTime;
60 } SIC_ENTRY, * LPSIC_ENTRY;
62 static HDPA sic_hdpa = 0;
64 static CRITICAL_SECTION SHELL32_SicCS;
65 static CRITICAL_SECTION_DEBUG critsect_debug =
67 0, 0, &SHELL32_SicCS,
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 /*****************************************************************************
74 * SIC_CompareEntries
76 * NOTES
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*/
83 return 1;
85 if (strcmpiW(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
86 return 1;
88 return 0;
90 /*****************************************************************************
91 * SIC_IconAppend [internal]
93 * NOTES
94 * appends an icon pair to the end of the cache
96 static INT SIC_IconAppend (LPCWSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
97 { LPSIC_ENTRY lpsice;
98 INT ret, index, index1;
99 WCHAR path[MAX_PATH];
100 TRACE("%s %i %p %p\n", debugstr_w(sSourceFile), dwSourceIndex, hSmallIcon ,hBigIcon);
102 lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
104 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
105 lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, (strlenW(path)+1)*sizeof(WCHAR) );
106 strcpyW( lpsice->sSourceFile, path );
108 lpsice->dwSourceIndex = dwSourceIndex;
110 EnterCriticalSection(&SHELL32_SicCS);
112 index = DPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
113 if ( INVALID_INDEX == index )
115 HeapFree(GetProcessHeap(), 0, lpsice->sSourceFile);
116 SHFree(lpsice);
117 ret = INVALID_INDEX;
119 else
121 index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
122 index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
124 if (index!=index1)
126 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
128 lpsice->dwListIndex = index;
129 ret = lpsice->dwListIndex;
132 LeaveCriticalSection(&SHELL32_SicCS);
133 return ret;
135 /****************************************************************************
136 * SIC_LoadIcon [internal]
138 * NOTES
139 * gets small/big icon by number from a file
141 static INT SIC_LoadIcon (LPCWSTR sSourceFile, INT dwSourceIndex)
142 { HICON hiconLarge=0;
143 HICON hiconSmall=0;
145 PrivateExtractIconsW( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 );
146 PrivateExtractIconsW( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
148 if ( !hiconLarge || !hiconSmall)
150 WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, debugstr_w(sSourceFile), hiconLarge, hiconSmall);
151 return -1;
153 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
155 /*****************************************************************************
156 * SIC_GetIconIndex [internal]
158 * Parameters
159 * sSourceFile [IN] filename of file containing the icon
160 * index [IN] index/resID (negated) in this file
162 * NOTES
163 * look in the cache for a proper icon. if not available the icon is taken
164 * from the file and cached
166 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex )
168 SIC_ENTRY sice;
169 INT ret, index = INVALID_INDEX;
170 WCHAR path[MAX_PATH];
172 TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
174 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
175 sice.sSourceFile = path;
176 sice.dwSourceIndex = dwSourceIndex;
178 EnterCriticalSection(&SHELL32_SicCS);
180 if (NULL != DPA_GetPtr (sic_hdpa, 0))
182 /* search linear from position 0*/
183 index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
186 if ( INVALID_INDEX == index )
188 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
190 else
192 TRACE("-- found\n");
193 ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
196 LeaveCriticalSection(&SHELL32_SicCS);
197 return ret;
199 /*****************************************************************************
200 * SIC_Initialize [internal]
202 * NOTES
203 * hack to load the resources from the shell32.dll under a different dll name
204 * will be removed when the resource-compiler is ready
206 BOOL SIC_Initialize(void)
208 HICON hSm, hLg;
209 UINT index;
210 int cx_small, cy_small;
211 int cx_large, cy_large;
213 cx_small = GetSystemMetrics(SM_CXSMICON);
214 cy_small = GetSystemMetrics(SM_CYSMICON);
215 cx_large = GetSystemMetrics(SM_CXICON);
216 cy_large = GetSystemMetrics(SM_CYICON);
218 TRACE("\n");
220 if (sic_hdpa) /* already initialized?*/
221 return TRUE;
223 sic_hdpa = DPA_Create(16);
225 if (!sic_hdpa)
227 return(FALSE);
230 ShellSmallIconList = ImageList_Create(16,16,ILC_COLOR32|ILC_MASK,0,0x20);
231 ShellBigIconList = ImageList_Create(32,32,ILC_COLOR32|ILC_MASK,0,0x20);
233 ImageList_SetBkColor(ShellSmallIconList, CLR_NONE);
234 ImageList_SetBkColor(ShellBigIconList, CLR_NONE);
236 for (index=1; index<39; index++)
238 hSm = (HICON)LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, cx_small, cy_small, LR_SHARED);
239 hLg = (HICON)LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, cx_large, cy_large, LR_SHARED);
241 if(!hSm)
243 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(1), IMAGE_ICON, cx_small, cy_small, LR_SHARED);
244 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(1), IMAGE_ICON, cx_large, cy_large, LR_SHARED);
246 SIC_IconAppend (swShell32Name, index, hSm, hLg);
249 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
251 return TRUE;
253 /*************************************************************************
254 * SIC_Destroy
256 * frees the cache
258 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
260 HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
261 SHFree(ptr);
262 return TRUE;
265 void SIC_Destroy(void)
267 TRACE("\n");
269 EnterCriticalSection(&SHELL32_SicCS);
271 if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
273 sic_hdpa = NULL;
274 ImageList_Destroy(ShellSmallIconList);
275 ShellSmallIconList = 0;
276 ImageList_Destroy(ShellBigIconList);
277 ShellBigIconList = 0;
279 LeaveCriticalSection(&SHELL32_SicCS);
280 DeleteCriticalSection(&SHELL32_SicCS);
283 /*************************************************************************
284 * Shell_GetImageList [SHELL32.71]
286 * PARAMETERS
287 * imglist[1|2] [OUT] pointer which receives imagelist handles
290 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
291 { TRACE("(%p,%p)\n",lpBigList,lpSmallList);
292 if (lpBigList)
293 { *lpBigList = ShellBigIconList;
295 if (lpSmallList)
296 { *lpSmallList = ShellSmallIconList;
299 return TRUE;
301 /*************************************************************************
302 * PidlToSicIndex [INTERNAL]
304 * PARAMETERS
305 * sh [IN] IShellFolder
306 * pidl [IN]
307 * bBigIcon [IN]
308 * uFlags [IN] GIL_*
309 * pIndex [OUT] index within the SIC
312 BOOL PidlToSicIndex (
313 IShellFolder * sh,
314 LPCITEMIDLIST pidl,
315 BOOL bBigIcon,
316 UINT uFlags,
317 int * pIndex)
319 IExtractIconW *ei;
320 WCHAR szIconFile[MAX_PATH]; /* file containing the icon */
321 INT iSourceIndex; /* index or resID(negated) in this file */
322 BOOL ret = FALSE;
323 UINT dwFlags = 0;
325 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
327 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconW, 0, (void **)&ei)))
329 if (SUCCEEDED(IExtractIconW_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
331 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
332 ret = TRUE;
334 IExtractIconW_Release(ei);
337 if (INVALID_INDEX == *pIndex) /* default icon when failed */
338 *pIndex = 0;
340 return ret;
344 /*************************************************************************
345 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
347 * PARAMETERS
348 * sh [IN] pointer to an instance of IShellFolder
349 * pidl [IN]
350 * pIndex [OUT][OPTIONAL] SIC index for big icon
353 int WINAPI SHMapPIDLToSystemImageListIndex(
354 IShellFolder *sh,
355 LPCITEMIDLIST pidl,
356 int *pIndex)
358 int Index;
360 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
361 pdump(pidl);
363 if (pIndex)
364 PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
365 PidlToSicIndex ( sh, pidl, 0, 0, &Index);
366 return Index;
369 /*************************************************************************
370 * Shell_GetCachedImageIndex [SHELL32.72]
373 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
375 INT ret, len;
376 LPWSTR szTemp;
378 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
380 len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
381 szTemp = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
382 MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
384 ret = SIC_GetIconIndex( szTemp, nIndex );
386 HeapFree( GetProcessHeap(), 0, szTemp );
388 return ret;
391 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
393 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
395 return SIC_GetIconIndex(szPath, nIndex);
398 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
399 { if( SHELL_OsIsUnicode())
400 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
401 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
404 /*************************************************************************
405 * ExtractIconExW [SHELL32.@]
406 * RETURNS
407 * 0 no icon found
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)
423 UINT ret = 0;
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 if (lpwstrFile)
431 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
432 ret = ExtractIconExW(lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
433 HeapFree(GetProcessHeap(), 0, lpwstrFile);
435 return ret;
438 /*************************************************************************
439 * ExtractAssociatedIconA (SHELL32.@)
441 * Return icon for given file (either from file itself or from associated
442 * executable) and patch parameters if needed.
444 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
446 HICON hIcon = NULL;
447 INT len = MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, NULL, 0);
448 LPWSTR lpIconPathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
450 TRACE("%p %s %p\n", hInst, debugstr_a(lpIconPath), lpiIcon);
452 if (lpIconPathW)
454 MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, lpIconPathW, len);
455 hIcon = ExtractAssociatedIconW(hInst, lpIconPathW, lpiIcon);
456 HeapFree(GetProcessHeap(), 0, lpIconPathW);
458 return hIcon;
461 HICON WINAPI ExtractAssociatedIconW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIcon)
463 HICON hIcon = NULL;
464 WORD wDummyIcon = 0;
466 TRACE("%p %s %p\n", hInst, debugstr_w(lpIconPath), lpiIcon);
468 if(lpiIcon == NULL)
469 lpiIcon = &wDummyIcon;
471 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
473 if( hIcon < (HICON)2 )
474 { if( hIcon == (HICON)1 ) /* no icons found in given file */
475 { WCHAR tempPath[MAX_PATH];
476 HINSTANCE uRet = FindExecutableW(lpIconPath,NULL,tempPath);
478 if( uRet > (HINSTANCE)32 && tempPath[0] )
479 { lstrcpyW(lpIconPath,tempPath);
480 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
481 if( hIcon > (HICON)2 )
482 return hIcon;
486 if( hIcon == (HICON)1 )
487 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
488 else
489 *lpiIcon = 6; /* generic icon - found nothing */
491 if (GetModuleFileNameW(hInst, lpIconPath, MAX_PATH))
492 hIcon = LoadIconW(hInst, MAKEINTRESOURCEW(*lpiIcon));
494 return hIcon;
497 /*************************************************************************
498 * ExtractAssociatedIconExW (SHELL32.@)
500 * Return icon for given file (either from file itself or from associated
501 * executable) and patch parameters if needed.
503 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
505 FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
506 return 0;
509 /*************************************************************************
510 * ExtractAssociatedIconExA (SHELL32.@)
512 * Return icon for given file (either from file itself or from associated
513 * executable) and patch parameters if needed.
515 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
517 HICON ret;
518 INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
519 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
521 TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
523 MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
524 ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
525 HeapFree(GetProcessHeap(), 0, lpwstrFile);
526 return ret;
530 /****************************************************************************
531 * SHDefExtractIconW [SHELL32.@]
533 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
534 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
536 UINT ret;
537 HICON hIcons[2];
538 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
540 ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
541 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
542 if (ret == 0xFFFFFFFF)
543 return E_FAIL;
544 if (ret > 0) {
545 *phiconLarge = hIcons[0];
546 *phiconSmall = hIcons[1];
547 return S_OK;
549 return S_FALSE;
552 /****************************************************************************
553 * SHDefExtractIconA [SHELL32.@]
555 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
556 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
558 HRESULT ret;
559 INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
560 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
562 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
564 MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
565 ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
566 HeapFree(GetProcessHeap(), 0, lpwstrFile);
567 return ret;