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

Last change on this file since 4032 was 4032, checked in by phaller, 25 years ago

Synchronized shell32 with wine

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