SHCoCreateInstance is now documented (spotted by Francois Gouget).
[wine/wine-kai.git] / dlls / shell32 / iconcache.c
blob7a323618738b16f5aecb330bac1509e2eacb8fde
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
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winreg.h"
35 #include "wine/winuser16.h"
36 #include "wine/winbase16.h"
37 #include "heap.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"
46 #include "shlwapi.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(shell);
50 /********************** THE ICON CACHE ********************************/
52 #define INVALID_INDEX -1
54 typedef struct
56 LPSTR sSourceFile; /* file (not path!) containing the icon */
57 DWORD dwSourceIndex; /* index within the file, if it is a resoure ID it will be negated */
58 DWORD dwListIndex; /* index within the iconlist */
59 DWORD dwFlags; /* GIL_* flags */
60 DWORD dwAccessTime;
61 } SIC_ENTRY, * LPSIC_ENTRY;
63 static HDPA sic_hdpa = 0;
65 static CRITICAL_SECTION SHELL32_SicCS;
66 static CRITICAL_SECTION_DEBUG critsect_debug =
68 0, 0, &SHELL32_SicCS,
69 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
70 0, 0, { 0, (DWORD)(__FILE__ ": SHELL32_SicCS") }
72 static CRITICAL_SECTION SHELL32_SicCS = { &critsect_debug, -1, 0, 0, 0, 0 };
74 /*****************************************************************************
75 * SIC_CompareEntries
77 * NOTES
78 * Callback for DPA_Search
80 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
81 { TRACE("%p %p %8lx\n", p1, p2, lparam);
83 if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
84 return 1;
86 if (strcasecmp(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
87 return 1;
89 return 0;
91 /*****************************************************************************
92 * SIC_IconAppend [internal]
94 * NOTES
95 * appends an icon pair to the end of the cache
97 static INT SIC_IconAppend (LPCSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
98 { LPSIC_ENTRY lpsice;
99 INT ret, index, index1;
100 char *path;
101 TRACE("%s %i %p %p\n", sSourceFile, dwSourceIndex, hSmallIcon ,hBigIcon);
103 lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
105 path = PathFindFileNameA(sSourceFile);
106 lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, strlen(path)+1 );
107 strcpy( lpsice->sSourceFile, path );
109 lpsice->dwSourceIndex = dwSourceIndex;
111 EnterCriticalSection(&SHELL32_SicCS);
113 index = DPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
114 if ( INVALID_INDEX == index )
116 HeapFree(GetProcessHeap(), 0, lpsice->sSourceFile);
117 SHFree(lpsice);
118 ret = INVALID_INDEX;
120 else
122 index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
123 index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
125 if (index!=index1)
127 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
129 lpsice->dwListIndex = index;
130 ret = lpsice->dwListIndex;
133 LeaveCriticalSection(&SHELL32_SicCS);
134 return ret;
136 /****************************************************************************
137 * SIC_LoadIcon [internal]
139 * NOTES
140 * gets small/big icon by number from a file
142 static INT SIC_LoadIcon (LPCSTR sSourceFile, INT dwSourceIndex)
143 { HICON hiconLarge=0;
144 HICON hiconSmall=0;
146 PrivateExtractIconsA( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 );
147 PrivateExtractIconsA( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
149 if ( !hiconLarge || !hiconSmall)
151 WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, sSourceFile, hiconLarge, hiconSmall);
152 return -1;
154 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
156 /*****************************************************************************
157 * SIC_GetIconIndex [internal]
159 * Parameters
160 * sSourceFile [IN] filename of file containing the icon
161 * index [IN] index/resID (negated) in this file
163 * NOTES
164 * look in the cache for a proper icon. if not available the icon is taken
165 * from the file and cached
167 INT SIC_GetIconIndex (LPCSTR sSourceFile, INT dwSourceIndex )
168 { SIC_ENTRY sice;
169 INT ret, index = INVALID_INDEX;
171 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
173 sice.sSourceFile = PathFindFileNameA(sSourceFile);
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);
188 else
190 TRACE("-- found\n");
191 ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
194 LeaveCriticalSection(&SHELL32_SicCS);
195 return ret;
197 /****************************************************************************
198 * SIC_GetIcon [internal]
200 * NOTES
201 * retrieves the specified icon from the iconcache. if not found tries to load the icon
203 static HICON WINE_UNUSED SIC_GetIcon (LPCSTR sSourceFile, INT dwSourceIndex, BOOL bSmallIcon )
204 { INT index;
206 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
208 index = SIC_GetIconIndex(sSourceFile, dwSourceIndex);
210 if (INVALID_INDEX == index)
212 return (HICON)INVALID_INDEX;
215 if (bSmallIcon)
216 return ImageList_GetIcon(ShellSmallIconList, index, ILD_NORMAL);
217 return ImageList_GetIcon(ShellBigIconList, index, ILD_NORMAL);
220 /*****************************************************************************
221 * SIC_Initialize [internal]
223 * NOTES
224 * hack to load the resources from the shell32.dll under a different dll name
225 * will be removed when the resource-compiler is ready
227 BOOL SIC_Initialize(void)
229 HICON hSm, hLg;
230 UINT index;
232 TRACE("\n");
234 if (sic_hdpa) /* already initialized?*/
235 return TRUE;
237 sic_hdpa = DPA_Create(16);
239 if (!sic_hdpa)
241 return(FALSE);
244 ShellSmallIconList = ImageList_Create(16,16,ILC_COLORDDB | ILC_MASK,0,0x20);
245 ShellBigIconList = ImageList_Create(32,32,ILC_COLORDDB | ILC_MASK,0,0x20);
247 ImageList_SetBkColor(ShellSmallIconList, GetSysColor(COLOR_WINDOW));
248 ImageList_SetBkColor(ShellBigIconList, GetSysColor(COLOR_WINDOW));
250 for (index=1; index<39; index++)
252 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 16, 16,LR_SHARED);
253 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 32, 32,LR_SHARED);
255 if(!hSm)
257 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 16, 16,LR_SHARED);
258 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 32, 32,LR_SHARED);
260 SIC_IconAppend ("shell32.dll", index, hSm, hLg);
263 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
265 return TRUE;
267 /*************************************************************************
268 * SIC_Destroy
270 * frees the cache
272 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
274 HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
275 SHFree(ptr);
276 return TRUE;
279 void SIC_Destroy(void)
281 TRACE("\n");
283 EnterCriticalSection(&SHELL32_SicCS);
285 if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
287 sic_hdpa = NULL;
289 LeaveCriticalSection(&SHELL32_SicCS);
290 DeleteCriticalSection(&SHELL32_SicCS);
292 /*************************************************************************
293 * Shell_GetImageList [SHELL32.71]
295 * PARAMETERS
296 * imglist[1|2] [OUT] pointer which recive imagelist handles
299 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
300 { TRACE("(%p,%p)\n",lpBigList,lpSmallList);
301 if (lpBigList)
302 { *lpBigList = ShellBigIconList;
304 if (lpSmallList)
305 { *lpSmallList = ShellSmallIconList;
308 return TRUE;
310 /*************************************************************************
311 * PidlToSicIndex [INTERNAL]
313 * PARAMETERS
314 * sh [IN] IShellFolder
315 * pidl [IN]
316 * bBigIcon [IN]
317 * uFlags [IN] GIL_*
318 * pIndex [OUT] index within the SIC
321 BOOL PidlToSicIndex (
322 IShellFolder * sh,
323 LPCITEMIDLIST pidl,
324 BOOL bBigIcon,
325 UINT uFlags,
326 UINT * pIndex)
328 IExtractIconA *ei;
329 char szIconFile[MAX_PATH]; /* file containing the icon */
330 INT iSourceIndex; /* index or resID(negated) in this file */
331 BOOL ret = FALSE;
332 UINT dwFlags = 0;
334 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
336 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconA, 0, (void **)&ei)))
338 if (SUCCEEDED(IExtractIconA_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
340 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
341 ret = TRUE;
343 IExtractIconA_Release(ei);
346 if (INVALID_INDEX == *pIndex) /* default icon when failed */
347 *pIndex = 1;
349 return ret;
353 /*************************************************************************
354 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
356 * PARAMETERS
357 * sh [IN] pointer to an instance of IShellFolder
358 * pidl [IN]
359 * pIndex [OUT][OPTIONAL] SIC index for big icon
362 int WINAPI SHMapPIDLToSystemImageListIndex(
363 LPSHELLFOLDER sh,
364 LPCITEMIDLIST pidl,
365 UINT * pIndex)
367 UINT Index;
369 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
370 pdump(pidl);
372 if (pIndex)
373 PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
374 PidlToSicIndex ( sh, pidl, 0, 0, &Index);
375 return Index;
378 /*************************************************************************
379 * Shell_GetCachedImageIndex [SHELL32.72]
382 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
384 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
385 return SIC_GetIconIndex(szPath, nIndex);
388 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
389 { INT ret;
390 LPSTR sTemp = HEAP_strdupWtoA (GetProcessHeap(),0,szPath);
392 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
394 ret = SIC_GetIconIndex(sTemp, nIndex);
395 HeapFree(GetProcessHeap(),0,sTemp);
396 return ret;
399 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
400 { if( SHELL_OsIsUnicode())
401 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
402 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
405 /*************************************************************************
406 * ExtractIconEx [SHELL32.@]
408 UINT WINAPI ExtractIconExAW(LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
409 { if (SHELL_OsIsUnicode())
410 return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
411 return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
414 /*************************************************************************
415 * ExtractIconExW [SHELL32.@]
416 * RETURNS:
417 * 0 no icon found
418 * -1 file is not valid
419 * or number of icons extracted
421 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
423 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
425 return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
428 /*************************************************************************
429 * ExtractIconExA [SHELL32.@]
431 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
433 UINT ret;
434 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
435 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
437 TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
439 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
440 ret = ExtractIconExW (lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
441 HeapFree(GetProcessHeap(), 0, lpwstrFile);
442 return ret;
445 /*************************************************************************
446 * ExtractAssociatedIconA (SHELL32.@)
448 * Return icon for given file (either from file itself or from associated
449 * executable) and patch parameters if needed.
451 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
453 HICON hIcon;
454 WORD wDummyIcon = 0;
456 TRACE("\n");
458 if(lpiIcon == NULL)
459 lpiIcon = &wDummyIcon;
461 hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
463 if( hIcon < (HICON)2 )
464 { if( hIcon == (HICON)1 ) /* no icons found in given file */
465 { char tempPath[0x80];
466 HINSTANCE uRet = FindExecutableA(lpIconPath,NULL,tempPath);
468 if( uRet > (HINSTANCE)32 && tempPath[0] )
469 { strcpy(lpIconPath,tempPath);
470 hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
471 if( hIcon > (HICON)2 )
472 return hIcon;
474 else hIcon = 0;
477 if( hIcon == (HICON)1 )
478 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
479 else
480 *lpiIcon = 6; /* generic icon - found nothing */
482 GetModuleFileNameA(hInst, lpIconPath, 0x80);
483 hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
485 return hIcon;
488 /*************************************************************************
489 * ExtractAssociatedIconExW (SHELL32.@)
491 * Return icon for given file (either from file itself or from associated
492 * executable) and patch parameters if needed.
494 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
496 FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
497 return 0;
500 /*************************************************************************
501 * ExtractAssociatedIconExA (SHELL32.@)
503 * Return icon for given file (either from file itself or from associated
504 * executable) and patch parameters if needed.
506 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
508 HICON ret;
509 INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
510 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
512 TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
514 MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
515 ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
516 HeapFree(GetProcessHeap(), 0, lpwstrFile);
517 return ret;
521 /****************************************************************************
522 * SHDefExtractIconW [SHELL32.@]
524 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
525 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
527 UINT ret;
528 HICON hIcons[2];
529 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
531 ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
532 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
533 if (ret == 0xFFFFFFFF)
534 return E_FAIL;
535 if (ret > 0) {
536 *phiconLarge = hIcons[0];
537 *phiconSmall = hIcons[1];
538 return S_OK;
540 return S_FALSE;
543 /****************************************************************************
544 * SHDefExtractIconA [SHELL32.@]
546 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
547 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
549 HRESULT ret;
550 INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
551 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
553 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
555 MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
556 ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
557 HeapFree(GetProcessHeap(), 0, lpwstrFile);
558 return ret;