ntdll: Add a helper for platform-specific threading initialization.
[wine.git] / dlls / shell32 / iconcache.c
blobd1be5408fbb94e19e5fa86f6e9c4253a933aa0f4
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 = -1;
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));
374 if (!hicons[i]) goto fail;
377 if (flags & GIL_FORSHORTCUT)
379 BOOL failed = FALSE;
381 for (i = 0; i < ARRAY_SIZE(hshortcuts); i++)
383 if (!(hshortcuts[i] = SIC_OverlayShortcutImage(hicons[i], i)))
385 WARN("Failed to create shortcut overlaid icons.\n");
386 failed = TRUE;
390 if (failed)
392 for (i = 0; i < ARRAY_SIZE(hshortcuts); i++)
393 DestroyIcon(hshortcuts[i]);
394 flags &= ~GIL_FORSHORTCUT;
396 else
398 for (i = 0; i < ARRAY_SIZE(hicons); i++)
400 DestroyIcon(hicons[i]);
401 hicons[i] = hshortcuts[i];
406 ret = SIC_IconAppend( sourcefile, index, hicons, flags );
408 fail:
409 for (i = 0; i < ARRAY_SIZE(hicons); i++)
410 DestroyIcon(hicons[i]);
411 return ret;
414 static int get_shell_icon_size(void)
416 WCHAR buf[32];
417 DWORD value = 32, size = sizeof(buf), type;
418 HKEY key;
420 if (!RegOpenKeyW( HKEY_CURRENT_USER, WindowMetrics, &key ))
422 if (!RegQueryValueExW( key, ShellIconSize, NULL, &type, (BYTE *)buf, &size ) && type == REG_SZ)
424 if (size == sizeof(buf)) buf[size / sizeof(WCHAR) - 1] = 0;
425 value = atoiW( buf );
427 RegCloseKey( key );
429 return value;
432 /*****************************************************************************
433 * SIC_Initialize [internal]
435 static BOOL WINAPI SIC_Initialize( INIT_ONCE *once, void *param, void **context )
437 HICON hicons[ARRAY_SIZE(shell_imagelists)];
438 SIZE sizes[ARRAY_SIZE(shell_imagelists)];
439 BOOL failed = FALSE;
440 unsigned int i;
442 if (!IsProcessDPIAware())
444 sizes[SHIL_LARGE].cx = sizes[SHIL_LARGE].cy = get_shell_icon_size();
445 sizes[SHIL_SMALL].cx = GetSystemMetrics( SM_CXSMICON );
446 sizes[SHIL_SMALL].cy = GetSystemMetrics( SM_CYSMICON );
448 else
450 sizes[SHIL_LARGE].cx = GetSystemMetrics( SM_CXICON );
451 sizes[SHIL_LARGE].cy = GetSystemMetrics( SM_CYICON );
452 sizes[SHIL_SMALL].cx = sizes[SHIL_LARGE].cx / 2;
453 sizes[SHIL_SMALL].cy = sizes[SHIL_LARGE].cy / 2;
456 sizes[SHIL_EXTRALARGE].cx = (GetSystemMetrics( SM_CXICON ) * 3) / 2;
457 sizes[SHIL_EXTRALARGE].cy = (GetSystemMetrics( SM_CYICON ) * 3) / 2;
458 sizes[SHIL_SYSSMALL].cx = GetSystemMetrics( SM_CXSMICON );
459 sizes[SHIL_SYSSMALL].cy = GetSystemMetrics( SM_CYSMICON );
460 sizes[SHIL_JUMBO].cx = sizes[SHIL_JUMBO].cy = 256;
462 TRACE("large %dx%d small %dx%d\n", sizes[SHIL_LARGE].cx, sizes[SHIL_LARGE].cy, sizes[SHIL_SMALL].cx, sizes[SHIL_SMALL].cy);
464 sic_hdpa = DPA_Create(16);
465 if (!sic_hdpa)
466 return(FALSE);
468 for (i = 0; i < ARRAY_SIZE(shell_imagelists); i++)
470 shell_imagelists[i] = ImageList_Create(sizes[i].cx, sizes[i].cy, ILC_COLOR32 | ILC_MASK, 0, 0x20);
471 ImageList_SetBkColor(shell_imagelists[i], CLR_NONE);
473 /* Load the generic file icon, which is used as the default if an icon isn't found. */
474 if (!(hicons[i] = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_FILE),
475 IMAGE_ICON, sizes[i].cx, sizes[i].cy, LR_SHARED)))
477 failed = TRUE;
481 if (failed)
483 FIXME("Failed to load IDI_SHELL_FILE icon!\n");
484 return FALSE;
487 SIC_IconAppend(swShell32Name, IDI_SHELL_FILE - 1, hicons, 0);
488 SIC_IconAppend(swShell32Name, -IDI_SHELL_FILE, hicons, 0);
490 TRACE("small list=%p, large list=%p\n", shell_imagelists[SHIL_SMALL], shell_imagelists[SHIL_LARGE]);
492 return TRUE;
495 /*************************************************************************
496 * SIC_Destroy
498 * frees the cache
500 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
502 heap_free(((LPSIC_ENTRY)ptr)->sSourceFile);
503 SHFree(ptr);
504 return TRUE;
507 void SIC_Destroy(void)
509 unsigned int i;
511 TRACE("\n");
513 EnterCriticalSection(&SHELL32_SicCS);
515 if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
517 for (i = 0; i < ARRAY_SIZE(shell_imagelists); i++)
519 if (shell_imagelists[i])
520 ImageList_Destroy(shell_imagelists[i]);
523 LeaveCriticalSection(&SHELL32_SicCS);
524 DeleteCriticalSection(&SHELL32_SicCS);
527 /*****************************************************************************
528 * SIC_GetIconIndex [internal]
530 * Parameters
531 * sSourceFile [IN] filename of file containing the icon
532 * index [IN] index/resID (negated) in this file
534 * NOTES
535 * look in the cache for a proper icon. if not available the icon is taken
536 * from the file and cached
538 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags )
540 SIC_ENTRY sice;
541 INT ret, index = INVALID_INDEX;
542 WCHAR path[MAX_PATH];
544 TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
546 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
547 sice.sSourceFile = path;
548 sice.dwSourceIndex = dwSourceIndex;
549 sice.dwFlags = dwFlags;
551 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
553 EnterCriticalSection(&SHELL32_SicCS);
555 if (NULL != DPA_GetPtr (sic_hdpa, 0))
557 /* search linear from position 0*/
558 index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
561 if ( INVALID_INDEX == index )
563 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex, dwFlags);
565 else
567 TRACE("-- found\n");
568 ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
571 LeaveCriticalSection(&SHELL32_SicCS);
572 return ret;
575 /*****************************************************************************
576 * SIC_LoadOverlayIcon [internal]
578 * Load a shell overlay icon and return its icon cache index.
580 static int SIC_LoadOverlayIcon(int icon_idx)
582 WCHAR buffer[1024], wszIdx[8];
583 HKEY hKeyShellIcons;
584 LPCWSTR iconPath;
585 int iconIdx;
587 static const WCHAR wszShellIcons[] = {
588 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
589 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
590 'E','x','p','l','o','r','e','r','\\','S','h','e','l','l',' ','I','c','o','n','s',0
592 static const WCHAR wszNumFmt[] = {'%','d',0};
594 iconPath = swShell32Name; /* default: load icon from shell32.dll */
595 iconIdx = icon_idx;
597 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszShellIcons, 0, KEY_READ, &hKeyShellIcons) == ERROR_SUCCESS)
599 DWORD count = sizeof(buffer);
601 sprintfW(wszIdx, wszNumFmt, icon_idx);
603 /* read icon path and index */
604 if (RegQueryValueExW(hKeyShellIcons, wszIdx, NULL, NULL, (LPBYTE)buffer, &count) == ERROR_SUCCESS)
606 LPWSTR p = strchrW(buffer, ',');
608 if (!p)
610 ERR("Icon index in %s/%s corrupted, no comma.\n", debugstr_w(wszShellIcons),debugstr_w(wszIdx));
611 RegCloseKey(hKeyShellIcons);
612 return -1;
614 *p++ = 0;
615 iconPath = buffer;
616 iconIdx = atoiW(p);
619 RegCloseKey(hKeyShellIcons);
622 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
624 return SIC_LoadIcon(iconPath, iconIdx, 0);
627 /*************************************************************************
628 * Shell_GetImageLists [SHELL32.71]
630 * PARAMETERS
631 * imglist[1|2] [OUT] pointer which receives imagelist handles
634 BOOL WINAPI Shell_GetImageLists(HIMAGELIST *large_list, HIMAGELIST *small_list)
636 TRACE("(%p, %p)\n", large_list, small_list);
638 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
639 if (large_list) *large_list = shell_imagelists[SHIL_LARGE];
640 if (small_list) *small_list = shell_imagelists[SHIL_SMALL];
641 return TRUE;
644 /*************************************************************************
645 * PidlToSicIndex [INTERNAL]
647 * PARAMETERS
648 * sh [IN] IShellFolder
649 * pidl [IN]
650 * bBigIcon [IN]
651 * uFlags [IN] GIL_*
652 * pIndex [OUT] index within the SIC
655 BOOL PidlToSicIndex (
656 IShellFolder * sh,
657 LPCITEMIDLIST pidl,
658 BOOL bBigIcon,
659 UINT uFlags,
660 int * pIndex)
662 IExtractIconW *ei;
663 WCHAR szIconFile[MAX_PATH]; /* file containing the icon */
664 INT iSourceIndex; /* index or resID(negated) in this file */
665 BOOL ret = FALSE;
666 UINT dwFlags = 0;
667 int iShortcutDefaultIndex = INVALID_INDEX;
669 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
671 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
673 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconW, 0, (void **)&ei)))
675 if (SUCCEEDED(IExtractIconW_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
677 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex, uFlags);
678 ret = TRUE;
680 IExtractIconW_Release(ei);
683 if (INVALID_INDEX == *pIndex) /* default icon when failed */
685 if (0 == (uFlags & GIL_FORSHORTCUT))
687 *pIndex = 0;
689 else
691 if (INVALID_INDEX == iShortcutDefaultIndex)
693 iShortcutDefaultIndex = SIC_LoadIcon(swShell32Name, 0, GIL_FORSHORTCUT);
695 *pIndex = (INVALID_INDEX != iShortcutDefaultIndex ? iShortcutDefaultIndex : 0);
699 return ret;
703 /*************************************************************************
704 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
706 * PARAMETERS
707 * sh [IN] pointer to an instance of IShellFolder
708 * pidl [IN]
709 * pIndex [OUT][OPTIONAL] SIC index for big icon
712 int WINAPI SHMapPIDLToSystemImageListIndex(
713 IShellFolder *sh,
714 LPCITEMIDLIST pidl,
715 int *pIndex)
717 int Index;
718 UINT uGilFlags = 0;
720 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
721 pdump(pidl);
723 if (SHELL_IsShortcut(pidl))
724 uGilFlags |= GIL_FORSHORTCUT;
726 if (pIndex)
727 if (!PidlToSicIndex ( sh, pidl, 1, uGilFlags, pIndex))
728 *pIndex = -1;
730 if (!PidlToSicIndex ( sh, pidl, 0, uGilFlags, &Index))
731 return -1;
733 return Index;
736 /*************************************************************************
737 * SHMapIDListToImageListIndexAsync [SHELL32.148]
739 HRESULT WINAPI SHMapIDListToImageListIndexAsync(IUnknown *pts, IShellFolder *psf,
740 LPCITEMIDLIST pidl, UINT flags,
741 void *pfn, void *pvData, void *pvHint,
742 int *piIndex, int *piIndexSel)
744 FIXME("(%p, %p, %p, 0x%08x, %p, %p, %p, %p, %p)\n",
745 pts, psf, pidl, flags, pfn, pvData, pvHint, piIndex, piIndexSel);
746 return E_FAIL;
749 /*************************************************************************
750 * Shell_GetCachedImageIndex [SHELL32.72]
753 static INT Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
755 INT ret, len;
756 LPWSTR szTemp;
758 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
760 len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
761 szTemp = heap_alloc( len * sizeof(WCHAR) );
762 MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
764 ret = SIC_GetIconIndex( szTemp, nIndex, 0 );
766 heap_free( szTemp );
768 return ret;
771 static INT Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
773 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
775 return SIC_GetIconIndex(szPath, nIndex, 0);
778 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
779 { if( SHELL_OsIsUnicode())
780 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
781 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
784 /*************************************************************************
785 * ExtractIconExW [SHELL32.@]
786 * RETURNS
787 * 0 no icon found
788 * -1 file is not valid
789 * or number of icons extracted
791 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
793 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
795 return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
798 /*************************************************************************
799 * ExtractIconExA [SHELL32.@]
801 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
803 UINT ret = 0;
804 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
805 LPWSTR lpwstrFile = heap_alloc( len * sizeof(WCHAR));
807 TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
809 if (lpwstrFile)
811 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
812 ret = ExtractIconExW(lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
813 heap_free(lpwstrFile);
815 return ret;
818 /*************************************************************************
819 * ExtractAssociatedIconA (SHELL32.@)
821 * Return icon for given file (either from file itself or from associated
822 * executable) and patch parameters if needed.
824 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
826 HICON hIcon = NULL;
827 INT len = MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, NULL, 0);
828 /* Note that we need to allocate MAX_PATH, since we are supposed to fill
829 * the correct executable if there is no icon in lpIconPath directly.
830 * lpIconPath itself is supposed to be large enough, so make sure lpIconPathW
831 * is large enough too. Yes, I am puking too.
833 LPWSTR lpIconPathW = heap_alloc(MAX_PATH * sizeof(WCHAR));
835 TRACE("%p %s %p\n", hInst, debugstr_a(lpIconPath), lpiIcon);
837 if (lpIconPathW)
839 MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, lpIconPathW, len);
840 hIcon = ExtractAssociatedIconW(hInst, lpIconPathW, lpiIcon);
841 WideCharToMultiByte(CP_ACP, 0, lpIconPathW, -1, lpIconPath, MAX_PATH , NULL, NULL);
842 heap_free(lpIconPathW);
844 return hIcon;
847 /*************************************************************************
848 * ExtractAssociatedIconW (SHELL32.@)
850 * Return icon for given file (either from file itself or from associated
851 * executable) and patch parameters if needed.
853 HICON WINAPI ExtractAssociatedIconW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIcon)
855 HICON hIcon = NULL;
856 WORD wDummyIcon = 0;
858 TRACE("%p %s %p\n", hInst, debugstr_w(lpIconPath), lpiIcon);
860 if(lpiIcon == NULL)
861 lpiIcon = &wDummyIcon;
863 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
865 if( hIcon < (HICON)2 )
866 { if( hIcon == (HICON)1 ) /* no icons found in given file */
867 { WCHAR tempPath[MAX_PATH];
868 HINSTANCE uRet = FindExecutableW(lpIconPath,NULL,tempPath);
870 if( uRet > (HINSTANCE)32 && tempPath[0] )
871 { lstrcpyW(lpIconPath,tempPath);
872 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
873 if( hIcon > (HICON)2 )
874 return hIcon;
878 if( hIcon == (HICON)1 )
879 *lpiIcon = 2; /* MS-DOS icon - we found .exe but no icons in it */
880 else
881 *lpiIcon = 6; /* generic icon - found nothing */
883 if (GetModuleFileNameW(hInst, lpIconPath, MAX_PATH))
884 hIcon = LoadIconW(hInst, MAKEINTRESOURCEW(*lpiIcon));
886 return hIcon;
889 /*************************************************************************
890 * ExtractAssociatedIconExW (SHELL32.@)
892 * Return icon for given file (either from file itself or from associated
893 * executable) and patch parameters if needed.
895 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
897 FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
898 return 0;
901 /*************************************************************************
902 * ExtractAssociatedIconExA (SHELL32.@)
904 * Return icon for given file (either from file itself or from associated
905 * executable) and patch parameters if needed.
907 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
909 HICON ret;
910 INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
911 LPWSTR lpwstrFile = heap_alloc( len * sizeof(WCHAR) );
913 TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
915 MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
916 ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
917 heap_free(lpwstrFile);
918 return ret;
922 /****************************************************************************
923 * SHDefExtractIconW [SHELL32.@]
925 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
926 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
928 UINT ret;
929 HICON hIcons[2];
930 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
932 ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
933 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
934 if (ret == 0xFFFFFFFF)
935 return E_FAIL;
936 if (ret > 0) {
937 if (phiconLarge)
938 *phiconLarge = hIcons[0];
939 else
940 DestroyIcon(hIcons[0]);
941 if (phiconSmall)
942 *phiconSmall = hIcons[1];
943 else
944 DestroyIcon(hIcons[1]);
945 return S_OK;
947 return S_FALSE;
950 /****************************************************************************
951 * SHDefExtractIconA [SHELL32.@]
953 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
954 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
956 HRESULT ret;
957 INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
958 LPWSTR lpwstrFile = heap_alloc(len * sizeof(WCHAR));
960 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
962 MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
963 ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
964 heap_free(lpwstrFile);
965 return ret;
969 /****************************************************************************
970 * SHGetIconOverlayIndexA [SHELL32.@]
972 * Returns the index of the overlay icon in the system image list.
974 INT WINAPI SHGetIconOverlayIndexA(LPCSTR pszIconPath, INT iIconIndex)
976 FIXME("%s, %d\n", debugstr_a(pszIconPath), iIconIndex);
978 return -1;
981 /****************************************************************************
982 * SHGetIconOverlayIndexW [SHELL32.@]
984 * Returns the index of the overlay icon in the system image list.
986 INT WINAPI SHGetIconOverlayIndexW(LPCWSTR pszIconPath, INT iIconIndex)
988 FIXME("%s, %d\n", debugstr_w(pszIconPath), iIconIndex);
990 return -1;
993 /****************************************************************************
994 * SHGetStockIconInfo [SHELL32.@]
996 * Receive information for builtin icons
998 * PARAMS
999 * id [I] selected icon-id to get information for
1000 * flags [I] selects the information to receive
1001 * sii [IO] SHSTOCKICONINFO structure to fill
1003 * RETURNS
1004 * Success: S_OK
1005 * Failure: A HRESULT failure code
1008 HRESULT WINAPI SHGetStockIconInfo(SHSTOCKICONID id, UINT flags, SHSTOCKICONINFO *sii)
1010 static const WCHAR shell32dll[] = {'\\','s','h','e','l','l','3','2','.','d','l','l',0};
1012 FIXME("(%d, 0x%x, %p) semi-stub\n", id, flags, sii);
1013 if ((id < 0) || (id >= SIID_MAX_ICONS) || !sii || (sii->cbSize != sizeof(SHSTOCKICONINFO))) {
1014 return E_INVALIDARG;
1017 GetSystemDirectoryW(sii->szPath, MAX_PATH);
1019 /* no icons defined: use default */
1020 sii->iIcon = -IDI_SHELL_FILE;
1021 lstrcatW(sii->szPath, shell32dll);
1023 if (flags)
1024 FIXME("flags 0x%x not implemented\n", flags);
1026 sii->hIcon = NULL;
1027 sii->iSysImageIndex = -1;
1029 TRACE("%3d: returning %s (%d)\n", id, debugstr_w(sii->szPath), sii->iIcon);
1031 return S_OK;
1034 /*************************************************************************
1035 * SHGetImageList (SHELL32.727)
1037 * Returns a copy of a shell image list.
1039 * NOTES
1040 * Windows XP features 4 sizes of image list, and Vista 5. Wine currently
1041 * only supports the traditional small and large image lists, so requests
1042 * for the others will currently fail.
1044 HRESULT WINAPI SHGetImageList(int iImageList, REFIID riid, void **ppv)
1046 TRACE("(%d, %s, %p)\n", iImageList, debugstr_guid(riid), ppv);
1048 if (iImageList < 0 || iImageList > SHIL_LAST)
1049 return E_FAIL;
1051 InitOnceExecuteOnce( &sic_init_once, SIC_Initialize, NULL, NULL );
1052 return HIMAGELIST_QueryInterface(shell_imagelists[iImageList], riid, ppv);