Add an assert if both arguments to DD_STRUCT_COPY_BYSIZE are equal.
[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 /*****************************************************************************
100  * SIC_OverlayShortcutImage                     [internal]
101  *
102  * NOTES
103  *  Creates a new icon as a copy of the passed-in icon, overlayed with a
104  *  shortcut image. 
105  */
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;
117
118         /* Get information about the source icon and shortcut overlay */
119         if (! GetIconInfo(SourceIcon, &SourceIconInfo)
120             || 0 == GetObjectW(SourceIconInfo.hbmColor, sizeof(BITMAP), &SourceBitmapInfo))
121         {
122           return NULL;
123         }
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))
130         {
131           return NULL;
132         }
133
134         TargetIconInfo = SourceIconInfo;
135         TargetIconInfo.hbmMask = NULL;
136         TargetIconInfo.hbmColor = NULL;
137
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;
143
144         ShortcutDC = CreateCompatibleDC(NULL);
145         if (NULL == ShortcutDC) goto fail;
146         OldShortcutBitmap = SelectObject(ShortcutDC, ShortcutIconInfo.hbmMask);
147         if (NULL == OldShortcutBitmap) goto fail;
148
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;
162
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))
169         {
170           goto fail;
171         }
172
173         /* Setup the source and target xor bitmap */
174         if (NULL == SelectObject(SourceDC, SourceIconInfo.hbmColor) ||
175             NULL == SelectObject(TargetDC, TargetIconInfo.hbmColor))
176         {
177           goto fail;
178         }
179
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))
187         {
188           goto fail;
189         }
190
191         if (NULL == SelectObject(ShortcutDC, ShortcutIconInfo.hbmColor)) goto fail;
192
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))
197         {
198           goto fail;
199         }
200
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);
209
210         /* Create the icon using the bitmaps prepared earlier */
211         TargetIcon = CreateIconIndirect(&TargetIconInfo);
212
213         /* CreateIconIndirect copies the bitmaps, so we can release our bitmaps now */
214         DeleteObject(TargetIconInfo.hbmColor);
215         DeleteObject(TargetIconInfo.hbmMask);
216
217         return TargetIcon;
218
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);
229
230         return NULL;
231 }
232
233 /*****************************************************************************
234  * SIC_IconAppend                       [internal]
235  *
236  * NOTES
237  *  appends an icon pair to the end of the cache
238  */
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);
244
245         lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
246
247         GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
248         lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, (strlenW(path)+1)*sizeof(WCHAR) );
249         strcpyW( lpsice->sSourceFile, path );
250
251         lpsice->dwSourceIndex = dwSourceIndex;
252         lpsice->dwFlags = dwFlags;
253
254         EnterCriticalSection(&SHELL32_SicCS);
255
256         index = DPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
257         if ( INVALID_INDEX == index )
258         {
259           HeapFree(GetProcessHeap(), 0, lpsice->sSourceFile);
260           SHFree(lpsice);
261           ret = INVALID_INDEX;
262         }
263         else
264         {
265           index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
266           index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
267
268           if (index!=index1)
269           {
270             FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
271           }
272           lpsice->dwListIndex = index;
273           ret = lpsice->dwListIndex;
274         }
275
276         LeaveCriticalSection(&SHELL32_SicCS);
277         return ret;
278 }
279 /****************************************************************************
280  * SIC_LoadIcon                         [internal]
281  *
282  * NOTES
283  *  gets small/big icon by number from a file
284  */
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;
290
291         PrivateExtractIconsW( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 );
292         PrivateExtractIconsW( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
293
294         if ( !hiconLarge ||  !hiconSmall)
295         {
296           WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, debugstr_w(sSourceFile), hiconLarge, hiconSmall);
297           return -1;
298         }
299
300         if (0 != (dwFlags & GIL_FORSHORTCUT))
301         {
302           hiconLargeShortcut = SIC_OverlayShortcutImage(hiconLarge);
303           hiconSmallShortcut = SIC_OverlayShortcutImage(hiconSmall);
304           if (NULL != hiconLargeShortcut && NULL != hiconSmallShortcut)
305           {
306             hiconLarge = hiconLargeShortcut;
307             hiconSmall = hiconSmallShortcut;
308           }
309           else
310           {
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;
315           }
316         }
317
318         return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge, dwFlags);
319 }
320 /*****************************************************************************
321  * SIC_GetIconIndex                     [internal]
322  *
323  * Parameters
324  *      sSourceFile     [IN]    filename of file containing the icon
325  *      index           [IN]    index/resID (negated) in this file
326  *
327  * NOTES
328  *  look in the cache for a proper icon. if not available the icon is taken
329  *  from the file and cached
330  */
331 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags )
332 {
333         SIC_ENTRY sice;
334         INT ret, index = INVALID_INDEX;
335         WCHAR path[MAX_PATH];
336
337         TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
338
339         GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
340         sice.sSourceFile = path;
341         sice.dwSourceIndex = dwSourceIndex;
342         sice.dwFlags = dwFlags;
343
344         EnterCriticalSection(&SHELL32_SicCS);
345
346         if (NULL != DPA_GetPtr (sic_hdpa, 0))
347         {
348           /* search linear from position 0*/
349           index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
350         }
351
352         if ( INVALID_INDEX == index )
353         {
354           if (0 == strcmpiW (sSourceFile, swShell32Name) &&
355               -IDI_SHELL_MY_DOCUMENTS <= dwSourceIndex &&
356               dwSourceIndex < IDI_SHELL_MY_DOCUMENTS)
357           {
358             /* Return default icon for icons not present in shell32 */
359             ret = 0;
360           }
361           else
362           {
363             ret = SIC_LoadIcon (sSourceFile, dwSourceIndex, dwFlags);
364           }
365         }
366         else
367         {
368           TRACE("-- found\n");
369           ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
370         }
371
372         LeaveCriticalSection(&SHELL32_SicCS);
373         return ret;
374 }
375 /*****************************************************************************
376  * SIC_Initialize                       [internal]
377  *
378  * NOTES
379  *  hack to load the resources from the shell32.dll under a different dll name
380  *  will be removed when the resource-compiler is ready
381  */
382 BOOL SIC_Initialize(void)
383 {
384         HICON           hSm, hLg;
385         UINT            index;
386         int             cx_small, cy_small;
387         int             cx_large, cy_large;
388
389         cx_small = GetSystemMetrics(SM_CXSMICON);
390         cy_small = GetSystemMetrics(SM_CYSMICON);
391         cx_large = GetSystemMetrics(SM_CXICON);
392         cy_large = GetSystemMetrics(SM_CYICON);
393
394         TRACE("\n");
395
396         if (sic_hdpa)   /* already initialized?*/
397           return TRUE;
398
399         sic_hdpa = DPA_Create(16);
400
401         if (!sic_hdpa)
402         {
403           return(FALSE);
404         }
405
406        ShellSmallIconList = ImageList_Create(16,16,ILC_COLOR32|ILC_MASK,0,0x20);
407        ShellBigIconList = ImageList_Create(32,32,ILC_COLOR32|ILC_MASK,0,0x20);
408
409        ImageList_SetBkColor(ShellSmallIconList, CLR_NONE);
410        ImageList_SetBkColor(ShellBigIconList, CLR_NONE);
411
412         for (index=1; index<=IDI_SHELL_MY_DOCUMENTS; index++)
413         {
414           hSm = (HICON)LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, cx_small, cy_small, LR_SHARED);
415           hLg = (HICON)LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, cx_large, cy_large, LR_SHARED);
416
417           if(hSm)
418           {
419             SIC_IconAppend (swShell32Name, index - 1, hSm, hLg, 0);
420             SIC_IconAppend (swShell32Name, -index, hSm, hLg, 0);
421           }
422         }
423
424         TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
425
426         return TRUE;
427 }
428 /*************************************************************************
429  * SIC_Destroy
430  *
431  * frees the cache
432  */
433 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
434 {
435         HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
436         SHFree(ptr);
437         return TRUE;
438 }
439
440 void SIC_Destroy(void)
441 {
442         TRACE("\n");
443
444         EnterCriticalSection(&SHELL32_SicCS);
445
446         if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
447
448         sic_hdpa = NULL;
449         ImageList_Destroy(ShellSmallIconList);
450         ShellSmallIconList = 0;
451         ImageList_Destroy(ShellBigIconList);
452         ShellBigIconList = 0;
453
454         LeaveCriticalSection(&SHELL32_SicCS);
455         DeleteCriticalSection(&SHELL32_SicCS);
456 }
457
458 /*************************************************************************
459  * Shell_GetImageList                   [SHELL32.71]
460  *
461  * PARAMETERS
462  *  imglist[1|2] [OUT] pointer which receives imagelist handles
463  *
464  */
465 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
466 {       TRACE("(%p,%p)\n",lpBigList,lpSmallList);
467         if (lpBigList)
468         { *lpBigList = ShellBigIconList;
469         }
470         if (lpSmallList)
471         { *lpSmallList = ShellSmallIconList;
472         }
473
474         return TRUE;
475 }
476 /*************************************************************************
477  * PidlToSicIndex                       [INTERNAL]
478  *
479  * PARAMETERS
480  *      sh      [IN]    IShellFolder
481  *      pidl    [IN]
482  *      bBigIcon [IN]
483  *      uFlags  [IN]    GIL_*
484  *      pIndex  [OUT]   index within the SIC
485  *
486  */
487 BOOL PidlToSicIndex (
488         IShellFolder * sh,
489         LPCITEMIDLIST pidl,
490         BOOL bBigIcon,
491         UINT uFlags,
492         int * pIndex)
493 {
494         IExtractIconW   *ei;
495         WCHAR           szIconFile[MAX_PATH];   /* file containing the icon */
496         char            szTemp[MAX_PATH];
497         INT             iSourceIndex;           /* index or resID(negated) in this file */
498         BOOL            ret = FALSE;
499         UINT            dwFlags = 0;
500         HKEY            keyCls;
501         int             iShortcutDefaultIndex = INVALID_INDEX;
502
503         TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
504
505         if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconW, 0, (void **)&ei)))
506         {
507           if (_ILGetExtension(pidl, szTemp, MAX_PATH) &&
508               HCR_MapTypeToValueA(szTemp, szTemp, MAX_PATH, TRUE))
509           {
510             if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_CLASSES_ROOT, szTemp, 0, KEY_QUERY_VALUE, &keyCls))
511             {
512               if (ERROR_SUCCESS == RegQueryValueExA(keyCls, "IsShortcut", NULL, NULL, NULL, NULL))
513               {
514                 uFlags |= GIL_FORSHORTCUT;
515               }
516               RegCloseKey(keyCls);
517             }
518           }
519           if (SUCCEEDED(IExtractIconW_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
520           {
521             *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex, uFlags);
522             ret = TRUE;
523           }
524           IExtractIconW_Release(ei);
525         }
526
527         if (INVALID_INDEX == *pIndex)   /* default icon when failed */
528         {
529           if (0 == (uFlags & GIL_FORSHORTCUT))
530           {
531             *pIndex = 0;
532           }
533           else
534           {
535             if (INVALID_INDEX == iShortcutDefaultIndex)
536             {
537               iShortcutDefaultIndex = SIC_LoadIcon(swShell32Name, 0, GIL_FORSHORTCUT);
538             }
539             *pIndex = (INVALID_INDEX != iShortcutDefaultIndex ? iShortcutDefaultIndex : 0);
540           }
541         }
542
543         return ret;
544
545 }
546
547 /*************************************************************************
548  * SHMapPIDLToSystemImageListIndex      [SHELL32.77]
549  *
550  * PARAMETERS
551  *      sh      [IN]            pointer to an instance of IShellFolder
552  *      pidl    [IN]
553  *      pIndex  [OUT][OPTIONAL] SIC index for big icon
554  *
555  */
556 int WINAPI SHMapPIDLToSystemImageListIndex(
557         IShellFolder *sh,
558         LPCITEMIDLIST pidl,
559         int *pIndex)
560 {
561         int Index;
562
563         TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
564         pdump(pidl);
565
566         if (pIndex)
567           PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
568         PidlToSicIndex ( sh, pidl, 0, 0, &Index);
569         return Index;
570 }
571
572 /*************************************************************************
573  * Shell_GetCachedImageIndex            [SHELL32.72]
574  *
575  */
576 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
577 {
578         INT ret, len;
579         LPWSTR szTemp;
580
581         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
582
583         len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
584         szTemp = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
585         MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
586
587         ret = SIC_GetIconIndex( szTemp, nIndex, 0 );
588
589         HeapFree( GetProcessHeap(), 0, szTemp );
590
591         return ret;
592 }
593
594 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
595 {
596         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
597
598         return SIC_GetIconIndex(szPath, nIndex, 0);
599 }
600
601 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
602 {       if( SHELL_OsIsUnicode())
603           return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
604         return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
605 }
606
607 /*************************************************************************
608  * ExtractIconExW                       [SHELL32.@]
609  * RETURNS
610  *  0 no icon found
611  *  -1 file is not valid
612  *  or number of icons extracted
613  */
614 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
615 {
616         TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
617
618         return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
619 }
620
621 /*************************************************************************
622  * ExtractIconExA                       [SHELL32.@]
623  */
624 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
625 {
626     UINT ret = 0;
627     INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
628     LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
629
630     TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
631
632     if (lpwstrFile)
633     {
634         MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
635         ret = ExtractIconExW(lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
636         HeapFree(GetProcessHeap(), 0, lpwstrFile);
637     }
638     return ret;
639 }
640
641 /*************************************************************************
642  *                              ExtractAssociatedIconA (SHELL32.@)
643  *
644  * Return icon for given file (either from file itself or from associated
645  * executable) and patch parameters if needed.
646  */
647 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
648 {       
649     HICON hIcon = NULL;
650     INT len = MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, NULL, 0);
651     /* Note that we need to allocate MAX_PATH, since we are supposed to fill
652      * the correct executable if there is no icon in lpIconPath directly.
653      * lpIconPath itself is supposed to be large enough, so make sure lpIconPathW
654      * is large enough too. Yes, I am puking too.
655      */
656     LPWSTR lpIconPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
657
658     TRACE("%p %s %p\n", hInst, debugstr_a(lpIconPath), lpiIcon);
659
660     if (lpIconPathW)
661     {
662         MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, lpIconPathW, len);
663         hIcon = ExtractAssociatedIconW(hInst, lpIconPathW, lpiIcon);
664         WideCharToMultiByte(CP_ACP, 0, lpIconPathW, -1, lpIconPath, MAX_PATH , NULL, NULL);
665         HeapFree(GetProcessHeap(), 0, lpIconPathW);
666     }
667     return hIcon;
668 }
669
670 /*************************************************************************
671  *                              ExtractAssociatedIconW (SHELL32.@)
672  *
673  * Return icon for given file (either from file itself or from associated
674  * executable) and patch parameters if needed.
675  */
676 HICON WINAPI ExtractAssociatedIconW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIcon)
677 {
678     HICON hIcon = NULL;
679     WORD wDummyIcon = 0;
680
681     TRACE("%p %s %p\n", hInst, debugstr_w(lpIconPath), lpiIcon);
682
683     if(lpiIcon == NULL)
684         lpiIcon = &wDummyIcon;
685
686     hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
687
688     if( hIcon < (HICON)2 )
689     { if( hIcon == (HICON)1 ) /* no icons found in given file */
690       { WCHAR tempPath[MAX_PATH];
691         HINSTANCE uRet = FindExecutableW(lpIconPath,NULL,tempPath);
692
693         if( uRet > (HINSTANCE)32 && tempPath[0] )
694         { lstrcpyW(lpIconPath,tempPath);
695           hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
696           if( hIcon > (HICON)2 )
697             return hIcon;
698         }
699       }
700
701       if( hIcon == (HICON)1 )
702         *lpiIcon = 2;   /* MSDOS icon - we found .exe but no icons in it */
703       else
704         *lpiIcon = 6;   /* generic icon - found nothing */
705
706       if (GetModuleFileNameW(hInst, lpIconPath, MAX_PATH))
707         hIcon = LoadIconW(hInst, MAKEINTRESOURCEW(*lpiIcon));
708     }
709     return hIcon;
710 }
711
712 /*************************************************************************
713  *                              ExtractAssociatedIconExW (SHELL32.@)
714  *
715  * Return icon for given file (either from file itself or from associated
716  * executable) and patch parameters if needed.
717  */
718 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
719 {
720   FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
721   return 0;
722 }
723
724 /*************************************************************************
725  *                              ExtractAssociatedIconExA (SHELL32.@)
726  *
727  * Return icon for given file (either from file itself or from associated
728  * executable) and patch parameters if needed.
729  */
730 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
731 {
732   HICON ret;
733   INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
734   LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
735
736   TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
737
738   MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
739   ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
740   HeapFree(GetProcessHeap(), 0, lpwstrFile);
741   return ret;
742 }
743
744
745 /****************************************************************************
746  * SHDefExtractIconW            [SHELL32.@]
747  */
748 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
749                                  HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
750 {
751         UINT ret;
752         HICON hIcons[2];
753         WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
754
755         ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
756         /* FIXME: deal with uFlags parameter which contains GIL_ flags */
757         if (ret == 0xFFFFFFFF)
758           return E_FAIL;
759         if (ret > 0) {
760           *phiconLarge = hIcons[0];
761           *phiconSmall = hIcons[1];
762           return S_OK;
763         }
764         return S_FALSE;
765 }
766
767 /****************************************************************************
768  * SHDefExtractIconA            [SHELL32.@]
769  */
770 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
771                                  HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
772 {
773   HRESULT ret;
774   INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
775   LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
776
777   TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
778
779   MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
780   ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
781   HeapFree(GetProcessHeap(), 0, lpwstrFile);
782   return ret;
783 }