Added regedit unit test, a couple minor changes to regedit.
[wine] / dlls / shell32 / iconcache.c
1 /*
2  *      shell icon cache (SIC)
3  *
4  * Copyright 1998, 1999 Juergen Schmied
5  *
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.
10  *
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.
15  *
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
19  */
20
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"
33
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"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(shell);
42
43 /********************** THE ICON CACHE ********************************/
44
45 #define INVALID_INDEX -1
46
47 typedef struct
48 {
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;
55
56 static HDPA             sic_hdpa = 0;
57 static CRITICAL_SECTION SHELL32_SicCS = CRITICAL_SECTION_INIT("SHELL32_SicCS");
58
59 /*****************************************************************************
60  * SIC_CompareEntries
61  *
62  * NOTES
63  *  Callback for DPA_Search
64  */
65 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
66 {       TRACE("%p %p %8lx\n", p1, p2, lparam);
67
68         if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
69           return 1;
70
71         if (strcasecmp(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
72           return 1;
73
74         return 0;
75 }
76 /*****************************************************************************
77  * SIC_IconAppend                       [internal]
78  *
79  * NOTES
80  *  appends a icon pair to the end of the cache
81  */
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);
87
88         lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
89
90         path = PathFindFileNameA(sSourceFile);
91         lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, strlen(path)+1 );
92         strcpy( lpsice->sSourceFile, path );
93
94         lpsice->dwSourceIndex = dwSourceIndex;
95
96         EnterCriticalSection(&SHELL32_SicCS);
97
98         index = pDPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
99         if ( INVALID_INDEX == index )
100         {
101           SHFree(lpsice);
102           ret = INVALID_INDEX;
103         }
104         else
105         {
106           index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
107           index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
108
109           if (index!=index1)
110           {
111             FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
112           }
113           lpsice->dwListIndex = index;
114           ret = lpsice->dwListIndex;
115         }
116
117         LeaveCriticalSection(&SHELL32_SicCS);
118         return ret;
119 }
120 /****************************************************************************
121  * SIC_LoadIcon                         [internal]
122  *
123  * NOTES
124  *  gets small/big icon by number from a file
125  */
126 static INT SIC_LoadIcon (LPCSTR sSourceFile, INT dwSourceIndex)
127 {       HICON   hiconLarge=0;
128         HICON   hiconSmall=0;
129
130         PrivateExtractIconsA( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 );
131         PrivateExtractIconsA( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
132
133         if ( !hiconLarge ||  !hiconSmall)
134         {
135           WARN("failure loading icon %i from %s (%x %x)\n", dwSourceIndex, sSourceFile, hiconLarge, hiconSmall);
136           return -1;
137         }
138         return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
139 }
140 /*****************************************************************************
141  * SIC_GetIconIndex                     [internal]
142  *
143  * Parameters
144  *      sSourceFile     [IN]    filename of file containing the icon
145  *      index           [IN]    index/resID (negated) in this file
146  *
147  * NOTES
148  *  look in the cache for a proper icon. if not available the icon is taken
149  *  from the file and cached
150  */
151 INT SIC_GetIconIndex (LPCSTR sSourceFile, INT dwSourceIndex )
152 {       SIC_ENTRY sice;
153         INT ret, index = INVALID_INDEX;
154
155         TRACE("%s %i\n", sSourceFile, dwSourceIndex);
156
157         sice.sSourceFile = PathFindFileNameA(sSourceFile);
158         sice.dwSourceIndex = dwSourceIndex;
159
160         EnterCriticalSection(&SHELL32_SicCS);
161
162         if (NULL != pDPA_GetPtr (sic_hdpa, 0))
163         {
164           /* search linear from position 0*/
165           index = pDPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
166         }
167
168         if ( INVALID_INDEX == index )
169         {
170           ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
171         }
172         else
173         {
174           TRACE("-- found\n");
175           ret = ((LPSIC_ENTRY)pDPA_GetPtr(sic_hdpa, index))->dwListIndex;
176         }
177
178         LeaveCriticalSection(&SHELL32_SicCS);
179         return ret;
180 }
181 /****************************************************************************
182  * SIC_GetIcon                          [internal]
183  *
184  * NOTES
185  *  retrieves the specified icon from the iconcache. if not found tries to load the icon
186  */
187 static HICON WINE_UNUSED SIC_GetIcon (LPCSTR sSourceFile, INT dwSourceIndex, BOOL bSmallIcon )
188 {       INT index;
189
190         TRACE("%s %i\n", sSourceFile, dwSourceIndex);
191
192         index = SIC_GetIconIndex(sSourceFile, dwSourceIndex);
193
194         if (INVALID_INDEX == index)
195         {
196           return INVALID_INDEX;
197         }
198
199         if (bSmallIcon)
200           return ImageList_GetIcon(ShellSmallIconList, index, ILD_NORMAL);
201         return ImageList_GetIcon(ShellBigIconList, index, ILD_NORMAL);
202
203 }
204 /*****************************************************************************
205  * SIC_Initialize                       [internal]
206  *
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
210  */
211 BOOL SIC_Initialize(void)
212 {
213         HICON           hSm, hLg;
214         UINT            index;
215
216         TRACE("\n");
217
218         if (sic_hdpa)   /* already initialized?*/
219           return TRUE;
220
221         sic_hdpa = pDPA_Create(16);
222
223         if (!sic_hdpa)
224         {
225           return(FALSE);
226         }
227
228         ShellSmallIconList = ImageList_Create(16,16,ILC_COLORDDB | ILC_MASK,0,0x20);
229         ShellBigIconList = ImageList_Create(32,32,ILC_COLORDDB | ILC_MASK,0,0x20);
230
231         ImageList_SetBkColor(ShellSmallIconList, GetSysColor(COLOR_WINDOW));
232         ImageList_SetBkColor(ShellBigIconList, GetSysColor(COLOR_WINDOW));
233
234         for (index=1; index<39; index++)
235         {
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);
238
239           if(!hSm)
240           {
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);
243           }
244           SIC_IconAppend ("shell32.dll", index, hSm, hLg);
245         }
246
247         TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
248
249         return TRUE;
250 }
251 /*************************************************************************
252  * SIC_Destroy
253  *
254  * frees the cache
255  */
256 void SIC_Destroy(void)
257 {
258         LPSIC_ENTRY lpsice;
259         int i;
260
261         TRACE("\n");
262
263         EnterCriticalSection(&SHELL32_SicCS);
264
265         if (sic_hdpa && NULL != pDPA_GetPtr (sic_hdpa, 0))
266         {
267           for (i=0; i < pDPA_GetPtrCount(sic_hdpa); ++i)
268           {
269             lpsice = pDPA_GetPtr(sic_hdpa, i);
270             SHFree(lpsice);
271           }
272           pDPA_Destroy(sic_hdpa);
273         }
274
275         sic_hdpa = NULL;
276
277         LeaveCriticalSection(&SHELL32_SicCS);
278         DeleteCriticalSection(&SHELL32_SicCS);
279 }
280 /*************************************************************************
281  * Shell_GetImageList                   [SHELL32.71]
282  *
283  * PARAMETERS
284  *  imglist[1|2] [OUT] pointer which recive imagelist handles
285  *
286  */
287 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
288 {       TRACE("(%p,%p)\n",lpBigList,lpSmallList);
289         if (lpBigList)
290         { *lpBigList = ShellBigIconList;
291         }
292         if (lpSmallList)
293         { *lpSmallList = ShellSmallIconList;
294         }
295
296         return TRUE;
297 }
298 /*************************************************************************
299  * PidlToSicIndex                       [INTERNAL]
300  *
301  * PARAMETERS
302  *      sh      [IN]    IShellFolder
303  *      pidl    [IN]
304  *      bBigIcon [IN]
305  *      uFlags  [IN]    GIL_*
306  *      pIndex  [OUT]   index within the SIC
307  *
308  */
309 BOOL PidlToSicIndex (
310         IShellFolder * sh,
311         LPITEMIDLIST pidl,
312         BOOL bBigIcon,
313         UINT uFlags,
314         UINT * pIndex)
315 {
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;
321
322         TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
323
324         if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconA, 0, (void **)&ei)))
325         {
326           if (SUCCEEDED(IExtractIconA_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
327           {
328             *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
329             ret = TRUE;
330           }
331           IExtractIconA_Release(ei);
332         }
333
334         if (INVALID_INDEX == *pIndex)   /* default icon when failed */
335           *pIndex = 1;
336
337         return ret;
338
339 }
340
341 /*************************************************************************
342  * SHMapPIDLToSystemImageListIndex      [SHELL32.77]
343  *
344  * PARAMETERS
345  *      sh      [IN]            pointer to an instance of IShellFolder
346  *      pidl    [IN]
347  *      pIndex  [OUT][OPTIONAL] SIC index for big icon
348  *
349  */
350 int WINAPI SHMapPIDLToSystemImageListIndex(
351         LPSHELLFOLDER sh,
352         LPCITEMIDLIST pidl,
353         UINT * pIndex)
354 {
355         UINT    Index;
356
357         TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
358         pdump(pidl);
359
360         if (pIndex)
361           PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
362         PidlToSicIndex ( sh, pidl, 0, 0, &Index);
363         return Index;
364 }
365
366 /*************************************************************************
367  * Shell_GetCachedImageIndex            [SHELL32.72]
368  *
369  */
370 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
371 {
372         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
373         return SIC_GetIconIndex(szPath, nIndex);
374 }
375
376 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
377 {       INT ret;
378         LPSTR sTemp = HEAP_strdupWtoA (GetProcessHeap(),0,szPath);
379
380         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
381
382         ret = SIC_GetIconIndex(sTemp, nIndex);
383         HeapFree(GetProcessHeap(),0,sTemp);
384         return ret;
385 }
386
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);
391 }
392
393 /*************************************************************************
394  * ExtractIconEx                        [SHELL32.@]
395  */
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);
400 }
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)
407  */
408 HICON WINAPI ExtractIconExA ( LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
409 {       HICON ret=0;
410
411         TRACE("file=%s idx=%i %p %p num=%i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons );
412
413         if (nIconIndex==-1)     /* Number of icons requested */
414             return PrivateExtractIconsA( lpszFile, -1, 0, 0, NULL, 0, 0, 0 );
415
416         if (phiconLarge)
417         {
418           ret = PrivateExtractIconsA( lpszFile, nIconIndex, 32, 32, phiconLarge, 0, nIcons, 0 );
419           if ( nIcons==1)
420           { ret = phiconLarge[0];
421           }
422         }
423
424         /* if no pointers given and one icon expected, return the handle directly*/
425         if (!phiconLarge && !phiconSmall && nIcons==1 )
426           phiconSmall = &ret;
427
428         if (phiconSmall)
429         {
430           ret = PrivateExtractIconsA( lpszFile, nIconIndex, 16, 16, phiconSmall, 0, nIcons, 0 );
431           if ( nIcons==1 )
432           { ret = phiconSmall[0];
433           }
434         }
435
436         return ret;
437 }
438 /*************************************************************************
439  * ExtractIconExW                       [SHELL32.@]
440  */
441 HICON WINAPI ExtractIconExW ( LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
442 {       LPSTR sFile;
443         DWORD ret;
444
445         TRACE("file=%s idx=%i %p %p num=%i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons );
446
447         sFile = HEAP_strdupWtoA (GetProcessHeap(),0,lpszFile);
448         ret = ExtractIconExA ( sFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
449         HeapFree(GetProcessHeap(),0,sFile);
450         return ret;
451 }