dbghelp: Add forwards for EnumerateLoadedModulesEx/EnumerateLoadedModulesExW.
[wine.git] / dlls / shell32 / iconcache.c
blob4549d5b0f70b81d32ddb966f73cf3005d5255d97
1 /*
2 * shell icon cache (SIC)
4 * Copyright 1998, 1999 Juergen Schmied
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
31 #define COBJMACROS
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "winuser.h"
37 #include "winreg.h"
38 #include "wine/debug.h"
40 #include "shellapi.h"
41 #include "objbase.h"
42 #include "pidl.h"
43 #include "shell32_main.h"
44 #include "undocshell.h"
45 #include "shresdef.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(shell);
49 /********************** THE ICON CACHE ********************************/
51 #define INVALID_INDEX -1
53 typedef struct
55 LPWSTR sSourceFile; /* file (not path!) containing the icon */
56 DWORD dwSourceIndex; /* index within the file, if it is a resource ID it will be negated */
57 DWORD dwListIndex; /* index within the iconlist */
58 DWORD dwFlags; /* GIL_* flags */
59 DWORD dwAccessTime;
60 } SIC_ENTRY, * LPSIC_ENTRY;
62 static HDPA sic_hdpa;
63 static INIT_ONCE sic_init_once = INIT_ONCE_STATIC_INIT;
64 static HIMAGELIST ShellSmallIconList;
65 static HIMAGELIST ShellBigIconList;
67 static CRITICAL_SECTION SHELL32_SicCS;
68 static CRITICAL_SECTION_DEBUG critsect_debug =
70 0, 0, &SHELL32_SicCS,
71 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
72 0, 0, { (DWORD_PTR)(__FILE__ ": SHELL32_SicCS") }
74 static CRITICAL_SECTION SHELL32_SicCS = { &critsect_debug, -1, 0, 0, 0, 0 };
76 /*****************************************************************************
77 * SIC_CompareEntries
79 * NOTES
80 * Callback for DPA_Search
82 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
84 LPSIC_ENTRY e1 = p1, e2 = p2;
86 TRACE("%p %p %8lx\n", p1, p2, lparam);
88 /* Icons in the cache are keyed by the name of the file they are
89 * loaded from, their resource index and the fact if they have a shortcut
90 * icon overlay or not.
92 if (e1->dwSourceIndex != e2->dwSourceIndex || /* first the faster one */
93 (e1->dwFlags & GIL_FORSHORTCUT) != (e2->dwFlags & GIL_FORSHORTCUT))
94 return 1;
96 if (strcmpiW(e1->sSourceFile,e2->sSourceFile))
97 return 1;
99 return 0;
102 /* declare SIC_LoadOverlayIcon() */
103 static int SIC_LoadOverlayIcon(int icon_idx);
105 /*****************************************************************************
106 * SIC_OverlayShortcutImage [internal]
108 * NOTES
109 * Creates a new icon as a copy of the passed-in icon, overlayed with a
110 * shortcut image.
112 static HICON SIC_OverlayShortcutImage(HICON SourceIcon, BOOL large)
113 { ICONINFO SourceIconInfo, ShortcutIconInfo, TargetIconInfo;
114 HICON ShortcutIcon, TargetIcon;
115 BITMAP SourceBitmapInfo, ShortcutBitmapInfo;
116 HDC SourceDC = NULL,
117 ShortcutDC = NULL,
118 TargetDC = NULL,
119 ScreenDC = NULL;
120 HBITMAP OldSourceBitmap = NULL,
121 OldShortcutBitmap = NULL,
122 OldTargetBitmap = NULL;
124 static int s_imgListIdx = -1;
126 /* Get information about the source icon and shortcut overlay */
127 if (! GetIconInfo(SourceIcon, &SourceIconInfo)
128 || 0 == GetObjectW(SourceIconInfo.hbmColor, sizeof(BITMAP), &SourceBitmapInfo))
130 return NULL;
133 /* search for the shortcut icon only once */
134 if (s_imgListIdx == -1)
135 s_imgListIdx = SIC_LoadOverlayIcon(- IDI_SHELL_SHORTCUT);
136 /* FIXME should use icon index 29 instead of the
137 resource id, but not all icons are present yet
138 so we can't use icon indices */
140 if (s_imgListIdx != -1)
142 if (large)
143 ShortcutIcon = ImageList_GetIcon(ShellBigIconList, s_imgListIdx, ILD_TRANSPARENT);
144 else
145 ShortcutIcon = ImageList_GetIcon(ShellSmallIconList, s_imgListIdx, ILD_TRANSPARENT);
146 } else
147 ShortcutIcon = NULL;
149 if (NULL == ShortcutIcon
150 || ! GetIconInfo(ShortcutIcon, &ShortcutIconInfo)
151 || 0 == GetObjectW(ShortcutIconInfo.hbmColor, sizeof(BITMAP), &ShortcutBitmapInfo))
153 return NULL;
156 TargetIconInfo = SourceIconInfo;
157 TargetIconInfo.hbmMask = NULL;
158 TargetIconInfo.hbmColor = NULL;
160 /* Setup the source, shortcut and target masks */
161 SourceDC = CreateCompatibleDC(NULL);
162 if (NULL == SourceDC) goto fail;
163 OldSourceBitmap = SelectObject(SourceDC, SourceIconInfo.hbmMask);
164 if (NULL == OldSourceBitmap) goto fail;
166 ShortcutDC = CreateCompatibleDC(NULL);
167 if (NULL == ShortcutDC) goto fail;
168 OldShortcutBitmap = SelectObject(ShortcutDC, ShortcutIconInfo.hbmMask);
169 if (NULL == OldShortcutBitmap) goto fail;
171 TargetDC = CreateCompatibleDC(NULL);
172 if (NULL == TargetDC) goto fail;
173 TargetIconInfo.hbmMask = CreateCompatibleBitmap(TargetDC, SourceBitmapInfo.bmWidth,
174 SourceBitmapInfo.bmHeight);
175 if (NULL == TargetIconInfo.hbmMask) goto fail;
176 ScreenDC = GetDC(NULL);
177 if (NULL == ScreenDC) goto fail;
178 TargetIconInfo.hbmColor = CreateCompatibleBitmap(ScreenDC, SourceBitmapInfo.bmWidth,
179 SourceBitmapInfo.bmHeight);
180 ReleaseDC(NULL, ScreenDC);
181 if (NULL == TargetIconInfo.hbmColor) goto fail;
182 OldTargetBitmap = SelectObject(TargetDC, TargetIconInfo.hbmMask);
183 if (NULL == OldTargetBitmap) goto fail;
185 /* Create the target mask by ANDing the source and shortcut masks */
186 if (! BitBlt(TargetDC, 0, 0, SourceBitmapInfo.bmWidth, SourceBitmapInfo.bmHeight,
187 SourceDC, 0, 0, SRCCOPY) ||
188 ! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
189 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
190 ShortcutDC, 0, 0, SRCAND))
192 goto fail;
195 /* Setup the source and target xor bitmap */
196 if (NULL == SelectObject(SourceDC, SourceIconInfo.hbmColor) ||
197 NULL == SelectObject(TargetDC, TargetIconInfo.hbmColor))
199 goto fail;
202 /* Copy the source xor bitmap to the target and clear out part of it by using
203 the shortcut mask */
204 if (! BitBlt(TargetDC, 0, 0, SourceBitmapInfo.bmWidth, SourceBitmapInfo.bmHeight,
205 SourceDC, 0, 0, SRCCOPY) ||
206 ! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
207 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
208 ShortcutDC, 0, 0, SRCAND))
210 goto fail;
213 if (NULL == SelectObject(ShortcutDC, ShortcutIconInfo.hbmColor)) goto fail;
215 /* Now put in the shortcut xor mask */
216 if (! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
217 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
218 ShortcutDC, 0, 0, SRCINVERT))
220 goto fail;
223 /* Clean up, we're not goto'ing to 'fail' after this so we can be lazy and not set
224 handles to NULL */
225 SelectObject(TargetDC, OldTargetBitmap);
226 DeleteObject(TargetDC);
227 SelectObject(ShortcutDC, OldShortcutBitmap);
228 DeleteObject(ShortcutDC);
229 SelectObject(SourceDC, OldSourceBitmap);
230 DeleteObject(SourceDC);
232 /* Create the icon using the bitmaps prepared earlier */
233 TargetIcon = CreateIconIndirect(&TargetIconInfo);
235 /* CreateIconIndirect copies the bitmaps, so we can release our bitmaps now */
236 DeleteObject(TargetIconInfo.hbmColor);
237 DeleteObject(TargetIconInfo.hbmMask);
239 return TargetIcon;
241 fail:
242 /* Clean up scratch resources we created */
243 if (NULL != OldTargetBitmap) SelectObject(TargetDC, OldTargetBitmap);
244 if (NULL != TargetIconInfo.hbmColor) DeleteObject(TargetIconInfo.hbmColor);
245 if (NULL != TargetIconInfo.hbmMask) DeleteObject(TargetIconInfo.hbmMask);
246 if (NULL != TargetDC) DeleteObject(TargetDC);
247 if (NULL != OldShortcutBitmap) SelectObject(ShortcutDC, OldShortcutBitmap);
248 if (NULL != ShortcutDC) DeleteObject(ShortcutDC);
249 if (NULL != OldSourceBitmap) SelectObject(SourceDC, OldSourceBitmap);
250 if (NULL != SourceDC) DeleteObject(SourceDC);
252 return NULL;
255 /*****************************************************************************
256 * SIC_IconAppend [internal]
258 * NOTES
259 * appends an icon pair to the end of the cache
261 static INT SIC_IconAppend (LPCWSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon, DWORD dwFlags)
262 { LPSIC_ENTRY lpsice;
263 INT ret, index, index1;
264 WCHAR path[MAX_PATH];
265 TRACE("%s %i %p %p\n", debugstr_w(sSourceFile), dwSourceIndex, hSmallIcon ,hBigIcon);
267 lpsice = SHAlloc(sizeof(SIC_ENTRY));
269 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
270 lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, (strlenW(path)+1)*sizeof(WCHAR) );
271 strcpyW( lpsice->sSourceFile, path );
273 lpsice->dwSourceIndex = dwSourceIndex;
274 lpsice->dwFlags = dwFlags;
276 EnterCriticalSection(&SHELL32_SicCS);
278 index = DPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
279 if ( INVALID_INDEX == index )
281 HeapFree(GetProcessHeap(), 0, lpsice->sSourceFile);
282 SHFree(lpsice);
283 ret = INVALID_INDEX;
285 else
287 index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
288 index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
290 if (index!=index1)
292 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
294 lpsice->dwListIndex = index;
295 ret = lpsice->dwListIndex;
298 LeaveCriticalSection(&SHELL32_SicCS);
299 return ret;
301 /****************************************************************************
302 * SIC_LoadIcon [internal]
304 * NOTES
305 * gets small/big icon by number from a file
307 static INT SIC_LoadIcon (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags)
308 { HICON hiconLarge=0;
309 HICON hiconSmall=0;
310 HICON hiconLargeShortcut;
311 HICON hiconSmallShortcut;
313 PrivateExtractIconsW( sSourceFile, dwSourceIndex, GetSystemMetrics(SM_CXICON),
314 GetSystemMetrics(SM_CYICON), &hiconLarge, 0, 1, 0 );
315 PrivateExtractIconsW( sSourceFile, dwSourceIndex, GetSystemMetrics(SM_CXSMICON),
316 GetSystemMetrics(SM_CYSMICON), &hiconSmall, 0, 1, 0 );
318 if ( !hiconLarge || !hiconSmall)
320 WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, debugstr_w(sSourceFile), hiconLarge, hiconSmall);
321 return -1;
324 if (0 != (dwFlags & GIL_FORSHORTCUT))
326 hiconLargeShortcut = SIC_OverlayShortcutImage(hiconLarge, TRUE);
327 hiconSmallShortcut = SIC_OverlayShortcutImage(hiconSmall, FALSE);
328 if (NULL != hiconLargeShortcut && NULL != hiconSmallShortcut)
330 hiconLarge = hiconLargeShortcut;
331 hiconSmall = hiconSmallShortcut;
333 else
335 WARN("Failed to create shortcut overlayed icons\n");
336 if (NULL != hiconLargeShortcut) DestroyIcon(hiconLargeShortcut);
337 if (NULL != hiconSmallShortcut) DestroyIcon(hiconSmallShortcut);
338 dwFlags &= ~ GIL_FORSHORTCUT;
342 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge, dwFlags);
344 /*****************************************************************************
345 * SIC_Initialize [internal]
347 static BOOL WINAPI SIC_Initialize( INIT_ONCE *once, void *param, void **context )
349 HICON hSm, hLg;
350 int cx_small, cy_small;
351 int cx_large, cy_large;
353 cx_small = GetSystemMetrics(SM_CXSMICON);
354 cy_small = GetSystemMetrics(SM_CYSMICON);
355 cx_large = GetSystemMetrics(SM_CXICON);
356 cy_large = GetSystemMetrics(SM_CYICON);
358 TRACE("\n");
360 sic_hdpa = DPA_Create(16);
362 if (!sic_hdpa)
364 return(FALSE);
367 ShellSmallIconList = ImageList_Create(cx_small,cy_small,ILC_COLOR32|ILC_MASK,0,0x20);
368 ShellBigIconList = ImageList_Create(cx_large,cy_large,ILC_COLOR32|ILC_MASK,0,0x20);
370 ImageList_SetBkColor(ShellSmallIconList, CLR_NONE);
371 ImageList_SetBkColor(ShellBigIconList, CLR_NONE);
373 /* Load the document icon, which is used as the default if an icon isn't found. */
374 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_DOCUMENT),
375 IMAGE_ICON, cx_small, cy_small, LR_SHARED);
376 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_DOCUMENT),
377 IMAGE_ICON, cx_large, cy_large, LR_SHARED);
379 if (!hSm || !hLg)
381 FIXME("Failed to load IDI_SHELL_DOCUMENT icon!\n");
382 return FALSE;
385 SIC_IconAppend (swShell32Name, IDI_SHELL_DOCUMENT-1, hSm, hLg, 0);
386 SIC_IconAppend (swShell32Name, -IDI_SHELL_DOCUMENT, hSm, hLg, 0);
388 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
390 return TRUE;
392 /*************************************************************************
393 * SIC_Destroy
395 * frees the cache
397 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
399 HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
400 SHFree(ptr);
401 return TRUE;
404 void SIC_Destroy(void)
406 TRACE("\n");
408 EnterCriticalSection(&SHELL32_SicCS);
410 if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
412 if (ShellSmallIconList)
413 ImageList_Destroy(ShellSmallIconList);
414 if (ShellBigIconList)
415 ImageList_Destroy(ShellBigIconList);
417 LeaveCriticalSection(&SHELL32_SicCS);
418 DeleteCriticalSection(&SHELL32_SicCS);
421 /*****************************************************************************
422 * SIC_GetIconIndex [internal]
424 * Parameters
425 * sSourceFile [IN] filename of file containing the icon
426 * index [IN] index/resID (negated) in this file
428 * NOTES
429 * look in the cache for a proper icon. if not available the icon is taken
430 * from the file and cached
432 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags )
434 SIC_ENTRY sice;
435 INT ret, index = INVALID_INDEX;
436 WCHAR path[MAX_PATH];
438 TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
440 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
441 sice.sSourceFile = path;
442 sice.dwSourceIndex = dwSourceIndex;
443 sice.dwFlags = dwFlags;
445 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
447 EnterCriticalSection(&SHELL32_SicCS);
449 if (NULL != DPA_GetPtr (sic_hdpa, 0))
451 /* search linear from position 0*/
452 index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
455 if ( INVALID_INDEX == index )
457 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex, dwFlags);
459 else
461 TRACE("-- found\n");
462 ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
465 LeaveCriticalSection(&SHELL32_SicCS);
466 return ret;
469 /*****************************************************************************
470 * SIC_LoadOverlayIcon [internal]
472 * Load a shell overlay icon and return its icon cache index.
474 static int SIC_LoadOverlayIcon(int icon_idx)
476 WCHAR buffer[1024], wszIdx[8];
477 HKEY hKeyShellIcons;
478 LPCWSTR iconPath;
479 int iconIdx;
481 static const WCHAR wszShellIcons[] = {
482 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
483 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
484 'E','x','p','l','o','r','e','r','\\','S','h','e','l','l',' ','I','c','o','n','s',0
486 static const WCHAR wszNumFmt[] = {'%','d',0};
488 iconPath = swShell32Name; /* default: load icon from shell32.dll */
489 iconIdx = icon_idx;
491 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszShellIcons, 0, KEY_READ, &hKeyShellIcons) == ERROR_SUCCESS)
493 DWORD count = sizeof(buffer);
495 sprintfW(wszIdx, wszNumFmt, icon_idx);
497 /* read icon path and index */
498 if (RegQueryValueExW(hKeyShellIcons, wszIdx, NULL, NULL, (LPBYTE)buffer, &count) == ERROR_SUCCESS)
500 LPWSTR p = strchrW(buffer, ',');
502 if (!p)
504 ERR("Icon index in %s/%s corrupted, no comma.\n", debugstr_w(wszShellIcons),debugstr_w(wszIdx));
505 RegCloseKey(hKeyShellIcons);
506 return -1;
508 *p++ = 0;
509 iconPath = buffer;
510 iconIdx = atoiW(p);
513 RegCloseKey(hKeyShellIcons);
516 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
518 return SIC_LoadIcon(iconPath, iconIdx, 0);
521 /*************************************************************************
522 * Shell_GetImageLists [SHELL32.71]
524 * PARAMETERS
525 * imglist[1|2] [OUT] pointer which receives imagelist handles
528 BOOL WINAPI Shell_GetImageLists(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
530 TRACE("(%p,%p)\n",lpBigList,lpSmallList);
531 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
532 if (lpBigList) *lpBigList = ShellBigIconList;
533 if (lpSmallList) *lpSmallList = ShellSmallIconList;
534 return TRUE;
536 /*************************************************************************
537 * PidlToSicIndex [INTERNAL]
539 * PARAMETERS
540 * sh [IN] IShellFolder
541 * pidl [IN]
542 * bBigIcon [IN]
543 * uFlags [IN] GIL_*
544 * pIndex [OUT] index within the SIC
547 BOOL PidlToSicIndex (
548 IShellFolder * sh,
549 LPCITEMIDLIST pidl,
550 BOOL bBigIcon,
551 UINT uFlags,
552 int * pIndex)
554 IExtractIconW *ei;
555 WCHAR szIconFile[MAX_PATH]; /* file containing the icon */
556 INT iSourceIndex; /* index or resID(negated) in this file */
557 BOOL ret = FALSE;
558 UINT dwFlags = 0;
559 int iShortcutDefaultIndex = INVALID_INDEX;
561 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
563 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
565 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconW, 0, (void **)&ei)))
567 if (SUCCEEDED(IExtractIconW_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
569 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex, uFlags);
570 ret = TRUE;
572 IExtractIconW_Release(ei);
575 if (INVALID_INDEX == *pIndex) /* default icon when failed */
577 if (0 == (uFlags & GIL_FORSHORTCUT))
579 *pIndex = 0;
581 else
583 if (INVALID_INDEX == iShortcutDefaultIndex)
585 iShortcutDefaultIndex = SIC_LoadIcon(swShell32Name, 0, GIL_FORSHORTCUT);
587 *pIndex = (INVALID_INDEX != iShortcutDefaultIndex ? iShortcutDefaultIndex : 0);
591 return ret;
595 /*************************************************************************
596 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
598 * PARAMETERS
599 * sh [IN] pointer to an instance of IShellFolder
600 * pidl [IN]
601 * pIndex [OUT][OPTIONAL] SIC index for big icon
604 int WINAPI SHMapPIDLToSystemImageListIndex(
605 IShellFolder *sh,
606 LPCITEMIDLIST pidl,
607 int *pIndex)
609 int Index;
610 UINT uGilFlags = 0;
612 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
613 pdump(pidl);
615 if (SHELL_IsShortcut(pidl))
616 uGilFlags |= GIL_FORSHORTCUT;
618 if (pIndex)
619 if (!PidlToSicIndex ( sh, pidl, 1, uGilFlags, pIndex))
620 *pIndex = -1;
622 if (!PidlToSicIndex ( sh, pidl, 0, uGilFlags, &Index))
623 return -1;
625 return Index;
628 /*************************************************************************
629 * SHMapIDListToImageListIndexAsync [SHELL32.148]
631 HRESULT WINAPI SHMapIDListToImageListIndexAsync(IUnknown *pts, IShellFolder *psf,
632 LPCITEMIDLIST pidl, UINT flags,
633 void *pfn, void *pvData, void *pvHint,
634 int *piIndex, int *piIndexSel)
636 FIXME("(%p, %p, %p, 0x%08x, %p, %p, %p, %p, %p)\n",
637 pts, psf, pidl, flags, pfn, pvData, pvHint, piIndex, piIndexSel);
638 return E_FAIL;
641 /*************************************************************************
642 * Shell_GetCachedImageIndex [SHELL32.72]
645 static INT Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
647 INT ret, len;
648 LPWSTR szTemp;
650 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
652 len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
653 szTemp = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
654 MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
656 ret = SIC_GetIconIndex( szTemp, nIndex, 0 );
658 HeapFree( GetProcessHeap(), 0, szTemp );
660 return ret;
663 static INT Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
665 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
667 return SIC_GetIconIndex(szPath, nIndex, 0);
670 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
671 { if( SHELL_OsIsUnicode())
672 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
673 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
676 /*************************************************************************
677 * ExtractIconExW [SHELL32.@]
678 * RETURNS
679 * 0 no icon found
680 * -1 file is not valid
681 * or number of icons extracted
683 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
685 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
687 return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
690 /*************************************************************************
691 * ExtractIconExA [SHELL32.@]
693 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
695 UINT ret = 0;
696 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
697 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
699 TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
701 if (lpwstrFile)
703 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
704 ret = ExtractIconExW(lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
705 HeapFree(GetProcessHeap(), 0, lpwstrFile);
707 return ret;
710 /*************************************************************************
711 * ExtractAssociatedIconA (SHELL32.@)
713 * Return icon for given file (either from file itself or from associated
714 * executable) and patch parameters if needed.
716 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
718 HICON hIcon = NULL;
719 INT len = MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, NULL, 0);
720 /* Note that we need to allocate MAX_PATH, since we are supposed to fill
721 * the correct executable if there is no icon in lpIconPath directly.
722 * lpIconPath itself is supposed to be large enough, so make sure lpIconPathW
723 * is large enough too. Yes, I am puking too.
725 LPWSTR lpIconPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
727 TRACE("%p %s %p\n", hInst, debugstr_a(lpIconPath), lpiIcon);
729 if (lpIconPathW)
731 MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, lpIconPathW, len);
732 hIcon = ExtractAssociatedIconW(hInst, lpIconPathW, lpiIcon);
733 WideCharToMultiByte(CP_ACP, 0, lpIconPathW, -1, lpIconPath, MAX_PATH , NULL, NULL);
734 HeapFree(GetProcessHeap(), 0, lpIconPathW);
736 return hIcon;
739 /*************************************************************************
740 * ExtractAssociatedIconW (SHELL32.@)
742 * Return icon for given file (either from file itself or from associated
743 * executable) and patch parameters if needed.
745 HICON WINAPI ExtractAssociatedIconW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIcon)
747 HICON hIcon = NULL;
748 WORD wDummyIcon = 0;
750 TRACE("%p %s %p\n", hInst, debugstr_w(lpIconPath), lpiIcon);
752 if(lpiIcon == NULL)
753 lpiIcon = &wDummyIcon;
755 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
757 if( hIcon < (HICON)2 )
758 { if( hIcon == (HICON)1 ) /* no icons found in given file */
759 { WCHAR tempPath[MAX_PATH];
760 HINSTANCE uRet = FindExecutableW(lpIconPath,NULL,tempPath);
762 if( uRet > (HINSTANCE)32 && tempPath[0] )
763 { lstrcpyW(lpIconPath,tempPath);
764 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
765 if( hIcon > (HICON)2 )
766 return hIcon;
770 if( hIcon == (HICON)1 )
771 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
772 else
773 *lpiIcon = 6; /* generic icon - found nothing */
775 if (GetModuleFileNameW(hInst, lpIconPath, MAX_PATH))
776 hIcon = LoadIconW(hInst, MAKEINTRESOURCEW(*lpiIcon));
778 return hIcon;
781 /*************************************************************************
782 * ExtractAssociatedIconExW (SHELL32.@)
784 * Return icon for given file (either from file itself or from associated
785 * executable) and patch parameters if needed.
787 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
789 FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
790 return 0;
793 /*************************************************************************
794 * ExtractAssociatedIconExA (SHELL32.@)
796 * Return icon for given file (either from file itself or from associated
797 * executable) and patch parameters if needed.
799 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
801 HICON ret;
802 INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
803 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
805 TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
807 MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
808 ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
809 HeapFree(GetProcessHeap(), 0, lpwstrFile);
810 return ret;
814 /****************************************************************************
815 * SHDefExtractIconW [SHELL32.@]
817 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
818 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
820 UINT ret;
821 HICON hIcons[2];
822 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
824 ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
825 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
826 if (ret == 0xFFFFFFFF)
827 return E_FAIL;
828 if (ret > 0) {
829 if (phiconLarge)
830 *phiconLarge = hIcons[0];
831 else
832 DestroyIcon(hIcons[0]);
833 if (phiconSmall)
834 *phiconSmall = hIcons[1];
835 else
836 DestroyIcon(hIcons[1]);
837 return S_OK;
839 return S_FALSE;
842 /****************************************************************************
843 * SHDefExtractIconA [SHELL32.@]
845 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
846 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
848 HRESULT ret;
849 INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
850 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
852 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
854 MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
855 ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
856 HeapFree(GetProcessHeap(), 0, lpwstrFile);
857 return ret;
861 /****************************************************************************
862 * SHGetIconOverlayIndexA [SHELL32.@]
864 * Returns the index of the overlay icon in the system image list.
866 INT WINAPI SHGetIconOverlayIndexA(LPCSTR pszIconPath, INT iIconIndex)
868 FIXME("%s, %d\n", debugstr_a(pszIconPath), iIconIndex);
870 return -1;
873 /****************************************************************************
874 * SHGetIconOverlayIndexW [SHELL32.@]
876 * Returns the index of the overlay icon in the system image list.
878 INT WINAPI SHGetIconOverlayIndexW(LPCWSTR pszIconPath, INT iIconIndex)
880 FIXME("%s, %d\n", debugstr_w(pszIconPath), iIconIndex);
882 return -1;
885 /****************************************************************************
886 * SHGetStockIconInfo [SHELL32.@]
888 * Receive information for builtin icons
890 * PARAMS
891 * id [I] selected icon-id to get information for
892 * flags [I] selects the information to receive
893 * sii [IO] SHSTOCKICONINFO structure to fill
895 * RETURNS
896 * Success: S_OK
897 * Failure: A HRESULT failure code
900 HRESULT WINAPI SHGetStockIconInfo(SHSTOCKICONID id, UINT flags, SHSTOCKICONINFO *sii)
902 static const WCHAR shell32dll[] = {'\\','s','h','e','l','l','3','2','.','d','l','l',0};
904 FIXME("(%d, 0x%x, %p) semi-stub\n", id, flags, sii);
905 if ((id < 0) || (id >= SIID_MAX_ICONS) || !sii || (sii->cbSize != sizeof(SHSTOCKICONINFO))) {
906 return E_INVALIDARG;
909 GetSystemDirectoryW(sii->szPath, MAX_PATH);
911 /* no icons defined: use default */
912 sii->iIcon = -IDI_SHELL_DOCUMENT;
913 lstrcatW(sii->szPath, shell32dll);
915 if (flags)
916 FIXME("flags 0x%x not implemented\n", flags);
918 sii->hIcon = NULL;
919 sii->iSysImageIndex = -1;
921 TRACE("%3d: returning %s (%d)\n", id, debugstr_w(sii->szPath), sii->iIcon);
923 return S_OK;