Implement ResetDC and PHYSICALOFFSET[X|Y] devcaps.
[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\n", p1, p2);
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           index = pDPA_Search (sic_hdpa, &sice, -1L, SIC_CompareEntries, 0, 0);
165         }
166
167         if ( INVALID_INDEX == index )
168         {
169           ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
170         }
171         else
172         {
173           TRACE("-- found\n");
174           ret = ((LPSIC_ENTRY)pDPA_GetPtr(sic_hdpa, index))->dwListIndex;
175         }
176
177         LeaveCriticalSection(&SHELL32_SicCS);
178         return ret;
179 }
180 /****************************************************************************
181  * SIC_GetIcon                          [internal]
182  *
183  * NOTES
184  *  retrieves the specified icon from the iconcache. if not found tries to load the icon
185  */
186 static HICON WINE_UNUSED SIC_GetIcon (LPCSTR sSourceFile, INT dwSourceIndex, BOOL bSmallIcon )
187 {       INT index;
188
189         TRACE("%s %i\n", sSourceFile, dwSourceIndex);
190
191         index = SIC_GetIconIndex(sSourceFile, dwSourceIndex);
192
193         if (INVALID_INDEX == index)
194         {
195           return INVALID_INDEX;
196         }
197
198         if (bSmallIcon)
199           return ImageList_GetIcon(ShellSmallIconList, index, ILD_NORMAL);
200         return ImageList_GetIcon(ShellBigIconList, index, ILD_NORMAL);
201         
202 }
203 /*****************************************************************************
204  * SIC_Initialize                       [internal]
205  *
206  * NOTES
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
209  */
210 BOOL SIC_Initialize(void)
211 {
212         HICON           hSm, hLg;
213         UINT            index;
214
215         TRACE("\n");
216
217         if (sic_hdpa)   /* already initialized?*/
218           return TRUE;
219           
220         sic_hdpa = pDPA_Create(16);
221         
222         if (!sic_hdpa)
223         {
224           return(FALSE);
225         }
226
227         ShellSmallIconList = ImageList_Create(16,16,ILC_COLORDDB | ILC_MASK,0,0x20);
228         ShellBigIconList = ImageList_Create(32,32,ILC_COLORDDB | ILC_MASK,0,0x20);
229
230         ImageList_SetBkColor(ShellSmallIconList, GetSysColor(COLOR_WINDOW));
231         ImageList_SetBkColor(ShellBigIconList, GetSysColor(COLOR_WINDOW));
232
233         for (index=1; index<39; index++)
234         {
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);
237
238           if(!hSm)
239           {
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);
242           }
243           SIC_IconAppend ("shell32.dll", index, hSm, hLg);
244         }
245
246         TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
247
248         return TRUE;
249 }
250 /*************************************************************************
251  * SIC_Destroy
252  *
253  * frees the cache
254  */
255 void SIC_Destroy(void)
256 {
257         LPSIC_ENTRY lpsice;
258         int i;
259
260         TRACE("\n");
261
262         EnterCriticalSection(&SHELL32_SicCS);
263
264         if (sic_hdpa && NULL != pDPA_GetPtr (sic_hdpa, 0))
265         {
266           for (i=0; i < pDPA_GetPtrCount(sic_hdpa); ++i)
267           {
268             lpsice = pDPA_GetPtr(sic_hdpa, i); 
269             SHFree(lpsice);
270           }
271           pDPA_Destroy(sic_hdpa);
272         }
273
274         sic_hdpa = NULL;
275
276         LeaveCriticalSection(&SHELL32_SicCS);
277         DeleteCriticalSection(&SHELL32_SicCS);
278 }
279 /*************************************************************************
280  * Shell_GetImageList                   [SHELL32.71]
281  *
282  * PARAMETERS
283  *  imglist[1|2] [OUT] pointer which recive imagelist handles
284  *
285  */
286 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
287 {       TRACE("(%p,%p)\n",lpBigList,lpSmallList);
288         if (lpBigList)
289         { *lpBigList = ShellBigIconList;
290         }
291         if (lpSmallList)
292         { *lpSmallList = ShellSmallIconList;
293         }
294
295         return TRUE;
296 }
297 /*************************************************************************
298  * PidlToSicIndex                       [INTERNAL]
299  *
300  * PARAMETERS
301  *      sh      [IN]    IShellFolder
302  *      pidl    [IN]
303  *      bBigIcon [IN]
304  *      uFlags  [IN]    GIL_*
305  *      pIndex  [OUT]   index within the SIC
306  *
307  */
308 BOOL PidlToSicIndex (
309         IShellFolder * sh,
310         LPITEMIDLIST pidl,
311         BOOL bBigIcon,
312         UINT uFlags,
313         UINT * pIndex)
314 {       
315         IExtractIconA   *ei;
316         char            szIconFile[MAX_PATH];   /* file containing the icon */
317         INT             iSourceIndex;           /* index or resID(negated) in this file */
318         BOOL            ret = FALSE;
319         UINT            dwFlags = 0;
320         
321         TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
322
323         if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconA, 0, (void **)&ei)))
324         {
325           if (SUCCEEDED(IExtractIconA_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
326           {
327             *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
328             ret = TRUE;
329           }
330           IExtractIconA_Release(ei);
331         }
332
333         if (INVALID_INDEX == *pIndex)   /* default icon when failed */
334           *pIndex = 1;
335
336         return ret;
337
338 }
339
340 /*************************************************************************
341  * SHMapPIDLToSystemImageListIndex      [SHELL32.77]
342  *
343  * PARAMETERS
344  *      sh      [IN]            pointer to an instance of IShellFolder 
345  *      pidl    [IN]
346  *      pIndex  [OUT][OPTIONAL] SIC index for big icon
347  *
348  */
349 int WINAPI SHMapPIDLToSystemImageListIndex(
350         LPSHELLFOLDER sh,
351         LPCITEMIDLIST pidl,
352         UINT * pIndex)
353 {
354         UINT    Index;
355
356         TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
357         pdump(pidl);
358         
359         if (pIndex)
360           PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
361         PidlToSicIndex ( sh, pidl, 0, 0, &Index);
362         return Index;
363 }
364
365 /*************************************************************************
366  * Shell_GetCachedImageIndex            [SHELL32.72]
367  *
368  */
369 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc) 
370 {
371         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
372         return SIC_GetIconIndex(szPath, nIndex);
373 }
374
375 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc) 
376 {       INT ret;
377         LPSTR sTemp = HEAP_strdupWtoA (GetProcessHeap(),0,szPath);
378         
379         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
380
381         ret = SIC_GetIconIndex(sTemp, nIndex);
382         HeapFree(GetProcessHeap(),0,sTemp);
383         return ret;
384 }
385
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);
390 }
391
392 /*************************************************************************
393  * ExtractIconEx                        [SHELL32.@]
394  */
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);
399 }
400 /*************************************************************************
401  * ExtractIconExA                       [SHELL32.@]
402  * RETURNS
403  *  0 no icon found 
404  *  1 file is not valid
405  *  HICON handle of a icon (phiconLarge/Small == NULL)
406  */
407 HICON WINAPI ExtractIconExA ( LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
408 {       HICON ret=0;
409         
410         TRACE("file=%s idx=%i %p %p num=%i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons );
411
412         if (nIconIndex==-1)     /* Number of icons requested */
413             return PrivateExtractIconsA( lpszFile, -1, 0, 0, NULL, 0, 0, 0 );
414
415         if (phiconLarge)
416         {
417           ret = PrivateExtractIconsA( lpszFile, nIconIndex, 32, 32, phiconLarge, 0, nIcons, 0 );
418           if ( nIcons==1)
419           { ret = phiconLarge[0];           
420           }
421         }
422
423         /* if no pointers given and one icon expected, return the handle directly*/
424         if (!phiconLarge && !phiconSmall && nIcons==1 )
425           phiconSmall = &ret;
426         
427         if (phiconSmall)
428         {
429           ret = PrivateExtractIconsA( lpszFile, nIconIndex, 16, 16, phiconSmall, 0, nIcons, 0 );
430           if ( nIcons==1 )
431           { ret = phiconSmall[0];
432           }
433         }
434
435         return ret;
436 }
437 /*************************************************************************
438  * ExtractIconExW                       [SHELL32.@]
439  */
440 HICON WINAPI ExtractIconExW ( LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
441 {       LPSTR sFile;
442         DWORD ret;
443         
444         TRACE("file=%s idx=%i %p %p num=%i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons );
445
446         sFile = HEAP_strdupWtoA (GetProcessHeap(),0,lpszFile);
447         ret = ExtractIconExA ( sFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
448         HeapFree(GetProcessHeap(),0,sFile);
449         return ret;
450 }