Create a branch for network fixes.
[reactos.git] / dll / win32 / shell32 / iconcache.c
1 /*
2 * shell icon cache (SIC)
3 *
4 * Copyright 1998, 1999 Juergen Schmied
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30
31 #define COBJMACROS
32
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"
39
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"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(shell);
48
49 /********************** THE ICON CACHE ********************************/
50
51 #define INVALID_INDEX -1
52
53 typedef struct
54 {
55 LPWSTR sSourceFile; /* file (not path!) containing the icon */
56 DWORD dwSourceIndex; /* index within the file, if it is a resoure 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;
61
62 static HDPA sic_hdpa = 0;
63
64 static CRITICAL_SECTION SHELL32_SicCS;
65 static CRITICAL_SECTION_DEBUG critsect_debug =
66 {
67 0, 0, &SHELL32_SicCS,
68 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
69 0, 0, { (DWORD_PTR)(__FILE__ ": SHELL32_SicCS") }
70 };
71 static CRITICAL_SECTION SHELL32_SicCS = { &critsect_debug, -1, 0, 0, 0, 0 };
72
73 /*****************************************************************************
74 * SIC_CompareEntries
75 *
76 * NOTES
77 * Callback for DPA_Search
78 */
79 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
80 { LPSIC_ENTRY e1 = (LPSIC_ENTRY)p1, e2 = (LPSIC_ENTRY)p2;
81
82 TRACE("%p %p %8lx\n", p1, p2, lparam);
83
84 /* Icons in the cache are keyed by the name of the file they are
85 * loaded from, their resource index and the fact if they have a shortcut
86 * icon overlay or not.
87 */
88 if (e1->dwSourceIndex != e2->dwSourceIndex || /* first the faster one */
89 (e1->dwFlags & GIL_FORSHORTCUT) != (e2->dwFlags & GIL_FORSHORTCUT))
90 return 1;
91
92 if (strcmpiW(e1->sSourceFile,e2->sSourceFile))
93 return 1;
94
95 return 0;
96 }
97
98 /* declare SIC_LoadOverlayIcon() */
99 static int SIC_LoadOverlayIcon(int icon_idx);
100
101 /*****************************************************************************
102 * SIC_OverlayShortcutImage [internal]
103 *
104 * NOTES
105 * Creates a new icon as a copy of the passed-in icon, overlayed with a
106 * shortcut image.
107 */
108 static HICON SIC_OverlayShortcutImage(HICON SourceIcon, BOOL large)
109 { ICONINFO SourceIconInfo, ShortcutIconInfo, TargetIconInfo;
110 HICON ShortcutIcon, TargetIcon;
111 BITMAP SourceBitmapInfo, ShortcutBitmapInfo;
112 HDC SourceDC = NULL,
113 ShortcutDC = NULL,
114 TargetDC = NULL,
115 ScreenDC = NULL;
116 HBITMAP OldSourceBitmap = NULL,
117 OldShortcutBitmap = NULL,
118 OldTargetBitmap = NULL;
119
120 static int s_imgListIdx = -1;
121
122 /* Get information about the source icon and shortcut overlay */
123 if (! GetIconInfo(SourceIcon, &SourceIconInfo)
124 || 0 == GetObjectW(SourceIconInfo.hbmColor, sizeof(BITMAP), &SourceBitmapInfo))
125 {
126 return NULL;
127 }
128
129 /* search for the shortcut icon only once */
130 if (s_imgListIdx == -1)
131 s_imgListIdx = SIC_LoadOverlayIcon(- IDI_SHELL_SHORTCUT);
132 /* FIXME should use icon index 29 instead of the
133 resource id, but not all icons are present yet
134 so we can't use icon indices */
135
136 if (s_imgListIdx != -1)
137 {
138 if (large)
139 ShortcutIcon = ImageList_GetIcon(ShellBigIconList, s_imgListIdx, ILD_TRANSPARENT);
140 else
141 ShortcutIcon = ImageList_GetIcon(ShellSmallIconList, s_imgListIdx, ILD_TRANSPARENT);
142 } else
143 ShortcutIcon = NULL;
144
145 if (NULL == ShortcutIcon
146 || ! GetIconInfo(ShortcutIcon, &ShortcutIconInfo)
147 || 0 == GetObjectW(ShortcutIconInfo.hbmColor, sizeof(BITMAP), &ShortcutBitmapInfo))
148 {
149 return NULL;
150 }
151
152 TargetIconInfo = SourceIconInfo;
153 TargetIconInfo.hbmMask = NULL;
154 TargetIconInfo.hbmColor = NULL;
155
156 /* Setup the source, shortcut and target masks */
157 SourceDC = CreateCompatibleDC(NULL);
158 if (NULL == SourceDC) goto fail;
159 OldSourceBitmap = SelectObject(SourceDC, SourceIconInfo.hbmMask);
160 if (NULL == OldSourceBitmap) goto fail;
161
162 ShortcutDC = CreateCompatibleDC(NULL);
163 if (NULL == ShortcutDC) goto fail;
164 OldShortcutBitmap = SelectObject(ShortcutDC, ShortcutIconInfo.hbmMask);
165 if (NULL == OldShortcutBitmap) goto fail;
166
167 TargetDC = CreateCompatibleDC(NULL);
168 if (NULL == TargetDC) goto fail;
169 TargetIconInfo.hbmMask = CreateCompatibleBitmap(TargetDC, SourceBitmapInfo.bmWidth,
170 SourceBitmapInfo.bmHeight);
171 if (NULL == TargetIconInfo.hbmMask) goto fail;
172 ScreenDC = GetDC(NULL);
173 if (NULL == ScreenDC) goto fail;
174 TargetIconInfo.hbmColor = CreateCompatibleBitmap(ScreenDC, SourceBitmapInfo.bmWidth,
175 SourceBitmapInfo.bmHeight);
176 ReleaseDC(NULL, ScreenDC);
177 if (NULL == TargetIconInfo.hbmColor) goto fail;
178 OldTargetBitmap = SelectObject(TargetDC, TargetIconInfo.hbmMask);
179 if (NULL == OldTargetBitmap) goto fail;
180
181 /* Create the target mask by ANDing the source and shortcut masks */
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))
187 {
188 goto fail;
189 }
190
191 /* Setup the source and target xor bitmap */
192 if (NULL == SelectObject(SourceDC, SourceIconInfo.hbmColor) ||
193 NULL == SelectObject(TargetDC, TargetIconInfo.hbmColor))
194 {
195 goto fail;
196 }
197
198 /* Copy the source xor bitmap to the target and clear out part of it by using
199 the shortcut mask */
200 if (! BitBlt(TargetDC, 0, 0, SourceBitmapInfo.bmWidth, SourceBitmapInfo.bmHeight,
201 SourceDC, 0, 0, SRCCOPY) ||
202 ! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
203 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
204 ShortcutDC, 0, 0, SRCAND))
205 {
206 goto fail;
207 }
208
209 if (NULL == SelectObject(ShortcutDC, ShortcutIconInfo.hbmColor)) goto fail;
210
211 /* Now put in the shortcut xor mask */
212 if (! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
213 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
214 ShortcutDC, 0, 0, SRCINVERT))
215 {
216 goto fail;
217 }
218
219 /* Clean up, we're not goto'ing to 'fail' after this so we can be lazy and not set
220 handles to NULL */
221 SelectObject(TargetDC, OldTargetBitmap);
222 DeleteObject(TargetDC);
223 SelectObject(ShortcutDC, OldShortcutBitmap);
224 DeleteObject(ShortcutDC);
225 SelectObject(SourceDC, OldSourceBitmap);
226 DeleteObject(SourceDC);
227
228 /* Create the icon using the bitmaps prepared earlier */
229 TargetIcon = CreateIconIndirect(&TargetIconInfo);
230
231 /* CreateIconIndirect copies the bitmaps, so we can release our bitmaps now */
232 DeleteObject(TargetIconInfo.hbmColor);
233 DeleteObject(TargetIconInfo.hbmMask);
234
235 return TargetIcon;
236
237 fail:
238 /* Clean up scratch resources we created */
239 if (NULL != OldTargetBitmap) SelectObject(TargetDC, OldTargetBitmap);
240 if (NULL != TargetIconInfo.hbmColor) DeleteObject(TargetIconInfo.hbmColor);
241 if (NULL != TargetIconInfo.hbmMask) DeleteObject(TargetIconInfo.hbmMask);
242 if (NULL != TargetDC) DeleteObject(TargetDC);
243 if (NULL != OldShortcutBitmap) SelectObject(ShortcutDC, OldShortcutBitmap);
244 if (NULL != ShortcutDC) DeleteObject(ShortcutDC);
245 if (NULL != OldSourceBitmap) SelectObject(SourceDC, OldSourceBitmap);
246 if (NULL != SourceDC) DeleteObject(SourceDC);
247
248 return NULL;
249 }
250
251 /*****************************************************************************
252 * SIC_IconAppend [internal]
253 *
254 * NOTES
255 * appends an icon pair to the end of the cache
256 */
257 static INT SIC_IconAppend (LPCWSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon, DWORD dwFlags)
258 { LPSIC_ENTRY lpsice;
259 INT ret, index, index1;
260 WCHAR path[MAX_PATH];
261 TRACE("%s %i %p %p\n", debugstr_w(sSourceFile), dwSourceIndex, hSmallIcon ,hBigIcon);
262
263 lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
264
265 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
266 lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, (strlenW(path)+1)*sizeof(WCHAR) );
267 strcpyW( lpsice->sSourceFile, path );
268
269 lpsice->dwSourceIndex = dwSourceIndex;
270 lpsice->dwFlags = dwFlags;
271
272 EnterCriticalSection(&SHELL32_SicCS);
273
274 index = DPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
275 if ( INVALID_INDEX == index )
276 {
277 HeapFree(GetProcessHeap(), 0, lpsice->sSourceFile);
278 SHFree(lpsice);
279 ret = INVALID_INDEX;
280 }
281 else
282 {
283 index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
284 index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
285
286 if (index!=index1)
287 {
288 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
289 }
290 lpsice->dwListIndex = index;
291 ret = lpsice->dwListIndex;
292 }
293
294 LeaveCriticalSection(&SHELL32_SicCS);
295 return ret;
296 }
297 /****************************************************************************
298 * SIC_LoadIcon [internal]
299 *
300 * NOTES
301 * gets small/big icon by number from a file
302 */
303 static INT SIC_LoadIcon (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags)
304 { HICON hiconLarge=0;
305 HICON hiconSmall=0;
306 HICON hiconLargeShortcut;
307 HICON hiconSmallShortcut;
308
309 #if defined(__CYGWIN__) || defined (__MINGW32__) || defined(_MSC_VER)
310 static UINT (WINAPI*PrivateExtractIconExW)(LPCWSTR,int,HICON*,HICON*,UINT) = NULL;
311
312 if (!PrivateExtractIconExW) {
313 HMODULE hUser32 = GetModuleHandleA("user32");
314 PrivateExtractIconExW = (UINT(WINAPI*)(LPCWSTR,int,HICON*,HICON*,UINT)) GetProcAddress(hUser32, "PrivateExtractIconExW");
315 }
316
317 if (PrivateExtractIconExW)
318 PrivateExtractIconExW(sSourceFile, dwSourceIndex, &hiconLarge, &hiconSmall, 1);
319 else
320 #endif
321 {
322 PrivateExtractIconsW(sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, NULL, 1, 0);
323 PrivateExtractIconsW(sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, NULL, 1, 0);
324 }
325
326 if ( !hiconLarge || !hiconSmall)
327 {
328 WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, debugstr_w(sSourceFile), hiconLarge, hiconSmall);
329 return -1;
330 }
331
332 if (0 != (dwFlags & GIL_FORSHORTCUT))
333 {
334 hiconLargeShortcut = SIC_OverlayShortcutImage(hiconLarge, TRUE);
335 hiconSmallShortcut = SIC_OverlayShortcutImage(hiconSmall, FALSE);
336 if (NULL != hiconLargeShortcut && NULL != hiconSmallShortcut)
337 {
338 hiconLarge = hiconLargeShortcut;
339 hiconSmall = hiconSmallShortcut;
340 }
341 else
342 {
343 WARN("Failed to create shortcut overlayed icons\n");
344 if (NULL != hiconLargeShortcut) DestroyIcon(hiconLargeShortcut);
345 if (NULL != hiconSmallShortcut) DestroyIcon(hiconSmallShortcut);
346 dwFlags &= ~ GIL_FORSHORTCUT;
347 }
348 }
349
350 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge, dwFlags);
351 }
352 /*****************************************************************************
353 * SIC_GetIconIndex [internal]
354 *
355 * Parameters
356 * sSourceFile [IN] filename of file containing the icon
357 * index [IN] index/resID (negated) in this file
358 *
359 * NOTES
360 * look in the cache for a proper icon. if not available the icon is taken
361 * from the file and cached
362 */
363 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags )
364 {
365 SIC_ENTRY sice;
366 INT ret, index = INVALID_INDEX;
367 WCHAR path[MAX_PATH];
368
369 TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
370
371 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
372 sice.sSourceFile = path;
373 sice.dwSourceIndex = dwSourceIndex;
374 sice.dwFlags = dwFlags;
375
376 EnterCriticalSection(&SHELL32_SicCS);
377
378 if (NULL != DPA_GetPtr (sic_hdpa, 0))
379 {
380 /* search linear from position 0*/
381 index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
382 }
383
384 if ( INVALID_INDEX == index )
385 {
386 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex, dwFlags);
387 }
388 else
389 {
390 TRACE("-- found\n");
391 ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
392 }
393
394 LeaveCriticalSection(&SHELL32_SicCS);
395 return ret;
396 }
397 /*****************************************************************************
398 * SIC_Initialize [internal]
399 */
400 BOOL SIC_Initialize(void)
401 {
402 HICON hSm, hLg;
403 int cx_small, cy_small;
404 int cx_large, cy_large;
405
406 cx_small = GetSystemMetrics(SM_CXSMICON);
407 cy_small = GetSystemMetrics(SM_CYSMICON);
408 cx_large = GetSystemMetrics(SM_CXICON);
409 cy_large = GetSystemMetrics(SM_CYICON);
410
411 TRACE("\n");
412
413 if (sic_hdpa) /* already initialized?*/
414 return TRUE;
415
416 sic_hdpa = DPA_Create(16);
417
418 if (!sic_hdpa)
419 {
420 return(FALSE);
421 }
422
423 ShellSmallIconList = ImageList_Create(cx_small,cy_small,ILC_COLOR32|ILC_MASK,0,0x20);
424 ShellBigIconList = ImageList_Create(cx_large,cy_large,ILC_COLOR32|ILC_MASK,0,0x20);
425
426 ImageList_SetBkColor(ShellSmallIconList, CLR_NONE);
427 ImageList_SetBkColor(ShellBigIconList, CLR_NONE);
428
429 /* Load the document icon, which is used as the default if an icon isn't found. */
430 hSm = (HICON)LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_DOCUMENT),
431 IMAGE_ICON, cx_small, cy_small, LR_SHARED);
432 hLg = (HICON)LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_DOCUMENT),
433 IMAGE_ICON, cx_large, cy_large, LR_SHARED);
434
435 if (!hSm || !hLg)
436 {
437 FIXME("Failed to load IDI_SHELL_DOCUMENT icon!\n");
438 return FALSE;
439 }
440
441 SIC_IconAppend (swShell32Name, IDI_SHELL_DOCUMENT-1, hSm, hLg, 0);
442 SIC_IconAppend (swShell32Name, -IDI_SHELL_DOCUMENT, hSm, hLg, 0);
443
444 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
445
446 return TRUE;
447 }
448 /*************************************************************************
449 * SIC_Destroy
450 *
451 * frees the cache
452 */
453 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
454 {
455 HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
456 SHFree(ptr);
457 return TRUE;
458 }
459
460 void SIC_Destroy(void)
461 {
462 TRACE("\n");
463
464 EnterCriticalSection(&SHELL32_SicCS);
465
466 if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
467
468 sic_hdpa = NULL;
469 ImageList_Destroy(ShellSmallIconList);
470 ShellSmallIconList = 0;
471 ImageList_Destroy(ShellBigIconList);
472 ShellBigIconList = 0;
473
474 LeaveCriticalSection(&SHELL32_SicCS);
475 //DeleteCriticalSection(&SHELL32_SicCS); //static
476 }
477
478 /*****************************************************************************
479 * SIC_LoadOverlayIcon [internal]
480 *
481 * Load a shell overlay icon and return its icon cache index.
482 */
483 static int SIC_LoadOverlayIcon(int icon_idx)
484 {
485 WCHAR buffer[1024], wszIdx[8];
486 HKEY hKeyShellIcons;
487 LPCWSTR iconPath;
488 int iconIdx;
489
490 static const WCHAR wszShellIcons[] = {
491 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
492 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
493 'E','x','p','l','o','r','e','r','\\','S','h','e','l','l',' ','I','c','o','n','s',0
494 };
495 static const WCHAR wszNumFmt[] = {'%','d',0};
496
497 iconPath = swShell32Name; /* default: load icon from shell32.dll */
498 iconIdx = icon_idx;
499
500 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszShellIcons, 0, KEY_READ, &hKeyShellIcons) == ERROR_SUCCESS)
501 {
502 DWORD count = sizeof(buffer);
503
504 sprintfW(wszIdx, wszNumFmt, icon_idx);
505
506 /* read icon path and index */
507 if (RegQueryValueExW(hKeyShellIcons, wszIdx, NULL, NULL, (LPBYTE)buffer, &count) == ERROR_SUCCESS)
508 {
509 LPWSTR p = strchrW(buffer, ',');
510
511 if (p)
512 *p++ = 0;
513
514 iconPath = buffer;
515 iconIdx = atoiW(p);
516 }
517
518 RegCloseKey(hKeyShellIcons);
519 }
520
521 return SIC_LoadIcon(iconPath, iconIdx, 0);
522 }
523
524 /*************************************************************************
525 * Shell_GetImageList [SHELL32.71]
526 *
527 * PARAMETERS
528 * imglist[1|2] [OUT] pointer which receives imagelist handles
529 *
530 */
531 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
532 { TRACE("(%p,%p)\n",lpBigList,lpSmallList);
533 if (lpBigList)
534 { *lpBigList = ShellBigIconList;
535 }
536 if (lpSmallList)
537 { *lpSmallList = ShellSmallIconList;
538 }
539
540 return TRUE;
541 }
542 /*************************************************************************
543 * PidlToSicIndex [INTERNAL]
544 *
545 * PARAMETERS
546 * sh [IN] IShellFolder
547 * pidl [IN]
548 * bBigIcon [IN]
549 * uFlags [IN] GIL_*
550 * pIndex [OUT] index within the SIC
551 *
552 */
553 BOOL PidlToSicIndex (
554 IShellFolder * sh,
555 LPCITEMIDLIST pidl,
556 BOOL bBigIcon,
557 UINT uFlags,
558 int * pIndex)
559 {
560 IExtractIconW *ei;
561 WCHAR szIconFile[MAX_PATH]; /* file containing the icon */
562 INT iSourceIndex; /* index or resID(negated) in this file */
563 BOOL ret = FALSE;
564 UINT dwFlags = 0;
565 int iShortcutDefaultIndex = INVALID_INDEX;
566
567 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
568
569 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconW, 0, (void **)&ei)))
570 {
571 if (SUCCEEDED(IExtractIconW_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
572 {
573 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex, uFlags);
574 ret = TRUE;
575 }
576 IExtractIconW_Release(ei);
577 }
578
579 if (INVALID_INDEX == *pIndex) /* default icon when failed */
580 {
581 if (0 == (uFlags & GIL_FORSHORTCUT))
582 {
583 *pIndex = 0;
584 }
585 else
586 {
587 if (INVALID_INDEX == iShortcutDefaultIndex)
588 {
589 iShortcutDefaultIndex = SIC_LoadIcon(swShell32Name, 0, GIL_FORSHORTCUT);
590 }
591 *pIndex = (INVALID_INDEX != iShortcutDefaultIndex ? iShortcutDefaultIndex : 0);
592 }
593 }
594
595 return ret;
596
597 }
598
599 /*************************************************************************
600 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
601 *
602 * PARAMETERS
603 * sh [IN] pointer to an instance of IShellFolder
604 * pidl [IN]
605 * pIndex [OUT][OPTIONAL] SIC index for big icon
606 *
607 */
608 int WINAPI SHMapPIDLToSystemImageListIndex(
609 IShellFolder *sh,
610 LPCITEMIDLIST pidl,
611 int *pIndex)
612 {
613 int Index;
614 UINT uGilFlags = 0;
615
616 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
617 pdump(pidl);
618
619 if (SHELL_IsShortcut(pidl))
620 uGilFlags |= GIL_FORSHORTCUT;
621
622 if (pIndex)
623 if (!PidlToSicIndex ( sh, pidl, 1, uGilFlags, pIndex))
624 *pIndex = -1;
625
626 if (!PidlToSicIndex ( sh, pidl, 0, uGilFlags, &Index))
627 return -1;
628
629 return Index;
630 }
631
632 /*************************************************************************
633 * SHMapIDListToImageListIndexAsync [SHELL32.148]
634 */
635 HRESULT WINAPI SHMapIDListToImageListIndexAsync(IUnknown *pts, IShellFolder *psf,
636 LPCITEMIDLIST pidl, UINT flags,
637 void *pfn, void *pvData, void *pvHint,
638 int *piIndex, int *piIndexSel)
639 {
640 FIXME("(%p, %p, %p, 0x%08x, %p, %p, %p, %p, %p)\n",
641 pts, psf, pidl, flags, pfn, pvData, pvHint, piIndex, piIndexSel);
642 return E_FAIL;
643 }
644
645 /*************************************************************************
646 * Shell_GetCachedImageIndex [SHELL32.72]
647 *
648 */
649 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
650 {
651 INT ret, len;
652 LPWSTR szTemp;
653
654 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
655
656 len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
657 szTemp = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
658 MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
659
660 ret = SIC_GetIconIndex( szTemp, nIndex, 0 );
661
662 HeapFree( GetProcessHeap(), 0, szTemp );
663
664 return ret;
665 }
666
667 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
668 {
669 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
670
671 return SIC_GetIconIndex(szPath, nIndex, 0);
672 }
673
674 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
675 { if( SHELL_OsIsUnicode())
676 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
677 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
678 }
679
680 /*************************************************************************
681 * ExtractIconExW [SHELL32.@]
682 * RETURNS
683 * 0 no icon found
684 * -1 file is not valid
685 * or number of icons extracted
686 */
687 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
688 {
689 /* get entry point of undocumented function PrivateExtractIconExW() in user32 */
690 #if defined(__CYGWIN__) || defined (__MINGW32__) || defined(_MSC_VER)
691 static UINT (WINAPI*PrivateExtractIconExW)(LPCWSTR,int,HICON*,HICON*,UINT) = NULL;
692
693 if (!PrivateExtractIconExW) {
694 HMODULE hUser32 = GetModuleHandleA("user32");
695 PrivateExtractIconExW = (UINT(WINAPI*)(LPCWSTR,int,HICON*,HICON*,UINT)) GetProcAddress(hUser32, "PrivateExtractIconExW");
696
697 if (!PrivateExtractIconExW)
698 return 0;
699 }
700 #endif
701
702 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
703
704 return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
705 }
706
707 /*************************************************************************
708 * ExtractIconExA [SHELL32.@]
709 */
710 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
711 {
712 UINT ret = 0;
713 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
714 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
715
716 TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
717
718 if (lpwstrFile)
719 {
720 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
721 ret = ExtractIconExW(lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
722 HeapFree(GetProcessHeap(), 0, lpwstrFile);
723 }
724 return ret;
725 }
726
727 /*************************************************************************
728 * ExtractAssociatedIconA (SHELL32.@)
729 *
730 * Return icon for given file (either from file itself or from associated
731 * executable) and patch parameters if needed.
732 */
733 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
734 {
735 HICON hIcon = NULL;
736 INT len = MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, NULL, 0);
737 /* Note that we need to allocate MAX_PATH, since we are supposed to fill
738 * the correct executable if there is no icon in lpIconPath directly.
739 * lpIconPath itself is supposed to be large enough, so make sure lpIconPathW
740 * is large enough too. Yes, I am puking too.
741 */
742 LPWSTR lpIconPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
743
744 TRACE("%p %s %p\n", hInst, debugstr_a(lpIconPath), lpiIcon);
745
746 if (lpIconPathW)
747 {
748 MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, lpIconPathW, len);
749 hIcon = ExtractAssociatedIconW(hInst, lpIconPathW, lpiIcon);
750 WideCharToMultiByte(CP_ACP, 0, lpIconPathW, -1, lpIconPath, MAX_PATH , NULL, NULL);
751 HeapFree(GetProcessHeap(), 0, lpIconPathW);
752 }
753 return hIcon;
754 }
755
756 /*************************************************************************
757 * ExtractAssociatedIconW (SHELL32.@)
758 *
759 * Return icon for given file (either from file itself or from associated
760 * executable) and patch parameters if needed.
761 */
762 HICON WINAPI ExtractAssociatedIconW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIcon)
763 {
764 HICON hIcon = NULL;
765 WORD wDummyIcon = 0;
766
767 TRACE("%p %s %p\n", hInst, debugstr_w(lpIconPath), lpiIcon);
768
769 if(lpiIcon == NULL)
770 lpiIcon = &wDummyIcon;
771
772 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
773
774 if( hIcon < (HICON)2 )
775 { if( hIcon == (HICON)1 ) /* no icons found in given file */
776 { WCHAR tempPath[MAX_PATH];
777 HINSTANCE uRet = FindExecutableW(lpIconPath,NULL,tempPath);
778
779 if( uRet > (HINSTANCE)32 && tempPath[0] )
780 { lstrcpyW(lpIconPath,tempPath);
781 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
782 if( hIcon > (HICON)2 )
783 return hIcon;
784 }
785 }
786
787 if( hIcon == (HICON)1 )
788 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
789 else
790 *lpiIcon = 6; /* generic icon - found nothing */
791
792 if (GetModuleFileNameW(hInst, lpIconPath, MAX_PATH))
793 hIcon = LoadIconW(hInst, MAKEINTRESOURCEW(*lpiIcon));
794 }
795 return hIcon;
796 }
797
798 /*************************************************************************
799 * ExtractAssociatedIconExW (SHELL32.@)
800 *
801 * Return icon for given file (either from file itself or from associated
802 * executable) and patch parameters if needed.
803 */
804 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
805 {
806 FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
807 return 0;
808 }
809
810 /*************************************************************************
811 * ExtractAssociatedIconExA (SHELL32.@)
812 *
813 * Return icon for given file (either from file itself or from associated
814 * executable) and patch parameters if needed.
815 */
816 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
817 {
818 HICON ret;
819 INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
820 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
821
822 TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
823
824 MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
825 ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
826 HeapFree(GetProcessHeap(), 0, lpwstrFile);
827 return ret;
828 }
829
830
831 /****************************************************************************
832 * SHDefExtractIconW [SHELL32.@]
833 */
834 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
835 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
836 {
837 UINT ret;
838 HICON hIcons[2];
839 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
840
841 ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
842 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
843 if (ret == 0xFFFFFFFF)
844 return E_FAIL;
845 if (ret > 0) {
846 *phiconLarge = hIcons[0];
847 *phiconSmall = hIcons[1];
848 return S_OK;
849 }
850 return S_FALSE;
851 }
852
853 /****************************************************************************
854 * SHDefExtractIconA [SHELL32.@]
855 */
856 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
857 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
858 {
859 HRESULT ret;
860 INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
861 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
862
863 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
864
865 MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
866 ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
867 HeapFree(GetProcessHeap(), 0, lpwstrFile);
868 return ret;
869 }