source: trunk/src/shell32/new/iconcache.cpp@ 791

Last change on this file since 791 was 791, checked in by phaller, 26 years ago

Add: shell32/new - a direct port of WINE's Shell32 implmentation

File size: 25.8 KB
Line 
1/*
2 * shell icon cache (SIC)
3 *
4 */
5#include <string.h>
6#include <odin.h>
7
8#define ICOM_CINTERFACE 1
9#define CINTERFACE 1
10
11#include "winbase.h"
12#include "winuser.h"
13#include "wingdi.h"
14#include "wine/winuser16.h"
15#include "wine/winbase16.h"
16#include "neexe.h"
17#include "cursoricon.h"
18#include "module.h"
19#include "heap.h"
20#include "debugtools.h"
21#include "winversion.h"
22
23#include "shellapi.h"
24#include "pidl.h"
25#include "shell32_main.h"
26
27#include <heapstring.h>
28#include <misc.h>
29
30DEFAULT_DEBUG_CHANNEL(shell)
31
32#include "pshpack1.h"
33
34typedef struct
35{
36 BYTE bWidth; /* Width, in pixels, of the image */
37 BYTE bHeight; /* Height, in pixels, of the image */
38 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
39 BYTE bReserved; /* Reserved ( must be 0) */
40 WORD wPlanes; /* Color Planes */
41 WORD wBitCount; /* Bits per pixel */
42 DWORD dwBytesInRes; /* How many bytes in this resource? */
43 DWORD dwImageOffset; /* Where in the file is this image? */
44} icoICONDIRENTRY, *LPicoICONDIRENTRY;
45
46typedef struct
47{
48 WORD idReserved; /* Reserved (must be 0) */
49 WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
50 WORD idCount; /* How many images */
51 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
52} icoICONDIR, *LPicoICONDIR;
53
54#include "poppack.h"
55
56#if 0
57static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
58{
59 TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
60 TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
61 TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
62 entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
63}
64static void dumpIcoDir ( LPicoICONDIR entry )
65{
66 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
67}
68#endif
69/*************************************************************************
70 * SHELL_GetResourceTable
71 */
72static DWORD SHELL_GetResourceTable(HFILE hFile, LPBYTE *retptr)
73{ IMAGE_DOS_HEADER mz_header;
74 char magic[4];
75 int size;
76
77 TRACE("0x%08x %p\n", hFile, retptr);
78
79 *retptr = NULL;
80 _llseek( hFile, 0, SEEK_SET );
81 if ((_lread(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) || (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
82 { if (mz_header.e_cblp == 1) /* .ICO file ? */
83 { *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
84 return 1;
85 }
86 else
87 return 0; /* failed */
88 }
89 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
90
91 if (_lread( hFile, magic, sizeof(magic) ) != sizeof(magic))
92 return 0;
93
94 _llseek( hFile, mz_header.e_lfanew, SEEK_SET);
95
96 if (*(DWORD*)magic == IMAGE_NT_SIGNATURE)
97 return IMAGE_NT_SIGNATURE;
98
99 if (*(WORD*)magic == IMAGE_OS2_SIGNATURE)
100 { IMAGE_OS2_HEADER ne_header;
101 LPBYTE pTypeInfo = (LPBYTE)-1;
102
103 if (_lread(hFile,&ne_header,sizeof(ne_header))!=sizeof(ne_header))
104 return 0;
105
106 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE)
107 return 0;
108
109 size = ne_header.rname_tab_offset - ne_header.resource_tab_offset;
110
111 if( size > sizeof(NE_TYPEINFO) )
112 { pTypeInfo = (BYTE*)HeapAlloc( GetProcessHeap(), 0, size);
113 if( pTypeInfo )
114 { _llseek(hFile, mz_header.e_lfanew+ne_header.resource_tab_offset, SEEK_SET);
115 if( _lread( hFile, (char*)pTypeInfo, size) != size )
116 { HeapFree( GetProcessHeap(), 0, pTypeInfo);
117 pTypeInfo = NULL;
118 }
119 }
120 }
121 *retptr = pTypeInfo;
122 return IMAGE_OS2_SIGNATURE;
123 }
124 return 0; /* failed */
125}
126/*************************************************************************
127 * SHELL_LoadResource
128 */
129static BYTE * SHELL_LoadResource( HFILE hFile, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
130{ BYTE* ptr;
131
132 TRACE("0x%08x %p 0x%08x\n", hFile, pNInfo, sizeShift);
133
134 *uSize = (DWORD)pNInfo->length << sizeShift;
135 if( (ptr = (BYTE*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
136 { _llseek( hFile, (DWORD)pNInfo->offset << sizeShift, SEEK_SET);
137 _lread( hFile, (char*)ptr, pNInfo->length << sizeShift);
138 return ptr;
139 }
140 return 0;
141}
142
143/*************************************************************************
144 * ICO_LoadIcon
145 */
146static BYTE * ICO_LoadIcon( HFILE hFile, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
147{ BYTE* ptr;
148
149 TRACE("0x%08x %p\n", hFile, lpiIDE);
150
151 *uSize = lpiIDE->dwBytesInRes;
152 if( (ptr = (BYTE*)HeapAlloc(GetProcessHeap(),0, *uSize)) )
153 { _llseek( hFile, lpiIDE->dwImageOffset, SEEK_SET);
154 _lread( hFile, (char*)ptr, lpiIDE->dwBytesInRes);
155 return ptr;
156 }
157
158 return 0;
159}
160
161/*************************************************************************
162 * ICO_GetIconDirectory
163 *
164 * Reads .ico file and build phony ICONDIR struct
165 * see http://www.microsoft.com/win32dev/ui/icons.htm
166 */
167#define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
168#define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
169
170static BYTE * ICO_GetIconDirectory( HFILE hFile, LPicoICONDIR* lplpiID, ULONG *uSize )
171{ CURSORICONDIR lpcid; /* icon resource in resource-dir format */
172 LPicoICONDIR lpiID; /* icon resource in file format */
173 int i;
174
175 TRACE("0x%08x %p\n", hFile, lplpiID);
176
177 _llseek( hFile, 0, SEEK_SET );
178 if( _lread(hFile,(char*)&lpcid, HEADER_SIZE_FILE) != HEADER_SIZE_FILE )
179 return 0;
180
181 if( lpcid.idReserved || (lpcid.idType != 1) || (!lpcid.idCount) )
182 return 0;
183
184 i = lpcid.idCount * sizeof(icoICONDIRENTRY);
185 lpiID = (LPicoICONDIR)HeapAlloc( GetProcessHeap(), 0, HEADER_SIZE_FILE + i);
186
187 if( _lread(hFile,(char*)lpiID->idEntries,i) == i )
188 { CURSORICONDIR * lpID; /* icon resource in resource format */
189 *uSize = lpcid.idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
190 if( (lpID = (CURSORICONDIR*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
191 {
192 /* copy the header */
193 lpID->idReserved = lpiID->idReserved = 0;
194 lpID->idType = lpiID->idType = 1;
195 lpID->idCount = lpiID->idCount = lpcid.idCount;
196
197 /* copy the entrys */
198 for( i=0; i < lpiID->idCount; i++ )
199 { memcpy((void*)&(lpID->idEntries[i]),(void*)&(lpiID->idEntries[i]), sizeof(CURSORICONDIRENTRY) - 2);
200 lpID->idEntries[i].wResId = i;
201 }
202
203 *lplpiID = lpiID;
204 return (BYTE *)lpID;
205 }
206 }
207 /* fail */
208
209 HeapFree( GetProcessHeap(), 0, lpiID);
210 return 0;
211}
212
213/*************************************************************************
214 * InternalExtractIcon [SHELL.39]
215 *
216 * This abortion is called directly by Progman
217 * fixme: the icon section is broken (don't have a handle for
218 * ICO_GetIconDirectory....)
219 *
220 */
221#define ICO_INVALID_FILE 1
222#define ICO_NO_ICONS 0
223
224HGLOBAL WINAPI ICO_ExtractIconEx(LPCSTR lpszExeFileName, HICON * RetPtr, UINT nIconIndex, UINT n, UINT cxDesired, UINT cyDesired )
225{ HGLOBAL hRet = ICO_NO_ICONS;
226 LPBYTE pData;
227 OFSTRUCT ofs;
228 DWORD sig;
229 HFILE hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
230 UINT16 iconDirCount = 0,iconCount = 0;
231 LPBYTE peimage;
232 HANDLE fmapping;
233 ULONG uSize;
234
235 TRACE("(file %s,start %d,extract %d\n", lpszExeFileName, nIconIndex, n);
236
237 if( hFile == HFILE_ERROR || !n )
238 return ICO_INVALID_FILE;
239
240 sig = SHELL_GetResourceTable(hFile,&pData);
241
242/* ico file */
243 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
244 { BYTE *pCIDir = 0;
245 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
246 NE_NAMEINFO *pIconStorage = NULL;
247 NE_NAMEINFO *pIconDir = NULL;
248 LPicoICONDIR lpiID = NULL;
249
250 TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
251
252 if( pData == (BYTE*)-1 )
253 { pCIDir = ICO_GetIconDirectory(hFile, &lpiID, &uSize); /* check for .ICO file */
254 if( pCIDir )
255 { iconDirCount = 1; iconCount = lpiID->idCount;
256 TRACE("-- icon found %p 0x%08lx 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
257 }
258 }
259 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
260 { if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
261 { iconDirCount = pTInfo->count;
262 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
263 TRACE("\tfound directory - %i icon families\n", iconDirCount);
264 }
265 if( pTInfo->type_id == NE_RSCTYPE_ICON )
266 { iconCount = pTInfo->count;
267 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
268 TRACE("\ttotal icons - %i\n", iconCount);
269 }
270 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
271 }
272
273 if( (pIconStorage && pIconDir) || lpiID ) /* load resources and create icons */
274 { if( nIconIndex == (UINT16)-1 )
275 { RetPtr[0] = iconDirCount;
276 }
277 else if( nIconIndex < iconDirCount )
278 { UINT16 i, icon;
279 if( n > iconDirCount - nIconIndex )
280 n = iconDirCount - nIconIndex;
281
282 for( i = nIconIndex; i < nIconIndex + n; i++ )
283 { /* .ICO files have only one icon directory */
284
285 if( lpiID == NULL ) /* *.ico */
286 pCIDir = SHELL_LoadResource( hFile, pIconDir + i, *(WORD*)pData, &uSize );
287 RetPtr[i-nIconIndex] = pLookupIconIdFromDirectoryEx( pCIDir, TRUE, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0);
288 HeapFree(GetProcessHeap(), 0, pCIDir);
289 }
290
291 for( icon = nIconIndex; icon < nIconIndex + n; icon++ )
292 { pCIDir = NULL;
293 if( lpiID )
294 { pCIDir = ICO_LoadIcon( hFile, lpiID->idEntries + RetPtr[icon-nIconIndex], &uSize);
295 }
296 else
297 { for( i = 0; i < iconCount; i++ )
298 { if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
299 { pCIDir = SHELL_LoadResource( hFile, pIconStorage + i,*(WORD*)pData, &uSize );
300 }
301 }
302 }
303 if( pCIDir )
304 { RetPtr[icon-nIconIndex] = (HICON) pCreateIconFromResourceEx(pCIDir,uSize,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
305 }
306 else
307 { RetPtr[icon-nIconIndex] = 0;
308 }
309 }
310 }
311 }
312 if( lpiID )
313 HeapFree( GetProcessHeap(), 0, lpiID);
314 else
315 HeapFree( GetProcessHeap(), 0, pData);
316 }
317/* end ico file */
318
319/* exe/dll */
320 if( sig == IMAGE_NT_SIGNATURE)
321 { LPBYTE idata,igdata;
322 PIMAGE_DOS_HEADER dheader;
323 PIMAGE_NT_HEADERS pe_header;
324 PIMAGE_SECTION_HEADER pe_sections;
325 PIMAGE_RESOURCE_DIRECTORY rootresdir,iconresdir,icongroupresdir;
326 PIMAGE_RESOURCE_DATA_ENTRY idataent,igdataent;
327 PIMAGE_RESOURCE_DIRECTORY_ENTRY xresent;
328 int i,j;
329
330 if ( !(fmapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL)))
331 { WARN("failed to create filemap.\n"); /* FIXME, INVALID_HANDLE_VALUE? */
332 hRet = ICO_INVALID_FILE;
333 goto end_2; /* failure */
334 }
335
336 if ( !(peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0)))
337 { WARN("failed to mmap filemap.\n");
338 hRet = ICO_INVALID_FILE;
339 goto end_2; /* failure */
340 }
341
342 dheader = (PIMAGE_DOS_HEADER)peimage;
343 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, SHELL_GetResourceTable checked that */
344 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header)); /* probably makes problems with short PE headers...*/
345 rootresdir = NULL;
346
347 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
348 { if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
349 continue;
350 /* FIXME: doesn't work when the resources are not in a seperate section */
351 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
352 { rootresdir = (PIMAGE_RESOURCE_DIRECTORY)((char*)peimage+pe_sections[i].PointerToRawData);
353 break;
354 }
355 }
356
357 if (!rootresdir)
358 { WARN("haven't found section for resource directory.\n");
359 goto end_4; /* failure */
360 }
361 /* search the group icon dir*/
362 if (!(icongroupresdir = GetResDirEntryW(rootresdir,RT_GROUP_ICONW, (DWORD)rootresdir,FALSE)))
363 { WARN("No Icongroupresourcedirectory!\n");
364 goto end_4; /* failure */
365 }
366 iconDirCount = icongroupresdir->NumberOfNamedEntries+icongroupresdir->NumberOfIdEntries;
367
368 /* number of icons requested */
369 if( nIconIndex == -1 )
370 { hRet = iconDirCount;
371 goto end_3; /* success */
372 }
373
374 if (nIconIndex >= iconDirCount)
375 { WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
376 goto end_4; /* failure */
377 }
378
379 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1); /* caller just wanted the number of entries */
380
381 if( n > iconDirCount - nIconIndex ) /* assure we don't get too much ... */
382 { n = iconDirCount - nIconIndex;
383 }
384
385 xresent = xresent+nIconIndex; /* starting from specified index ... */
386
387 for (i=0;i<n;i++,xresent++)
388 { PIMAGE_RESOURCE_DIRECTORY resdir;
389
390 /* go down this resource entry, name */
391 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s.OffsetToDirectory));
392
393 /* default language (0) */
394 resdir = GetResDirEntryW(resdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
395 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
396
397 /* lookup address in mapped image for virtual address */
398 igdata = NULL;
399
400 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
401 { if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
402 continue;
403 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
404 continue;
405 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
406 }
407
408 if (!igdata)
409 { WARN("no matching real address for icongroup!\n");
410 goto end_4; /* failure */
411 }
412 RetPtr[i] = (HICON)pLookupIconIdFromDirectoryEx(igdata, TRUE, cxDesired, cyDesired, LR_DEFAULTCOLOR);
413 }
414
415 if (!(iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,(DWORD)rootresdir,FALSE)))
416 { WARN("No Iconresourcedirectory!\n");
417 goto end_4; /* failure */
418 }
419
420 for (i=0;i<n;i++)
421 { PIMAGE_RESOURCE_DIRECTORY xresdir;
422 xresdir = GetResDirEntryW(iconresdir,(LPWSTR)(DWORD)RetPtr[i],(DWORD)rootresdir,FALSE);
423 xresdir = GetResDirEntryW(xresdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
424 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
425 idata = NULL;
426
427 /* map virtual to address in image */
428 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
429 { if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
430 continue;
431 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
432 continue;
433 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
434 }
435 if (!idata)
436 { WARN("no matching real address found for icondata!\n");
437 RetPtr[i]=0;
438 continue;
439 }
440 RetPtr[i] = (HICON) pCreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
441 }
442 hRet = RetPtr[0]; /* return first icon */
443 goto end_3; /* sucess */
444 }
445 hRet = ICO_INVALID_FILE;
446 goto end_1; /* unknown filetype */
447
448/* cleaning up (try & catch would be nicer:-) ) */
449end_4: hRet = 0; /* failure */
450end_3: UnmapViewOfFile(peimage); /* success */
451end_2: CloseHandle(fmapping);
452end_1: _lclose( hFile);
453 return hRet;
454}
455
456/********************** THE ICON CACHE ********************************/
457
458#define INVALID_INDEX -1
459
460typedef struct
461{ LPCSTR sSourceFile; /* file (not path!) containing the icon */
462 DWORD dwSourceIndex; /* index within the file, if it is a resoure ID it will be negated */
463 DWORD dwListIndex; /* index within the iconlist */
464 DWORD dwFlags; /* GIL_* flags */
465 DWORD dwAccessTime;
466} SIC_ENTRY, * LPSIC_ENTRY;
467
468static HDPA sic_hdpa = 0;
469static CRITICAL_SECTION SHELL32_SicCS;
470
471/*****************************************************************************
472 * SIC_CompareEntrys [called by comctl32.dll]
473 *
474 * NOTES
475 * Callback for DPA_Search
476 */
477INT CALLBACK SIC_CompareEntrys( LPVOID p1, LPVOID p2, LPARAM lparam)
478{ TRACE("%p %p\n", p1, p2);
479
480 if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
481 return 1;
482
483 if (strcasecmp(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
484 return 1;
485
486 return 0;
487}
488/*****************************************************************************
489 * SIC_IconAppend [internal]
490 *
491 * NOTES
492 * appends a icon pair to the end of the cache
493 */
494static INT SIC_IconAppend (LPCSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
495{ LPSIC_ENTRY lpsice;
496 INT ret, index, index1;
497
498 TRACE("%s %i %x %x\n", sSourceFile, dwSourceIndex, hSmallIcon ,hBigIcon);
499
500 lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
501
502 lpsice->sSourceFile = HEAP_strdupA (GetProcessHeap(), 0, PathFindFilenameA(sSourceFile));
503 lpsice->dwSourceIndex = dwSourceIndex;
504
505 EnterCriticalSection(&SHELL32_SicCS);
506
507 index = pDPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
508 if ( INVALID_INDEX == index )
509 {
510 SHFree(lpsice);
511 ret = INVALID_INDEX;
512 }
513 else
514 {
515 index = pImageList_AddIcon (ShellSmallIconList, hSmallIcon);
516 index1= pImageList_AddIcon (ShellBigIconList, hBigIcon);
517
518 if (index!=index1)
519 {
520 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
521 }
522 lpsice->dwListIndex = index;
523 ret = lpsice->dwListIndex;
524 }
525
526 LeaveCriticalSection(&SHELL32_SicCS);
527 return ret;
528}
529/****************************************************************************
530 * SIC_LoadIcon [internal]
531 *
532 * NOTES
533 * gets small/big icon by number from a file
534 */
535static INT SIC_LoadIcon (LPCSTR sSourceFile, INT dwSourceIndex)
536{ HICON hiconLarge=0;
537 HICON hiconSmall=0;
538
539 ICO_ExtractIconEx(sSourceFile, &hiconLarge, dwSourceIndex, 1, 32, 32 );
540 ICO_ExtractIconEx(sSourceFile, &hiconSmall, dwSourceIndex, 1, 16, 16 );
541
542
543 if ( !hiconLarge || !hiconSmall)
544 {
545 WARN("failure loading icon %i from %s (%x %x)\n", dwSourceIndex, sSourceFile, hiconLarge, hiconSmall);
546 return -1;
547 }
548 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
549}
550/*****************************************************************************
551 * SIC_GetIconIndex [internal]
552 *
553 * Parameters
554 * sSourceFile [IN] filename of file containing the icon
555 * index [IN] index/resID (negated) in this file
556 *
557 * NOTES
558 * look in the cache for a proper icon. if not available the icon is taken
559 * from the file and cached
560 */
561INT SIC_GetIconIndex (LPCSTR sSourceFile, INT dwSourceIndex )
562{ SIC_ENTRY sice;
563 INT ret, index = INVALID_INDEX;
564
565 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
566
567 sice.sSourceFile = PathFindFilenameA(sSourceFile);
568 sice.dwSourceIndex = dwSourceIndex;
569
570 EnterCriticalSection(&SHELL32_SicCS);
571
572 if (NULL != pDPA_GetPtr (sic_hdpa, 0))
573 {
574 index = pDPA_Search (sic_hdpa, &sice, -1L, SIC_CompareEntrys, 0, 0);
575 }
576
577 if ( INVALID_INDEX == index )
578 {
579 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
580 }
581 else
582 {
583 TRACE("-- found\n");
584 ret = ((LPSIC_ENTRY)pDPA_GetPtr(sic_hdpa, index))->dwListIndex;
585 }
586
587 LeaveCriticalSection(&SHELL32_SicCS);
588 return ret;
589}
590/****************************************************************************
591 * SIC_LoadIcon [internal]
592 *
593 * NOTES
594 * retrives the specified icon from the iconcache. if not found try's to load the icon
595 */
596static HICON WINE_UNUSED SIC_GetIcon (LPCSTR sSourceFile, INT dwSourceIndex, BOOL bSmallIcon )
597{ INT index;
598
599 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
600
601 index = SIC_GetIconIndex(sSourceFile, dwSourceIndex);
602
603 if (INVALID_INDEX == index)
604 {
605 return INVALID_INDEX;
606 }
607
608 if (bSmallIcon)
609 return pImageList_GetIcon(ShellSmallIconList, index, ILD_NORMAL);
610 return pImageList_GetIcon(ShellBigIconList, index, ILD_NORMAL);
611
612}
613/*****************************************************************************
614 * SIC_Initialize [internal]
615 *
616 * NOTES
617 * hack to load the resources from the shell32.dll under a different dll name
618 * will be removed when the resource-compiler is ready
619 */
620BOOL SIC_Initialize(void)
621{
622 HICON hSm, hLg;
623 UINT index;
624
625 TRACE("\n");
626
627 if (sic_hdpa) /* already initialized?*/
628 return TRUE;
629
630 InitializeCriticalSection(&SHELL32_SicCS);
631
632 sic_hdpa = pDPA_Create(16);
633
634 if (!sic_hdpa)
635 {
636 return(FALSE);
637 }
638
639 ShellSmallIconList = pImageList_Create(16,16,ILC_COLORDDB | ILC_MASK,0,0x20);
640 ShellBigIconList = pImageList_Create(32,32,ILC_COLORDDB | ILC_MASK,0,0x20);
641
642 pImageList_SetBkColor(ShellSmallIconList, GetSysColor(COLOR_WINDOW));
643 pImageList_SetBkColor(ShellBigIconList, GetSysColor(COLOR_WINDOW));
644
645 for (index=1; index<39; index++)
646 {
647 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 16, 16,LR_SHARED);
648 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 32, 32,LR_SHARED);
649
650 if(!hSm)
651 {
652 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 16, 16,LR_SHARED);
653 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 32, 32,LR_SHARED);
654 }
655 SIC_IconAppend ("shell32.dll", index, hSm, hLg);
656 }
657
658 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
659
660 return TRUE;
661}
662/*************************************************************************
663 * SIC_Destroy
664 *
665 * frees the cache
666 */
667void SIC_Destroy(void)
668{
669 LPSIC_ENTRY lpsice;
670 int i;
671
672 TRACE("\n");
673
674 EnterCriticalSection(&SHELL32_SicCS);
675
676 if (sic_hdpa && NULL != pDPA_GetPtr (sic_hdpa, 0))
677 {
678 for (i=0; i < pDPA_GetPtrCount(sic_hdpa); ++i)
679 {
680 lpsice = pDPA_GetPtr(sic_hdpa, i);
681 SHFree(lpsice);
682 }
683 pDPA_Destroy(sic_hdpa);
684 }
685
686 sic_hdpa = NULL;
687
688 LeaveCriticalSection(&SHELL32_SicCS);
689}
690/*************************************************************************
691 * Shell_GetImageList [SHELL32.71]
692 *
693 * PARAMETERS
694 * imglist[1|2] [OUT] pointer which recive imagelist handles
695 *
696 */
697BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
698{ TRACE("(%p,%p)\n",lpBigList,lpSmallList);
699 if (lpBigList)
700 { *lpBigList = ShellBigIconList;
701 }
702 if (lpSmallList)
703 { *lpSmallList = ShellSmallIconList;
704 }
705
706 return TRUE;
707}
708/*************************************************************************
709 * PidlToSicIndex [INTERNAL]
710 *
711 * PARAMETERS
712 * sh [IN] IShellFolder
713 * pidl [IN]
714 * bBigIcon [IN]
715 * pIndex [OUT] index within the SIC
716 *
717 */
718BOOL PidlToSicIndex (IShellFolder * sh, LPITEMIDLIST pidl, BOOL bBigIcon, UINT * pIndex)
719{
720 IExtractIcon *ei;
721 char szIconFile[MAX_PATH]; /* file containing the icon */
722 INT iSourceIndex; /* index or resID(negated) in this file */
723 BOOL ret = FALSE;
724 UINT dwFlags = 0;
725
726 TRACE("sf=%p pidl=%p\n", sh, pidl);
727
728 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconA, 0, (void **)&ei)))
729 {
730 if (NOERROR==IExtractIconA_GetIconLocation(ei, 0, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags))
731 { *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
732 ret = TRUE;
733 }
734 IExtractIconA_Release(ei);
735 }
736
737 if (INVALID_INDEX == *pIndex) /* default icon when failed */
738 *pIndex = 1;
739
740 return ret;
741
742}
743
744/*************************************************************************
745 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
746 *
747 * PARAMETERS
748 * sh [IN] pointer to an instance of IShellFolder
749 * pidl [IN]
750 * pIndex [OUT][OPTIONAL] SIC index for big icon
751 *
752 */
753UINT WINAPI SHMapPIDLToSystemImageListIndex(LPSHELLFOLDER sh, LPITEMIDLIST pidl, UINT * pIndex)
754{
755 UINT Index;
756
757 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
758 pdump(pidl);
759
760 if (pIndex)
761 PidlToSicIndex ( sh, pidl, 1, pIndex);
762 PidlToSicIndex ( sh, pidl, 0, &Index);
763 return Index;
764}
765
766/*************************************************************************
767 * Shell_GetCachedImageIndex [SHELL32.72]
768 *
769 */
770INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
771{
772 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
773 return SIC_GetIconIndex(szPath, nIndex);
774}
775
776INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
777{ INT ret;
778 LPSTR sTemp = HEAP_strdupWtoA (GetProcessHeap(),0,szPath);
779
780 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
781
782 ret = SIC_GetIconIndex(sTemp, nIndex);
783 HeapFree(GetProcessHeap(),0,sTemp);
784 return ret;
785}
786
787INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
788{ if( VERSION_OsIsUnicode())
789 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
790 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
791}
792
793/*************************************************************************
794 * ExtracticonExAW [shell32.189]
795 */
796HICON WINAPI ExtractIconExAW ( LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
797{ if (VERSION_OsIsUnicode())
798 return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
799 return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
800}
801/*************************************************************************
802 * ExtracticonExA [shell32.190]
803 * RETURNS
804 * 0 no icon found
805 * 1 file is not valid
806 * HICON handle of a icon (phiconLarge/Small == NULL)
807 */
808HICON WINAPI ExtractIconExA ( LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
809{ HICON ret=0;
810
811 TRACE("file=%s idx=%i %p %p num=%i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons );
812
813 if (nIconIndex==-1) /* Number of icons requested */
814 return ICO_ExtractIconEx(lpszFile, NULL, -1, 0, 0, 0 );
815
816
817 if (phiconLarge)
818 { ret = ICO_ExtractIconEx(lpszFile, phiconLarge, nIconIndex, nIcons, 32, 32 );
819 if ( nIcons==1)
820 { ret = phiconLarge[0];
821 }
822 }
823
824 /* if no pointers given and one icon expected, return the handle directly*/
825 if (!phiconLarge && ! phiconSmall && nIcons==1 )
826 phiconSmall = &ret;
827
828 if (phiconSmall)
829 { ret = ICO_ExtractIconEx(lpszFile, phiconSmall, nIconIndex, nIcons, 16, 16 );
830 if ( nIcons==1 )
831 { ret = phiconSmall[0];
832 }
833 }
834
835 return ret;
836}
837/*************************************************************************
838 * ExtracticonExW [shell32.191]
839 */
840HICON WINAPI ExtractIconExW ( LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
841{ LPSTR sFile;
842 DWORD ret;
843
844 TRACE("file=%s idx=%i %p %p num=%i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons );
845
846 sFile = HEAP_strdupWtoA (GetProcessHeap(),0,lpszFile);
847 ret = ExtractIconExA ( sFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
848 HeapFree(GetProcessHeap(),0,sFile);
849 return ret;
850}
Note: See TracBrowser for help on using the repository browser.