crypt32: Use the available ARRAY_SIZE() macro.
[wine.git] / dlls / shell32 / iconcache.c
blobd55bde9bd7695d9af717c08c3ee79f00ed113777
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 shell_imagelists[SHIL_LAST+1];
66 static CRITICAL_SECTION SHELL32_SicCS;
67 static CRITICAL_SECTION_DEBUG critsect_debug =
69 0, 0, &SHELL32_SicCS,
70 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
71 0, 0, { (DWORD_PTR)(__FILE__ ": SHELL32_SicCS") }
73 static CRITICAL_SECTION SHELL32_SicCS = { &critsect_debug, -1, 0, 0, 0, 0 };
76 static const WCHAR WindowMetrics[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\','D','e','s','k','t','o','p','\\',
77 'W','i','n','d','o','w','M','e','t','r','i','c','s',0};
78 static const WCHAR ShellIconSize[] = {'S','h','e','l','l',' ','I','c','o','n',' ','S','i','z','e',0};
80 #define SIC_COMPARE_LISTINDEX 1
82 /*****************************************************************************
83 * SIC_CompareEntries
85 * NOTES
86 * Callback for DPA_Search
88 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
90 LPSIC_ENTRY e1 = p1, e2 = p2;
92 TRACE("%p %p %8lx\n", p1, p2, lparam);
94 /* Icons in the cache are keyed by the name of the file they are
95 * loaded from, their resource index and the fact if they have a shortcut
96 * icon overlay or not.
99 if (lparam & SIC_COMPARE_LISTINDEX)
100 return e1->dwListIndex != e2->dwListIndex;
102 if (e1->dwSourceIndex != e2->dwSourceIndex || /* first the faster one */
103 (e1->dwFlags & GIL_FORSHORTCUT) != (e2->dwFlags & GIL_FORSHORTCUT))
104 return 1;
106 if (strcmpiW(e1->sSourceFile,e2->sSourceFile))
107 return 1;
109 return 0;
112 /**************************************************************************************
113 * SIC_get_location
115 * Returns the source file and resource index of an icon with the given imagelist index
117 HRESULT SIC_get_location( int list_idx, WCHAR *file, DWORD *size, int *res_idx )
119 SIC_ENTRY seek, *found;
120 DWORD needed;
121 HRESULT hr = E_INVALIDARG;
122 int dpa_idx;
124 seek.dwListIndex = list_idx;
126 EnterCriticalSection( &SHELL32_SicCS );
128 dpa_idx = DPA_Search( sic_hdpa, &seek, 0, SIC_CompareEntries, SIC_COMPARE_LISTINDEX, 0 );
129 if (dpa_idx != -1)
131 found = DPA_GetPtr( sic_hdpa, dpa_idx );
132 needed = (strlenW( found->sSourceFile ) + 1) * sizeof(WCHAR);
133 if (needed <= *size)
135 memcpy( file, found->sSourceFile, needed );
136 *res_idx = found->dwSourceIndex;
137 hr = S_OK;
139 else
141 *size = needed;
142 hr = E_NOT_SUFFICIENT_BUFFER;
145 LeaveCriticalSection( &SHELL32_SicCS );
147 return hr;
150 /* declare SIC_LoadOverlayIcon() */
151 static int SIC_LoadOverlayIcon(int icon_idx);
153 /*****************************************************************************
154 * SIC_OverlayShortcutImage [internal]
156 * NOTES
157 * Creates a new icon as a copy of the passed-in icon, overlaid with a
158 * shortcut image.
160 static HICON SIC_OverlayShortcutImage(HICON SourceIcon, int type)
162 ICONINFO SourceIconInfo, ShortcutIconInfo, TargetIconInfo;
163 HICON ShortcutIcon, TargetIcon;
164 BITMAP SourceBitmapInfo, ShortcutBitmapInfo;
165 HDC SourceDC = NULL,
166 ShortcutDC = NULL,
167 TargetDC = NULL,
168 ScreenDC = NULL;
169 HBITMAP OldSourceBitmap = NULL,
170 OldShortcutBitmap = NULL,
171 OldTargetBitmap = NULL;
173 static int s_imgListIdx = -1;
175 /* Get information about the source icon and shortcut overlay */
176 if (! GetIconInfo(SourceIcon, &SourceIconInfo)
177 || 0 == GetObjectW(SourceIconInfo.hbmColor, sizeof(BITMAP), &SourceBitmapInfo))
179 return NULL;
182 /* search for the shortcut icon only once */
183 if (s_imgListIdx == -1)
184 s_imgListIdx = SIC_LoadOverlayIcon(- IDI_SHELL_SHORTCUT);
185 /* FIXME should use icon index 29 instead of the
186 resource id, but not all icons are present yet
187 so we can't use icon indices */
189 if (s_imgListIdx != -1)
190 ShortcutIcon = ImageList_GetIcon(shell_imagelists[type], s_imgListIdx, ILD_TRANSPARENT);
191 else
192 ShortcutIcon = NULL;
194 if (NULL == ShortcutIcon || ! GetIconInfo(ShortcutIcon, &ShortcutIconInfo)
195 || 0 == GetObjectW(ShortcutIconInfo.hbmColor, sizeof(BITMAP), &ShortcutBitmapInfo))
197 return NULL;
200 TargetIconInfo = SourceIconInfo;
201 TargetIconInfo.hbmMask = NULL;
202 TargetIconInfo.hbmColor = NULL;
204 /* Setup the source, shortcut and target masks */
205 SourceDC = CreateCompatibleDC(NULL);
206 if (NULL == SourceDC) goto fail;
207 OldSourceBitmap = SelectObject(SourceDC, SourceIconInfo.hbmMask);
208 if (NULL == OldSourceBitmap) goto fail;
210 ShortcutDC = CreateCompatibleDC(NULL);
211 if (NULL == ShortcutDC) goto fail;
212 OldShortcutBitmap = SelectObject(ShortcutDC, ShortcutIconInfo.hbmMask);
213 if (NULL == OldShortcutBitmap) goto fail;
215 TargetDC = CreateCompatibleDC(NULL);
216 if (NULL == TargetDC) goto fail;
217 TargetIconInfo.hbmMask = CreateCompatibleBitmap(TargetDC, SourceBitmapInfo.bmWidth,
218 SourceBitmapInfo.bmHeight);
219 if (NULL == TargetIconInfo.hbmMask) goto fail;
220 ScreenDC = GetDC(NULL);
221 if (NULL == ScreenDC) goto fail;
222 TargetIconInfo.hbmColor = CreateCompatibleBitmap(ScreenDC, SourceBitmapInfo.bmWidth,
223 SourceBitmapInfo.bmHeight);
224 ReleaseDC(NULL, ScreenDC);
225 if (NULL == TargetIconInfo.hbmColor) goto fail;
226 OldTargetBitmap = SelectObject(TargetDC, TargetIconInfo.hbmMask);
227 if (NULL == OldTargetBitmap) goto fail;
229 /* Create the target mask by ANDing the source and shortcut masks */
230 if (! BitBlt(TargetDC, 0, 0, SourceBitmapInfo.bmWidth, SourceBitmapInfo.bmHeight,
231 SourceDC, 0, 0, SRCCOPY) ||
232 ! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
233 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
234 ShortcutDC, 0, 0, SRCAND))
236 goto fail;
239 /* Setup the source and target xor bitmap */
240 if (NULL == SelectObject(SourceDC, SourceIconInfo.hbmColor) ||
241 NULL == SelectObject(TargetDC, TargetIconInfo.hbmColor))
243 goto fail;
246 /* Copy the source xor bitmap to the target and clear out part of it by using
247 the shortcut mask */
248 if (! BitBlt(TargetDC, 0, 0, SourceBitmapInfo.bmWidth, SourceBitmapInfo.bmHeight,
249 SourceDC, 0, 0, SRCCOPY) ||
250 ! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
251 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
252 ShortcutDC, 0, 0, SRCAND))
254 goto fail;
257 if (NULL == SelectObject(ShortcutDC, ShortcutIconInfo.hbmColor)) goto fail;
259 /* Now put in the shortcut xor mask */
260 if (! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
261 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
262 ShortcutDC, 0, 0, SRCINVERT))
264 goto fail;
267 /* Clean up, we're not goto'ing to 'fail' after this so we can be lazy and not set
268 handles to NULL */
269 SelectObject(TargetDC, OldTargetBitmap);
270 DeleteObject(TargetDC);
271 SelectObject(ShortcutDC, OldShortcutBitmap);
272 DeleteObject(ShortcutDC);
273 SelectObject(SourceDC, OldSourceBitmap);
274 DeleteObject(SourceDC);
276 /* Create the icon using the bitmaps prepared earlier */
277 TargetIcon = CreateIconIndirect(&TargetIconInfo);
279 /* CreateIconIndirect copies the bitmaps, so we can release our bitmaps now */
280 DeleteObject(TargetIconInfo.hbmColor);
281 DeleteObject(TargetIconInfo.hbmMask);
283 return TargetIcon;
285 fail:
286 /* Clean up scratch resources we created */
287 if (NULL != OldTargetBitmap) SelectObject(TargetDC, OldTargetBitmap);
288 if (NULL != TargetIconInfo.hbmColor) DeleteObject(TargetIconInfo.hbmColor);
289 if (NULL != TargetIconInfo.hbmMask) DeleteObject(TargetIconInfo.hbmMask);
290 if (NULL != TargetDC) DeleteObject(TargetDC);
291 if (NULL != OldShortcutBitmap) SelectObject(ShortcutDC, OldShortcutBitmap);
292 if (NULL != ShortcutDC) DeleteObject(ShortcutDC);
293 if (NULL != OldSourceBitmap) SelectObject(SourceDC, OldSourceBitmap);
294 if (NULL != SourceDC) DeleteObject(SourceDC);
296 return NULL;
299 /*****************************************************************************
300 * SIC_IconAppend [internal]
302 static INT SIC_IconAppend (const WCHAR *sourcefile, INT src_index, HICON *hicons, DWORD flags)
304 INT ret, index, index1;
305 WCHAR path[MAX_PATH];
306 SIC_ENTRY *entry;
307 unsigned int i;
309 TRACE("%s %i %p %#x\n", debugstr_w(sourcefile), src_index, hicons, flags);
311 entry = SHAlloc(sizeof(*entry));
313 GetFullPathNameW(sourcefile, MAX_PATH, path, NULL);
314 entry->sSourceFile = heap_alloc( (strlenW(path)+1)*sizeof(WCHAR) );
315 strcpyW( entry->sSourceFile, path );
317 entry->dwSourceIndex = src_index;
318 entry->dwFlags = flags;
320 EnterCriticalSection(&SHELL32_SicCS);
322 index = DPA_InsertPtr(sic_hdpa, 0x7fff, entry);
323 if ( INVALID_INDEX == index )
325 heap_free(entry->sSourceFile);
326 SHFree(entry);
327 ret = INVALID_INDEX;
329 else
331 index = -1;
332 for (i = 0; i < ARRAY_SIZE(shell_imagelists); i++)
334 index1 = ImageList_AddIcon(shell_imagelists[i], hicons[i]);
335 if (index != -1 && index1 != index)
336 WARN("Imagelists out of sync, list %d.\n", i);
337 index = index1;
340 entry->dwListIndex = index;
341 ret = entry->dwListIndex;
344 LeaveCriticalSection(&SHELL32_SicCS);
345 return ret;
348 static BOOL get_imagelist_icon_size(int list, SIZE *size)
350 if (list < 0 || list >= ARRAY_SIZE(shell_imagelists)) return FALSE;
352 return ImageList_GetIconSize( shell_imagelists[list], &size->cx, &size->cy );
355 /****************************************************************************
356 * SIC_LoadIcon [internal]
358 * NOTES
359 * gets icons by index from the file
361 static INT SIC_LoadIcon (const WCHAR *sourcefile, INT index, DWORD flags)
363 HICON hicons[ARRAY_SIZE(shell_imagelists)] = { 0 };
364 HICON hshortcuts[ARRAY_SIZE(hicons)] = { 0 };
365 unsigned int i;
366 SIZE size;
367 int ret;
369 for (i = 0; i < ARRAY_SIZE(hicons); i++)
371 get_imagelist_icon_size( i, &size );
372 if (!PrivateExtractIconsW( sourcefile, index, size.cx, size.cy, &hicons[i], 0, 1, 0 ))
373 WARN("Failed to load icon %d from %s.\n", index, debugstr_w(sourcefile));
376 if (flags & GIL_FORSHORTCUT)
378 BOOL failed = FALSE;
380 for (i = 0; i < ARRAY_SIZE(hshortcuts); i++)
382 if (!(hshortcuts[i] = SIC_OverlayShortcutImage(hicons[i], i)))
384 WARN("Failed to create shortcut overlaid icons.\n");
385 failed = TRUE;
389 if (failed)
391 for (i = 0; i < ARRAY_SIZE(hshortcuts); i++)
392 DestroyIcon(hshortcuts[i]);
393 flags &= ~GIL_FORSHORTCUT;
395 else
397 for (i = 0; i < ARRAY_SIZE(hicons); i++)
399 DestroyIcon(hicons[i]);
400 hicons[i] = hshortcuts[i];
405 ret = SIC_IconAppend( sourcefile, index, hicons, flags );
406 for (i = 0; i < ARRAY_SIZE(hicons); i++)
407 DestroyIcon(hicons[i]);
408 return ret;
411 static int get_shell_icon_size(void)
413 WCHAR buf[32];
414 DWORD value = 32, size = sizeof(buf), type;
415 HKEY key;
417 if (!RegOpenKeyW( HKEY_CURRENT_USER, WindowMetrics, &key ))
419 if (!RegQueryValueExW( key, ShellIconSize, NULL, &type, (BYTE *)buf, &size ) && type == REG_SZ)
421 if (size == sizeof(buf)) buf[size / sizeof(WCHAR) - 1] = 0;
422 value = atoiW( buf );
424 RegCloseKey( key );
426 return value;
429 /*****************************************************************************
430 * SIC_Initialize [internal]
432 static BOOL WINAPI SIC_Initialize( INIT_ONCE *once, void *param, void **context )
434 HICON hicons[ARRAY_SIZE(shell_imagelists)];
435 SIZE sizes[ARRAY_SIZE(shell_imagelists)];
436 BOOL failed = FALSE;
437 unsigned int i;
439 if (!IsProcessDPIAware())
441 sizes[SHIL_LARGE].cx = sizes[SHIL_LARGE].cy = get_shell_icon_size();
442 sizes[SHIL_SMALL].cx = GetSystemMetrics( SM_CXSMICON );
443 sizes[SHIL_SMALL].cy = GetSystemMetrics( SM_CYSMICON );
445 else
447 sizes[SHIL_LARGE].cx = GetSystemMetrics( SM_CXICON );
448 sizes[SHIL_LARGE].cy = GetSystemMetrics( SM_CYICON );
449 sizes[SHIL_SMALL].cx = sizes[SHIL_LARGE].cx / 2;
450 sizes[SHIL_SMALL].cy = sizes[SHIL_LARGE].cy / 2;
453 sizes[SHIL_EXTRALARGE].cx = (GetSystemMetrics( SM_CXICON ) * 3) / 2;
454 sizes[SHIL_EXTRALARGE].cy = (GetSystemMetrics( SM_CYICON ) * 3) / 2;
455 sizes[SHIL_SYSSMALL].cx = GetSystemMetrics( SM_CXSMICON );
456 sizes[SHIL_SYSSMALL].cy = GetSystemMetrics( SM_CYSMICON );
457 sizes[SHIL_JUMBO].cx = sizes[SHIL_JUMBO].cy = 256;
459 TRACE("large %dx%d small %dx%d\n", sizes[SHIL_LARGE].cx, sizes[SHIL_LARGE].cy, sizes[SHIL_SMALL].cx, sizes[SHIL_SMALL].cy);
461 sic_hdpa = DPA_Create(16);
462 if (!sic_hdpa)
463 return(FALSE);
465 for (i = 0; i < ARRAY_SIZE(shell_imagelists); i++)
467 shell_imagelists[i] = ImageList_Create(sizes[i].cx, sizes[i].cy, ILC_COLOR32 | ILC_MASK, 0, 0x20);
468 ImageList_SetBkColor(shell_imagelists[i], CLR_NONE);
470 /* Load the document icon, which is used as the default if an icon isn't found. */
471 if (!(hicons[i] = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_DOCUMENT),
472 IMAGE_ICON, sizes[i].cx, sizes[i].cy, LR_SHARED)))
474 failed = TRUE;
478 if (failed)
480 FIXME("Failed to load IDI_SHELL_DOCUMENT icon!\n");
481 return FALSE;
484 SIC_IconAppend(swShell32Name, IDI_SHELL_DOCUMENT - 1, hicons, 0);
485 SIC_IconAppend(swShell32Name, -IDI_SHELL_DOCUMENT, hicons, 0);
487 TRACE("small list=%p, large list=%p\n", shell_imagelists[SHIL_SMALL], shell_imagelists[SHIL_LARGE]);
489 return TRUE;
492 /*************************************************************************
493 * SIC_Destroy
495 * frees the cache
497 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
499 heap_free(((LPSIC_ENTRY)ptr)->sSourceFile);
500 SHFree(ptr);
501 return TRUE;
504 void SIC_Destroy(void)
506 unsigned int i;
508 TRACE("\n");
510 EnterCriticalSection(&SHELL32_SicCS);
512 if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
514 for (i = 0; i < ARRAY_SIZE(shell_imagelists); i++)
516 if (shell_imagelists[i])
517 ImageList_Destroy(shell_imagelists[i]);
520 LeaveCriticalSection(&SHELL32_SicCS);
521 DeleteCriticalSection(&SHELL32_SicCS);
524 /*****************************************************************************
525 * SIC_GetIconIndex [internal]
527 * Parameters
528 * sSourceFile [IN] filename of file containing the icon
529 * index [IN] index/resID (negated) in this file
531 * NOTES
532 * look in the cache for a proper icon. if not available the icon is taken
533 * from the file and cached
535 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags )
537 SIC_ENTRY sice;
538 INT ret, index = INVALID_INDEX;
539 WCHAR path[MAX_PATH];
541 TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
543 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
544 sice.sSourceFile = path;
545 sice.dwSourceIndex = dwSourceIndex;
546 sice.dwFlags = dwFlags;
548 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
550 EnterCriticalSection(&SHELL32_SicCS);
552 if (NULL != DPA_GetPtr (sic_hdpa, 0))
554 /* search linear from position 0*/
555 index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
558 if ( INVALID_INDEX == index )
560 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex, dwFlags);
562 else
564 TRACE("-- found\n");
565 ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
568 LeaveCriticalSection(&SHELL32_SicCS);
569 return ret;
572 /*****************************************************************************
573 * SIC_LoadOverlayIcon [internal]
575 * Load a shell overlay icon and return its icon cache index.
577 static int SIC_LoadOverlayIcon(int icon_idx)
579 WCHAR buffer[1024], wszIdx[8];
580 HKEY hKeyShellIcons;
581 LPCWSTR iconPath;
582 int iconIdx;
584 static const WCHAR wszShellIcons[] = {
585 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
586 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
587 'E','x','p','l','o','r','e','r','\\','S','h','e','l','l',' ','I','c','o','n','s',0
589 static const WCHAR wszNumFmt[] = {'%','d',0};
591 iconPath = swShell32Name; /* default: load icon from shell32.dll */
592 iconIdx = icon_idx;
594 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszShellIcons, 0, KEY_READ, &hKeyShellIcons) == ERROR_SUCCESS)
596 DWORD count = sizeof(buffer);
598 sprintfW(wszIdx, wszNumFmt, icon_idx);
600 /* read icon path and index */
601 if (RegQueryValueExW(hKeyShellIcons, wszIdx, NULL, NULL, (LPBYTE)buffer, &count) == ERROR_SUCCESS)
603 LPWSTR p = strchrW(buffer, ',');
605 if (!p)
607 ERR("Icon index in %s/%s corrupted, no comma.\n", debugstr_w(wszShellIcons),debugstr_w(wszIdx));
608 RegCloseKey(hKeyShellIcons);
609 return -1;
611 *p++ = 0;
612 iconPath = buffer;
613 iconIdx = atoiW(p);
616 RegCloseKey(hKeyShellIcons);
619 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
621 return SIC_LoadIcon(iconPath, iconIdx, 0);
624 /*************************************************************************
625 * Shell_GetImageLists [SHELL32.71]
627 * PARAMETERS
628 * imglist[1|2] [OUT] pointer which receives imagelist handles
631 BOOL WINAPI Shell_GetImageLists(HIMAGELIST *large_list, HIMAGELIST *small_list)
633 TRACE("(%p, %p)\n", large_list, small_list);
635 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
636 if (large_list) *large_list = shell_imagelists[SHIL_LARGE];
637 if (small_list) *small_list = shell_imagelists[SHIL_SMALL];
638 return TRUE;
641 /*************************************************************************
642 * PidlToSicIndex [INTERNAL]
644 * PARAMETERS
645 * sh [IN] IShellFolder
646 * pidl [IN]
647 * bBigIcon [IN]
648 * uFlags [IN] GIL_*
649 * pIndex [OUT] index within the SIC
652 BOOL PidlToSicIndex (
653 IShellFolder * sh,
654 LPCITEMIDLIST pidl,
655 BOOL bBigIcon,
656 UINT uFlags,
657 int * pIndex)
659 IExtractIconW *ei;
660 WCHAR szIconFile[MAX_PATH]; /* file containing the icon */
661 INT iSourceIndex; /* index or resID(negated) in this file */
662 BOOL ret = FALSE;
663 UINT dwFlags = 0;
664 int iShortcutDefaultIndex = INVALID_INDEX;
666 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
668 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
670 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconW, 0, (void **)&ei)))
672 if (SUCCEEDED(IExtractIconW_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
674 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex, uFlags);
675 ret = TRUE;
677 IExtractIconW_Release(ei);
680 if (INVALID_INDEX == *pIndex) /* default icon when failed */
682 if (0 == (uFlags & GIL_FORSHORTCUT))
684 *pIndex = 0;
686 else
688 if (INVALID_INDEX == iShortcutDefaultIndex)
690 iShortcutDefaultIndex = SIC_LoadIcon(swShell32Name, 0, GIL_FORSHORTCUT);
692 *pIndex = (INVALID_INDEX != iShortcutDefaultIndex ? iShortcutDefaultIndex : 0);
696 return ret;
700 /*************************************************************************
701 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
703 * PARAMETERS
704 * sh [IN] pointer to an instance of IShellFolder
705 * pidl [IN]
706 * pIndex [OUT][OPTIONAL] SIC index for big icon
709 int WINAPI SHMapPIDLToSystemImageListIndex(
710 IShellFolder *sh,
711 LPCITEMIDLIST pidl,
712 int *pIndex)
714 int Index;
715 UINT uGilFlags = 0;
717 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
718 pdump(pidl);
720 if (SHELL_IsShortcut(pidl))
721 uGilFlags |= GIL_FORSHORTCUT;
723 if (pIndex)
724 if (!PidlToSicIndex ( sh, pidl, 1, uGilFlags, pIndex))
725 *pIndex = -1;
727 if (!PidlToSicIndex ( sh, pidl, 0, uGilFlags, &Index))
728 return -1;
730 return Index;
733 /*************************************************************************
734 * SHMapIDListToImageListIndexAsync [SHELL32.148]
736 HRESULT WINAPI SHMapIDListToImageListIndexAsync(IUnknown *pts, IShellFolder *psf,
737 LPCITEMIDLIST pidl, UINT flags,
738 void *pfn, void *pvData, void *pvHint,
739 int *piIndex, int *piIndexSel)
741 FIXME("(%p, %p, %p, 0x%08x, %p, %p, %p, %p, %p)\n",
742 pts, psf, pidl, flags, pfn, pvData, pvHint, piIndex, piIndexSel);
743 return E_FAIL;
746 /*************************************************************************
747 * Shell_GetCachedImageIndex [SHELL32.72]
750 static INT Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
752 INT ret, len;
753 LPWSTR szTemp;
755 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
757 len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
758 szTemp = heap_alloc( len * sizeof(WCHAR) );
759 MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
761 ret = SIC_GetIconIndex( szTemp, nIndex, 0 );
763 heap_free( szTemp );
765 return ret;
768 static INT Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
770 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
772 return SIC_GetIconIndex(szPath, nIndex, 0);
775 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
776 { if( SHELL_OsIsUnicode())
777 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
778 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
781 /*************************************************************************
782 * ExtractIconExW [SHELL32.@]
783 * RETURNS
784 * 0 no icon found
785 * -1 file is not valid
786 * or number of icons extracted
788 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
790 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
792 return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
795 /*************************************************************************
796 * ExtractIconExA [SHELL32.@]
798 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
800 UINT ret = 0;
801 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
802 LPWSTR lpwstrFile = heap_alloc( len * sizeof(WCHAR));
804 TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
806 if (lpwstrFile)
808 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
809 ret = ExtractIconExW(lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
810 heap_free(lpwstrFile);
812 return ret;
815 /*************************************************************************
816 * ExtractAssociatedIconA (SHELL32.@)
818 * Return icon for given file (either from file itself or from associated
819 * executable) and patch parameters if needed.
821 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
823 HICON hIcon = NULL;
824 INT len = MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, NULL, 0);
825 /* Note that we need to allocate MAX_PATH, since we are supposed to fill
826 * the correct executable if there is no icon in lpIconPath directly.
827 * lpIconPath itself is supposed to be large enough, so make sure lpIconPathW
828 * is large enough too. Yes, I am puking too.
830 LPWSTR lpIconPathW = heap_alloc(MAX_PATH * sizeof(WCHAR));
832 TRACE("%p %s %p\n", hInst, debugstr_a(lpIconPath), lpiIcon);
834 if (lpIconPathW)
836 MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, lpIconPathW, len);
837 hIcon = ExtractAssociatedIconW(hInst, lpIconPathW, lpiIcon);
838 WideCharToMultiByte(CP_ACP, 0, lpIconPathW, -1, lpIconPath, MAX_PATH , NULL, NULL);
839 heap_free(lpIconPathW);
841 return hIcon;
844 /*************************************************************************
845 * ExtractAssociatedIconW (SHELL32.@)
847 * Return icon for given file (either from file itself or from associated
848 * executable) and patch parameters if needed.
850 HICON WINAPI ExtractAssociatedIconW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIcon)
852 HICON hIcon = NULL;
853 WORD wDummyIcon = 0;
855 TRACE("%p %s %p\n", hInst, debugstr_w(lpIconPath), lpiIcon);
857 if(lpiIcon == NULL)
858 lpiIcon = &wDummyIcon;
860 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
862 if( hIcon < (HICON)2 )
863 { if( hIcon == (HICON)1 ) /* no icons found in given file */
864 { WCHAR tempPath[MAX_PATH];
865 HINSTANCE uRet = FindExecutableW(lpIconPath,NULL,tempPath);
867 if( uRet > (HINSTANCE)32 && tempPath[0] )
868 { lstrcpyW(lpIconPath,tempPath);
869 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
870 if( hIcon > (HICON)2 )
871 return hIcon;
875 if( hIcon == (HICON)1 )
876 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
877 else
878 *lpiIcon = 6; /* generic icon - found nothing */
880 if (GetModuleFileNameW(hInst, lpIconPath, MAX_PATH))
881 hIcon = LoadIconW(hInst, MAKEINTRESOURCEW(*lpiIcon));
883 return hIcon;
886 /*************************************************************************
887 * ExtractAssociatedIconExW (SHELL32.@)
889 * Return icon for given file (either from file itself or from associated
890 * executable) and patch parameters if needed.
892 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
894 FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
895 return 0;
898 /*************************************************************************
899 * ExtractAssociatedIconExA (SHELL32.@)
901 * Return icon for given file (either from file itself or from associated
902 * executable) and patch parameters if needed.
904 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
906 HICON ret;
907 INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
908 LPWSTR lpwstrFile = heap_alloc( len * sizeof(WCHAR) );
910 TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
912 MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
913 ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
914 heap_free(lpwstrFile);
915 return ret;
919 /****************************************************************************
920 * SHDefExtractIconW [SHELL32.@]
922 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
923 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
925 UINT ret;
926 HICON hIcons[2];
927 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
929 ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
930 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
931 if (ret == 0xFFFFFFFF)
932 return E_FAIL;
933 if (ret > 0) {
934 if (phiconLarge)
935 *phiconLarge = hIcons[0];
936 else
937 DestroyIcon(hIcons[0]);
938 if (phiconSmall)
939 *phiconSmall = hIcons[1];
940 else
941 DestroyIcon(hIcons[1]);
942 return S_OK;
944 return S_FALSE;
947 /****************************************************************************
948 * SHDefExtractIconA [SHELL32.@]
950 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
951 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
953 HRESULT ret;
954 INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
955 LPWSTR lpwstrFile = heap_alloc(len * sizeof(WCHAR));
957 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
959 MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
960 ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
961 heap_free(lpwstrFile);
962 return ret;
966 /****************************************************************************
967 * SHGetIconOverlayIndexA [SHELL32.@]
969 * Returns the index of the overlay icon in the system image list.
971 INT WINAPI SHGetIconOverlayIndexA(LPCSTR pszIconPath, INT iIconIndex)
973 FIXME("%s, %d\n", debugstr_a(pszIconPath), iIconIndex);
975 return -1;
978 /****************************************************************************
979 * SHGetIconOverlayIndexW [SHELL32.@]
981 * Returns the index of the overlay icon in the system image list.
983 INT WINAPI SHGetIconOverlayIndexW(LPCWSTR pszIconPath, INT iIconIndex)
985 FIXME("%s, %d\n", debugstr_w(pszIconPath), iIconIndex);
987 return -1;
990 /****************************************************************************
991 * SHGetStockIconInfo [SHELL32.@]
993 * Receive information for builtin icons
995 * PARAMS
996 * id [I] selected icon-id to get information for
997 * flags [I] selects the information to receive
998 * sii [IO] SHSTOCKICONINFO structure to fill
1000 * RETURNS
1001 * Success: S_OK
1002 * Failure: A HRESULT failure code
1005 HRESULT WINAPI SHGetStockIconInfo(SHSTOCKICONID id, UINT flags, SHSTOCKICONINFO *sii)
1007 static const WCHAR shell32dll[] = {'\\','s','h','e','l','l','3','2','.','d','l','l',0};
1009 FIXME("(%d, 0x%x, %p) semi-stub\n", id, flags, sii);
1010 if ((id < 0) || (id >= SIID_MAX_ICONS) || !sii || (sii->cbSize != sizeof(SHSTOCKICONINFO))) {
1011 return E_INVALIDARG;
1014 GetSystemDirectoryW(sii->szPath, MAX_PATH);
1016 /* no icons defined: use default */
1017 sii->iIcon = -IDI_SHELL_DOCUMENT;
1018 lstrcatW(sii->szPath, shell32dll);
1020 if (flags)
1021 FIXME("flags 0x%x not implemented\n", flags);
1023 sii->hIcon = NULL;
1024 sii->iSysImageIndex = -1;
1026 TRACE("%3d: returning %s (%d)\n", id, debugstr_w(sii->szPath), sii->iIcon);
1028 return S_OK;
1031 /*************************************************************************
1032 * SHGetImageList (SHELL32.727)
1034 * Returns a copy of a shell image list.
1036 * NOTES
1037 * Windows XP features 4 sizes of image list, and Vista 5. Wine currently
1038 * only supports the traditional small and large image lists, so requests
1039 * for the others will currently fail.
1041 HRESULT WINAPI SHGetImageList(int iImageList, REFIID riid, void **ppv)
1043 TRACE("(%d, %s, %p)\n", iImageList, debugstr_guid(riid), ppv);
1045 if (iImageList < 0 || iImageList > SHIL_LAST)
1046 return E_FAIL;
1048 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
1049 return HIMAGELIST_QueryInterface(shell_imagelists[iImageList], riid, ppv);