Fixed mapping of file extensions to text.
[wine/dcerpc.git] / dlls / shell32 / iconcache.c
blobe5d3f37e9193e00f5b808512200e9459f922864d
1 /*
2 * shell icon cache (SIC)
4 */
5 #include <string.h>
6 #include "winbase.h"
7 #include "winuser.h"
8 #include "wingdi.h"
9 #include "wine/winuser16.h"
10 #include "wine/winbase16.h"
11 #include "neexe.h"
12 #include "cursoricon.h"
13 #include "module.h"
14 #include "heap.h"
15 #include "debugtools.h"
16 #include "winversion.h"
18 #include "shellapi.h"
19 #include "pidl.h"
20 #include "shell32_main.h"
22 DEFAULT_DEBUG_CHANNEL(shell)
24 #include "pshpack1.h"
26 typedef struct
28 BYTE bWidth; /* Width, in pixels, of the image */
29 BYTE bHeight; /* Height, in pixels, of the image */
30 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
31 BYTE bReserved; /* Reserved ( must be 0) */
32 WORD wPlanes; /* Color Planes */
33 WORD wBitCount; /* Bits per pixel */
34 DWORD dwBytesInRes; /* How many bytes in this resource? */
35 DWORD dwImageOffset; /* Where in the file is this image? */
36 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
38 typedef struct
40 WORD idReserved; /* Reserved (must be 0) */
41 WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
42 WORD idCount; /* How many images */
43 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
44 } icoICONDIR, *LPicoICONDIR;
46 #include "poppack.h"
48 #if 0
49 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
51 TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
52 TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
53 TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
54 entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
56 static void dumpIcoDir ( LPicoICONDIR entry )
58 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
60 #endif
61 /*************************************************************************
62 * SHELL_GetResourceTable
64 static DWORD SHELL_GetResourceTable(HFILE hFile, LPBYTE *retptr)
65 { IMAGE_DOS_HEADER mz_header;
66 char magic[4];
67 int size;
69 TRACE("0x%08x %p\n", hFile, retptr);
71 *retptr = NULL;
72 _llseek( hFile, 0, SEEK_SET );
73 if ((_lread(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) || (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
74 { if (mz_header.e_cblp == 1) /* .ICO file ? */
75 { *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
76 return 1;
78 else
79 return 0; /* failed */
81 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
83 if (_lread( hFile, magic, sizeof(magic) ) != sizeof(magic))
84 return 0;
86 _llseek( hFile, mz_header.e_lfanew, SEEK_SET);
88 if (*(DWORD*)magic == IMAGE_NT_SIGNATURE)
89 return IMAGE_NT_SIGNATURE;
91 if (*(WORD*)magic == IMAGE_OS2_SIGNATURE)
92 { IMAGE_OS2_HEADER ne_header;
93 LPBYTE pTypeInfo = (LPBYTE)-1;
95 if (_lread(hFile,&ne_header,sizeof(ne_header))!=sizeof(ne_header))
96 return 0;
98 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE)
99 return 0;
101 size = ne_header.rname_tab_offset - ne_header.resource_tab_offset;
103 if( size > sizeof(NE_TYPEINFO) )
104 { pTypeInfo = (BYTE*)HeapAlloc( GetProcessHeap(), 0, size);
105 if( pTypeInfo )
106 { _llseek(hFile, mz_header.e_lfanew+ne_header.resource_tab_offset, SEEK_SET);
107 if( _lread( hFile, (char*)pTypeInfo, size) != size )
108 { HeapFree( GetProcessHeap(), 0, pTypeInfo);
109 pTypeInfo = NULL;
113 *retptr = pTypeInfo;
114 return IMAGE_OS2_SIGNATURE;
116 return 0; /* failed */
118 /*************************************************************************
119 * SHELL_LoadResource
121 static BYTE * SHELL_LoadResource( HFILE hFile, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
122 { BYTE* ptr;
124 TRACE("0x%08x %p 0x%08x\n", hFile, pNInfo, sizeShift);
126 *uSize = (DWORD)pNInfo->length << sizeShift;
127 if( (ptr = (BYTE*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
128 { _llseek( hFile, (DWORD)pNInfo->offset << sizeShift, SEEK_SET);
129 _lread( hFile, (char*)ptr, pNInfo->length << sizeShift);
130 return ptr;
132 return 0;
135 /*************************************************************************
136 * ICO_LoadIcon
138 static BYTE * ICO_LoadIcon( HFILE hFile, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
139 { BYTE* ptr;
141 TRACE("0x%08x %p\n", hFile, lpiIDE);
143 *uSize = lpiIDE->dwBytesInRes;
144 if( (ptr = (BYTE*)HeapAlloc(GetProcessHeap(),0, *uSize)) )
145 { _llseek( hFile, lpiIDE->dwImageOffset, SEEK_SET);
146 _lread( hFile, (char*)ptr, lpiIDE->dwBytesInRes);
147 return ptr;
150 return 0;
153 /*************************************************************************
154 * ICO_GetIconDirectory
156 * Reads .ico file and build phony ICONDIR struct
157 * see http://www.microsoft.com/win32dev/ui/icons.htm
159 #define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
160 #define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
162 static BYTE * ICO_GetIconDirectory( HFILE hFile, LPicoICONDIR* lplpiID, ULONG *uSize )
163 { CURSORICONDIR lpcid; /* icon resource in resource-dir format */
164 LPicoICONDIR lpiID; /* icon resource in file format */
165 int i;
167 TRACE("0x%08x %p\n", hFile, lplpiID);
169 _llseek( hFile, 0, SEEK_SET );
170 if( _lread(hFile,(char*)&lpcid, HEADER_SIZE_FILE) != HEADER_SIZE_FILE )
171 return 0;
173 if( lpcid.idReserved || (lpcid.idType != 1) || (!lpcid.idCount) )
174 return 0;
176 i = lpcid.idCount * sizeof(icoICONDIRENTRY);
177 lpiID = (LPicoICONDIR)HeapAlloc( GetProcessHeap(), 0, HEADER_SIZE_FILE + i);
179 if( _lread(hFile,(char*)lpiID->idEntries,i) == i )
180 { CURSORICONDIR * lpID; /* icon resource in resource format */
181 *uSize = lpcid.idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
182 if( (lpID = (CURSORICONDIR*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
184 /* copy the header */
185 lpID->idReserved = lpiID->idReserved = 0;
186 lpID->idType = lpiID->idType = 1;
187 lpID->idCount = lpiID->idCount = lpcid.idCount;
189 /* copy the entrys */
190 for( i=0; i < lpiID->idCount; i++ )
191 { memcpy((void*)&(lpID->idEntries[i]),(void*)&(lpiID->idEntries[i]), sizeof(CURSORICONDIRENTRY) - 2);
192 lpID->idEntries[i].wResId = i;
195 *lplpiID = lpiID;
196 return (BYTE *)lpID;
199 /* fail */
201 HeapFree( GetProcessHeap(), 0, lpiID);
202 return 0;
205 /*************************************************************************
206 * InternalExtractIcon [SHELL.39]
208 * This abortion is called directly by Progman
209 * fixme: the icon section is broken (don't have a handle for
210 * ICO_GetIconDirectory....)
213 #define ICO_INVALID_FILE 1
214 #define ICO_NO_ICONS 0
216 HGLOBAL WINAPI ICO_ExtractIconEx(LPCSTR lpszExeFileName, HICON * RetPtr, UINT nIconIndex, UINT n, UINT cxDesired, UINT cyDesired )
217 { HGLOBAL hRet = ICO_NO_ICONS;
218 LPBYTE pData;
219 OFSTRUCT ofs;
220 DWORD sig;
221 HFILE hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
222 UINT16 iconDirCount = 0,iconCount = 0;
223 LPBYTE peimage;
224 HANDLE fmapping;
225 ULONG uSize;
227 TRACE("(file %s,start %d,extract %d\n", lpszExeFileName, nIconIndex, n);
229 if( hFile == HFILE_ERROR || !n )
230 return ICO_INVALID_FILE;
232 sig = SHELL_GetResourceTable(hFile,&pData);
234 /* ico file */
235 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
236 { BYTE *pCIDir = 0;
237 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
238 NE_NAMEINFO *pIconStorage = NULL;
239 NE_NAMEINFO *pIconDir = NULL;
240 LPicoICONDIR lpiID = NULL;
242 TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
244 if( pData == (BYTE*)-1 )
245 { pCIDir = ICO_GetIconDirectory(hFile, &lpiID, &uSize); /* check for .ICO file */
246 if( pCIDir )
247 { iconDirCount = 1; iconCount = lpiID->idCount;
248 TRACE("-- icon found %p 0x%08lx 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
251 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
252 { if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
253 { iconDirCount = pTInfo->count;
254 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
255 TRACE("\tfound directory - %i icon families\n", iconDirCount);
257 if( pTInfo->type_id == NE_RSCTYPE_ICON )
258 { iconCount = pTInfo->count;
259 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
260 TRACE("\ttotal icons - %i\n", iconCount);
262 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
265 if( (pIconStorage && pIconDir) || lpiID ) /* load resources and create icons */
266 { if( nIconIndex == (UINT16)-1 )
267 { RetPtr[0] = iconDirCount;
269 else if( nIconIndex < iconDirCount )
270 { UINT16 i, icon;
271 if( n > iconDirCount - nIconIndex )
272 n = iconDirCount - nIconIndex;
274 for( i = nIconIndex; i < nIconIndex + n; i++ )
275 { /* .ICO files have only one icon directory */
277 if( lpiID == NULL ) /* *.ico */
278 pCIDir = SHELL_LoadResource( hFile, pIconDir + i, *(WORD*)pData, &uSize );
279 RetPtr[i-nIconIndex] = pLookupIconIdFromDirectoryEx( pCIDir, TRUE, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0);
280 HeapFree(GetProcessHeap(), 0, pCIDir);
283 for( icon = nIconIndex; icon < nIconIndex + n; icon++ )
284 { pCIDir = NULL;
285 if( lpiID )
286 { pCIDir = ICO_LoadIcon( hFile, lpiID->idEntries + RetPtr[icon-nIconIndex], &uSize);
288 else
289 { for( i = 0; i < iconCount; i++ )
290 { if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
291 { pCIDir = SHELL_LoadResource( hFile, pIconStorage + i,*(WORD*)pData, &uSize );
295 if( pCIDir )
296 { RetPtr[icon-nIconIndex] = (HICON) pCreateIconFromResourceEx(pCIDir,uSize,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
298 else
299 { RetPtr[icon-nIconIndex] = 0;
304 if( lpiID )
305 HeapFree( GetProcessHeap(), 0, lpiID);
306 else
307 HeapFree( GetProcessHeap(), 0, pData);
309 /* end ico file */
311 /* exe/dll */
312 if( sig == IMAGE_NT_SIGNATURE)
313 { LPBYTE idata,igdata;
314 PIMAGE_DOS_HEADER dheader;
315 PIMAGE_NT_HEADERS pe_header;
316 PIMAGE_SECTION_HEADER pe_sections;
317 PIMAGE_RESOURCE_DIRECTORY rootresdir,iconresdir,icongroupresdir;
318 PIMAGE_RESOURCE_DATA_ENTRY idataent,igdataent;
319 PIMAGE_RESOURCE_DIRECTORY_ENTRY xresent;
320 int i,j;
322 if ( !(fmapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL)))
323 { WARN("failed to create filemap.\n"); /* FIXME, INVALID_HANDLE_VALUE? */
324 hRet = ICO_INVALID_FILE;
325 goto end_2; /* failure */
328 if ( !(peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0)))
329 { WARN("failed to mmap filemap.\n");
330 hRet = ICO_INVALID_FILE;
331 goto end_2; /* failure */
334 dheader = (PIMAGE_DOS_HEADER)peimage;
335 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, SHELL_GetResourceTable checked that */
336 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header)); /* probably makes problems with short PE headers...*/
337 rootresdir = NULL;
339 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
340 { if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
341 continue;
342 /* FIXME: doesn't work when the resources are not in a seperate section */
343 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
344 { rootresdir = (PIMAGE_RESOURCE_DIRECTORY)((char*)peimage+pe_sections[i].PointerToRawData);
345 break;
349 if (!rootresdir)
350 { WARN("haven't found section for resource directory.\n");
351 goto end_4; /* failure */
353 /* search the group icon dir*/
354 if (!(icongroupresdir = GetResDirEntryW(rootresdir,RT_GROUP_ICONW, (DWORD)rootresdir,FALSE)))
355 { WARN("No Icongroupresourcedirectory!\n");
356 goto end_4; /* failure */
358 iconDirCount = icongroupresdir->NumberOfNamedEntries+icongroupresdir->NumberOfIdEntries;
360 /* number of icons requested */
361 if( nIconIndex == -1 )
362 { hRet = iconDirCount;
363 goto end_3; /* success */
366 if (nIconIndex >= iconDirCount)
367 { WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
368 goto end_4; /* failure */
371 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1); /* caller just wanted the number of entries */
373 if( n > iconDirCount - nIconIndex ) /* assure we don't get too much ... */
374 { n = iconDirCount - nIconIndex;
377 xresent = xresent+nIconIndex; /* starting from specified index ... */
379 for (i=0;i<n;i++,xresent++)
380 { PIMAGE_RESOURCE_DIRECTORY resdir;
382 /* go down this resource entry, name */
383 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s.OffsetToDirectory));
385 /* default language (0) */
386 resdir = GetResDirEntryW(resdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
387 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
389 /* lookup address in mapped image for virtual address */
390 igdata = NULL;
392 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
393 { if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
394 continue;
395 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
396 continue;
397 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
400 if (!igdata)
401 { WARN("no matching real address for icongroup!\n");
402 goto end_4; /* failure */
404 RetPtr[i] = (HICON)pLookupIconIdFromDirectoryEx(igdata, TRUE, cxDesired, cyDesired, LR_DEFAULTCOLOR);
407 if (!(iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,(DWORD)rootresdir,FALSE)))
408 { WARN("No Iconresourcedirectory!\n");
409 goto end_4; /* failure */
412 for (i=0;i<n;i++)
413 { PIMAGE_RESOURCE_DIRECTORY xresdir;
414 xresdir = GetResDirEntryW(iconresdir,(LPWSTR)(DWORD)RetPtr[i],(DWORD)rootresdir,FALSE);
415 xresdir = GetResDirEntryW(xresdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
416 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
417 idata = NULL;
419 /* map virtual to address in image */
420 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
421 { if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
422 continue;
423 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
424 continue;
425 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
427 if (!idata)
428 { WARN("no matching real address found for icondata!\n");
429 RetPtr[i]=0;
430 continue;
432 RetPtr[i] = (HICON) pCreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
434 hRet = RetPtr[0]; /* return first icon */
435 goto end_3; /* sucess */
437 hRet = ICO_INVALID_FILE;
438 goto end_1; /* unknown filetype */
440 /* cleaning up (try & catch would be nicer:-) ) */
441 end_4: hRet = 0; /* failure */
442 end_3: UnmapViewOfFile(peimage); /* success */
443 end_2: CloseHandle(fmapping);
444 end_1: _lclose( hFile);
445 return hRet;
448 /********************** THE ICON CACHE ********************************/
450 #define INVALID_INDEX -1
452 typedef struct
453 { LPCSTR sSourceFile; /* file (not path!) containing the icon */
454 DWORD dwSourceIndex; /* index within the file, if it is a resoure ID it will be negated */
455 DWORD dwListIndex; /* index within the iconlist */
456 DWORD dwFlags; /* GIL_* flags */
457 DWORD dwAccessTime;
458 } SIC_ENTRY, * LPSIC_ENTRY;
460 /*****************************************************************************
461 * SIC_CompareEntrys [called by comctl32.dll]
463 * NOTES
464 * Callback for DPA_Search
466 INT CALLBACK SIC_CompareEntrys( LPVOID p1, LPVOID p2, LPARAM lparam)
467 { TRACE("%p %p\n", p1, p2);
469 if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
470 return 1;
472 if (strcasecmp(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
473 return 1;
475 return 0;
477 /*****************************************************************************
478 * SIC_IconAppend [internal]
480 * NOTES
481 * appends a icon pair to the end of the cache
483 static INT SIC_IconAppend (LPCSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
484 { LPSIC_ENTRY lpsice;
485 INT index, index1;
487 TRACE("%s %i %x %x\n", sSourceFile, dwSourceIndex, hSmallIcon ,hBigIcon);
489 lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
491 lpsice->sSourceFile = HEAP_strdupA (GetProcessHeap(), 0, PathFindFilenameA(sSourceFile));
492 lpsice->dwSourceIndex = dwSourceIndex;
494 index = pDPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
495 if ( INVALID_INDEX == index )
496 { SHFree(lpsice);
497 return INVALID_INDEX;
500 index = pImageList_AddIcon (ShellSmallIconList, hSmallIcon);
501 index1= pImageList_AddIcon (ShellBigIconList, hBigIcon);
503 if (index!=index1)
504 { FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
506 lpsice->dwListIndex = index;
508 return lpsice->dwListIndex;
511 /****************************************************************************
512 * SIC_LoadIcon [internal]
514 * NOTES
515 * gets small/big icon by number from a file
517 static INT SIC_LoadIcon (LPCSTR sSourceFile, INT dwSourceIndex)
518 { HICON hiconLarge=0;
519 HICON hiconSmall=0;
521 ICO_ExtractIconEx(sSourceFile, &hiconLarge, dwSourceIndex, 1, 32, 32 );
522 ICO_ExtractIconEx(sSourceFile, &hiconSmall, dwSourceIndex, 1, 16, 16 );
525 if ( !hiconLarge || !hiconSmall)
526 { WARN("failure loading icon %i from %s (%x %x)\n", dwSourceIndex, sSourceFile, hiconLarge, hiconSmall);
527 return -1;
529 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
531 /*****************************************************************************
532 * SIC_GetIconIndex [internal]
534 * Parameters
535 * sSourceFile [IN] filename of file containing the icon
536 * index [IN] index/resID (negated) in this file
538 * NOTES
539 * look in the cache for a proper icon. if not available the icon is taken
540 * from the file and cached
542 INT SIC_GetIconIndex (LPCSTR sSourceFile, INT dwSourceIndex )
543 { SIC_ENTRY sice;
544 INT index = INVALID_INDEX;
546 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
548 sice.sSourceFile = PathFindFilenameA(sSourceFile);
549 sice.dwSourceIndex = dwSourceIndex;
551 if (NULL != pDPA_GetPtr (sic_hdpa, 0))
552 { index = pDPA_Search (sic_hdpa, &sice, -1L, SIC_CompareEntrys, 0, 0);
555 if ( INVALID_INDEX == index )
556 { return SIC_LoadIcon (sSourceFile, dwSourceIndex);
559 TRACE("-- found\n");
560 return ((LPSIC_ENTRY)pDPA_GetPtr(sic_hdpa, index))->dwListIndex;
562 /****************************************************************************
563 * SIC_LoadIcon [internal]
565 * NOTES
566 * retrives the specified icon from the iconcache. if not found try's to load the icon
568 static HICON WINE_UNUSED SIC_GetIcon (LPCSTR sSourceFile, INT dwSourceIndex, BOOL bSmallIcon )
569 { INT index;
571 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
573 index = SIC_GetIconIndex(sSourceFile, dwSourceIndex);
574 if (INVALID_INDEX == index)
575 { return INVALID_INDEX;
578 if (bSmallIcon)
579 return pImageList_GetIcon(ShellSmallIconList, index, ILD_NORMAL);
580 return pImageList_GetIcon(ShellBigIconList, index, ILD_NORMAL);
583 /*****************************************************************************
584 * SIC_Initialize [internal]
586 * NOTES
587 * hack to load the resources from the shell32.dll under a different dll name
588 * will be removed when the resource-compiler is ready
590 BOOL SIC_Initialize(void)
591 { CHAR szShellPath[MAX_PATH];
592 HGLOBAL hSmRet, hLgRet;
593 HICON *pSmRet, *pLgRet;
594 UINT index;
596 TRACE("\n");
598 if (sic_hdpa) /* already initialized?*/
599 return TRUE;
601 sic_hdpa = pDPA_Create(16);
603 if (!sic_hdpa)
604 { return(FALSE);
607 GetSystemDirectoryA(szShellPath,MAX_PATH);
608 PathAddBackslashA(szShellPath);
609 strcat(szShellPath,"shell32.dll");
611 hSmRet = GlobalAlloc( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON)*40);
612 hLgRet = GlobalAlloc( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON)*40);
614 pSmRet = (HICON*)GlobalLock(hSmRet);
615 pLgRet = (HICON*)GlobalLock(hLgRet);
617 ExtractIconExA ( szShellPath, 0, pLgRet, pSmRet, 40 );
619 ShellSmallIconList = pImageList_Create(16,16,ILC_COLORDDB | ILC_MASK,0,0x20);
620 ShellBigIconList = pImageList_Create(32,32,ILC_COLORDDB | ILC_MASK,0,0x20);
622 pImageList_SetBkColor(ShellSmallIconList, GetSysColor(COLOR_WINDOW));
623 pImageList_SetBkColor(ShellBigIconList, GetSysColor(COLOR_WINDOW));
625 for (index=0; index<40; index++)
626 { if (! pSmRet[index] )
627 { MESSAGE("*** failure loading resources from %s\n", szShellPath );
628 MESSAGE("*** this is a hack for loading the internal and external dll at the same time\n");
629 MESSAGE("*** you can ignore it but you will miss some icons in win95 dialogs\n\n");
630 break;
632 SIC_IconAppend (szShellPath, index, pSmRet[index], pLgRet[index]);
635 GlobalUnlock(hLgRet);
636 GlobalFree(hLgRet);
638 GlobalUnlock(hSmRet);
639 GlobalFree(hSmRet);
641 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
643 return TRUE;
645 /*************************************************************************
646 * SIC_Destroy
648 * frees the cache
650 void SIC_Destroy(void)
652 LPSIC_ENTRY lpsice;
653 int i;
655 TRACE("\n");
657 if (sic_hdpa && NULL != pDPA_GetPtr (sic_hdpa, 0))
659 for (i=0; i < pDPA_GetPtrCount(sic_hdpa); ++i)
661 lpsice = pDPA_GetPtr(sic_hdpa, i);
662 SHFree(lpsice);
664 pDPA_Destroy(sic_hdpa);
667 sic_hdpa = NULL;
669 /*************************************************************************
670 * Shell_GetImageList [SHELL32.71]
672 * PARAMETERS
673 * imglist[1|2] [OUT] pointer which recive imagelist handles
676 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
677 { TRACE("(%p,%p)\n",lpBigList,lpSmallList);
678 if (lpBigList)
679 { *lpBigList = ShellBigIconList;
681 if (lpSmallList)
682 { *lpSmallList = ShellSmallIconList;
685 return TRUE;
687 /*************************************************************************
688 * PidlToSicIndex [INTERNAL]
690 * PARAMETERS
691 * sh [IN] IShellFolder
692 * pidl [IN]
693 * bBigIcon [IN]
694 * pIndex [OUT] index within the SIC
697 BOOL PidlToSicIndex (IShellFolder * sh, LPITEMIDLIST pidl, BOOL bBigIcon, UINT * pIndex)
699 IExtractIcon *ei;
700 char szIconFile[MAX_PATH]; /* file containing the icon */
701 INT iSourceIndex; /* index or resID(negated) in this file */
702 BOOL ret = FALSE;
703 UINT dwFlags = 0;
705 TRACE("sf=%p pidl=%p\n", sh, pidl);
707 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconA, 0, (void **)&ei)))
709 if (NOERROR==IExtractIconA_GetIconLocation(ei, 0, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags))
710 { *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
711 ret = TRUE;
713 IExtractIconA_Release(ei);
716 if (INVALID_INDEX == *pIndex) /* default icon when failed */
717 *pIndex = 1;
719 return ret;
723 /*************************************************************************
724 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
726 * PARAMETERS
727 * sh [IN] pointer to an instance of IShellFolder
728 * pidl [IN]
729 * pIndex [OUT][OPTIONAL] SIC index for big icon
732 UINT WINAPI SHMapPIDLToSystemImageListIndex(LPSHELLFOLDER sh, LPITEMIDLIST pidl, UINT * pIndex)
734 UINT Index;
736 WARN("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
737 pdump(pidl);
739 if (pIndex)
740 PidlToSicIndex ( sh, pidl, 1, pIndex);
741 PidlToSicIndex ( sh, pidl, 0, &Index);
742 return Index;
745 /*************************************************************************
746 * Shell_GetCachedImageIndex [SHELL32.72]
749 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
751 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
752 return SIC_GetIconIndex(szPath, nIndex);
755 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
756 { INT ret;
757 LPSTR sTemp = HEAP_strdupWtoA (GetProcessHeap(),0,szPath);
759 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
761 ret = SIC_GetIconIndex(sTemp, nIndex);
762 HeapFree(GetProcessHeap(),0,sTemp);
763 return ret;
766 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
767 { if( VERSION_OsIsUnicode())
768 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
769 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
772 /*************************************************************************
773 * ExtracticonExAW [shell32.189]
775 HICON WINAPI ExtractIconExAW ( LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
776 { if (VERSION_OsIsUnicode())
777 return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
778 return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
780 /*************************************************************************
781 * ExtracticonExA [shell32.190]
782 * RETURNS
783 * 0 no icon found
784 * 1 file is not valid
785 * HICON handle of a icon (phiconLarge/Small == NULL)
787 HICON WINAPI ExtractIconExA ( LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
788 { HICON ret=0;
790 TRACE("file=%s idx=%i %p %p num=%i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons );
792 if (nIconIndex==-1) /* Number of icons requested */
793 return ICO_ExtractIconEx(lpszFile, NULL, -1, 0, 0, 0 );
796 if (phiconLarge)
797 { ret = ICO_ExtractIconEx(lpszFile, phiconLarge, nIconIndex, nIcons, 32, 32 );
798 if ( nIcons==1)
799 { ret = phiconLarge[0];
803 /* if no pointers given and one icon expected, return the handle directly*/
804 if (!phiconLarge && ! phiconSmall && nIcons==1 )
805 phiconSmall = &ret;
807 if (phiconSmall)
808 { ret = ICO_ExtractIconEx(lpszFile, phiconSmall, nIconIndex, nIcons, 16, 16 );
809 if ( nIcons==1 )
810 { ret = phiconSmall[0];
814 return ret;
816 /*************************************************************************
817 * ExtracticonExW [shell32.191]
819 HICON WINAPI ExtractIconExW ( LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
820 { LPSTR sFile;
821 DWORD ret;
823 TRACE("file=%s idx=%i %p %p num=%i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons );
825 sFile = HEAP_strdupWtoA (GetProcessHeap(),0,lpszFile);
826 ret = ExtractIconExA ( sFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
827 HeapFree(GetProcessHeap(),0,sFile);
828 return ret;