Merged the two serializer and unserializer functions into one, cleaned
[wine.git] / dlls / shell32 / iconcache.c
blob751265e6a30b7cd7083cb1154ed9924afe5ba5bc
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 <string.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include "winbase.h"
25 #include "windef.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "wine/winuser16.h"
30 #include "wine/winbase16.h"
31 #include "heap.h"
32 #include "wine/debug.h"
34 #include "shellapi.h"
35 #include "shlguid.h"
36 #include "pidl.h"
37 #include "shell32_main.h"
38 #include "undocshell.h"
39 #include "shlwapi.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(shell);
43 /********************** THE ICON CACHE ********************************/
45 #define INVALID_INDEX -1
47 typedef struct
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 */
53 DWORD dwAccessTime;
54 } SIC_ENTRY, * LPSIC_ENTRY;
56 static HDPA sic_hdpa = 0;
57 static CRITICAL_SECTION SHELL32_SicCS = CRITICAL_SECTION_INIT("SHELL32_SicCS");
59 /*****************************************************************************
60 * SIC_CompareEntries
62 * NOTES
63 * Callback for DPA_Search
65 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
66 { TRACE("%p %p %8lx\n", p1, p2, lparam);
68 if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
69 return 1;
71 if (strcasecmp(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
72 return 1;
74 return 0;
76 /*****************************************************************************
77 * SIC_IconAppend [internal]
79 * NOTES
80 * appends a icon pair to the end of the cache
82 static INT SIC_IconAppend (LPCSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
83 { LPSIC_ENTRY lpsice;
84 INT ret, index, index1;
85 char *path;
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 )
101 SHFree(lpsice);
102 ret = INVALID_INDEX;
104 else
106 index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
107 index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
109 if (index!=index1)
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);
118 return ret;
120 /****************************************************************************
121 * SIC_LoadIcon [internal]
123 * NOTES
124 * gets small/big icon by number from a file
126 static INT SIC_LoadIcon (LPCSTR sSourceFile, INT dwSourceIndex)
127 { HICON hiconLarge=0;
128 HICON hiconSmall=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);
136 return -1;
138 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
140 /*****************************************************************************
141 * SIC_GetIconIndex [internal]
143 * Parameters
144 * sSourceFile [IN] filename of file containing the icon
145 * index [IN] index/resID (negated) in this file
147 * NOTES
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 )
152 { SIC_ENTRY sice;
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 /* search linear from position 0*/
165 index = pDPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
168 if ( INVALID_INDEX == index )
170 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
172 else
174 TRACE("-- found\n");
175 ret = ((LPSIC_ENTRY)pDPA_GetPtr(sic_hdpa, index))->dwListIndex;
178 LeaveCriticalSection(&SHELL32_SicCS);
179 return ret;
181 /****************************************************************************
182 * SIC_GetIcon [internal]
184 * NOTES
185 * retrieves the specified icon from the iconcache. if not found tries to load the icon
187 static HICON WINE_UNUSED SIC_GetIcon (LPCSTR sSourceFile, INT dwSourceIndex, BOOL bSmallIcon )
188 { INT index;
190 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
192 index = SIC_GetIconIndex(sSourceFile, dwSourceIndex);
194 if (INVALID_INDEX == index)
196 return INVALID_INDEX;
199 if (bSmallIcon)
200 return ImageList_GetIcon(ShellSmallIconList, index, ILD_NORMAL);
201 return ImageList_GetIcon(ShellBigIconList, index, ILD_NORMAL);
204 /*****************************************************************************
205 * SIC_Initialize [internal]
207 * NOTES
208 * hack to load the resources from the shell32.dll under a different dll name
209 * will be removed when the resource-compiler is ready
211 BOOL SIC_Initialize(void)
213 HICON hSm, hLg;
214 UINT index;
216 TRACE("\n");
218 if (sic_hdpa) /* already initialized?*/
219 return TRUE;
221 sic_hdpa = pDPA_Create(16);
223 if (!sic_hdpa)
225 return(FALSE);
228 ShellSmallIconList = ImageList_Create(16,16,ILC_COLORDDB | ILC_MASK,0,0x20);
229 ShellBigIconList = ImageList_Create(32,32,ILC_COLORDDB | ILC_MASK,0,0x20);
231 ImageList_SetBkColor(ShellSmallIconList, GetSysColor(COLOR_WINDOW));
232 ImageList_SetBkColor(ShellBigIconList, GetSysColor(COLOR_WINDOW));
234 for (index=1; index<39; index++)
236 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 16, 16,LR_SHARED);
237 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 32, 32,LR_SHARED);
239 if(!hSm)
241 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 16, 16,LR_SHARED);
242 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 32, 32,LR_SHARED);
244 SIC_IconAppend ("shell32.dll", index, hSm, hLg);
247 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
249 return TRUE;
251 /*************************************************************************
252 * SIC_Destroy
254 * frees the cache
256 void SIC_Destroy(void)
258 LPSIC_ENTRY lpsice;
259 int i;
261 TRACE("\n");
263 EnterCriticalSection(&SHELL32_SicCS);
265 if (sic_hdpa && NULL != pDPA_GetPtr (sic_hdpa, 0))
267 for (i=0; i < pDPA_GetPtrCount(sic_hdpa); ++i)
269 lpsice = pDPA_GetPtr(sic_hdpa, i);
270 SHFree(lpsice);
272 pDPA_Destroy(sic_hdpa);
275 sic_hdpa = NULL;
277 LeaveCriticalSection(&SHELL32_SicCS);
278 DeleteCriticalSection(&SHELL32_SicCS);
280 /*************************************************************************
281 * Shell_GetImageList [SHELL32.71]
283 * PARAMETERS
284 * imglist[1|2] [OUT] pointer which recive imagelist handles
287 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
288 { TRACE("(%p,%p)\n",lpBigList,lpSmallList);
289 if (lpBigList)
290 { *lpBigList = ShellBigIconList;
292 if (lpSmallList)
293 { *lpSmallList = ShellSmallIconList;
296 return TRUE;
298 /*************************************************************************
299 * PidlToSicIndex [INTERNAL]
301 * PARAMETERS
302 * sh [IN] IShellFolder
303 * pidl [IN]
304 * bBigIcon [IN]
305 * uFlags [IN] GIL_*
306 * pIndex [OUT] index within the SIC
309 BOOL PidlToSicIndex (
310 IShellFolder * sh,
311 LPITEMIDLIST pidl,
312 BOOL bBigIcon,
313 UINT uFlags,
314 UINT * pIndex)
316 IExtractIconA *ei;
317 char szIconFile[MAX_PATH]; /* file containing the icon */
318 INT iSourceIndex; /* index or resID(negated) in this file */
319 BOOL ret = FALSE;
320 UINT dwFlags = 0;
322 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
324 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconA, 0, (void **)&ei)))
326 if (SUCCEEDED(IExtractIconA_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
328 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
329 ret = TRUE;
331 IExtractIconA_Release(ei);
334 if (INVALID_INDEX == *pIndex) /* default icon when failed */
335 *pIndex = 1;
337 return ret;
341 /*************************************************************************
342 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
344 * PARAMETERS
345 * sh [IN] pointer to an instance of IShellFolder
346 * pidl [IN]
347 * pIndex [OUT][OPTIONAL] SIC index for big icon
350 int WINAPI SHMapPIDLToSystemImageListIndex(
351 LPSHELLFOLDER sh,
352 LPCITEMIDLIST pidl,
353 UINT * pIndex)
355 UINT Index;
357 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
358 pdump(pidl);
360 if (pIndex)
361 PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
362 PidlToSicIndex ( sh, pidl, 0, 0, &Index);
363 return Index;
366 /*************************************************************************
367 * Shell_GetCachedImageIndex [SHELL32.72]
370 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
372 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
373 return SIC_GetIconIndex(szPath, nIndex);
376 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
377 { INT ret;
378 LPSTR sTemp = HEAP_strdupWtoA (GetProcessHeap(),0,szPath);
380 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
382 ret = SIC_GetIconIndex(sTemp, nIndex);
383 HeapFree(GetProcessHeap(),0,sTemp);
384 return ret;
387 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
388 { if( SHELL_OsIsUnicode())
389 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
390 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
393 /*************************************************************************
394 * ExtractIconEx [SHELL32.@]
396 HICON WINAPI ExtractIconExAW ( LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
397 { if (SHELL_OsIsUnicode())
398 return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
399 return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
401 /*************************************************************************
402 * ExtractIconExA [SHELL32.@]
403 * RETURNS
404 * 0 no icon found
405 * 1 file is not valid
406 * HICON handle of a icon (phiconLarge/Small == NULL)
408 HICON WINAPI ExtractIconExA ( LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
409 { HICON ret=0;
411 TRACE("file=%s idx=%i %p %p num=%i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons );
413 if (nIconIndex==-1) /* Number of icons requested */
414 return PrivateExtractIconsA( lpszFile, -1, 0, 0, NULL, 0, 0, 0 );
416 if (phiconLarge)
418 ret = PrivateExtractIconsA( lpszFile, nIconIndex, 32, 32, phiconLarge, 0, nIcons, 0 );
419 if ( nIcons==1)
420 { ret = phiconLarge[0];
424 /* if no pointers given and one icon expected, return the handle directly*/
425 if (!phiconLarge && !phiconSmall && nIcons==1 )
426 phiconSmall = &ret;
428 if (phiconSmall)
430 ret = PrivateExtractIconsA( lpszFile, nIconIndex, 16, 16, phiconSmall, 0, nIcons, 0 );
431 if ( nIcons==1 )
432 { ret = phiconSmall[0];
436 return ret;
438 /*************************************************************************
439 * ExtractIconExW [SHELL32.@]
441 HICON WINAPI ExtractIconExW ( LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
442 { LPSTR sFile;
443 DWORD ret;
445 TRACE("file=%s idx=%i %p %p num=%i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons );
447 sFile = HEAP_strdupWtoA (GetProcessHeap(),0,lpszFile);
448 ret = ExtractIconExA ( sFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
449 HeapFree(GetProcessHeap(),0,sFile);
450 return ret;