advapi32/tests: Add trailing slashes tests for RegOpen/CreateKey.
[wine.git] / dlls / shell32 / iconcache.c
blobb29490896d7b6341b2d6fc45a241afb4e72b1b67
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, 32, 32, &hiconLarge, 0, 1, 0 );
314 PrivateExtractIconsW( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
316 if ( !hiconLarge || !hiconSmall)
318 WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, debugstr_w(sSourceFile), hiconLarge, hiconSmall);
319 return -1;
322 if (0 != (dwFlags & GIL_FORSHORTCUT))
324 hiconLargeShortcut = SIC_OverlayShortcutImage(hiconLarge, TRUE);
325 hiconSmallShortcut = SIC_OverlayShortcutImage(hiconSmall, FALSE);
326 if (NULL != hiconLargeShortcut && NULL != hiconSmallShortcut)
328 hiconLarge = hiconLargeShortcut;
329 hiconSmall = hiconSmallShortcut;
331 else
333 WARN("Failed to create shortcut overlayed icons\n");
334 if (NULL != hiconLargeShortcut) DestroyIcon(hiconLargeShortcut);
335 if (NULL != hiconSmallShortcut) DestroyIcon(hiconSmallShortcut);
336 dwFlags &= ~ GIL_FORSHORTCUT;
340 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge, dwFlags);
342 /*****************************************************************************
343 * SIC_Initialize [internal]
345 static BOOL WINAPI SIC_Initialize( INIT_ONCE *once, void *param, void **context )
347 HICON hSm, hLg;
348 int cx_small, cy_small;
349 int cx_large, cy_large;
351 cx_small = GetSystemMetrics(SM_CXSMICON);
352 cy_small = GetSystemMetrics(SM_CYSMICON);
353 cx_large = GetSystemMetrics(SM_CXICON);
354 cy_large = GetSystemMetrics(SM_CYICON);
356 TRACE("\n");
358 sic_hdpa = DPA_Create(16);
360 if (!sic_hdpa)
362 return(FALSE);
365 ShellSmallIconList = ImageList_Create(cx_small,cy_small,ILC_COLOR32|ILC_MASK,0,0x20);
366 ShellBigIconList = ImageList_Create(cx_large,cy_large,ILC_COLOR32|ILC_MASK,0,0x20);
368 ImageList_SetBkColor(ShellSmallIconList, CLR_NONE);
369 ImageList_SetBkColor(ShellBigIconList, CLR_NONE);
371 /* Load the document icon, which is used as the default if an icon isn't found. */
372 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_DOCUMENT),
373 IMAGE_ICON, cx_small, cy_small, LR_SHARED);
374 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_DOCUMENT),
375 IMAGE_ICON, cx_large, cy_large, LR_SHARED);
377 if (!hSm || !hLg)
379 FIXME("Failed to load IDI_SHELL_DOCUMENT icon!\n");
380 return FALSE;
383 SIC_IconAppend (swShell32Name, IDI_SHELL_DOCUMENT-1, hSm, hLg, 0);
384 SIC_IconAppend (swShell32Name, -IDI_SHELL_DOCUMENT, hSm, hLg, 0);
386 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
388 return TRUE;
390 /*************************************************************************
391 * SIC_Destroy
393 * frees the cache
395 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
397 HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
398 SHFree(ptr);
399 return TRUE;
402 void SIC_Destroy(void)
404 TRACE("\n");
406 EnterCriticalSection(&SHELL32_SicCS);
408 if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
410 if (ShellSmallIconList)
411 ImageList_Destroy(ShellSmallIconList);
412 if (ShellBigIconList)
413 ImageList_Destroy(ShellBigIconList);
415 LeaveCriticalSection(&SHELL32_SicCS);
416 DeleteCriticalSection(&SHELL32_SicCS);
419 /*****************************************************************************
420 * SIC_GetIconIndex [internal]
422 * Parameters
423 * sSourceFile [IN] filename of file containing the icon
424 * index [IN] index/resID (negated) in this file
426 * NOTES
427 * look in the cache for a proper icon. if not available the icon is taken
428 * from the file and cached
430 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags )
432 SIC_ENTRY sice;
433 INT ret, index = INVALID_INDEX;
434 WCHAR path[MAX_PATH];
436 TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
438 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
439 sice.sSourceFile = path;
440 sice.dwSourceIndex = dwSourceIndex;
441 sice.dwFlags = dwFlags;
443 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
445 EnterCriticalSection(&SHELL32_SicCS);
447 if (NULL != DPA_GetPtr (sic_hdpa, 0))
449 /* search linear from position 0*/
450 index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
453 if ( INVALID_INDEX == index )
455 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex, dwFlags);
457 else
459 TRACE("-- found\n");
460 ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
463 LeaveCriticalSection(&SHELL32_SicCS);
464 return ret;
467 /*****************************************************************************
468 * SIC_LoadOverlayIcon [internal]
470 * Load a shell overlay icon and return its icon cache index.
472 static int SIC_LoadOverlayIcon(int icon_idx)
474 WCHAR buffer[1024], wszIdx[8];
475 HKEY hKeyShellIcons;
476 LPCWSTR iconPath;
477 int iconIdx;
479 static const WCHAR wszShellIcons[] = {
480 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
481 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
482 'E','x','p','l','o','r','e','r','\\','S','h','e','l','l',' ','I','c','o','n','s',0
484 static const WCHAR wszNumFmt[] = {'%','d',0};
486 iconPath = swShell32Name; /* default: load icon from shell32.dll */
487 iconIdx = icon_idx;
489 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszShellIcons, 0, KEY_READ, &hKeyShellIcons) == ERROR_SUCCESS)
491 DWORD count = sizeof(buffer);
493 sprintfW(wszIdx, wszNumFmt, icon_idx);
495 /* read icon path and index */
496 if (RegQueryValueExW(hKeyShellIcons, wszIdx, NULL, NULL, (LPBYTE)buffer, &count) == ERROR_SUCCESS)
498 LPWSTR p = strchrW(buffer, ',');
500 if (!p)
502 ERR("Icon index in %s/%s corrupted, no comma.\n", debugstr_w(wszShellIcons),debugstr_w(wszIdx));
503 RegCloseKey(hKeyShellIcons);
504 return -1;
506 *p++ = 0;
507 iconPath = buffer;
508 iconIdx = atoiW(p);
511 RegCloseKey(hKeyShellIcons);
514 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
516 return SIC_LoadIcon(iconPath, iconIdx, 0);
519 /*************************************************************************
520 * Shell_GetImageLists [SHELL32.71]
522 * PARAMETERS
523 * imglist[1|2] [OUT] pointer which receives imagelist handles
526 BOOL WINAPI Shell_GetImageLists(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
528 TRACE("(%p,%p)\n",lpBigList,lpSmallList);
529 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
530 if (lpBigList) *lpBigList = ShellBigIconList;
531 if (lpSmallList) *lpSmallList = ShellSmallIconList;
532 return TRUE;
534 /*************************************************************************
535 * PidlToSicIndex [INTERNAL]
537 * PARAMETERS
538 * sh [IN] IShellFolder
539 * pidl [IN]
540 * bBigIcon [IN]
541 * uFlags [IN] GIL_*
542 * pIndex [OUT] index within the SIC
545 BOOL PidlToSicIndex (
546 IShellFolder * sh,
547 LPCITEMIDLIST pidl,
548 BOOL bBigIcon,
549 UINT uFlags,
550 int * pIndex)
552 IExtractIconW *ei;
553 WCHAR szIconFile[MAX_PATH]; /* file containing the icon */
554 INT iSourceIndex; /* index or resID(negated) in this file */
555 BOOL ret = FALSE;
556 UINT dwFlags = 0;
557 int iShortcutDefaultIndex = INVALID_INDEX;
559 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
561 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
563 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconW, 0, (void **)&ei)))
565 if (SUCCEEDED(IExtractIconW_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
567 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex, uFlags);
568 ret = TRUE;
570 IExtractIconW_Release(ei);
573 if (INVALID_INDEX == *pIndex) /* default icon when failed */
575 if (0 == (uFlags & GIL_FORSHORTCUT))
577 *pIndex = 0;
579 else
581 if (INVALID_INDEX == iShortcutDefaultIndex)
583 iShortcutDefaultIndex = SIC_LoadIcon(swShell32Name, 0, GIL_FORSHORTCUT);
585 *pIndex = (INVALID_INDEX != iShortcutDefaultIndex ? iShortcutDefaultIndex : 0);
589 return ret;
593 /*************************************************************************
594 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
596 * PARAMETERS
597 * sh [IN] pointer to an instance of IShellFolder
598 * pidl [IN]
599 * pIndex [OUT][OPTIONAL] SIC index for big icon
602 int WINAPI SHMapPIDLToSystemImageListIndex(
603 IShellFolder *sh,
604 LPCITEMIDLIST pidl,
605 int *pIndex)
607 int Index;
608 UINT uGilFlags = 0;
610 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
611 pdump(pidl);
613 if (SHELL_IsShortcut(pidl))
614 uGilFlags |= GIL_FORSHORTCUT;
616 if (pIndex)
617 if (!PidlToSicIndex ( sh, pidl, 1, uGilFlags, pIndex))
618 *pIndex = -1;
620 if (!PidlToSicIndex ( sh, pidl, 0, uGilFlags, &Index))
621 return -1;
623 return Index;
626 /*************************************************************************
627 * SHMapIDListToImageListIndexAsync [SHELL32.148]
629 HRESULT WINAPI SHMapIDListToImageListIndexAsync(IUnknown *pts, IShellFolder *psf,
630 LPCITEMIDLIST pidl, UINT flags,
631 void *pfn, void *pvData, void *pvHint,
632 int *piIndex, int *piIndexSel)
634 FIXME("(%p, %p, %p, 0x%08x, %p, %p, %p, %p, %p)\n",
635 pts, psf, pidl, flags, pfn, pvData, pvHint, piIndex, piIndexSel);
636 return E_FAIL;
639 /*************************************************************************
640 * Shell_GetCachedImageIndex [SHELL32.72]
643 static INT Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
645 INT ret, len;
646 LPWSTR szTemp;
648 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
650 len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
651 szTemp = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
652 MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
654 ret = SIC_GetIconIndex( szTemp, nIndex, 0 );
656 HeapFree( GetProcessHeap(), 0, szTemp );
658 return ret;
661 static INT Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
663 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
665 return SIC_GetIconIndex(szPath, nIndex, 0);
668 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
669 { if( SHELL_OsIsUnicode())
670 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
671 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
674 /*************************************************************************
675 * ExtractIconExW [SHELL32.@]
676 * RETURNS
677 * 0 no icon found
678 * -1 file is not valid
679 * or number of icons extracted
681 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
683 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
685 return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
688 /*************************************************************************
689 * ExtractIconExA [SHELL32.@]
691 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
693 UINT ret = 0;
694 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
695 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
697 TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
699 if (lpwstrFile)
701 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
702 ret = ExtractIconExW(lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
703 HeapFree(GetProcessHeap(), 0, lpwstrFile);
705 return ret;
708 /*************************************************************************
709 * ExtractAssociatedIconA (SHELL32.@)
711 * Return icon for given file (either from file itself or from associated
712 * executable) and patch parameters if needed.
714 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
716 HICON hIcon = NULL;
717 INT len = MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, NULL, 0);
718 /* Note that we need to allocate MAX_PATH, since we are supposed to fill
719 * the correct executable if there is no icon in lpIconPath directly.
720 * lpIconPath itself is supposed to be large enough, so make sure lpIconPathW
721 * is large enough too. Yes, I am puking too.
723 LPWSTR lpIconPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
725 TRACE("%p %s %p\n", hInst, debugstr_a(lpIconPath), lpiIcon);
727 if (lpIconPathW)
729 MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, lpIconPathW, len);
730 hIcon = ExtractAssociatedIconW(hInst, lpIconPathW, lpiIcon);
731 WideCharToMultiByte(CP_ACP, 0, lpIconPathW, -1, lpIconPath, MAX_PATH , NULL, NULL);
732 HeapFree(GetProcessHeap(), 0, lpIconPathW);
734 return hIcon;
737 /*************************************************************************
738 * ExtractAssociatedIconW (SHELL32.@)
740 * Return icon for given file (either from file itself or from associated
741 * executable) and patch parameters if needed.
743 HICON WINAPI ExtractAssociatedIconW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIcon)
745 HICON hIcon = NULL;
746 WORD wDummyIcon = 0;
748 TRACE("%p %s %p\n", hInst, debugstr_w(lpIconPath), lpiIcon);
750 if(lpiIcon == NULL)
751 lpiIcon = &wDummyIcon;
753 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
755 if( hIcon < (HICON)2 )
756 { if( hIcon == (HICON)1 ) /* no icons found in given file */
757 { WCHAR tempPath[MAX_PATH];
758 HINSTANCE uRet = FindExecutableW(lpIconPath,NULL,tempPath);
760 if( uRet > (HINSTANCE)32 && tempPath[0] )
761 { lstrcpyW(lpIconPath,tempPath);
762 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
763 if( hIcon > (HICON)2 )
764 return hIcon;
768 if( hIcon == (HICON)1 )
769 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
770 else
771 *lpiIcon = 6; /* generic icon - found nothing */
773 if (GetModuleFileNameW(hInst, lpIconPath, MAX_PATH))
774 hIcon = LoadIconW(hInst, MAKEINTRESOURCEW(*lpiIcon));
776 return hIcon;
779 /*************************************************************************
780 * ExtractAssociatedIconExW (SHELL32.@)
782 * Return icon for given file (either from file itself or from associated
783 * executable) and patch parameters if needed.
785 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
787 FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
788 return 0;
791 /*************************************************************************
792 * ExtractAssociatedIconExA (SHELL32.@)
794 * Return icon for given file (either from file itself or from associated
795 * executable) and patch parameters if needed.
797 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
799 HICON ret;
800 INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
801 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
803 TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
805 MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
806 ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
807 HeapFree(GetProcessHeap(), 0, lpwstrFile);
808 return ret;
812 /****************************************************************************
813 * SHDefExtractIconW [SHELL32.@]
815 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
816 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
818 UINT ret;
819 HICON hIcons[2];
820 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
822 ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
823 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
824 if (ret == 0xFFFFFFFF)
825 return E_FAIL;
826 if (ret > 0) {
827 if (phiconLarge)
828 *phiconLarge = hIcons[0];
829 else
830 DestroyIcon(hIcons[0]);
831 if (phiconSmall)
832 *phiconSmall = hIcons[1];
833 else
834 DestroyIcon(hIcons[1]);
835 return S_OK;
837 return S_FALSE;
840 /****************************************************************************
841 * SHDefExtractIconA [SHELL32.@]
843 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
844 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
846 HRESULT ret;
847 INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
848 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
850 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
852 MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
853 ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
854 HeapFree(GetProcessHeap(), 0, lpwstrFile);
855 return ret;
859 /****************************************************************************
860 * SHGetIconOverlayIndexA [SHELL32.@]
862 * Returns the index of the overlay icon in the system image list.
864 INT WINAPI SHGetIconOverlayIndexA(LPCSTR pszIconPath, INT iIconIndex)
866 FIXME("%s, %d\n", debugstr_a(pszIconPath), iIconIndex);
868 return -1;
871 /****************************************************************************
872 * SHGetIconOverlayIndexW [SHELL32.@]
874 * Returns the index of the overlay icon in the system image list.
876 INT WINAPI SHGetIconOverlayIndexW(LPCWSTR pszIconPath, INT iIconIndex)
878 FIXME("%s, %d\n", debugstr_w(pszIconPath), iIconIndex);
880 return -1;
883 /****************************************************************************
884 * SHGetStockIconInfo [SHELL32.@]
886 * Receive information for builtin icons
888 * PARAMS
889 * id [I] selected icon-id to get information for
890 * flags [I] selects the information to receive
891 * sii [IO] SHSTOCKICONINFO structure to fill
893 * RETURNS
894 * Success: S_OK
895 * Failure: A HRESULT failure code
898 HRESULT WINAPI SHGetStockIconInfo(SHSTOCKICONID id, UINT flags, SHSTOCKICONINFO *sii)
900 static const WCHAR shell32dll[] = {'\\','s','h','e','l','l','3','2','.','d','l','l',0};
902 FIXME("(%d, 0x%x, %p) semi-stub\n", id, flags, sii);
903 if ((id < 0) || (id >= SIID_MAX_ICONS) || !sii || (sii->cbSize != sizeof(SHSTOCKICONINFO))) {
904 return E_INVALIDARG;
907 GetSystemDirectoryW(sii->szPath, MAX_PATH);
909 /* no icons defined: use default */
910 sii->iIcon = -IDI_SHELL_DOCUMENT;
911 lstrcatW(sii->szPath, shell32dll);
913 if (flags)
914 FIXME("flags 0x%x not implemented\n", flags);
916 sii->hIcon = NULL;
917 sii->iSysImageIndex = -1;
919 TRACE("%3d: returning %s (%d)\n", id, debugstr_w(sii->szPath), sii->iIcon);
921 return S_OK;