shell32: Add support for SHIL_SYSSMALL list.
[wine.git] / dlls / shell32 / iconcache.c
blobf26ff0e49d7956cbabe5fc4fba40138fe2ccea15
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_SYSSMALL+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 );
458 TRACE("large %dx%d small %dx%d\n", sizes[SHIL_LARGE].cx, sizes[SHIL_LARGE].cy, sizes[SHIL_SMALL].cx, sizes[SHIL_SMALL].cy);
460 sic_hdpa = DPA_Create(16);
461 if (!sic_hdpa)
462 return(FALSE);
464 for (i = 0; i < ARRAY_SIZE(shell_imagelists); i++)
466 shell_imagelists[i] = ImageList_Create(sizes[i].cx, sizes[i].cy, ILC_COLOR32 | ILC_MASK, 0, 0x20);
467 ImageList_SetBkColor(shell_imagelists[i], CLR_NONE);
469 /* Load the document icon, which is used as the default if an icon isn't found. */
470 if (!(hicons[i] = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_DOCUMENT),
471 IMAGE_ICON, sizes[i].cx, sizes[i].cy, LR_SHARED)))
473 failed = TRUE;
477 if (failed)
479 FIXME("Failed to load IDI_SHELL_DOCUMENT icon!\n");
480 return FALSE;
483 SIC_IconAppend(swShell32Name, IDI_SHELL_DOCUMENT - 1, hicons, 0);
484 SIC_IconAppend(swShell32Name, -IDI_SHELL_DOCUMENT, hicons, 0);
486 TRACE("small list=%p, large list=%p\n", shell_imagelists[SHIL_SMALL], shell_imagelists[SHIL_LARGE]);
488 return TRUE;
491 /*************************************************************************
492 * SIC_Destroy
494 * frees the cache
496 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
498 heap_free(((LPSIC_ENTRY)ptr)->sSourceFile);
499 SHFree(ptr);
500 return TRUE;
503 void SIC_Destroy(void)
505 unsigned int i;
507 TRACE("\n");
509 EnterCriticalSection(&SHELL32_SicCS);
511 if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
513 for (i = 0; i < ARRAY_SIZE(shell_imagelists); i++)
515 if (shell_imagelists[i])
516 ImageList_Destroy(shell_imagelists[i]);
519 LeaveCriticalSection(&SHELL32_SicCS);
520 DeleteCriticalSection(&SHELL32_SicCS);
523 /*****************************************************************************
524 * SIC_GetIconIndex [internal]
526 * Parameters
527 * sSourceFile [IN] filename of file containing the icon
528 * index [IN] index/resID (negated) in this file
530 * NOTES
531 * look in the cache for a proper icon. if not available the icon is taken
532 * from the file and cached
534 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags )
536 SIC_ENTRY sice;
537 INT ret, index = INVALID_INDEX;
538 WCHAR path[MAX_PATH];
540 TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
542 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
543 sice.sSourceFile = path;
544 sice.dwSourceIndex = dwSourceIndex;
545 sice.dwFlags = dwFlags;
547 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
549 EnterCriticalSection(&SHELL32_SicCS);
551 if (NULL != DPA_GetPtr (sic_hdpa, 0))
553 /* search linear from position 0*/
554 index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
557 if ( INVALID_INDEX == index )
559 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex, dwFlags);
561 else
563 TRACE("-- found\n");
564 ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
567 LeaveCriticalSection(&SHELL32_SicCS);
568 return ret;
571 /*****************************************************************************
572 * SIC_LoadOverlayIcon [internal]
574 * Load a shell overlay icon and return its icon cache index.
576 static int SIC_LoadOverlayIcon(int icon_idx)
578 WCHAR buffer[1024], wszIdx[8];
579 HKEY hKeyShellIcons;
580 LPCWSTR iconPath;
581 int iconIdx;
583 static const WCHAR wszShellIcons[] = {
584 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
585 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
586 'E','x','p','l','o','r','e','r','\\','S','h','e','l','l',' ','I','c','o','n','s',0
588 static const WCHAR wszNumFmt[] = {'%','d',0};
590 iconPath = swShell32Name; /* default: load icon from shell32.dll */
591 iconIdx = icon_idx;
593 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszShellIcons, 0, KEY_READ, &hKeyShellIcons) == ERROR_SUCCESS)
595 DWORD count = sizeof(buffer);
597 sprintfW(wszIdx, wszNumFmt, icon_idx);
599 /* read icon path and index */
600 if (RegQueryValueExW(hKeyShellIcons, wszIdx, NULL, NULL, (LPBYTE)buffer, &count) == ERROR_SUCCESS)
602 LPWSTR p = strchrW(buffer, ',');
604 if (!p)
606 ERR("Icon index in %s/%s corrupted, no comma.\n", debugstr_w(wszShellIcons),debugstr_w(wszIdx));
607 RegCloseKey(hKeyShellIcons);
608 return -1;
610 *p++ = 0;
611 iconPath = buffer;
612 iconIdx = atoiW(p);
615 RegCloseKey(hKeyShellIcons);
618 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
620 return SIC_LoadIcon(iconPath, iconIdx, 0);
623 /*************************************************************************
624 * Shell_GetImageLists [SHELL32.71]
626 * PARAMETERS
627 * imglist[1|2] [OUT] pointer which receives imagelist handles
630 BOOL WINAPI Shell_GetImageLists(HIMAGELIST *large_list, HIMAGELIST *small_list)
632 TRACE("(%p, %p)\n", large_list, small_list);
634 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
635 if (large_list) *large_list = shell_imagelists[SHIL_LARGE];
636 if (small_list) *small_list = shell_imagelists[SHIL_SMALL];
637 return TRUE;
640 /*************************************************************************
641 * PidlToSicIndex [INTERNAL]
643 * PARAMETERS
644 * sh [IN] IShellFolder
645 * pidl [IN]
646 * bBigIcon [IN]
647 * uFlags [IN] GIL_*
648 * pIndex [OUT] index within the SIC
651 BOOL PidlToSicIndex (
652 IShellFolder * sh,
653 LPCITEMIDLIST pidl,
654 BOOL bBigIcon,
655 UINT uFlags,
656 int * pIndex)
658 IExtractIconW *ei;
659 WCHAR szIconFile[MAX_PATH]; /* file containing the icon */
660 INT iSourceIndex; /* index or resID(negated) in this file */
661 BOOL ret = FALSE;
662 UINT dwFlags = 0;
663 int iShortcutDefaultIndex = INVALID_INDEX;
665 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
667 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
669 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconW, 0, (void **)&ei)))
671 if (SUCCEEDED(IExtractIconW_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
673 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex, uFlags);
674 ret = TRUE;
676 IExtractIconW_Release(ei);
679 if (INVALID_INDEX == *pIndex) /* default icon when failed */
681 if (0 == (uFlags & GIL_FORSHORTCUT))
683 *pIndex = 0;
685 else
687 if (INVALID_INDEX == iShortcutDefaultIndex)
689 iShortcutDefaultIndex = SIC_LoadIcon(swShell32Name, 0, GIL_FORSHORTCUT);
691 *pIndex = (INVALID_INDEX != iShortcutDefaultIndex ? iShortcutDefaultIndex : 0);
695 return ret;
699 /*************************************************************************
700 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
702 * PARAMETERS
703 * sh [IN] pointer to an instance of IShellFolder
704 * pidl [IN]
705 * pIndex [OUT][OPTIONAL] SIC index for big icon
708 int WINAPI SHMapPIDLToSystemImageListIndex(
709 IShellFolder *sh,
710 LPCITEMIDLIST pidl,
711 int *pIndex)
713 int Index;
714 UINT uGilFlags = 0;
716 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
717 pdump(pidl);
719 if (SHELL_IsShortcut(pidl))
720 uGilFlags |= GIL_FORSHORTCUT;
722 if (pIndex)
723 if (!PidlToSicIndex ( sh, pidl, 1, uGilFlags, pIndex))
724 *pIndex = -1;
726 if (!PidlToSicIndex ( sh, pidl, 0, uGilFlags, &Index))
727 return -1;
729 return Index;
732 /*************************************************************************
733 * SHMapIDListToImageListIndexAsync [SHELL32.148]
735 HRESULT WINAPI SHMapIDListToImageListIndexAsync(IUnknown *pts, IShellFolder *psf,
736 LPCITEMIDLIST pidl, UINT flags,
737 void *pfn, void *pvData, void *pvHint,
738 int *piIndex, int *piIndexSel)
740 FIXME("(%p, %p, %p, 0x%08x, %p, %p, %p, %p, %p)\n",
741 pts, psf, pidl, flags, pfn, pvData, pvHint, piIndex, piIndexSel);
742 return E_FAIL;
745 /*************************************************************************
746 * Shell_GetCachedImageIndex [SHELL32.72]
749 static INT Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
751 INT ret, len;
752 LPWSTR szTemp;
754 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
756 len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
757 szTemp = heap_alloc( len * sizeof(WCHAR) );
758 MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
760 ret = SIC_GetIconIndex( szTemp, nIndex, 0 );
762 heap_free( szTemp );
764 return ret;
767 static INT Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
769 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
771 return SIC_GetIconIndex(szPath, nIndex, 0);
774 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
775 { if( SHELL_OsIsUnicode())
776 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
777 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
780 /*************************************************************************
781 * ExtractIconExW [SHELL32.@]
782 * RETURNS
783 * 0 no icon found
784 * -1 file is not valid
785 * or number of icons extracted
787 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
789 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
791 return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
794 /*************************************************************************
795 * ExtractIconExA [SHELL32.@]
797 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
799 UINT ret = 0;
800 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
801 LPWSTR lpwstrFile = heap_alloc( len * sizeof(WCHAR));
803 TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
805 if (lpwstrFile)
807 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
808 ret = ExtractIconExW(lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
809 heap_free(lpwstrFile);
811 return ret;
814 /*************************************************************************
815 * ExtractAssociatedIconA (SHELL32.@)
817 * Return icon for given file (either from file itself or from associated
818 * executable) and patch parameters if needed.
820 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
822 HICON hIcon = NULL;
823 INT len = MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, NULL, 0);
824 /* Note that we need to allocate MAX_PATH, since we are supposed to fill
825 * the correct executable if there is no icon in lpIconPath directly.
826 * lpIconPath itself is supposed to be large enough, so make sure lpIconPathW
827 * is large enough too. Yes, I am puking too.
829 LPWSTR lpIconPathW = heap_alloc(MAX_PATH * sizeof(WCHAR));
831 TRACE("%p %s %p\n", hInst, debugstr_a(lpIconPath), lpiIcon);
833 if (lpIconPathW)
835 MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, lpIconPathW, len);
836 hIcon = ExtractAssociatedIconW(hInst, lpIconPathW, lpiIcon);
837 WideCharToMultiByte(CP_ACP, 0, lpIconPathW, -1, lpIconPath, MAX_PATH , NULL, NULL);
838 heap_free(lpIconPathW);
840 return hIcon;
843 /*************************************************************************
844 * ExtractAssociatedIconW (SHELL32.@)
846 * Return icon for given file (either from file itself or from associated
847 * executable) and patch parameters if needed.
849 HICON WINAPI ExtractAssociatedIconW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIcon)
851 HICON hIcon = NULL;
852 WORD wDummyIcon = 0;
854 TRACE("%p %s %p\n", hInst, debugstr_w(lpIconPath), lpiIcon);
856 if(lpiIcon == NULL)
857 lpiIcon = &wDummyIcon;
859 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
861 if( hIcon < (HICON)2 )
862 { if( hIcon == (HICON)1 ) /* no icons found in given file */
863 { WCHAR tempPath[MAX_PATH];
864 HINSTANCE uRet = FindExecutableW(lpIconPath,NULL,tempPath);
866 if( uRet > (HINSTANCE)32 && tempPath[0] )
867 { lstrcpyW(lpIconPath,tempPath);
868 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
869 if( hIcon > (HICON)2 )
870 return hIcon;
874 if( hIcon == (HICON)1 )
875 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
876 else
877 *lpiIcon = 6; /* generic icon - found nothing */
879 if (GetModuleFileNameW(hInst, lpIconPath, MAX_PATH))
880 hIcon = LoadIconW(hInst, MAKEINTRESOURCEW(*lpiIcon));
882 return hIcon;
885 /*************************************************************************
886 * ExtractAssociatedIconExW (SHELL32.@)
888 * Return icon for given file (either from file itself or from associated
889 * executable) and patch parameters if needed.
891 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
893 FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
894 return 0;
897 /*************************************************************************
898 * ExtractAssociatedIconExA (SHELL32.@)
900 * Return icon for given file (either from file itself or from associated
901 * executable) and patch parameters if needed.
903 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
905 HICON ret;
906 INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
907 LPWSTR lpwstrFile = heap_alloc( len * sizeof(WCHAR) );
909 TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
911 MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
912 ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
913 heap_free(lpwstrFile);
914 return ret;
918 /****************************************************************************
919 * SHDefExtractIconW [SHELL32.@]
921 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
922 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
924 UINT ret;
925 HICON hIcons[2];
926 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
928 ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
929 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
930 if (ret == 0xFFFFFFFF)
931 return E_FAIL;
932 if (ret > 0) {
933 if (phiconLarge)
934 *phiconLarge = hIcons[0];
935 else
936 DestroyIcon(hIcons[0]);
937 if (phiconSmall)
938 *phiconSmall = hIcons[1];
939 else
940 DestroyIcon(hIcons[1]);
941 return S_OK;
943 return S_FALSE;
946 /****************************************************************************
947 * SHDefExtractIconA [SHELL32.@]
949 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
950 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
952 HRESULT ret;
953 INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
954 LPWSTR lpwstrFile = heap_alloc(len * sizeof(WCHAR));
956 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
958 MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
959 ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
960 heap_free(lpwstrFile);
961 return ret;
965 /****************************************************************************
966 * SHGetIconOverlayIndexA [SHELL32.@]
968 * Returns the index of the overlay icon in the system image list.
970 INT WINAPI SHGetIconOverlayIndexA(LPCSTR pszIconPath, INT iIconIndex)
972 FIXME("%s, %d\n", debugstr_a(pszIconPath), iIconIndex);
974 return -1;
977 /****************************************************************************
978 * SHGetIconOverlayIndexW [SHELL32.@]
980 * Returns the index of the overlay icon in the system image list.
982 INT WINAPI SHGetIconOverlayIndexW(LPCWSTR pszIconPath, INT iIconIndex)
984 FIXME("%s, %d\n", debugstr_w(pszIconPath), iIconIndex);
986 return -1;
989 /****************************************************************************
990 * SHGetStockIconInfo [SHELL32.@]
992 * Receive information for builtin icons
994 * PARAMS
995 * id [I] selected icon-id to get information for
996 * flags [I] selects the information to receive
997 * sii [IO] SHSTOCKICONINFO structure to fill
999 * RETURNS
1000 * Success: S_OK
1001 * Failure: A HRESULT failure code
1004 HRESULT WINAPI SHGetStockIconInfo(SHSTOCKICONID id, UINT flags, SHSTOCKICONINFO *sii)
1006 static const WCHAR shell32dll[] = {'\\','s','h','e','l','l','3','2','.','d','l','l',0};
1008 FIXME("(%d, 0x%x, %p) semi-stub\n", id, flags, sii);
1009 if ((id < 0) || (id >= SIID_MAX_ICONS) || !sii || (sii->cbSize != sizeof(SHSTOCKICONINFO))) {
1010 return E_INVALIDARG;
1013 GetSystemDirectoryW(sii->szPath, MAX_PATH);
1015 /* no icons defined: use default */
1016 sii->iIcon = -IDI_SHELL_DOCUMENT;
1017 lstrcatW(sii->szPath, shell32dll);
1019 if (flags)
1020 FIXME("flags 0x%x not implemented\n", flags);
1022 sii->hIcon = NULL;
1023 sii->iSysImageIndex = -1;
1025 TRACE("%3d: returning %s (%d)\n", id, debugstr_w(sii->szPath), sii->iIcon);
1027 return S_OK;
1030 /*************************************************************************
1031 * SHGetImageList (SHELL32.727)
1033 * Returns a copy of a shell image list.
1035 * NOTES
1036 * Windows XP features 4 sizes of image list, and Vista 5. Wine currently
1037 * only supports the traditional small and large image lists, so requests
1038 * for the others will currently fail.
1040 HRESULT WINAPI SHGetImageList(int iImageList, REFIID riid, void **ppv)
1042 if (iImageList < 0 || iImageList > SHIL_SYSSMALL)
1044 FIXME("Unsupported image list %i requested\n", iImageList);
1045 return E_FAIL;
1048 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
1049 return HIMAGELIST_QueryInterface(shell_imagelists[iImageList], riid, ppv);