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/winuser16.h"
36 #include "wine/winbase16.h"
38 #include "wine/debug.h"
44 #include "shell32_main.h"
45 #include "undocshell.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
50 /********************** THE ICON CACHE ********************************/
52 #define INVALID_INDEX -1
56 LPWSTR 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 */
61 } SIC_ENTRY
, * LPSIC_ENTRY
;
63 static HDPA sic_hdpa
= 0;
65 static CRITICAL_SECTION SHELL32_SicCS
;
66 static CRITICAL_SECTION_DEBUG critsect_debug
=
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 /*****************************************************************************
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*/
86 if (strcmpiW(((LPSIC_ENTRY
)p1
)->sSourceFile
,((LPSIC_ENTRY
)p2
)->sSourceFile
))
91 /*****************************************************************************
92 * SIC_IconAppend [internal]
95 * appends an icon pair to the end of the cache
97 static INT
SIC_IconAppend (LPCWSTR sSourceFile
, INT dwSourceIndex
, HICON hSmallIcon
, HICON hBigIcon
)
99 INT ret
, index
, index1
;
100 WCHAR path
[MAX_PATH
];
101 TRACE("%s %i %p %p\n", debugstr_w(sSourceFile
), dwSourceIndex
, hSmallIcon
,hBigIcon
);
103 lpsice
= (LPSIC_ENTRY
) SHAlloc (sizeof (SIC_ENTRY
));
105 GetFullPathNameW(sSourceFile
, MAX_PATH
, path
, NULL
);
106 lpsice
->sSourceFile
= HeapAlloc( GetProcessHeap(), 0, (strlenW(path
)+1)*sizeof(WCHAR
) );
107 strcpyW( 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
);
122 index
= ImageList_AddIcon (ShellSmallIconList
, hSmallIcon
);
123 index1
= ImageList_AddIcon (ShellBigIconList
, hBigIcon
);
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
);
136 /****************************************************************************
137 * SIC_LoadIcon [internal]
140 * gets small/big icon by number from a file
142 static INT
SIC_LoadIcon (LPCWSTR sSourceFile
, INT dwSourceIndex
)
143 { HICON hiconLarge
=0;
146 PrivateExtractIconsW( sSourceFile
, dwSourceIndex
, 32, 32, &hiconLarge
, 0, 1, 0 );
147 PrivateExtractIconsW( sSourceFile
, dwSourceIndex
, 16, 16, &hiconSmall
, 0, 1, 0 );
149 if ( !hiconLarge
|| !hiconSmall
)
151 WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex
, debugstr_w(sSourceFile
), hiconLarge
, hiconSmall
);
154 return SIC_IconAppend (sSourceFile
, dwSourceIndex
, hiconSmall
, hiconLarge
);
156 /*****************************************************************************
157 * SIC_GetIconIndex [internal]
160 * sSourceFile [IN] filename of file containing the icon
161 * index [IN] index/resID (negated) in this file
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 (LPCWSTR sSourceFile
, INT dwSourceIndex
)
170 INT ret
, index
= INVALID_INDEX
;
171 WCHAR path
[MAX_PATH
];
173 TRACE("%s %i\n", debugstr_w(sSourceFile
), dwSourceIndex
);
175 GetFullPathNameW(sSourceFile
, MAX_PATH
, path
, NULL
);
176 sice
.sSourceFile
= path
;
177 sice
.dwSourceIndex
= dwSourceIndex
;
179 EnterCriticalSection(&SHELL32_SicCS
);
181 if (NULL
!= DPA_GetPtr (sic_hdpa
, 0))
183 /* search linear from position 0*/
184 index
= DPA_Search (sic_hdpa
, &sice
, 0, SIC_CompareEntries
, 0, 0);
187 if ( INVALID_INDEX
== index
)
189 ret
= SIC_LoadIcon (sSourceFile
, dwSourceIndex
);
194 ret
= ((LPSIC_ENTRY
)DPA_GetPtr(sic_hdpa
, index
))->dwListIndex
;
197 LeaveCriticalSection(&SHELL32_SicCS
);
200 /*****************************************************************************
201 * SIC_Initialize [internal]
204 * hack to load the resources from the shell32.dll under a different dll name
205 * will be removed when the resource-compiler is ready
207 BOOL
SIC_Initialize(void)
214 if (sic_hdpa
) /* already initialized?*/
217 sic_hdpa
= DPA_Create(16);
224 ShellSmallIconList
= ImageList_Create(16,16,ILC_COLOR32
|ILC_MASK
,0,0x20);
225 ShellBigIconList
= ImageList_Create(32,32,ILC_COLOR32
|ILC_MASK
,0,0x20);
227 ImageList_SetBkColor(ShellSmallIconList
, CLR_NONE
);
228 ImageList_SetBkColor(ShellBigIconList
, CLR_NONE
);
230 for (index
=1; index
<39; index
++)
232 hSm
= LoadImageA(shell32_hInstance
, MAKEINTRESOURCEA(index
), IMAGE_ICON
, 16, 16,LR_SHARED
);
233 hLg
= LoadImageA(shell32_hInstance
, MAKEINTRESOURCEA(index
), IMAGE_ICON
, 32, 32,LR_SHARED
);
237 hSm
= LoadImageA(shell32_hInstance
, MAKEINTRESOURCEA(0), IMAGE_ICON
, 16, 16,LR_SHARED
);
238 hLg
= LoadImageA(shell32_hInstance
, MAKEINTRESOURCEA(0), IMAGE_ICON
, 32, 32,LR_SHARED
);
240 SIC_IconAppend (swShell32Name
, index
, hSm
, hLg
);
243 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList
, ShellBigIconList
);
247 /*************************************************************************
252 static INT CALLBACK
sic_free( LPVOID ptr
, LPVOID lparam
)
254 HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY
)ptr
)->sSourceFile
);
259 void SIC_Destroy(void)
263 EnterCriticalSection(&SHELL32_SicCS
);
265 if (sic_hdpa
) DPA_DestroyCallback(sic_hdpa
, sic_free
, NULL
);
268 ImageList_Destroy(ShellSmallIconList
);
269 ShellSmallIconList
= 0;
270 ImageList_Destroy(ShellBigIconList
);
271 ShellBigIconList
= 0;
273 LeaveCriticalSection(&SHELL32_SicCS
);
274 DeleteCriticalSection(&SHELL32_SicCS
);
277 /*************************************************************************
278 * Shell_GetImageList [SHELL32.71]
281 * imglist[1|2] [OUT] pointer which receives imagelist handles
284 BOOL WINAPI
Shell_GetImageList(HIMAGELIST
* lpBigList
, HIMAGELIST
* lpSmallList
)
285 { TRACE("(%p,%p)\n",lpBigList
,lpSmallList
);
287 { *lpBigList
= ShellBigIconList
;
290 { *lpSmallList
= ShellSmallIconList
;
295 /*************************************************************************
296 * PidlToSicIndex [INTERNAL]
299 * sh [IN] IShellFolder
303 * pIndex [OUT] index within the SIC
306 BOOL
PidlToSicIndex (
314 WCHAR szIconFile
[MAX_PATH
]; /* file containing the icon */
315 INT iSourceIndex
; /* index or resID(negated) in this file */
319 TRACE("sf=%p pidl=%p %s\n", sh
, pidl
, bBigIcon
?"Big":"Small");
321 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh
, 0, 1, &pidl
, &IID_IExtractIconW
, 0, (void **)&ei
)))
323 if (SUCCEEDED(IExtractIconW_GetIconLocation(ei
, uFlags
, szIconFile
, MAX_PATH
, &iSourceIndex
, &dwFlags
)))
325 *pIndex
= SIC_GetIconIndex(szIconFile
, iSourceIndex
);
328 IExtractIconW_Release(ei
);
331 if (INVALID_INDEX
== *pIndex
) /* default icon when failed */
338 /*************************************************************************
339 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
342 * sh [IN] pointer to an instance of IShellFolder
344 * pIndex [OUT][OPTIONAL] SIC index for big icon
347 int WINAPI
SHMapPIDLToSystemImageListIndex(
354 TRACE("(SF=%p,pidl=%p,%p)\n",sh
,pidl
,pIndex
);
358 PidlToSicIndex ( sh
, pidl
, 1, 0, pIndex
);
359 PidlToSicIndex ( sh
, pidl
, 0, 0, &Index
);
363 /*************************************************************************
364 * Shell_GetCachedImageIndex [SHELL32.72]
367 INT WINAPI
Shell_GetCachedImageIndexA(LPCSTR szPath
, INT nIndex
, BOOL bSimulateDoc
)
372 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath
), nIndex
, bSimulateDoc
);
374 len
= MultiByteToWideChar( CP_ACP
, 0, szPath
, -1, NULL
, 0 );
375 szTemp
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
376 MultiByteToWideChar( CP_ACP
, 0, szPath
, -1, szTemp
, len
);
378 ret
= SIC_GetIconIndex( szTemp
, nIndex
);
380 HeapFree( GetProcessHeap(), 0, szTemp
);
385 INT WINAPI
Shell_GetCachedImageIndexW(LPCWSTR szPath
, INT nIndex
, BOOL bSimulateDoc
)
387 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath
), nIndex
, bSimulateDoc
);
389 return SIC_GetIconIndex(szPath
, nIndex
);
392 INT WINAPI
Shell_GetCachedImageIndexAW(LPCVOID szPath
, INT nIndex
, BOOL bSimulateDoc
)
393 { if( SHELL_OsIsUnicode())
394 return Shell_GetCachedImageIndexW(szPath
, nIndex
, bSimulateDoc
);
395 return Shell_GetCachedImageIndexA(szPath
, nIndex
, bSimulateDoc
);
398 /*************************************************************************
399 * ExtractIconEx [SHELL32.@]
401 UINT WINAPI
ExtractIconExAW(LPCVOID lpszFile
, INT nIconIndex
, HICON
* phiconLarge
, HICON
* phiconSmall
, UINT nIcons
)
402 { if (SHELL_OsIsUnicode())
403 return ExtractIconExW ( lpszFile
, nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
404 return ExtractIconExA ( lpszFile
, nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
407 /*************************************************************************
408 * ExtractIconExW [SHELL32.@]
411 * -1 file is not valid
412 * or number of icons extracted
414 UINT WINAPI
ExtractIconExW(LPCWSTR lpszFile
, INT nIconIndex
, HICON
* phiconLarge
, HICON
* phiconSmall
, UINT nIcons
)
416 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile
), nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
418 return PrivateExtractIconExW(lpszFile
, nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
421 /*************************************************************************
422 * ExtractIconExA [SHELL32.@]
424 UINT WINAPI
ExtractIconExA(LPCSTR lpszFile
, INT nIconIndex
, HICON
* phiconLarge
, HICON
* phiconSmall
, UINT nIcons
)
427 INT len
= MultiByteToWideChar(CP_ACP
, 0, lpszFile
, -1, NULL
, 0);
428 LPWSTR lpwstrFile
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
430 TRACE("%s %i %p %p %i\n", lpszFile
, nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
432 MultiByteToWideChar(CP_ACP
, 0, lpszFile
, -1, lpwstrFile
, len
);
433 ret
= ExtractIconExW (lpwstrFile
, nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
434 HeapFree(GetProcessHeap(), 0, lpwstrFile
);
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
)
452 lpiIcon
= &wDummyIcon
;
454 hIcon
= ExtractIconA(hInst
, lpIconPath
, *lpiIcon
);
456 if( hIcon
< (HICON
)2 )
457 { if( hIcon
== (HICON
)1 ) /* no icons found in given file */
458 { char tempPath
[0x80];
459 HINSTANCE uRet
= FindExecutableA(lpIconPath
,NULL
,tempPath
);
461 if( uRet
> (HINSTANCE
)32 && tempPath
[0] )
462 { strcpy(lpIconPath
,tempPath
);
463 hIcon
= ExtractIconA(hInst
, lpIconPath
, *lpiIcon
);
464 if( hIcon
> (HICON
)2 )
470 if( hIcon
== (HICON
)1 )
471 *lpiIcon
= 2; /* MSDOS icon - we found .exe but no icons in it */
473 *lpiIcon
= 6; /* generic icon - found nothing */
475 if (GetModuleFileNameA(hInst
, lpIconPath
, 0x80))
477 /* terminate string (GetModuleFileName doesn't do if buffer is too small) */
478 lpIconPath
[0x80 - 1] = '\0';
479 hIcon
= LoadIconA( hInst
, MAKEINTRESOURCEA(*lpiIcon
));
485 /*************************************************************************
486 * ExtractAssociatedIconExW (SHELL32.@)
488 * Return icon for given file (either from file itself or from associated
489 * executable) and patch parameters if needed.
491 HICON WINAPI
ExtractAssociatedIconExW(HINSTANCE hInst
, LPWSTR lpIconPath
, LPWORD lpiIconIdx
, LPWORD lpiIconId
)
493 FIXME("%p %s %p %p): stub\n", hInst
, debugstr_w(lpIconPath
), lpiIconIdx
, lpiIconId
);
497 /*************************************************************************
498 * ExtractAssociatedIconExA (SHELL32.@)
500 * Return icon for given file (either from file itself or from associated
501 * executable) and patch parameters if needed.
503 HICON WINAPI
ExtractAssociatedIconExA(HINSTANCE hInst
, LPSTR lpIconPath
, LPWORD lpiIconIdx
, LPWORD lpiIconId
)
506 INT len
= MultiByteToWideChar( CP_ACP
, 0, lpIconPath
, -1, NULL
, 0 );
507 LPWSTR lpwstrFile
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
509 TRACE("%p %s %p %p)\n", hInst
, lpIconPath
, lpiIconIdx
, lpiIconId
);
511 MultiByteToWideChar( CP_ACP
, 0, lpIconPath
, -1, lpwstrFile
, len
);
512 ret
= ExtractAssociatedIconExW(hInst
, lpwstrFile
, lpiIconIdx
, lpiIconId
);
513 HeapFree(GetProcessHeap(), 0, lpwstrFile
);
518 /****************************************************************************
519 * SHDefExtractIconW [SHELL32.@]
521 HRESULT WINAPI
SHDefExtractIconW(LPCWSTR pszIconFile
, int iIndex
, UINT uFlags
,
522 HICON
* phiconLarge
, HICON
* phiconSmall
, UINT nIconSize
)
526 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile
), iIndex
, uFlags
, phiconLarge
, phiconSmall
, nIconSize
);
528 ret
= PrivateExtractIconsW(pszIconFile
, iIndex
, nIconSize
, nIconSize
, hIcons
, NULL
, 2, LR_DEFAULTCOLOR
);
529 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
530 if (ret
== 0xFFFFFFFF)
533 *phiconLarge
= hIcons
[0];
534 *phiconSmall
= hIcons
[1];
540 /****************************************************************************
541 * SHDefExtractIconA [SHELL32.@]
543 HRESULT WINAPI
SHDefExtractIconA(LPCSTR pszIconFile
, int iIndex
, UINT uFlags
,
544 HICON
* phiconLarge
, HICON
* phiconSmall
, UINT nIconSize
)
547 INT len
= MultiByteToWideChar(CP_ACP
, 0, pszIconFile
, -1, NULL
, 0);
548 LPWSTR lpwstrFile
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
550 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile
, iIndex
, uFlags
, phiconLarge
, phiconSmall
, nIconSize
);
552 MultiByteToWideChar(CP_ACP
, 0, pszIconFile
, -1, lpwstrFile
, len
);
553 ret
= SHDefExtractIconW(lpwstrFile
, iIndex
, uFlags
, phiconLarge
, phiconSmall
, nIconSize
);
554 HeapFree(GetProcessHeap(), 0, lpwstrFile
);