Added OLEIVERB_INPLACEACTIVATE implementation in IOleObject::DoVerb.
[wine.git] / dlls / shell32 / iconcache.c
blobe35091729d3a75e3baa42692b761db60f365c159
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "shlguid.h"
43 #include "pidl.h"
44 #include "shell32_main.h"
45 #include "undocshell.h"
46 #include "shresdef.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(shell);
50 /********************** THE ICON CACHE ********************************/
52 #define INVALID_INDEX -1
54 typedef struct
56 LPWSTR sSourceFile; /* file (not path!) containing the icon */
57 DWORD dwSourceIndex; /* index within the file, if it is a resoure ID it will be negated */
58 DWORD dwListIndex; /* index within the iconlist */
59 DWORD dwFlags; /* GIL_* flags */
60 DWORD dwAccessTime;
61 } SIC_ENTRY, * LPSIC_ENTRY;
63 static HDPA sic_hdpa = 0;
65 static CRITICAL_SECTION SHELL32_SicCS;
66 static CRITICAL_SECTION_DEBUG critsect_debug =
68 0, 0, &SHELL32_SicCS,
69 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
70 0, 0, { (DWORD_PTR)(__FILE__ ": SHELL32_SicCS") }
72 static CRITICAL_SECTION SHELL32_SicCS = { &critsect_debug, -1, 0, 0, 0, 0 };
74 /*****************************************************************************
75 * SIC_CompareEntries
77 * NOTES
78 * Callback for DPA_Search
80 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
81 { LPSIC_ENTRY e1 = (LPSIC_ENTRY)p1, e2 = (LPSIC_ENTRY)p2;
83 TRACE("%p %p %8lx\n", p1, p2, lparam);
85 /* Icons in the cache are keyed by the name of the file they are
86 * loaded from, their resource index and the fact if they have a shortcut
87 * icon overlay or not.
89 if (e1->dwSourceIndex != e2->dwSourceIndex || /* first the faster one */
90 (e1->dwFlags & GIL_FORSHORTCUT) != (e2->dwFlags & GIL_FORSHORTCUT))
91 return 1;
93 if (strcmpiW(e1->sSourceFile,e2->sSourceFile))
94 return 1;
96 return 0;
99 /*****************************************************************************
100 * SIC_OverlayShortcutImage [internal]
102 * NOTES
103 * Creates a new icon as a copy of the passed-in icon, overlayed with a
104 * shortcut image.
106 static HICON SIC_OverlayShortcutImage(HICON SourceIcon)
107 { ICONINFO SourceIconInfo, ShortcutIconInfo, TargetIconInfo;
108 HICON ShortcutIcon, TargetIcon;
109 BITMAP SourceBitmapInfo, ShortcutBitmapInfo;
110 HDC SourceDC = NULL,
111 ShortcutDC = NULL,
112 TargetDC = NULL,
113 ScreenDC = NULL;
114 HBITMAP OldSourceBitmap = NULL,
115 OldShortcutBitmap = NULL,
116 OldTargetBitmap = NULL;
118 /* Get information about the source icon and shortcut overlay */
119 if (! GetIconInfo(SourceIcon, &SourceIconInfo)
120 || 0 == GetObjectW(SourceIconInfo.hbmColor, sizeof(BITMAP), &SourceBitmapInfo))
122 return NULL;
124 ShortcutIcon = LoadImageW(shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_SHORTCUT),
125 IMAGE_ICON, SourceBitmapInfo.bmWidth, SourceBitmapInfo.bmWidth,
126 LR_SHARED);
127 if (NULL == ShortcutIcon
128 || ! GetIconInfo(ShortcutIcon, &ShortcutIconInfo)
129 || 0 == GetObjectW(ShortcutIconInfo.hbmColor, sizeof(BITMAP), &ShortcutBitmapInfo))
131 return NULL;
134 TargetIconInfo = SourceIconInfo;
135 TargetIconInfo.hbmMask = NULL;
136 TargetIconInfo.hbmColor = NULL;
138 /* Setup the source, shortcut and target masks */
139 SourceDC = CreateCompatibleDC(NULL);
140 if (NULL == SourceDC) goto fail;
141 OldSourceBitmap = SelectObject(SourceDC, SourceIconInfo.hbmMask);
142 if (NULL == OldSourceBitmap) goto fail;
144 ShortcutDC = CreateCompatibleDC(NULL);
145 if (NULL == ShortcutDC) goto fail;
146 OldShortcutBitmap = SelectObject(ShortcutDC, ShortcutIconInfo.hbmMask);
147 if (NULL == OldShortcutBitmap) goto fail;
149 TargetDC = CreateCompatibleDC(NULL);
150 if (NULL == TargetDC) goto fail;
151 TargetIconInfo.hbmMask = CreateCompatibleBitmap(TargetDC, SourceBitmapInfo.bmWidth,
152 SourceBitmapInfo.bmHeight);
153 if (NULL == TargetIconInfo.hbmMask) goto fail;
154 ScreenDC = GetDC(NULL);
155 if (NULL == ScreenDC) goto fail;
156 TargetIconInfo.hbmColor = CreateCompatibleBitmap(ScreenDC, SourceBitmapInfo.bmWidth,
157 SourceBitmapInfo.bmHeight);
158 ReleaseDC(NULL, ScreenDC);
159 if (NULL == TargetIconInfo.hbmColor) goto fail;
160 OldTargetBitmap = SelectObject(TargetDC, TargetIconInfo.hbmMask);
161 if (NULL == OldTargetBitmap) goto fail;
163 /* Create the target mask by ANDing the source and shortcut masks */
164 if (! BitBlt(TargetDC, 0, 0, SourceBitmapInfo.bmWidth, SourceBitmapInfo.bmHeight,
165 SourceDC, 0, 0, SRCCOPY) ||
166 ! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
167 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
168 ShortcutDC, 0, 0, SRCAND))
170 goto fail;
173 /* Setup the source and target xor bitmap */
174 if (NULL == SelectObject(SourceDC, SourceIconInfo.hbmColor) ||
175 NULL == SelectObject(TargetDC, TargetIconInfo.hbmColor))
177 goto fail;
180 /* Copy the source xor bitmap to the target and clear out part of it by using
181 the shortcut mask */
182 if (! BitBlt(TargetDC, 0, 0, SourceBitmapInfo.bmWidth, SourceBitmapInfo.bmHeight,
183 SourceDC, 0, 0, SRCCOPY) ||
184 ! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
185 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
186 ShortcutDC, 0, 0, SRCAND))
188 goto fail;
191 if (NULL == SelectObject(ShortcutDC, ShortcutIconInfo.hbmColor)) goto fail;
193 /* Now put in the shortcut xor mask */
194 if (! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
195 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
196 ShortcutDC, 0, 0, SRCINVERT))
198 goto fail;
201 /* Clean up, we're not goto'ing to 'fail' after this so we can be lazy and not set
202 handles to NULL */
203 SelectObject(TargetDC, OldTargetBitmap);
204 DeleteObject(TargetDC);
205 SelectObject(ShortcutDC, OldShortcutBitmap);
206 DeleteObject(ShortcutDC);
207 SelectObject(SourceDC, OldSourceBitmap);
208 DeleteObject(SourceDC);
210 /* Create the icon using the bitmaps prepared earlier */
211 TargetIcon = CreateIconIndirect(&TargetIconInfo);
213 /* CreateIconIndirect copies the bitmaps, so we can release our bitmaps now */
214 DeleteObject(TargetIconInfo.hbmColor);
215 DeleteObject(TargetIconInfo.hbmMask);
217 return TargetIcon;
219 fail:
220 /* Clean up scratch resources we created */
221 if (NULL != OldTargetBitmap) SelectObject(TargetDC, OldTargetBitmap);
222 if (NULL != TargetIconInfo.hbmColor) DeleteObject(TargetIconInfo.hbmColor);
223 if (NULL != TargetIconInfo.hbmMask) DeleteObject(TargetIconInfo.hbmMask);
224 if (NULL != TargetDC) DeleteObject(TargetDC);
225 if (NULL != OldShortcutBitmap) SelectObject(ShortcutDC, OldShortcutBitmap);
226 if (NULL != ShortcutDC) DeleteObject(ShortcutDC);
227 if (NULL != OldSourceBitmap) SelectObject(SourceDC, OldSourceBitmap);
228 if (NULL != SourceDC) DeleteObject(SourceDC);
230 return NULL;
233 /*****************************************************************************
234 * SIC_IconAppend [internal]
236 * NOTES
237 * appends an icon pair to the end of the cache
239 static INT SIC_IconAppend (LPCWSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon, DWORD dwFlags)
240 { LPSIC_ENTRY lpsice;
241 INT ret, index, index1;
242 WCHAR path[MAX_PATH];
243 TRACE("%s %i %p %p\n", debugstr_w(sSourceFile), dwSourceIndex, hSmallIcon ,hBigIcon);
245 lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
247 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
248 lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, (strlenW(path)+1)*sizeof(WCHAR) );
249 strcpyW( lpsice->sSourceFile, path );
251 lpsice->dwSourceIndex = dwSourceIndex;
252 lpsice->dwFlags = dwFlags;
254 EnterCriticalSection(&SHELL32_SicCS);
256 index = DPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
257 if ( INVALID_INDEX == index )
259 HeapFree(GetProcessHeap(), 0, lpsice->sSourceFile);
260 SHFree(lpsice);
261 ret = INVALID_INDEX;
263 else
265 index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
266 index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
268 if (index!=index1)
270 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
272 lpsice->dwListIndex = index;
273 ret = lpsice->dwListIndex;
276 LeaveCriticalSection(&SHELL32_SicCS);
277 return ret;
279 /****************************************************************************
280 * SIC_LoadIcon [internal]
282 * NOTES
283 * gets small/big icon by number from a file
285 static INT SIC_LoadIcon (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags)
286 { HICON hiconLarge=0;
287 HICON hiconSmall=0;
288 HICON hiconLargeShortcut;
289 HICON hiconSmallShortcut;
291 PrivateExtractIconsW( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 );
292 PrivateExtractIconsW( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
294 if ( !hiconLarge || !hiconSmall)
296 WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, debugstr_w(sSourceFile), hiconLarge, hiconSmall);
297 return -1;
300 if (0 != (dwFlags & GIL_FORSHORTCUT))
302 hiconLargeShortcut = SIC_OverlayShortcutImage(hiconLarge);
303 hiconSmallShortcut = SIC_OverlayShortcutImage(hiconSmall);
304 if (NULL != hiconLargeShortcut && NULL != hiconSmallShortcut)
306 hiconLarge = hiconLargeShortcut;
307 hiconSmall = hiconSmallShortcut;
309 else
311 WARN("Failed to create shortcut overlayed icons\n");
312 if (NULL != hiconLargeShortcut) DestroyIcon(hiconLargeShortcut);
313 if (NULL != hiconSmallShortcut) DestroyIcon(hiconSmallShortcut);
314 dwFlags &= ~ GIL_FORSHORTCUT;
318 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge, dwFlags);
320 /*****************************************************************************
321 * SIC_GetIconIndex [internal]
323 * Parameters
324 * sSourceFile [IN] filename of file containing the icon
325 * index [IN] index/resID (negated) in this file
327 * NOTES
328 * look in the cache for a proper icon. if not available the icon is taken
329 * from the file and cached
331 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags )
333 SIC_ENTRY sice;
334 INT ret, index = INVALID_INDEX;
335 WCHAR path[MAX_PATH];
337 TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
339 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
340 sice.sSourceFile = path;
341 sice.dwSourceIndex = dwSourceIndex;
342 sice.dwFlags = dwFlags;
344 EnterCriticalSection(&SHELL32_SicCS);
346 if (NULL != DPA_GetPtr (sic_hdpa, 0))
348 /* search linear from position 0*/
349 index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
352 if ( INVALID_INDEX == index )
354 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex, dwFlags);
356 else
358 TRACE("-- found\n");
359 ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
362 LeaveCriticalSection(&SHELL32_SicCS);
363 return ret;
365 /*****************************************************************************
366 * SIC_Initialize [internal]
368 BOOL SIC_Initialize(void)
370 HICON hSm, hLg;
371 int cx_small, cy_small;
372 int cx_large, cy_large;
374 cx_small = GetSystemMetrics(SM_CXSMICON);
375 cy_small = GetSystemMetrics(SM_CYSMICON);
376 cx_large = GetSystemMetrics(SM_CXICON);
377 cy_large = GetSystemMetrics(SM_CYICON);
379 TRACE("\n");
381 if (sic_hdpa) /* already initialized?*/
382 return TRUE;
384 sic_hdpa = DPA_Create(16);
386 if (!sic_hdpa)
388 return(FALSE);
391 ShellSmallIconList = ImageList_Create(cx_small,cy_small,ILC_COLOR32|ILC_MASK,0,0x20);
392 ShellBigIconList = ImageList_Create(cx_large,cy_large,ILC_COLOR32|ILC_MASK,0,0x20);
394 ImageList_SetBkColor(ShellSmallIconList, CLR_NONE);
395 ImageList_SetBkColor(ShellBigIconList, CLR_NONE);
397 /* Load the document icon, which is used as the default if an icon isn't found. */
398 hSm = (HICON)LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_DOCUMENT),
399 IMAGE_ICON, cx_small, cy_small, LR_SHARED);
400 hLg = (HICON)LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_DOCUMENT),
401 IMAGE_ICON, cx_large, cy_large, LR_SHARED);
403 if (!hSm || !hLg)
405 FIXME("Failed to load IDI_SHELL_DOCUMENT icon!\n");
406 return FALSE;
409 SIC_IconAppend (swShell32Name, IDI_SHELL_DOCUMENT-1, hSm, hLg, 0);
410 SIC_IconAppend (swShell32Name, -IDI_SHELL_DOCUMENT, hSm, hLg, 0);
412 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
414 return TRUE;
416 /*************************************************************************
417 * SIC_Destroy
419 * frees the cache
421 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
423 HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
424 SHFree(ptr);
425 return TRUE;
428 void SIC_Destroy(void)
430 TRACE("\n");
432 EnterCriticalSection(&SHELL32_SicCS);
434 if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
436 sic_hdpa = NULL;
437 ImageList_Destroy(ShellSmallIconList);
438 ShellSmallIconList = 0;
439 ImageList_Destroy(ShellBigIconList);
440 ShellBigIconList = 0;
442 LeaveCriticalSection(&SHELL32_SicCS);
443 DeleteCriticalSection(&SHELL32_SicCS);
446 /*************************************************************************
447 * Shell_GetImageList [SHELL32.71]
449 * PARAMETERS
450 * imglist[1|2] [OUT] pointer which receives imagelist handles
453 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
454 { TRACE("(%p,%p)\n",lpBigList,lpSmallList);
455 if (lpBigList)
456 { *lpBigList = ShellBigIconList;
458 if (lpSmallList)
459 { *lpSmallList = ShellSmallIconList;
462 return TRUE;
464 /*************************************************************************
465 * PidlToSicIndex [INTERNAL]
467 * PARAMETERS
468 * sh [IN] IShellFolder
469 * pidl [IN]
470 * bBigIcon [IN]
471 * uFlags [IN] GIL_*
472 * pIndex [OUT] index within the SIC
475 BOOL PidlToSicIndex (
476 IShellFolder * sh,
477 LPCITEMIDLIST pidl,
478 BOOL bBigIcon,
479 UINT uFlags,
480 int * pIndex)
482 IExtractIconW *ei;
483 WCHAR szIconFile[MAX_PATH]; /* file containing the icon */
484 char szTemp[MAX_PATH];
485 INT iSourceIndex; /* index or resID(negated) in this file */
486 BOOL ret = FALSE;
487 UINT dwFlags = 0;
488 HKEY keyCls;
489 int iShortcutDefaultIndex = INVALID_INDEX;
491 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
493 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconW, 0, (void **)&ei)))
495 if (_ILGetExtension(pidl, szTemp, MAX_PATH) &&
496 HCR_MapTypeToValueA(szTemp, szTemp, MAX_PATH, TRUE))
498 if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_CLASSES_ROOT, szTemp, 0, KEY_QUERY_VALUE, &keyCls))
500 if (ERROR_SUCCESS == RegQueryValueExA(keyCls, "IsShortcut", NULL, NULL, NULL, NULL))
502 uFlags |= GIL_FORSHORTCUT;
504 RegCloseKey(keyCls);
507 if (SUCCEEDED(IExtractIconW_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
509 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex, uFlags);
510 ret = TRUE;
512 IExtractIconW_Release(ei);
515 if (INVALID_INDEX == *pIndex) /* default icon when failed */
517 if (0 == (uFlags & GIL_FORSHORTCUT))
519 *pIndex = 0;
521 else
523 if (INVALID_INDEX == iShortcutDefaultIndex)
525 iShortcutDefaultIndex = SIC_LoadIcon(swShell32Name, 0, GIL_FORSHORTCUT);
527 *pIndex = (INVALID_INDEX != iShortcutDefaultIndex ? iShortcutDefaultIndex : 0);
531 return ret;
535 /*************************************************************************
536 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
538 * PARAMETERS
539 * sh [IN] pointer to an instance of IShellFolder
540 * pidl [IN]
541 * pIndex [OUT][OPTIONAL] SIC index for big icon
544 int WINAPI SHMapPIDLToSystemImageListIndex(
545 IShellFolder *sh,
546 LPCITEMIDLIST pidl,
547 int *pIndex)
549 int Index;
551 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
552 pdump(pidl);
554 if (pIndex)
555 PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
556 PidlToSicIndex ( sh, pidl, 0, 0, &Index);
557 return Index;
560 /*************************************************************************
561 * Shell_GetCachedImageIndex [SHELL32.72]
564 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
566 INT ret, len;
567 LPWSTR szTemp;
569 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
571 len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
572 szTemp = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
573 MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
575 ret = SIC_GetIconIndex( szTemp, nIndex, 0 );
577 HeapFree( GetProcessHeap(), 0, szTemp );
579 return ret;
582 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
584 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
586 return SIC_GetIconIndex(szPath, nIndex, 0);
589 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
590 { if( SHELL_OsIsUnicode())
591 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
592 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
595 /*************************************************************************
596 * ExtractIconExW [SHELL32.@]
597 * RETURNS
598 * 0 no icon found
599 * -1 file is not valid
600 * or number of icons extracted
602 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
604 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
606 return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
609 /*************************************************************************
610 * ExtractIconExA [SHELL32.@]
612 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
614 UINT ret = 0;
615 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
616 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
618 TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
620 if (lpwstrFile)
622 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
623 ret = ExtractIconExW(lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
624 HeapFree(GetProcessHeap(), 0, lpwstrFile);
626 return ret;
629 /*************************************************************************
630 * ExtractAssociatedIconA (SHELL32.@)
632 * Return icon for given file (either from file itself or from associated
633 * executable) and patch parameters if needed.
635 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
637 HICON hIcon = NULL;
638 INT len = MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, NULL, 0);
639 /* Note that we need to allocate MAX_PATH, since we are supposed to fill
640 * the correct executable if there is no icon in lpIconPath directly.
641 * lpIconPath itself is supposed to be large enough, so make sure lpIconPathW
642 * is large enough too. Yes, I am puking too.
644 LPWSTR lpIconPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
646 TRACE("%p %s %p\n", hInst, debugstr_a(lpIconPath), lpiIcon);
648 if (lpIconPathW)
650 MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, lpIconPathW, len);
651 hIcon = ExtractAssociatedIconW(hInst, lpIconPathW, lpiIcon);
652 WideCharToMultiByte(CP_ACP, 0, lpIconPathW, -1, lpIconPath, MAX_PATH , NULL, NULL);
653 HeapFree(GetProcessHeap(), 0, lpIconPathW);
655 return hIcon;
658 /*************************************************************************
659 * ExtractAssociatedIconW (SHELL32.@)
661 * Return icon for given file (either from file itself or from associated
662 * executable) and patch parameters if needed.
664 HICON WINAPI ExtractAssociatedIconW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIcon)
666 HICON hIcon = NULL;
667 WORD wDummyIcon = 0;
669 TRACE("%p %s %p\n", hInst, debugstr_w(lpIconPath), lpiIcon);
671 if(lpiIcon == NULL)
672 lpiIcon = &wDummyIcon;
674 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
676 if( hIcon < (HICON)2 )
677 { if( hIcon == (HICON)1 ) /* no icons found in given file */
678 { WCHAR tempPath[MAX_PATH];
679 HINSTANCE uRet = FindExecutableW(lpIconPath,NULL,tempPath);
681 if( uRet > (HINSTANCE)32 && tempPath[0] )
682 { lstrcpyW(lpIconPath,tempPath);
683 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
684 if( hIcon > (HICON)2 )
685 return hIcon;
689 if( hIcon == (HICON)1 )
690 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
691 else
692 *lpiIcon = 6; /* generic icon - found nothing */
694 if (GetModuleFileNameW(hInst, lpIconPath, MAX_PATH))
695 hIcon = LoadIconW(hInst, MAKEINTRESOURCEW(*lpiIcon));
697 return hIcon;
700 /*************************************************************************
701 * ExtractAssociatedIconExW (SHELL32.@)
703 * Return icon for given file (either from file itself or from associated
704 * executable) and patch parameters if needed.
706 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
708 FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
709 return 0;
712 /*************************************************************************
713 * ExtractAssociatedIconExA (SHELL32.@)
715 * Return icon for given file (either from file itself or from associated
716 * executable) and patch parameters if needed.
718 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
720 HICON ret;
721 INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
722 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
724 TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
726 MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
727 ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
728 HeapFree(GetProcessHeap(), 0, lpwstrFile);
729 return ret;
733 /****************************************************************************
734 * SHDefExtractIconW [SHELL32.@]
736 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
737 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
739 UINT ret;
740 HICON hIcons[2];
741 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
743 ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
744 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
745 if (ret == 0xFFFFFFFF)
746 return E_FAIL;
747 if (ret > 0) {
748 *phiconLarge = hIcons[0];
749 *phiconSmall = hIcons[1];
750 return S_OK;
752 return S_FALSE;
755 /****************************************************************************
756 * SHDefExtractIconA [SHELL32.@]
758 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
759 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
761 HRESULT ret;
762 INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
763 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
765 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
767 MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
768 ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
769 HeapFree(GetProcessHeap(), 0, lpwstrFile);
770 return ret;