reshuffling of dlls
[reactos.git] / reactos / dll / 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "shlguid.h"
43 #include "pidl.h"
44 #include "shell32_main.h"
45 #include "undocshell.h"
46 #include "shresdef.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(shell);
49
50 /********************** THE ICON CACHE ********************************/
51
52 #define INVALID_INDEX -1
53
54 typedef struct
55 {
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;
62
63 static HDPA sic_hdpa = 0;
64
65 static CRITICAL_SECTION SHELL32_SicCS;
66 static CRITICAL_SECTION_DEBUG critsect_debug =
67 {
68 0, 0, &SHELL32_SicCS,
69 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
70 0, 0, { (DWORD_PTR)(__FILE__ ": SHELL32_SicCS") }
71 };
72 static CRITICAL_SECTION SHELL32_SicCS = { &critsect_debug, -1, 0, 0, 0, 0 };
73
74 /*****************************************************************************
75 * SIC_CompareEntries
76 *
77 * NOTES
78 * Callback for DPA_Search
79 */
80 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
81 { LPSIC_ENTRY e1 = (LPSIC_ENTRY)p1, e2 = (LPSIC_ENTRY)p2;
82
83 TRACE("%p %p %8lx\n", p1, p2, lparam);
84
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.
88 */
89 if (e1->dwSourceIndex != e2->dwSourceIndex || /* first the faster one */
90 (e1->dwFlags & GIL_FORSHORTCUT) != (e2->dwFlags & GIL_FORSHORTCUT))
91 return 1;
92
93 if (strcmpiW(e1->sSourceFile,e2->sSourceFile))
94 return 1;
95
96 return 0;
97 }
98
99 /* declare SIC_LoadOverlayIcon() */
100 static int SIC_LoadOverlayIcon(int idx);
101
102 /*****************************************************************************
103 * SIC_OverlayShortcutImage [internal]
104 *
105 * NOTES
106 * Creates a new icon as a copy of the passed-in icon, overlayed with a
107 * shortcut image.
108 */
109 static HICON SIC_OverlayShortcutImage(HICON SourceIcon, BOOL large)
110 { ICONINFO SourceIconInfo, ShortcutIconInfo, TargetIconInfo;
111 HICON ShortcutIcon, TargetIcon;
112 BITMAP SourceBitmapInfo, ShortcutBitmapInfo;
113 HDC SourceDC = NULL,
114 ShortcutDC = NULL,
115 TargetDC = NULL,
116 ScreenDC = NULL;
117 HBITMAP OldSourceBitmap = NULL,
118 OldShortcutBitmap = NULL,
119 OldTargetBitmap = NULL;
120
121 static int s_imgListIdx = -1;
122
123 /* Get information about the source icon and shortcut overlay */
124 if (! GetIconInfo(SourceIcon, &SourceIconInfo)
125 || 0 == GetObjectW(SourceIconInfo.hbmColor, sizeof(BITMAP), &SourceBitmapInfo))
126 {
127 return NULL;
128 }
129
130 /* search for the shortcut icon only once */
131 if (s_imgListIdx == -1)
132 s_imgListIdx = SIC_LoadOverlayIcon(- IDI_SHELL_SHORTCUT);
133 /* FIXME should use icon index 29 instead of the
134 resource id, but not all icons are present yet
135 so we can't use icon indices */
136
137 if (s_imgListIdx != -1)
138 {
139 if (large)
140 ShortcutIcon = ImageList_GetIcon(ShellBigIconList, s_imgListIdx, ILD_TRANSPARENT);
141 else
142 ShortcutIcon = ImageList_GetIcon(ShellSmallIconList, s_imgListIdx, ILD_TRANSPARENT);
143 } else
144 ShortcutIcon = NULL;
145
146 if (NULL == ShortcutIcon
147 || ! GetIconInfo(ShortcutIcon, &ShortcutIconInfo)
148 || 0 == GetObjectW(ShortcutIconInfo.hbmColor, sizeof(BITMAP), &ShortcutBitmapInfo))
149 {
150 return NULL;
151 }
152
153 TargetIconInfo = SourceIconInfo;
154 TargetIconInfo.hbmMask = NULL;
155 TargetIconInfo.hbmColor = NULL;
156
157 /* Setup the source, shortcut and target masks */
158 SourceDC = CreateCompatibleDC(NULL);
159 if (NULL == SourceDC) goto fail;
160 OldSourceBitmap = SelectObject(SourceDC, SourceIconInfo.hbmMask);
161 if (NULL == OldSourceBitmap) goto fail;
162
163 ShortcutDC = CreateCompatibleDC(NULL);
164 if (NULL == ShortcutDC) goto fail;
165 OldShortcutBitmap = SelectObject(ShortcutDC, ShortcutIconInfo.hbmMask);
166 if (NULL == OldShortcutBitmap) goto fail;
167
168 TargetDC = CreateCompatibleDC(NULL);
169 if (NULL == TargetDC) goto fail;
170 TargetIconInfo.hbmMask = CreateCompatibleBitmap(TargetDC, SourceBitmapInfo.bmWidth,
171 SourceBitmapInfo.bmHeight);
172 if (NULL == TargetIconInfo.hbmMask) goto fail;
173 ScreenDC = GetDC(NULL);
174 if (NULL == ScreenDC) goto fail;
175 TargetIconInfo.hbmColor = CreateCompatibleBitmap(ScreenDC, SourceBitmapInfo.bmWidth,
176 SourceBitmapInfo.bmHeight);
177 ReleaseDC(NULL, ScreenDC);
178 if (NULL == TargetIconInfo.hbmColor) goto fail;
179 OldTargetBitmap = SelectObject(TargetDC, TargetIconInfo.hbmMask);
180 if (NULL == OldTargetBitmap) goto fail;
181
182 /* Create the target mask by ANDing the source and shortcut masks */
183 if (! BitBlt(TargetDC, 0, 0, SourceBitmapInfo.bmWidth, SourceBitmapInfo.bmHeight,
184 SourceDC, 0, 0, SRCCOPY) ||
185 ! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
186 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
187 ShortcutDC, 0, 0, SRCAND))
188 {
189 goto fail;
190 }
191
192 /* Setup the source and target xor bitmap */
193 if (NULL == SelectObject(SourceDC, SourceIconInfo.hbmColor) ||
194 NULL == SelectObject(TargetDC, TargetIconInfo.hbmColor))
195 {
196 goto fail;
197 }
198
199 /* Copy the source xor bitmap to the target and clear out part of it by using
200 the shortcut mask */
201 if (! BitBlt(TargetDC, 0, 0, SourceBitmapInfo.bmWidth, SourceBitmapInfo.bmHeight,
202 SourceDC, 0, 0, SRCCOPY) ||
203 ! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
204 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
205 ShortcutDC, 0, 0, SRCAND))
206 {
207 goto fail;
208 }
209
210 if (NULL == SelectObject(ShortcutDC, ShortcutIconInfo.hbmColor)) goto fail;
211
212 /* Now put in the shortcut xor mask */
213 if (! BitBlt(TargetDC, 0, SourceBitmapInfo.bmHeight - ShortcutBitmapInfo.bmHeight,
214 ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
215 ShortcutDC, 0, 0, SRCINVERT))
216 {
217 goto fail;
218 }
219
220 /* Clean up, we're not goto'ing to 'fail' after this so we can be lazy and not set
221 handles to NULL */
222 SelectObject(TargetDC, OldTargetBitmap);
223 DeleteObject(TargetDC);
224 SelectObject(ShortcutDC, OldShortcutBitmap);
225 DeleteObject(ShortcutDC);
226 SelectObject(SourceDC, OldSourceBitmap);
227 DeleteObject(SourceDC);
228
229 /* Create the icon using the bitmaps prepared earlier */
230 TargetIcon = CreateIconIndirect(&TargetIconInfo);
231
232 /* CreateIconIndirect copies the bitmaps, so we can release our bitmaps now */
233 DeleteObject(TargetIconInfo.hbmColor);
234 DeleteObject(TargetIconInfo.hbmMask);
235
236 return TargetIcon;
237
238 fail:
239 /* Clean up scratch resources we created */
240 if (NULL != OldTargetBitmap) SelectObject(TargetDC, OldTargetBitmap);
241 if (NULL != TargetIconInfo.hbmColor) DeleteObject(TargetIconInfo.hbmColor);
242 if (NULL != TargetIconInfo.hbmMask) DeleteObject(TargetIconInfo.hbmMask);
243 if (NULL != TargetDC) DeleteObject(TargetDC);
244 if (NULL != OldShortcutBitmap) SelectObject(ShortcutDC, OldShortcutBitmap);
245 if (NULL != ShortcutDC) DeleteObject(ShortcutDC);
246 if (NULL != OldSourceBitmap) SelectObject(SourceDC, OldSourceBitmap);
247 if (NULL != SourceDC) DeleteObject(SourceDC);
248
249 return NULL;
250 }
251
252 /*****************************************************************************
253 * SIC_IconAppend [internal]
254 *
255 * NOTES
256 * appends an icon pair to the end of the cache
257 */
258 static INT SIC_IconAppend (LPCWSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon, DWORD dwFlags)
259 { LPSIC_ENTRY lpsice;
260 INT ret, index, index1;
261 WCHAR path[MAX_PATH];
262 TRACE("%s %i %p %p\n", debugstr_w(sSourceFile), dwSourceIndex, hSmallIcon ,hBigIcon);
263
264 lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
265
266 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
267 lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, (strlenW(path)+1)*sizeof(WCHAR) );
268 strcpyW( lpsice->sSourceFile, path );
269
270 lpsice->dwSourceIndex = dwSourceIndex;
271 lpsice->dwFlags = dwFlags;
272
273 EnterCriticalSection(&SHELL32_SicCS);
274
275 index = DPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
276 if ( INVALID_INDEX == index )
277 {
278 HeapFree(GetProcessHeap(), 0, lpsice->sSourceFile);
279 SHFree(lpsice);
280 ret = INVALID_INDEX;
281 }
282 else
283 {
284 index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
285 index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
286
287 if (index!=index1)
288 {
289 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
290 }
291 lpsice->dwListIndex = index;
292 ret = lpsice->dwListIndex;
293 }
294
295 LeaveCriticalSection(&SHELL32_SicCS);
296 return ret;
297 }
298 /****************************************************************************
299 * SIC_LoadIcon [internal]
300 *
301 * NOTES
302 * gets small/big icon by number from a file
303 */
304 static INT SIC_LoadIcon (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags)
305 { HICON hiconLarge=0;
306 HICON hiconSmall=0;
307 HICON hiconLargeShortcut;
308 HICON hiconSmallShortcut;
309
310 #if defined(__CYGWIN__) || defined (__MINGW32__) || defined(_MSC_VER)
311 static UINT (WINAPI*PrivateExtractIconExW)(LPCWSTR,int,HICON*,HICON*,UINT) = NULL;
312
313 if (!PrivateExtractIconExW) {
314 HMODULE hUser32 = GetModuleHandleA("user32");
315 PrivateExtractIconExW = (UINT(WINAPI*)(LPCWSTR,int,HICON*,HICON*,UINT)) GetProcAddress(hUser32, "PrivateExtractIconExW");
316 }
317
318 if (PrivateExtractIconExW)
319 PrivateExtractIconExW(sSourceFile, dwSourceIndex, &hiconLarge, &hiconSmall, 1);
320 else
321 #endif
322 {
323 PrivateExtractIconsW(sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, NULL, 1, 0);
324 PrivateExtractIconsW(sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, NULL, 1, 0);
325 }
326
327 if ( !hiconLarge || !hiconSmall)
328 {
329 WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, debugstr_w(sSourceFile), hiconLarge, hiconSmall);
330 return -1;
331 }
332
333 if (0 != (dwFlags & GIL_FORSHORTCUT))
334 {
335 hiconLargeShortcut = SIC_OverlayShortcutImage(hiconLarge, TRUE);
336 hiconSmallShortcut = SIC_OverlayShortcutImage(hiconSmall, FALSE);
337 if (NULL != hiconLargeShortcut && NULL != hiconSmallShortcut)
338 {
339 hiconLarge = hiconLargeShortcut;
340 hiconSmall = hiconSmallShortcut;
341 }
342 else
343 {
344 WARN("Failed to create shortcut overlayed icons\n");
345 if (NULL != hiconLargeShortcut) DestroyIcon(hiconLargeShortcut);
346 if (NULL != hiconSmallShortcut) DestroyIcon(hiconSmallShortcut);
347 dwFlags &= ~ GIL_FORSHORTCUT;
348 }
349 }
350
351 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge, dwFlags);
352 }
353 /*****************************************************************************
354 * SIC_GetIconIndex [internal]
355 *
356 * Parameters
357 * sSourceFile [IN] filename of file containing the icon
358 * index [IN] index/resID (negated) in this file
359 *
360 * NOTES
361 * look in the cache for a proper icon. if not available the icon is taken
362 * from the file and cached
363 */
364 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags )
365 {
366 SIC_ENTRY sice;
367 INT ret, index = INVALID_INDEX;
368 WCHAR path[MAX_PATH];
369
370 TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
371
372 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
373 sice.sSourceFile = path;
374 sice.dwSourceIndex = dwSourceIndex;
375 sice.dwFlags = dwFlags;
376
377 EnterCriticalSection(&SHELL32_SicCS);
378
379 if (NULL != DPA_GetPtr (sic_hdpa, 0))
380 {
381 /* search linear from position 0*/
382 index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
383 }
384
385 if ( INVALID_INDEX == index )
386 {
387 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex, dwFlags);
388 }
389 else
390 {
391 TRACE("-- found\n");
392 ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
393 }
394
395 LeaveCriticalSection(&SHELL32_SicCS);
396 return ret;
397 }
398 /*****************************************************************************
399 * SIC_Initialize [internal]
400 */
401 BOOL SIC_Initialize(void)
402 {
403 HICON hSm, hLg;
404 int cx_small, cy_small;
405 int cx_large, cy_large;
406
407 cx_small = GetSystemMetrics(SM_CXSMICON);
408 cy_small = GetSystemMetrics(SM_CYSMICON);
409 cx_large = GetSystemMetrics(SM_CXICON);
410 cy_large = GetSystemMetrics(SM_CYICON);
411
412 TRACE("\n");
413
414 if (sic_hdpa) /* already initialized?*/
415 return TRUE;
416
417 sic_hdpa = DPA_Create(16);
418
419 if (!sic_hdpa)
420 {
421 return(FALSE);
422 }
423
424 ShellSmallIconList = ImageList_Create(cx_small,cy_small,ILC_COLOR32|ILC_MASK,0,0x20);
425 ShellBigIconList = ImageList_Create(cx_large,cy_large,ILC_COLOR32|ILC_MASK,0,0x20);
426
427 ImageList_SetBkColor(ShellSmallIconList, CLR_NONE);
428 ImageList_SetBkColor(ShellBigIconList, CLR_NONE);
429
430 /* Load the document icon, which is used as the default if an icon isn't found. */
431 hSm = (HICON)LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_DOCUMENT),
432 IMAGE_ICON, cx_small, cy_small, LR_SHARED);
433 hLg = (HICON)LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(IDI_SHELL_DOCUMENT),
434 IMAGE_ICON, cx_large, cy_large, LR_SHARED);
435
436 if (!hSm || !hLg)
437 {
438 FIXME("Failed to load IDI_SHELL_DOCUMENT icon!\n");
439 return FALSE;
440 }
441
442 SIC_IconAppend (swShell32Name, IDI_SHELL_DOCUMENT-1, hSm, hLg, 0);
443 SIC_IconAppend (swShell32Name, -IDI_SHELL_DOCUMENT, hSm, hLg, 0);
444
445 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
446
447 return TRUE;
448 }
449 /*************************************************************************
450 * SIC_Destroy
451 *
452 * frees the cache
453 */
454 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
455 {
456 HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
457 SHFree(ptr);
458 return TRUE;
459 }
460
461 void SIC_Destroy(void)
462 {
463 TRACE("\n");
464
465 EnterCriticalSection(&SHELL32_SicCS);
466
467 if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
468
469 sic_hdpa = NULL;
470 ImageList_Destroy(ShellSmallIconList);
471 ShellSmallIconList = 0;
472 ImageList_Destroy(ShellBigIconList);
473 ShellBigIconList = 0;
474
475 LeaveCriticalSection(&SHELL32_SicCS);
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 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 = 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, 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 * Shell_GetCachedImageIndex [SHELL32.72]
634 *
635 */
636 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
637 {
638 INT ret, len;
639 LPWSTR szTemp;
640
641 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
642
643 len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
644 szTemp = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
645 MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
646
647 ret = SIC_GetIconIndex( szTemp, nIndex, 0 );
648
649 HeapFree( GetProcessHeap(), 0, szTemp );
650
651 return ret;
652 }
653
654 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
655 {
656 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
657
658 return SIC_GetIconIndex(szPath, nIndex, 0);
659 }
660
661 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
662 { if( SHELL_OsIsUnicode())
663 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
664 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
665 }
666
667 /*************************************************************************
668 * ExtractIconExW [SHELL32.@]
669 * RETURNS
670 * 0 no icon found
671 * -1 file is not valid
672 * or number of icons extracted
673 */
674 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
675 {
676 /* get entry point of undocumented function PrivateExtractIconExW() in user32 */
677 #if defined(__CYGWIN__) || defined (__MINGW32__) || defined(_MSC_VER)
678 static UINT (WINAPI*PrivateExtractIconExW)(LPCWSTR,int,HICON*,HICON*,UINT) = NULL;
679
680 if (!PrivateExtractIconExW) {
681 HMODULE hUser32 = GetModuleHandleA("user32");
682 PrivateExtractIconExW = (UINT(WINAPI*)(LPCWSTR,int,HICON*,HICON*,UINT)) GetProcAddress(hUser32, "PrivateExtractIconExW");
683
684 if (!PrivateExtractIconExW)
685 return 0;
686 }
687 #endif
688
689 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
690
691 return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
692 }
693
694 /*************************************************************************
695 * ExtractIconExA [SHELL32.@]
696 */
697 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
698 {
699 UINT ret = 0;
700 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
701 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
702
703 TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
704
705 if (lpwstrFile)
706 {
707 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
708 ret = ExtractIconExW(lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
709 HeapFree(GetProcessHeap(), 0, lpwstrFile);
710 }
711 return ret;
712 }
713
714 /*************************************************************************
715 * ExtractAssociatedIconA (SHELL32.@)
716 *
717 * Return icon for given file (either from file itself or from associated
718 * executable) and patch parameters if needed.
719 */
720 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
721 {
722 HICON hIcon = NULL;
723 INT len = MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, NULL, 0);
724 /* Note that we need to allocate MAX_PATH, since we are supposed to fill
725 * the correct executable if there is no icon in lpIconPath directly.
726 * lpIconPath itself is supposed to be large enough, so make sure lpIconPathW
727 * is large enough too. Yes, I am puking too.
728 */
729 LPWSTR lpIconPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
730
731 TRACE("%p %s %p\n", hInst, debugstr_a(lpIconPath), lpiIcon);
732
733 if (lpIconPathW)
734 {
735 MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, lpIconPathW, len);
736 hIcon = ExtractAssociatedIconW(hInst, lpIconPathW, lpiIcon);
737 WideCharToMultiByte(CP_ACP, 0, lpIconPathW, -1, lpIconPath, MAX_PATH , NULL, NULL);
738 HeapFree(GetProcessHeap(), 0, lpIconPathW);
739 }
740 return hIcon;
741 }
742
743 /*************************************************************************
744 * ExtractAssociatedIconW (SHELL32.@)
745 *
746 * Return icon for given file (either from file itself or from associated
747 * executable) and patch parameters if needed.
748 */
749 HICON WINAPI ExtractAssociatedIconW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIcon)
750 {
751 HICON hIcon = NULL;
752 WORD wDummyIcon = 0;
753
754 TRACE("%p %s %p\n", hInst, debugstr_w(lpIconPath), lpiIcon);
755
756 if(lpiIcon == NULL)
757 lpiIcon = &wDummyIcon;
758
759 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
760
761 if( hIcon < (HICON)2 )
762 { if( hIcon == (HICON)1 ) /* no icons found in given file */
763 { WCHAR tempPath[MAX_PATH];
764 HINSTANCE uRet = FindExecutableW(lpIconPath,NULL,tempPath);
765
766 if( uRet > (HINSTANCE)32 && tempPath[0] )
767 { lstrcpyW(lpIconPath,tempPath);
768 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
769 if( hIcon > (HICON)2 )
770 return hIcon;
771 }
772 }
773
774 if( hIcon == (HICON)1 )
775 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
776 else
777 *lpiIcon = 6; /* generic icon - found nothing */
778
779 if (GetModuleFileNameW(hInst, lpIconPath, MAX_PATH))
780 hIcon = LoadIconW(hInst, MAKEINTRESOURCEW(*lpiIcon));
781 }
782 return hIcon;
783 }
784
785 /*************************************************************************
786 * ExtractAssociatedIconExW (SHELL32.@)
787 *
788 * Return icon for given file (either from file itself or from associated
789 * executable) and patch parameters if needed.
790 */
791 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
792 {
793 FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
794 return 0;
795 }
796
797 /*************************************************************************
798 * ExtractAssociatedIconExA (SHELL32.@)
799 *
800 * Return icon for given file (either from file itself or from associated
801 * executable) and patch parameters if needed.
802 */
803 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
804 {
805 HICON ret;
806 INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
807 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
808
809 TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
810
811 MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
812 ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
813 HeapFree(GetProcessHeap(), 0, lpwstrFile);
814 return ret;
815 }
816
817
818 /****************************************************************************
819 * SHDefExtractIconW [SHELL32.@]
820 */
821 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
822 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
823 {
824 UINT ret;
825 HICON hIcons[2];
826 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
827
828 ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
829 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
830 if (ret == 0xFFFFFFFF)
831 return E_FAIL;
832 if (ret > 0) {
833 *phiconLarge = hIcons[0];
834 *phiconSmall = hIcons[1];
835 return S_OK;
836 }
837 return S_FALSE;
838 }
839
840 /****************************************************************************
841 * SHDefExtractIconA [SHELL32.@]
842 */
843 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
844 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
845 {
846 HRESULT ret;
847 INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
848 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
849
850 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
851
852 MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
853 ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
854 HeapFree(GetProcessHeap(), 0, lpwstrFile);
855 return ret;
856 }