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\n", p1
, p2
);
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 index
= pDPA_Search (sic_hdpa
, &sice
, -1L, SIC_CompareEntries
, 0, 0);
167 if ( INVALID_INDEX
== index
)
169 ret
= SIC_LoadIcon (sSourceFile
, dwSourceIndex
);
174 ret
= ((LPSIC_ENTRY
)pDPA_GetPtr(sic_hdpa
, index
))->dwListIndex
;
177 LeaveCriticalSection(&SHELL32_SicCS
);
180 /****************************************************************************
181 * SIC_GetIcon [internal]
184 * retrieves the specified icon from the iconcache. if not found tries to load the icon
186 static HICON WINE_UNUSED
SIC_GetIcon (LPCSTR sSourceFile
, INT dwSourceIndex
, BOOL bSmallIcon
)
189 TRACE("%s %i\n", sSourceFile
, dwSourceIndex
);
191 index
= SIC_GetIconIndex(sSourceFile
, dwSourceIndex
);
193 if (INVALID_INDEX
== index
)
195 return INVALID_INDEX
;
199 return ImageList_GetIcon(ShellSmallIconList
, index
, ILD_NORMAL
);
200 return ImageList_GetIcon(ShellBigIconList
, index
, ILD_NORMAL
);
203 /*****************************************************************************
204 * SIC_Initialize [internal]
207 * hack to load the resources from the shell32.dll under a different dll name
208 * will be removed when the resource-compiler is ready
210 BOOL
SIC_Initialize(void)
217 if (sic_hdpa
) /* already initialized?*/
220 sic_hdpa
= pDPA_Create(16);
227 ShellSmallIconList
= ImageList_Create(16,16,ILC_COLORDDB
| ILC_MASK
,0,0x20);
228 ShellBigIconList
= ImageList_Create(32,32,ILC_COLORDDB
| ILC_MASK
,0,0x20);
230 ImageList_SetBkColor(ShellSmallIconList
, GetSysColor(COLOR_WINDOW
));
231 ImageList_SetBkColor(ShellBigIconList
, GetSysColor(COLOR_WINDOW
));
233 for (index
=1; index
<39; index
++)
235 hSm
= LoadImageA(shell32_hInstance
, MAKEINTRESOURCEA(index
), IMAGE_ICON
, 16, 16,LR_SHARED
);
236 hLg
= LoadImageA(shell32_hInstance
, MAKEINTRESOURCEA(index
), IMAGE_ICON
, 32, 32,LR_SHARED
);
240 hSm
= LoadImageA(shell32_hInstance
, MAKEINTRESOURCEA(0), IMAGE_ICON
, 16, 16,LR_SHARED
);
241 hLg
= LoadImageA(shell32_hInstance
, MAKEINTRESOURCEA(0), IMAGE_ICON
, 32, 32,LR_SHARED
);
243 SIC_IconAppend ("shell32.dll", index
, hSm
, hLg
);
246 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList
, ShellBigIconList
);
250 /*************************************************************************
255 void SIC_Destroy(void)
262 EnterCriticalSection(&SHELL32_SicCS
);
264 if (sic_hdpa
&& NULL
!= pDPA_GetPtr (sic_hdpa
, 0))
266 for (i
=0; i
< pDPA_GetPtrCount(sic_hdpa
); ++i
)
268 lpsice
= pDPA_GetPtr(sic_hdpa
, i
);
271 pDPA_Destroy(sic_hdpa
);
276 LeaveCriticalSection(&SHELL32_SicCS
);
277 DeleteCriticalSection(&SHELL32_SicCS
);
279 /*************************************************************************
280 * Shell_GetImageList [SHELL32.71]
283 * imglist[1|2] [OUT] pointer which recive imagelist handles
286 BOOL WINAPI
Shell_GetImageList(HIMAGELIST
* lpBigList
, HIMAGELIST
* lpSmallList
)
287 { TRACE("(%p,%p)\n",lpBigList
,lpSmallList
);
289 { *lpBigList
= ShellBigIconList
;
292 { *lpSmallList
= ShellSmallIconList
;
297 /*************************************************************************
298 * PidlToSicIndex [INTERNAL]
301 * sh [IN] IShellFolder
305 * pIndex [OUT] index within the SIC
308 BOOL
PidlToSicIndex (
316 char szIconFile
[MAX_PATH
]; /* file containing the icon */
317 INT iSourceIndex
; /* index or resID(negated) in this file */
321 TRACE("sf=%p pidl=%p %s\n", sh
, pidl
, bBigIcon
?"Big":"Small");
323 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh
, 0, 1, &pidl
, &IID_IExtractIconA
, 0, (void **)&ei
)))
325 if (SUCCEEDED(IExtractIconA_GetIconLocation(ei
, uFlags
, szIconFile
, MAX_PATH
, &iSourceIndex
, &dwFlags
)))
327 *pIndex
= SIC_GetIconIndex(szIconFile
, iSourceIndex
);
330 IExtractIconA_Release(ei
);
333 if (INVALID_INDEX
== *pIndex
) /* default icon when failed */
340 /*************************************************************************
341 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
344 * sh [IN] pointer to an instance of IShellFolder
346 * pIndex [OUT][OPTIONAL] SIC index for big icon
349 int WINAPI
SHMapPIDLToSystemImageListIndex(
356 TRACE("(SF=%p,pidl=%p,%p)\n",sh
,pidl
,pIndex
);
360 PidlToSicIndex ( sh
, pidl
, 1, 0, pIndex
);
361 PidlToSicIndex ( sh
, pidl
, 0, 0, &Index
);
365 /*************************************************************************
366 * Shell_GetCachedImageIndex [SHELL32.72]
369 INT WINAPI
Shell_GetCachedImageIndexA(LPCSTR szPath
, INT nIndex
, BOOL bSimulateDoc
)
371 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath
), nIndex
, bSimulateDoc
);
372 return SIC_GetIconIndex(szPath
, nIndex
);
375 INT WINAPI
Shell_GetCachedImageIndexW(LPCWSTR szPath
, INT nIndex
, BOOL bSimulateDoc
)
377 LPSTR sTemp
= HEAP_strdupWtoA (GetProcessHeap(),0,szPath
);
379 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath
), nIndex
, bSimulateDoc
);
381 ret
= SIC_GetIconIndex(sTemp
, nIndex
);
382 HeapFree(GetProcessHeap(),0,sTemp
);
386 INT WINAPI
Shell_GetCachedImageIndexAW(LPCVOID szPath
, INT nIndex
, BOOL bSimulateDoc
)
387 { if( SHELL_OsIsUnicode())
388 return Shell_GetCachedImageIndexW(szPath
, nIndex
, bSimulateDoc
);
389 return Shell_GetCachedImageIndexA(szPath
, nIndex
, bSimulateDoc
);
392 /*************************************************************************
393 * ExtractIconEx [SHELL32.@]
395 HICON WINAPI
ExtractIconExAW ( LPCVOID lpszFile
, INT nIconIndex
, HICON
* phiconLarge
, HICON
* phiconSmall
, UINT nIcons
)
396 { if (SHELL_OsIsUnicode())
397 return ExtractIconExW ( lpszFile
, nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
398 return ExtractIconExA ( lpszFile
, nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
400 /*************************************************************************
401 * ExtractIconExA [SHELL32.@]
404 * 1 file is not valid
405 * HICON handle of a icon (phiconLarge/Small == NULL)
407 HICON WINAPI
ExtractIconExA ( LPCSTR lpszFile
, INT nIconIndex
, HICON
* phiconLarge
, HICON
* phiconSmall
, UINT nIcons
)
410 TRACE("file=%s idx=%i %p %p num=%i\n", lpszFile
, nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
412 if (nIconIndex
==-1) /* Number of icons requested */
413 return PrivateExtractIconsA( lpszFile
, -1, 0, 0, NULL
, 0, 0, 0 );
417 ret
= PrivateExtractIconsA( lpszFile
, nIconIndex
, 32, 32, phiconLarge
, 0, nIcons
, 0 );
419 { ret
= phiconLarge
[0];
423 /* if no pointers given and one icon expected, return the handle directly*/
424 if (!phiconLarge
&& !phiconSmall
&& nIcons
==1 )
429 ret
= PrivateExtractIconsA( lpszFile
, nIconIndex
, 16, 16, phiconSmall
, 0, nIcons
, 0 );
431 { ret
= phiconSmall
[0];
437 /*************************************************************************
438 * ExtractIconExW [SHELL32.@]
440 HICON WINAPI
ExtractIconExW ( LPCWSTR lpszFile
, INT nIconIndex
, HICON
* phiconLarge
, HICON
* phiconSmall
, UINT nIcons
)
444 TRACE("file=%s idx=%i %p %p num=%i\n", debugstr_w(lpszFile
), nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
446 sFile
= HEAP_strdupWtoA (GetProcessHeap(),0,lpszFile
);
447 ret
= ExtractIconExA ( sFile
, nIconIndex
, phiconLarge
, phiconSmall
, nIcons
);
448 HeapFree(GetProcessHeap(),0,sFile
);