source: trunk/src/shell32/iconcache.cpp@ 3243

Last change on this file since 3243 was 3243, checked in by cbratschi, 25 years ago

merged with Corel WINE 20000324

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