comctl32/tests: Use BOOL type where appropriate.
[wine.git] / dlls / shell32 / iconcache.c
bloba2373fa9a49299deeb7a7109d01b547fa4042303
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;
65 static CRITICAL_SECTION SHELL32_SicCS;
66 static CRITICAL_SECTION_DEBUG critsect_debug =
68 0, 0, &SHELL32_SicCS,
69 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
70 0, 0, { (DWORD_PTR)(__FILE__ ": SHELL32_SicCS") }
72 static CRITICAL_SECTION SHELL32_SicCS = { &critsect_debug, -1, 0, 0, 0, 0 };
74 /*****************************************************************************
75 * SIC_CompareEntries
77 * NOTES
78 * Callback for DPA_Search
80 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
82 LPSIC_ENTRY e1 = p1, e2 = p2;
84 TRACE("%p %p %8lx\n", p1, p2, lparam);
86 /* Icons in the cache are keyed by the name of the file they are
87 * loaded from, their resource index and the fact if they have a shortcut
88 * icon overlay or not.
90 if (e1->dwSourceIndex != e2->dwSourceIndex || /* first the faster one */
91 (e1->dwFlags & GIL_FORSHORTCUT) != (e2->dwFlags & GIL_FORSHORTCUT))
92 return 1;
94 if (strcmpiW(e1->sSourceFile,e2->sSourceFile))
95 return 1;
97 return 0;
100 /* declare SIC_LoadOverlayIcon() */
101 static int SIC_LoadOverlayIcon(int icon_idx);
103 /*****************************************************************************
104 * SIC_OverlayShortcutImage [internal]
106 * NOTES
107 * Creates a new icon as a copy of the passed-in icon, overlayed with a
108 * shortcut image.
110 static HICON SIC_OverlayShortcutImage(HICON SourceIcon, BOOL large)
111 { ICONINFO SourceIconInfo, ShortcutIconInfo, TargetIconInfo;
112 HICON ShortcutIcon, TargetIcon;
113 BITMAP SourceBitmapInfo, ShortcutBitmapInfo;
114 HDC SourceDC = NULL,
115 ShortcutDC = NULL,
116 TargetDC = NULL,
117 ScreenDC = NULL;
118 HBITMAP OldSourceBitmap = NULL,
119 OldShortcutBitmap = NULL,
120 OldTargetBitmap = NULL;
122 static int s_imgListIdx = -1;
124 /* Get information about the source icon and shortcut overlay */
125 if (! GetIconInfo(SourceIcon, &SourceIconInfo)
126 || 0 == GetObjectW(SourceIconInfo.hbmColor, sizeof(BITMAP), &SourceBitmapInfo))
128 return NULL;
131 /* search for the shortcut icon only once */
132 if (s_imgListIdx == -1)
133 s_imgListIdx = SIC_LoadOverlayIcon(- IDI_SHELL_SHORTCUT);
134 /* FIXME should use icon index 29 instead of the
135 resource id, but not all icons are present yet
136 so we can't use icon indices */
138 if (s_imgListIdx != -1)
140 if (large)
141 ShortcutIcon = ImageList_GetIcon(ShellBigIconList, s_imgListIdx, ILD_TRANSPARENT);
142 else
143 ShortcutIcon = ImageList_GetIcon(ShellSmallIconList, s_imgListIdx, ILD_TRANSPARENT);
144 } else
145 ShortcutIcon = NULL;
147 if (NULL == ShortcutIcon
148 || ! GetIconInfo(ShortcutIcon, &ShortcutIconInfo)
149 || 0 == GetObjectW(ShortcutIconInfo.hbmColor, sizeof(BITMAP), &ShortcutBitmapInfo))
151 return NULL;
154 TargetIconInfo = SourceIconInfo;
155 TargetIconInfo.hbmMask = NULL;
156 TargetIconInfo.hbmColor = NULL;
158 /* Setup the source, shortcut and target masks */
159 SourceDC = CreateCompatibleDC(NULL);
160 if (NULL == SourceDC) goto fail;
161 OldSourceBitmap = SelectObject(SourceDC, SourceIconInfo.hbmMask);
162 if (NULL == OldSourceBitmap) goto fail;
164 ShortcutDC = CreateCompatibleDC(NULL);
165 if (NULL == ShortcutDC) goto fail;
166 OldShortcutBitmap = SelectObject(ShortcutDC, ShortcutIconInfo.hbmMask);
167 if (NULL == OldShortcutBitmap) goto fail;
169 TargetDC = CreateCompatibleDC(NULL);
170 if (NULL == TargetDC) goto fail;
171 TargetIconInfo.hbmMask = CreateCompatibleBitmap(TargetDC, SourceBitmapInfo.bmWidth,
172 SourceBitmapInfo.bmHeight);
173 if (NULL == TargetIconInfo.hbmMask) goto fail;
174 ScreenDC = GetDC(NULL);
175 if (NULL == ScreenDC) goto fail;
176 TargetIconInfo.hbmColor = CreateCompatibleBitmap(ScreenDC, SourceBitmapInfo.bmWidth,
177 SourceBitmapInfo.bmHeight);
178 ReleaseDC(NULL, ScreenDC);
179 if (NULL == TargetIconInfo.hbmColor) goto fail;
180 OldTargetBitmap = SelectObject(TargetDC, TargetIconInfo.hbmMask);
181 if (NULL == OldTargetBitmap) goto fail;
183 /* Create the target mask by ANDing the source and shortcut masks */
184 if (! BitBlt(TargetDC, 0, 0, SourceBitmapInfo.bmWidth, SourceBitmapInfo.bmHeight,
185 SourceDC, 0, 0, SRCCOPY) ||
186 ! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
187 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
188 ShortcutDC, 0, 0, SRCAND))
190 goto fail;
193 /* Setup the source and target xor bitmap */
194 if (NULL == SelectObject(SourceDC, SourceIconInfo.hbmColor) ||
195 NULL == SelectObject(TargetDC, TargetIconInfo.hbmColor))
197 goto fail;
200 /* Copy the source xor bitmap to the target and clear out part of it by using
201 the shortcut mask */
202 if (! BitBlt(TargetDC, 0, 0, SourceBitmapInfo.bmWidth, SourceBitmapInfo.bmHeight,
203 SourceDC, 0, 0, SRCCOPY) ||
204 ! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
205 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
206 ShortcutDC, 0, 0, SRCAND))
208 goto fail;
211 if (NULL == SelectObject(ShortcutDC, ShortcutIconInfo.hbmColor)) goto fail;
213 /* Now put in the shortcut xor mask */
214 if (! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
215 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
216 ShortcutDC, 0, 0, SRCINVERT))
218 goto fail;
221 /* Clean up, we're not goto'ing to 'fail' after this so we can be lazy and not set
222 handles to NULL */
223 SelectObject(TargetDC, OldTargetBitmap);
224 DeleteObject(TargetDC);
225 SelectObject(ShortcutDC, OldShortcutBitmap);
226 DeleteObject(ShortcutDC);
227 SelectObject(SourceDC, OldSourceBitmap);
228 DeleteObject(SourceDC);
230 /* Create the icon using the bitmaps prepared earlier */
231 TargetIcon = CreateIconIndirect(&TargetIconInfo);
233 /* CreateIconIndirect copies the bitmaps, so we can release our bitmaps now */
234 DeleteObject(TargetIconInfo.hbmColor);
235 DeleteObject(TargetIconInfo.hbmMask);
237 return TargetIcon;
239 fail:
240 /* Clean up scratch resources we created */
241 if (NULL != OldTargetBitmap) SelectObject(TargetDC, OldTargetBitmap);
242 if (NULL != TargetIconInfo.hbmColor) DeleteObject(TargetIconInfo.hbmColor);
243 if (NULL != TargetIconInfo.hbmMask) DeleteObject(TargetIconInfo.hbmMask);
244 if (NULL != TargetDC) DeleteObject(TargetDC);
245 if (NULL != OldShortcutBitmap) SelectObject(ShortcutDC, OldShortcutBitmap);
246 if (NULL != ShortcutDC) DeleteObject(ShortcutDC);
247 if (NULL != OldSourceBitmap) SelectObject(SourceDC, OldSourceBitmap);
248 if (NULL != SourceDC) DeleteObject(SourceDC);
250 return NULL;
253 /*****************************************************************************
254 * SIC_IconAppend [internal]
256 * NOTES
257 * appends an icon pair to the end of the cache
259 static INT SIC_IconAppend (LPCWSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon, DWORD dwFlags)
260 { LPSIC_ENTRY lpsice;
261 INT ret, index, index1;
262 WCHAR path[MAX_PATH];
263 TRACE("%s %i %p %p\n", debugstr_w(sSourceFile), dwSourceIndex, hSmallIcon ,hBigIcon);
265 lpsice = SHAlloc(sizeof(SIC_ENTRY));
267 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
268 lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, (strlenW(path)+1)*sizeof(WCHAR) );
269 strcpyW( lpsice->sSourceFile, path );
271 lpsice->dwSourceIndex = dwSourceIndex;
272 lpsice->dwFlags = dwFlags;
274 EnterCriticalSection(&SHELL32_SicCS);
276 index = DPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
277 if ( INVALID_INDEX == index )
279 HeapFree(GetProcessHeap(), 0, lpsice->sSourceFile);
280 SHFree(lpsice);
281 ret = INVALID_INDEX;
283 else
285 index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
286 index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
288 if (index!=index1)
290 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
292 lpsice->dwListIndex = index;
293 ret = lpsice->dwListIndex;
296 LeaveCriticalSection(&SHELL32_SicCS);
297 return ret;
299 /****************************************************************************
300 * SIC_LoadIcon [internal]
302 * NOTES
303 * gets small/big icon by number from a file
305 static INT SIC_LoadIcon (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags)
306 { HICON hiconLarge=0;
307 HICON hiconSmall=0;
308 HICON hiconLargeShortcut;
309 HICON hiconSmallShortcut;
311 PrivateExtractIconsW( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 );
312 PrivateExtractIconsW( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
314 if ( !hiconLarge || !hiconSmall)
316 WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, debugstr_w(sSourceFile), hiconLarge, hiconSmall);
317 return -1;
320 if (0 != (dwFlags & GIL_FORSHORTCUT))
322 hiconLargeShortcut = SIC_OverlayShortcutImage(hiconLarge, TRUE);
323 hiconSmallShortcut = SIC_OverlayShortcutImage(hiconSmall, FALSE);
324 if (NULL != hiconLargeShortcut && NULL != hiconSmallShortcut)
326 hiconLarge = hiconLargeShortcut;
327 hiconSmall = hiconSmallShortcut;
329 else
331 WARN("Failed to create shortcut overlayed icons\n");
332 if (NULL != hiconLargeShortcut) DestroyIcon(hiconLargeShortcut);
333 if (NULL != hiconSmallShortcut) DestroyIcon(hiconSmallShortcut);
334 dwFlags &= ~ GIL_FORSHORTCUT;
338 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge, dwFlags);
340 /*****************************************************************************
341 * SIC_Initialize [internal]
343 static BOOL WINAPI SIC_Initialize( INIT_ONCE *once, void *param, void **context )
345 HICON hSm, hLg;
346 int cx_small, cy_small;
347 int cx_large, cy_large;
349 cx_small = GetSystemMetrics(SM_CXSMICON);
350 cy_small = GetSystemMetrics(SM_CYSMICON);
351 cx_large = GetSystemMetrics(SM_CXICON);
352 cy_large = GetSystemMetrics(SM_CYICON);
354 TRACE("\n");
356 sic_hdpa = DPA_Create(16);
358 if (!sic_hdpa)
360 return(FALSE);
363 ShellSmallIconList = ImageList_Create(cx_small,cy_small,ILC_COLOR32|ILC_MASK,0,0x20);
364 ShellBigIconList = ImageList_Create(cx_large,cy_large,ILC_COLOR32|ILC_MASK,0,0x20);
366 ImageList_SetBkColor(ShellSmallIconList, CLR_NONE);
367 ImageList_SetBkColor(ShellBigIconList, CLR_NONE);
369 /* Load the document icon, which is used as the default if an icon isn't found. */
370 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_DOCUMENT),
371 IMAGE_ICON, cx_small, cy_small, LR_SHARED);
372 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_DOCUMENT),
373 IMAGE_ICON, cx_large, cy_large, LR_SHARED);
375 if (!hSm || !hLg)
377 FIXME("Failed to load IDI_SHELL_DOCUMENT icon!\n");
378 return FALSE;
381 SIC_IconAppend (swShell32Name, IDI_SHELL_DOCUMENT-1, hSm, hLg, 0);
382 SIC_IconAppend (swShell32Name, -IDI_SHELL_DOCUMENT, hSm, hLg, 0);
384 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
386 return TRUE;
388 /*************************************************************************
389 * SIC_Destroy
391 * frees the cache
393 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
395 HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
396 SHFree(ptr);
397 return TRUE;
400 void SIC_Destroy(void)
402 TRACE("\n");
404 EnterCriticalSection(&SHELL32_SicCS);
406 if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
408 ImageList_Destroy(ShellSmallIconList);
409 ImageList_Destroy(ShellBigIconList);
411 LeaveCriticalSection(&SHELL32_SicCS);
412 DeleteCriticalSection(&SHELL32_SicCS);
415 /*****************************************************************************
416 * SIC_GetIconIndex [internal]
418 * Parameters
419 * sSourceFile [IN] filename of file containing the icon
420 * index [IN] index/resID (negated) in this file
422 * NOTES
423 * look in the cache for a proper icon. if not available the icon is taken
424 * from the file and cached
426 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags )
428 SIC_ENTRY sice;
429 INT ret, index = INVALID_INDEX;
430 WCHAR path[MAX_PATH];
432 TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
434 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
435 sice.sSourceFile = path;
436 sice.dwSourceIndex = dwSourceIndex;
437 sice.dwFlags = dwFlags;
439 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
441 EnterCriticalSection(&SHELL32_SicCS);
443 if (NULL != DPA_GetPtr (sic_hdpa, 0))
445 /* search linear from position 0*/
446 index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
449 if ( INVALID_INDEX == index )
451 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex, dwFlags);
453 else
455 TRACE("-- found\n");
456 ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
459 LeaveCriticalSection(&SHELL32_SicCS);
460 return ret;
463 /*****************************************************************************
464 * SIC_LoadOverlayIcon [internal]
466 * Load a shell overlay icon and return its icon cache index.
468 static int SIC_LoadOverlayIcon(int icon_idx)
470 WCHAR buffer[1024], wszIdx[8];
471 HKEY hKeyShellIcons;
472 LPCWSTR iconPath;
473 int iconIdx;
475 static const WCHAR wszShellIcons[] = {
476 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
477 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
478 'E','x','p','l','o','r','e','r','\\','S','h','e','l','l',' ','I','c','o','n','s',0
480 static const WCHAR wszNumFmt[] = {'%','d',0};
482 iconPath = swShell32Name; /* default: load icon from shell32.dll */
483 iconIdx = icon_idx;
485 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszShellIcons, 0, KEY_READ, &hKeyShellIcons) == ERROR_SUCCESS)
487 DWORD count = sizeof(buffer);
489 sprintfW(wszIdx, wszNumFmt, icon_idx);
491 /* read icon path and index */
492 if (RegQueryValueExW(hKeyShellIcons, wszIdx, NULL, NULL, (LPBYTE)buffer, &count) == ERROR_SUCCESS)
494 LPWSTR p = strchrW(buffer, ',');
496 if (!p)
498 ERR("Icon index in %s/%s corrupted, no comma.\n", debugstr_w(wszShellIcons),debugstr_w(wszIdx));
499 RegCloseKey(hKeyShellIcons);
500 return -1;
502 *p++ = 0;
503 iconPath = buffer;
504 iconIdx = atoiW(p);
507 RegCloseKey(hKeyShellIcons);
510 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
512 return SIC_LoadIcon(iconPath, iconIdx, 0);
515 /*************************************************************************
516 * Shell_GetImageLists [SHELL32.71]
518 * PARAMETERS
519 * imglist[1|2] [OUT] pointer which receives imagelist handles
522 BOOL WINAPI Shell_GetImageLists(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
524 TRACE("(%p,%p)\n",lpBigList,lpSmallList);
525 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
526 if (lpBigList) *lpBigList = ShellBigIconList;
527 if (lpSmallList) *lpSmallList = ShellSmallIconList;
528 return TRUE;
530 /*************************************************************************
531 * PidlToSicIndex [INTERNAL]
533 * PARAMETERS
534 * sh [IN] IShellFolder
535 * pidl [IN]
536 * bBigIcon [IN]
537 * uFlags [IN] GIL_*
538 * pIndex [OUT] index within the SIC
541 BOOL PidlToSicIndex (
542 IShellFolder * sh,
543 LPCITEMIDLIST pidl,
544 BOOL bBigIcon,
545 UINT uFlags,
546 int * pIndex)
548 IExtractIconW *ei;
549 WCHAR szIconFile[MAX_PATH]; /* file containing the icon */
550 INT iSourceIndex; /* index or resID(negated) in this file */
551 BOOL ret = FALSE;
552 UINT dwFlags = 0;
553 int iShortcutDefaultIndex = INVALID_INDEX;
555 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
557 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
559 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconW, 0, (void **)&ei)))
561 if (SUCCEEDED(IExtractIconW_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
563 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex, uFlags);
564 ret = TRUE;
566 IExtractIconW_Release(ei);
569 if (INVALID_INDEX == *pIndex) /* default icon when failed */
571 if (0 == (uFlags & GIL_FORSHORTCUT))
573 *pIndex = 0;
575 else
577 if (INVALID_INDEX == iShortcutDefaultIndex)
579 iShortcutDefaultIndex = SIC_LoadIcon(swShell32Name, 0, GIL_FORSHORTCUT);
581 *pIndex = (INVALID_INDEX != iShortcutDefaultIndex ? iShortcutDefaultIndex : 0);
585 return ret;
589 /*************************************************************************
590 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
592 * PARAMETERS
593 * sh [IN] pointer to an instance of IShellFolder
594 * pidl [IN]
595 * pIndex [OUT][OPTIONAL] SIC index for big icon
598 int WINAPI SHMapPIDLToSystemImageListIndex(
599 IShellFolder *sh,
600 LPCITEMIDLIST pidl,
601 int *pIndex)
603 int Index;
604 UINT uGilFlags = 0;
606 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
607 pdump(pidl);
609 if (SHELL_IsShortcut(pidl))
610 uGilFlags |= GIL_FORSHORTCUT;
612 if (pIndex)
613 if (!PidlToSicIndex ( sh, pidl, 1, uGilFlags, pIndex))
614 *pIndex = -1;
616 if (!PidlToSicIndex ( sh, pidl, 0, uGilFlags, &Index))
617 return -1;
619 return Index;
622 /*************************************************************************
623 * SHMapIDListToImageListIndexAsync [SHELL32.148]
625 HRESULT WINAPI SHMapIDListToImageListIndexAsync(IUnknown *pts, IShellFolder *psf,
626 LPCITEMIDLIST pidl, UINT flags,
627 void *pfn, void *pvData, void *pvHint,
628 int *piIndex, int *piIndexSel)
630 FIXME("(%p, %p, %p, 0x%08x, %p, %p, %p, %p, %p)\n",
631 pts, psf, pidl, flags, pfn, pvData, pvHint, piIndex, piIndexSel);
632 return E_FAIL;
635 /*************************************************************************
636 * Shell_GetCachedImageIndex [SHELL32.72]
639 static INT Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
641 INT ret, len;
642 LPWSTR szTemp;
644 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
646 len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
647 szTemp = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
648 MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
650 ret = SIC_GetIconIndex( szTemp, nIndex, 0 );
652 HeapFree( GetProcessHeap(), 0, szTemp );
654 return ret;
657 static INT Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
659 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
661 return SIC_GetIconIndex(szPath, nIndex, 0);
664 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
665 { if( SHELL_OsIsUnicode())
666 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
667 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
670 /*************************************************************************
671 * ExtractIconExW [SHELL32.@]
672 * RETURNS
673 * 0 no icon found
674 * -1 file is not valid
675 * or number of icons extracted
677 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
679 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
681 return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
684 /*************************************************************************
685 * ExtractIconExA [SHELL32.@]
687 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
689 UINT ret = 0;
690 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
691 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
693 TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
695 if (lpwstrFile)
697 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
698 ret = ExtractIconExW(lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
699 HeapFree(GetProcessHeap(), 0, lpwstrFile);
701 return ret;
704 /*************************************************************************
705 * ExtractAssociatedIconA (SHELL32.@)
707 * Return icon for given file (either from file itself or from associated
708 * executable) and patch parameters if needed.
710 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
712 HICON hIcon = NULL;
713 INT len = MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, NULL, 0);
714 /* Note that we need to allocate MAX_PATH, since we are supposed to fill
715 * the correct executable if there is no icon in lpIconPath directly.
716 * lpIconPath itself is supposed to be large enough, so make sure lpIconPathW
717 * is large enough too. Yes, I am puking too.
719 LPWSTR lpIconPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
721 TRACE("%p %s %p\n", hInst, debugstr_a(lpIconPath), lpiIcon);
723 if (lpIconPathW)
725 MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, lpIconPathW, len);
726 hIcon = ExtractAssociatedIconW(hInst, lpIconPathW, lpiIcon);
727 WideCharToMultiByte(CP_ACP, 0, lpIconPathW, -1, lpIconPath, MAX_PATH , NULL, NULL);
728 HeapFree(GetProcessHeap(), 0, lpIconPathW);
730 return hIcon;
733 /*************************************************************************
734 * ExtractAssociatedIconW (SHELL32.@)
736 * Return icon for given file (either from file itself or from associated
737 * executable) and patch parameters if needed.
739 HICON WINAPI ExtractAssociatedIconW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIcon)
741 HICON hIcon = NULL;
742 WORD wDummyIcon = 0;
744 TRACE("%p %s %p\n", hInst, debugstr_w(lpIconPath), lpiIcon);
746 if(lpiIcon == NULL)
747 lpiIcon = &wDummyIcon;
749 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
751 if( hIcon < (HICON)2 )
752 { if( hIcon == (HICON)1 ) /* no icons found in given file */
753 { WCHAR tempPath[MAX_PATH];
754 HINSTANCE uRet = FindExecutableW(lpIconPath,NULL,tempPath);
756 if( uRet > (HINSTANCE)32 && tempPath[0] )
757 { lstrcpyW(lpIconPath,tempPath);
758 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
759 if( hIcon > (HICON)2 )
760 return hIcon;
764 if( hIcon == (HICON)1 )
765 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
766 else
767 *lpiIcon = 6; /* generic icon - found nothing */
769 if (GetModuleFileNameW(hInst, lpIconPath, MAX_PATH))
770 hIcon = LoadIconW(hInst, MAKEINTRESOURCEW(*lpiIcon));
772 return hIcon;
775 /*************************************************************************
776 * ExtractAssociatedIconExW (SHELL32.@)
778 * Return icon for given file (either from file itself or from associated
779 * executable) and patch parameters if needed.
781 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
783 FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
784 return 0;
787 /*************************************************************************
788 * ExtractAssociatedIconExA (SHELL32.@)
790 * Return icon for given file (either from file itself or from associated
791 * executable) and patch parameters if needed.
793 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
795 HICON ret;
796 INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
797 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
799 TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
801 MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
802 ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
803 HeapFree(GetProcessHeap(), 0, lpwstrFile);
804 return ret;
808 /****************************************************************************
809 * SHDefExtractIconW [SHELL32.@]
811 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
812 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
814 UINT ret;
815 HICON hIcons[2];
816 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
818 ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
819 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
820 if (ret == 0xFFFFFFFF)
821 return E_FAIL;
822 if (ret > 0) {
823 if (phiconLarge)
824 *phiconLarge = hIcons[0];
825 else
826 DestroyIcon(hIcons[0]);
827 if (phiconSmall)
828 *phiconSmall = hIcons[1];
829 else
830 DestroyIcon(hIcons[1]);
831 return S_OK;
833 return S_FALSE;
836 /****************************************************************************
837 * SHDefExtractIconA [SHELL32.@]
839 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
840 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
842 HRESULT ret;
843 INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
844 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
846 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
848 MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
849 ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
850 HeapFree(GetProcessHeap(), 0, lpwstrFile);
851 return ret;
855 /****************************************************************************
856 * SHGetIconOverlayIndexA [SHELL32.@]
858 * Returns the index of the overlay icon in the system image list.
860 INT WINAPI SHGetIconOverlayIndexA(LPCSTR pszIconPath, INT iIconIndex)
862 FIXME("%s, %d\n", debugstr_a(pszIconPath), iIconIndex);
864 return -1;
867 /****************************************************************************
868 * SHGetIconOverlayIndexW [SHELL32.@]
870 * Returns the index of the overlay icon in the system image list.
872 INT WINAPI SHGetIconOverlayIndexW(LPCWSTR pszIconPath, INT iIconIndex)
874 FIXME("%s, %d\n", debugstr_w(pszIconPath), iIconIndex);
876 return -1;
879 /****************************************************************************
880 * SHGetStockIconInfo [SHELL32.@]
882 * Receive information for builtin icons
884 * PARAMS
885 * id [I] selected icon-id to get information for
886 * flags [I] selects the information to receive
887 * sii [IO] SHSTOCKICONINFO structure to fill
889 * RETURNS
890 * Success: S_OK
891 * Failure: A HRESULT failure code
894 HRESULT WINAPI SHGetStockIconInfo(SHSTOCKICONID id, UINT flags, SHSTOCKICONINFO *sii)
896 static const WCHAR shell32dll[] = {'\\','s','h','e','l','l','3','2','.','d','l','l',0};
898 FIXME("(%d, 0x%x, %p) semi-stub\n", id, flags, sii);
899 if ((id < 0) || (id >= SIID_MAX_ICONS) || !sii || (sii->cbSize != sizeof(SHSTOCKICONINFO))) {
900 return E_INVALIDARG;
903 GetSystemDirectoryW(sii->szPath, MAX_PATH);
905 /* no icons defined: use default */
906 sii->iIcon = -IDI_SHELL_DOCUMENT;
907 lstrcatW(sii->szPath, shell32dll);
909 if (flags)
910 FIXME("flags 0x%x not implemented\n", flags);
912 sii->hIcon = NULL;
913 sii->iSysImageIndex = -1;
915 TRACE("%3d: returning %s (%d)\n", id, debugstr_w(sii->szPath), sii->iIcon);
917 return S_OK;