source: trunk/src/shell32/iconcache.c@ 4607

Last change on this file since 4607 was 4607, checked in by sandervl, 25 years ago

KS: ICO_ExtractIconEx fix for LX images

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