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

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

complete merge with shell32 from wine 20000801

File size: 26.5 KB
Line 
1/* $Id: iconcache.c,v 1.1 2000-08-30 13:52:51 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 goto end_1; /* unknown filetype */
472
473/* cleaning up (try & catch would be nicer:-) ) */
474end_3: UnmapViewOfFile(peimage); /* success */
475end_2: CloseHandle(fmapping);
476end_1: _lclose( hFile);
477 return hRet;
478}
479
480/********************** THE ICON CACHE ********************************/
481
482#define INVALID_INDEX -1
483
484typedef struct
485{ LPCSTR sSourceFile; /* file (not path!) containing the icon */
486 DWORD dwSourceIndex; /* index within the file, if it is a resoure ID it will be negated */
487 DWORD dwListIndex; /* index within the iconlist */
488 DWORD dwFlags; /* GIL_* flags */
489 DWORD dwAccessTime;
490} SIC_ENTRY, * LPSIC_ENTRY;
491
492static HDPA sic_hdpa = 0;
493static CRITICAL_SECTION SHELL32_SicCS = CRITICAL_SECTION_INIT;
494
495/*****************************************************************************
496 * SIC_CompareEntrys [called by comctl32.dll]
497 *
498 * NOTES
499 * Callback for DPA_Search
500 */
501INT CALLBACK SIC_CompareEntrys( LPVOID p1, LPVOID p2, LPARAM lparam)
502{ TRACE("%p %p\n", p1, p2);
503
504 if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
505 return 1;
506
507 if (strcasecmp(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
508 return 1;
509
510 return 0;
511}
512/*****************************************************************************
513 * SIC_IconAppend [internal]
514 *
515 * NOTES
516 * appends a icon pair to the end of the cache
517 */
518static INT SIC_IconAppend (LPCSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
519{ LPSIC_ENTRY lpsice;
520 INT ret, index, index1;
521
522 TRACE("%s %i %x %x\n", sSourceFile, dwSourceIndex, hSmallIcon ,hBigIcon);
523
524 lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
525
526 lpsice->sSourceFile = HEAP_strdupA (GetProcessHeap(), 0, PathFindFileNameA(sSourceFile));
527 lpsice->dwSourceIndex = dwSourceIndex;
528
529 EnterCriticalSection(&SHELL32_SicCS);
530
531 index = pDPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
532 if ( INVALID_INDEX == index )
533 {
534 SHFree(lpsice);
535 ret = INVALID_INDEX;
536 }
537 else
538 {
539 index = pImageList_AddIcon (ShellSmallIconList, hSmallIcon);
540 index1= pImageList_AddIcon (ShellBigIconList, hBigIcon);
541
542 if (index!=index1)
543 {
544 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
545 }
546 lpsice->dwListIndex = index;
547 ret = lpsice->dwListIndex;
548 }
549
550 LeaveCriticalSection(&SHELL32_SicCS);
551 return ret;
552}
553/****************************************************************************
554 * SIC_LoadIcon [internal]
555 *
556 * NOTES
557 * gets small/big icon by number from a file
558 */
559static INT SIC_LoadIcon (LPCSTR sSourceFile, INT dwSourceIndex)
560{ HICON hiconLarge=0;
561 HICON hiconSmall=0;
562
563 ICO_ExtractIconEx(sSourceFile, &hiconLarge, dwSourceIndex, 1, 32, 32 );
564 ICO_ExtractIconEx(sSourceFile, &hiconSmall, dwSourceIndex, 1, 16, 16 );
565
566
567 if ( !hiconLarge || !hiconSmall)
568 {
569 WARN("failure loading icon %i from %s (%x %x)\n", dwSourceIndex, sSourceFile, hiconLarge, hiconSmall);
570 return -1;
571 }
572 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
573}
574/*****************************************************************************
575 * SIC_GetIconIndex [internal]
576 *
577 * Parameters
578 * sSourceFile [IN] filename of file containing the icon
579 * index [IN] index/resID (negated) in this file
580 *
581 * NOTES
582 * look in the cache for a proper icon. if not available the icon is taken
583 * from the file and cached
584 */
585INT SIC_GetIconIndex (LPCSTR sSourceFile, INT dwSourceIndex )
586{ SIC_ENTRY sice;
587 INT ret, index = INVALID_INDEX;
588
589 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
590
591 sice.sSourceFile = PathFindFileNameA(sSourceFile);
592 sice.dwSourceIndex = dwSourceIndex;
593
594 EnterCriticalSection(&SHELL32_SicCS);
595
596 if (NULL != pDPA_GetPtr (sic_hdpa, 0))
597 {
598 index = pDPA_Search (sic_hdpa, &sice, -1L, SIC_CompareEntrys, 0, 0);
599 }
600
601 if ( INVALID_INDEX == index )
602 {
603 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
604 }
605 else
606 {
607 TRACE("-- found\n");
608 ret = ((LPSIC_ENTRY)pDPA_GetPtr(sic_hdpa, index))->dwListIndex;
609 }
610
611 LeaveCriticalSection(&SHELL32_SicCS);
612 return ret;
613}
614/****************************************************************************
615 * SIC_LoadIcon [internal]
616 *
617 * NOTES
618 * retrives the specified icon from the iconcache. if not found try's to load the icon
619 */
620static HICON WINE_UNUSED SIC_GetIcon (LPCSTR sSourceFile, INT dwSourceIndex, BOOL bSmallIcon )
621{ INT index;
622
623 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
624
625 index = SIC_GetIconIndex(sSourceFile, dwSourceIndex);
626
627 if (INVALID_INDEX == index)
628 {
629 return INVALID_INDEX;
630 }
631
632 if (bSmallIcon)
633 return pImageList_GetIcon(ShellSmallIconList, index, ILD_NORMAL);
634 return pImageList_GetIcon(ShellBigIconList, index, ILD_NORMAL);
635
636}
637/*****************************************************************************
638 * SIC_Initialize [internal]
639 *
640 * NOTES
641 * hack to load the resources from the shell32.dll under a different dll name
642 * will be removed when the resource-compiler is ready
643 */
644BOOL SIC_Initialize(void)
645{
646 HICON hSm, hLg;
647 UINT index;
648
649 TRACE("\n");
650
651 if (sic_hdpa) /* already initialized?*/
652 return TRUE;
653
654 sic_hdpa = pDPA_Create(16);
655
656 if (!sic_hdpa)
657 {
658 return(FALSE);
659 }
660
661 ShellSmallIconList = pImageList_Create(16,16,ILC_COLORDDB | ILC_MASK,0,0x20);
662 ShellBigIconList = pImageList_Create(32,32,ILC_COLORDDB | ILC_MASK,0,0x20);
663
664 pImageList_SetBkColor(ShellSmallIconList, GetSysColor(COLOR_WINDOW));
665 pImageList_SetBkColor(ShellBigIconList, GetSysColor(COLOR_WINDOW));
666
667 for (index=1; index<39; index++)
668 {
669 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 16, 16,LR_SHARED);
670 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 32, 32,LR_SHARED);
671
672 if(!hSm)
673 {
674 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 16, 16,LR_SHARED);
675 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 32, 32,LR_SHARED);
676 }
677 SIC_IconAppend ("shell32.dll", index, hSm, hLg);
678 }
679
680 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
681
682 return TRUE;
683}
684/*************************************************************************
685 * SIC_Destroy
686 *
687 * frees the cache
688 */
689void SIC_Destroy(void)
690{
691 LPSIC_ENTRY lpsice;
692 int i;
693
694 TRACE("\n");
695
696 EnterCriticalSection(&SHELL32_SicCS);
697
698 if (sic_hdpa && NULL != pDPA_GetPtr (sic_hdpa, 0))
699 {
700 for (i=0; i < pDPA_GetPtrCount(sic_hdpa); ++i)
701 {
702 lpsice = pDPA_GetPtr(sic_hdpa, i);
703 SHFree(lpsice);
704 }
705 pDPA_Destroy(sic_hdpa);
706 }
707
708 sic_hdpa = NULL;
709
710 LeaveCriticalSection(&SHELL32_SicCS);
711 DeleteCriticalSection(&SHELL32_SicCS);
712}
713/*************************************************************************
714 * Shell_GetImageList [SHELL32.71]
715 *
716 * PARAMETERS
717 * imglist[1|2] [OUT] pointer which recive imagelist handles
718 *
719 */
720BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
721{ TRACE("(%p,%p)\n",lpBigList,lpSmallList);
722 if (lpBigList)
723 { *lpBigList = ShellBigIconList;
724 }
725 if (lpSmallList)
726 { *lpSmallList = ShellSmallIconList;
727 }
728
729 return TRUE;
730}
731/*************************************************************************
732 * PidlToSicIndex [INTERNAL]
733 *
734 * PARAMETERS
735 * sh [IN] IShellFolder
736 * pidl [IN]
737 * bBigIcon [IN]
738 * uFlags [IN] GIL_*
739 * pIndex [OUT] index within the SIC
740 *
741 */
742BOOL PidlToSicIndex (
743 IShellFolder * sh,
744 LPITEMIDLIST pidl,
745 BOOL bBigIcon,
746 UINT uFlags,
747 UINT * pIndex)
748{
749 IExtractIconA *ei;
750 char szIconFile[MAX_PATH]; /* file containing the icon */
751 INT iSourceIndex; /* index or resID(negated) in this file */
752 BOOL ret = FALSE;
753 UINT dwFlags = 0;
754
755 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
756
757 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconA, 0, (void **)&ei)))
758 {
759 if (SUCCEEDED(IExtractIconA_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
760 {
761 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
762 ret = TRUE;
763 }
764 IExtractIconA_Release(ei);
765 }
766
767 if (INVALID_INDEX == *pIndex) /* default icon when failed */
768 *pIndex = 1;
769
770 return ret;
771
772}
773
774/*************************************************************************
775 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
776 *
777 * PARAMETERS
778 * sh [IN] pointer to an instance of IShellFolder
779 * pidl [IN]
780 * pIndex [OUT][OPTIONAL] SIC index for big icon
781 *
782 */
783int WINAPI SHMapPIDLToSystemImageListIndex(
784 LPSHELLFOLDER sh,
785 LPCITEMIDLIST pidl,
786 UINT * pIndex)
787{
788 UINT Index;
789
790 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
791 pdump(pidl);
792
793 if (pIndex)
794 PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
795 PidlToSicIndex ( sh, pidl, 0, 0, &Index);
796 return Index;
797}
798
799/*************************************************************************
800 * Shell_GetCachedImageIndex [SHELL32.72]
801 *
802 */
803INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
804{
805 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
806 return SIC_GetIconIndex(szPath, nIndex);
807}
808
809INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
810{ INT ret;
811 LPSTR sTemp = HEAP_strdupWtoA (GetProcessHeap(),0,szPath);
812
813 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
814
815 ret = SIC_GetIconIndex(sTemp, nIndex);
816 HeapFree(GetProcessHeap(),0,sTemp);
817 return ret;
818}
819
820INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
821{ if( SHELL_OsIsUnicode())
822 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
823 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
824}
825
826/*************************************************************************
827 * ExtractIconEx [shell32.189]
828 */
829HICON WINAPI ExtractIconExAW ( LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
830{ if (SHELL_OsIsUnicode())
831 return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
832 return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
833}
834/*************************************************************************
835 * ExtractIconExA [shell32.190]
836 * RETURNS
837 * 0 no icon found
838 * 1 file is not valid
839 * HICON handle of a icon (phiconLarge/Small == NULL)
840 */
841HICON WINAPI ExtractIconExA ( LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
842{ HICON ret=0;
843
844 TRACE("file=%s idx=%i %p %p num=%i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons );
845
846 if (nIconIndex==-1) /* Number of icons requested */
847 return ICO_ExtractIconEx(lpszFile, NULL, -1, 0, 0, 0 );
848
849
850 if (phiconLarge)
851 { ret = ICO_ExtractIconEx(lpszFile, phiconLarge, nIconIndex, nIcons, 32, 32 );
852 if ( nIcons==1)
853 { ret = phiconLarge[0];
854 }
855 }
856
857 /* if no pointers given and one icon expected, return the handle directly*/
858 if (!phiconLarge && ! phiconSmall && nIcons==1 )
859 phiconSmall = &ret;
860
861 if (phiconSmall)
862 { ret = ICO_ExtractIconEx(lpszFile, phiconSmall, nIconIndex, nIcons, 16, 16 );
863 if ( nIcons==1 )
864 { ret = phiconSmall[0];
865 }
866 }
867
868 return ret;
869}
870/*************************************************************************
871 * ExtractIconExW [shell32.191]
872 */
873HICON WINAPI ExtractIconExW ( LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
874{ LPSTR sFile;
875 DWORD ret;
876
877 TRACE("file=%s idx=%i %p %p num=%i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons );
878
879 sFile = HEAP_strdupWtoA (GetProcessHeap(),0,lpszFile);
880 ret = ExtractIconExA ( sFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
881 HeapFree(GetProcessHeap(),0,sFile);
882 return ret;
883}
Note: See TracBrowser for help on using the repository browser.