Moved server.h to include/wine.
[wine] / dlls / shell32 / iconcache.c
1 /*
2  *      shell icon cache (SIC)
3  *
4  */
5 #include <string.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include "winbase.h"
9 #include "windef.h"
10 #include "wingdi.h"
11 #include "winuser.h"
12 #include "wine/winuser16.h"
13 #include "wine/winbase16.h"
14 #include "heap.h"
15 #include "debugtools.h"
16
17 #include "shellapi.h"
18 #include "shlguid.h"
19 #include "pidl.h"
20 #include "shell32_main.h"
21 #include "wine/undocshell.h"
22 #include "shlwapi.h"
23
24 DEFAULT_DEBUG_CHANNEL(shell);
25
26 /********************** THE ICON CACHE ********************************/
27
28 #define INVALID_INDEX -1
29
30 typedef struct
31 {       LPCSTR sSourceFile;     /* file (not path!) containing the icon */
32         DWORD dwSourceIndex;    /* index within the file, if it is a resoure ID it will be negated */
33         DWORD dwListIndex;      /* index within the iconlist */
34         DWORD dwFlags;          /* GIL_* flags */
35         DWORD dwAccessTime;
36 } SIC_ENTRY, * LPSIC_ENTRY;
37
38 static HDPA             sic_hdpa = 0;
39 static CRITICAL_SECTION SHELL32_SicCS = CRITICAL_SECTION_INIT;
40
41 /*****************************************************************************
42  * SIC_CompareEntrys                    [called by comctl32.dll]
43  *
44  * NOTES
45  *  Callback for DPA_Search
46  */
47 INT CALLBACK SIC_CompareEntrys( LPVOID p1, LPVOID p2, LPARAM lparam)
48 {       TRACE("%p %p\n", p1, p2);
49
50         if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
51           return 1;
52
53         if (strcasecmp(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
54           return 1;
55
56         return 0;  
57 }
58 /*****************************************************************************
59  * SIC_IconAppend                       [internal]
60  *
61  * NOTES
62  *  appends a icon pair to the end of the cache
63  */
64 static INT SIC_IconAppend (LPCSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
65 {       LPSIC_ENTRY lpsice;
66         INT ret, index, index1;
67         
68         TRACE("%s %i %x %x\n", sSourceFile, dwSourceIndex, hSmallIcon ,hBigIcon);
69
70         lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
71
72         lpsice->sSourceFile = HEAP_strdupA (GetProcessHeap(), 0, PathFindFileNameA(sSourceFile));
73         lpsice->dwSourceIndex = dwSourceIndex;
74         
75         EnterCriticalSection(&SHELL32_SicCS);
76
77         index = pDPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
78         if ( INVALID_INDEX == index )
79         {
80           SHFree(lpsice);
81           ret = INVALID_INDEX;
82         }
83         else
84         {
85           index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
86           index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
87
88           if (index!=index1)
89           {
90             FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
91           }
92           lpsice->dwListIndex = index;
93           ret = lpsice->dwListIndex;
94         }
95
96         LeaveCriticalSection(&SHELL32_SicCS);
97         return ret;     
98 }
99 /****************************************************************************
100  * SIC_LoadIcon                         [internal]
101  *
102  * NOTES
103  *  gets small/big icon by number from a file
104  */
105 static INT SIC_LoadIcon (LPCSTR sSourceFile, INT dwSourceIndex)
106 {       HICON   hiconLarge=0;
107         HICON   hiconSmall=0;
108
109         PrivateExtractIconsA( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 ); 
110         PrivateExtractIconsA( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 ); 
111
112         if ( !hiconLarge ||  !hiconSmall)
113         {
114           WARN("failure loading icon %i from %s (%x %x)\n", dwSourceIndex, sSourceFile, hiconLarge, hiconSmall);
115           return -1;
116         }
117         return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);             
118 }
119 /*****************************************************************************
120  * SIC_GetIconIndex                     [internal]
121  *
122  * Parameters
123  *      sSourceFile     [IN]    filename of file containing the icon
124  *      index           [IN]    index/resID (negated) in this file
125  *
126  * NOTES
127  *  look in the cache for a proper icon. if not available the icon is taken
128  *  from the file and cached
129  */
130 INT SIC_GetIconIndex (LPCSTR sSourceFile, INT dwSourceIndex )
131 {       SIC_ENTRY sice;
132         INT ret, index = INVALID_INDEX;
133                 
134         TRACE("%s %i\n", sSourceFile, dwSourceIndex);
135
136         sice.sSourceFile = PathFindFileNameA(sSourceFile);
137         sice.dwSourceIndex = dwSourceIndex;
138         
139         EnterCriticalSection(&SHELL32_SicCS);
140
141         if (NULL != pDPA_GetPtr (sic_hdpa, 0))
142         {
143           index = pDPA_Search (sic_hdpa, &sice, -1L, SIC_CompareEntrys, 0, 0);
144         }
145
146         if ( INVALID_INDEX == index )
147         {
148           ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
149         }
150         else
151         {
152           TRACE("-- found\n");
153           ret = ((LPSIC_ENTRY)pDPA_GetPtr(sic_hdpa, index))->dwListIndex;
154         }
155
156         LeaveCriticalSection(&SHELL32_SicCS);
157         return ret;
158 }
159 /****************************************************************************
160  * SIC_GetIcon                          [internal]
161  *
162  * NOTES
163  *  retrives the specified icon from the iconcache. if not found try's to load the icon
164  */
165 static HICON WINE_UNUSED SIC_GetIcon (LPCSTR sSourceFile, INT dwSourceIndex, BOOL bSmallIcon )
166 {       INT index;
167
168         TRACE("%s %i\n", sSourceFile, dwSourceIndex);
169
170         index = SIC_GetIconIndex(sSourceFile, dwSourceIndex);
171
172         if (INVALID_INDEX == index)
173         {
174           return INVALID_INDEX;
175         }
176
177         if (bSmallIcon)
178           return ImageList_GetIcon(ShellSmallIconList, index, ILD_NORMAL);
179         return ImageList_GetIcon(ShellBigIconList, index, ILD_NORMAL);
180         
181 }
182 /*****************************************************************************
183  * SIC_Initialize                       [internal]
184  *
185  * NOTES
186  *  hack to load the resources from the shell32.dll under a different dll name 
187  *  will be removed when the resource-compiler is ready
188  */
189 BOOL SIC_Initialize(void)
190 {
191         HICON           hSm, hLg;
192         UINT            index;
193
194         TRACE("\n");
195
196         if (sic_hdpa)   /* already initialized?*/
197           return TRUE;
198           
199         sic_hdpa = pDPA_Create(16);
200         
201         if (!sic_hdpa)
202         {
203           return(FALSE);
204         }
205
206         ShellSmallIconList = ImageList_Create(16,16,ILC_COLORDDB | ILC_MASK,0,0x20);
207         ShellBigIconList = ImageList_Create(32,32,ILC_COLORDDB | ILC_MASK,0,0x20);
208
209         ImageList_SetBkColor(ShellSmallIconList, GetSysColor(COLOR_WINDOW));
210         ImageList_SetBkColor(ShellBigIconList, GetSysColor(COLOR_WINDOW));
211
212         for (index=1; index<39; index++)
213         {
214           hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 16, 16,LR_SHARED);
215           hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 32, 32,LR_SHARED);
216
217           if(!hSm)
218           {
219             hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 16, 16,LR_SHARED);
220             hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 32, 32,LR_SHARED);
221           }
222           SIC_IconAppend ("shell32.dll", index, hSm, hLg);
223         }
224
225         TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
226
227         return TRUE;
228 }
229 /*************************************************************************
230  * SIC_Destroy
231  *
232  * frees the cache
233  */
234 void SIC_Destroy(void)
235 {
236         LPSIC_ENTRY lpsice;
237         int i;
238
239         TRACE("\n");
240
241         EnterCriticalSection(&SHELL32_SicCS);
242
243         if (sic_hdpa && NULL != pDPA_GetPtr (sic_hdpa, 0))
244         {
245           for (i=0; i < pDPA_GetPtrCount(sic_hdpa); ++i)
246           {
247             lpsice = pDPA_GetPtr(sic_hdpa, i); 
248             SHFree(lpsice);
249           }
250           pDPA_Destroy(sic_hdpa);
251         }
252
253         sic_hdpa = NULL;
254
255         LeaveCriticalSection(&SHELL32_SicCS);
256         DeleteCriticalSection(&SHELL32_SicCS);
257 }
258 /*************************************************************************
259  * Shell_GetImageList                   [SHELL32.71]
260  *
261  * PARAMETERS
262  *  imglist[1|2] [OUT] pointer which recive imagelist handles
263  *
264  */
265 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
266 {       TRACE("(%p,%p)\n",lpBigList,lpSmallList);
267         if (lpBigList)
268         { *lpBigList = ShellBigIconList;
269         }
270         if (lpSmallList)
271         { *lpSmallList = ShellSmallIconList;
272         }
273
274         return TRUE;
275 }
276 /*************************************************************************
277  * PidlToSicIndex                       [INTERNAL]
278  *
279  * PARAMETERS
280  *      sh      [IN]    IShellFolder
281  *      pidl    [IN]
282  *      bBigIcon [IN]
283  *      uFlags  [IN]    GIL_*
284  *      pIndex  [OUT]   index within the SIC
285  *
286  */
287 BOOL PidlToSicIndex (
288         IShellFolder * sh,
289         LPITEMIDLIST pidl,
290         BOOL bBigIcon,
291         UINT uFlags,
292         UINT * pIndex)
293 {       
294         IExtractIconA   *ei;
295         char            szIconFile[MAX_PATH];   /* file containing the icon */
296         INT             iSourceIndex;           /* index or resID(negated) in this file */
297         BOOL            ret = FALSE;
298         UINT            dwFlags = 0;
299         
300         TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
301
302         if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconA, 0, (void **)&ei)))
303         {
304           if (SUCCEEDED(IExtractIconA_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
305           {
306             *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
307             ret = TRUE;
308           }
309           IExtractIconA_Release(ei);
310         }
311
312         if (INVALID_INDEX == *pIndex)   /* default icon when failed */
313           *pIndex = 1;
314
315         return ret;
316
317 }
318
319 /*************************************************************************
320  * SHMapPIDLToSystemImageListIndex      [SHELL32.77]
321  *
322  * PARAMETERS
323  *      sh      [IN]            pointer to an instance of IShellFolder 
324  *      pidl    [IN]
325  *      pIndex  [OUT][OPTIONAL] SIC index for big icon
326  *
327  */
328 int WINAPI SHMapPIDLToSystemImageListIndex(
329         LPSHELLFOLDER sh,
330         LPCITEMIDLIST pidl,
331         UINT * pIndex)
332 {
333         UINT    Index;
334
335         TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
336         pdump(pidl);
337         
338         if (pIndex)
339           PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
340         PidlToSicIndex ( sh, pidl, 0, 0, &Index);
341         return Index;
342 }
343
344 /*************************************************************************
345  * Shell_GetCachedImageIndex            [SHELL32.72]
346  *
347  */
348 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc) 
349 {
350         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
351         return SIC_GetIconIndex(szPath, nIndex);
352 }
353
354 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc) 
355 {       INT ret;
356         LPSTR sTemp = HEAP_strdupWtoA (GetProcessHeap(),0,szPath);
357         
358         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
359
360         ret = SIC_GetIconIndex(sTemp, nIndex);
361         HeapFree(GetProcessHeap(),0,sTemp);
362         return ret;
363 }
364
365 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
366 {       if( SHELL_OsIsUnicode())
367           return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
368         return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
369 }
370
371 /*************************************************************************
372  * ExtractIconEx                        [SHELL32.@]
373  */
374 HICON WINAPI ExtractIconExAW ( LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
375 {       if (SHELL_OsIsUnicode())
376           return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
377         return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
378 }
379 /*************************************************************************
380  * ExtractIconExA                       [SHELL32.@]
381  * RETURNS
382  *  0 no icon found 
383  *  1 file is not valid
384  *  HICON handle of a icon (phiconLarge/Small == NULL)
385  */
386 HICON WINAPI ExtractIconExA ( LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
387 {       HICON ret=0;
388         
389         TRACE("file=%s idx=%i %p %p num=%i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons );
390
391         if (nIconIndex==-1)     /* Number of icons requested */
392             return PrivateExtractIconsA( lpszFile, -1, 0, 0, NULL, 0, 0, 0 );
393
394         if (phiconLarge)
395         {
396           ret = PrivateExtractIconsA( lpszFile, nIconIndex, 32, 32, phiconLarge, 0, nIcons, 0 );
397           if ( nIcons==1)
398           { ret = phiconLarge[0];           
399           }
400         }
401
402         /* if no pointers given and one icon expected, return the handle directly*/
403         if (!phiconLarge && ! phiconSmall && nIcons==1 )
404           phiconSmall = &ret;
405         
406         if (phiconSmall)
407         {
408           ret = PrivateExtractIconsA( lpszFile, nIconIndex, 16, 16, phiconSmall, 0, nIcons, 0 );
409           if ( nIcons==1 )
410           { ret = phiconSmall[0];
411           }
412         }
413
414         return ret;
415 }
416 /*************************************************************************
417  * ExtractIconExW                       [SHELL32.@]
418  */
419 HICON WINAPI ExtractIconExW ( LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
420 {       LPSTR sFile;
421         DWORD ret;
422         
423         TRACE("file=%s idx=%i %p %p num=%i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons );
424
425         sFile = HEAP_strdupWtoA (GetProcessHeap(),0,lpszFile);
426         ret = ExtractIconExA ( sFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
427         HeapFree(GetProcessHeap(),0,sFile);
428         return ret;
429 }