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

Last change on this file since 815 was 815, checked in by achimha, 26 years ago

shell32/new compilation fixes

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