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