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

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

Fix: IconCache (with removed NE-support)

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