Removed W->A from DEFWND_ImmIsUIMessageW.
[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 "shlwapi.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, { 0, (DWORD)(__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 {       TRACE("%p %p %8lx\n", p1, p2, lparam);
82
83         if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
84           return 1;
85
86         if (strcmpiW(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
87           return 1;
88
89         return 0;
90 }
91 /*****************************************************************************
92  * SIC_IconAppend                       [internal]
93  *
94  * NOTES
95  *  appends an icon pair to the end of the cache
96  */
97 static INT SIC_IconAppend (LPCWSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
98 {       LPSIC_ENTRY lpsice;
99         INT ret, index, index1;
100         WCHAR path[MAX_PATH];
101         TRACE("%s %i %p %p\n", debugstr_w(sSourceFile), dwSourceIndex, hSmallIcon ,hBigIcon);
102
103         lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
104
105        GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
106         lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, (strlenW(path)+1)*sizeof(WCHAR) );
107         strcpyW( lpsice->sSourceFile, path );
108
109         lpsice->dwSourceIndex = dwSourceIndex;
110
111         EnterCriticalSection(&SHELL32_SicCS);
112
113         index = DPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
114         if ( INVALID_INDEX == index )
115         {
116           HeapFree(GetProcessHeap(), 0, lpsice->sSourceFile);
117           SHFree(lpsice);
118           ret = INVALID_INDEX;
119         }
120         else
121         {
122           index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
123           index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
124
125           if (index!=index1)
126           {
127             FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
128           }
129           lpsice->dwListIndex = index;
130           ret = lpsice->dwListIndex;
131         }
132
133         LeaveCriticalSection(&SHELL32_SicCS);
134         return ret;
135 }
136 /****************************************************************************
137  * SIC_LoadIcon                         [internal]
138  *
139  * NOTES
140  *  gets small/big icon by number from a file
141  */
142 static INT SIC_LoadIcon (LPCWSTR sSourceFile, INT dwSourceIndex)
143 {       HICON   hiconLarge=0;
144         HICON   hiconSmall=0;
145
146         PrivateExtractIconsW( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 );
147         PrivateExtractIconsW( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
148
149         if ( !hiconLarge ||  !hiconSmall)
150         {
151           WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, debugstr_w(sSourceFile), hiconLarge, hiconSmall);
152           return -1;
153         }
154         return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
155 }
156 /*****************************************************************************
157  * SIC_GetIconIndex                     [internal]
158  *
159  * Parameters
160  *      sSourceFile     [IN]    filename of file containing the icon
161  *      index           [IN]    index/resID (negated) in this file
162  *
163  * NOTES
164  *  look in the cache for a proper icon. if not available the icon is taken
165  *  from the file and cached
166  */
167 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex )
168 {
169         SIC_ENTRY sice;
170         INT ret, index = INVALID_INDEX;
171         WCHAR path[MAX_PATH];
172
173         TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
174
175         GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
176         sice.sSourceFile = path;
177         sice.dwSourceIndex = dwSourceIndex;
178
179         EnterCriticalSection(&SHELL32_SicCS);
180
181         if (NULL != DPA_GetPtr (sic_hdpa, 0))
182         {
183           /* search linear from position 0*/
184           index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
185         }
186
187         if ( INVALID_INDEX == index )
188         {
189           ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
190         }
191         else
192         {
193           TRACE("-- found\n");
194           ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
195         }
196
197         LeaveCriticalSection(&SHELL32_SicCS);
198         return ret;
199 }
200 /*****************************************************************************
201  * SIC_Initialize                       [internal]
202  *
203  * NOTES
204  *  hack to load the resources from the shell32.dll under a different dll name
205  *  will be removed when the resource-compiler is ready
206  */
207 BOOL SIC_Initialize(void)
208 {
209         HICON           hSm, hLg;
210         UINT            index;
211         int             cx_small, cy_small;
212         int             cx_large, cy_large;
213
214         cx_small = GetSystemMetrics(SM_CXSMICON);
215         cy_small = GetSystemMetrics(SM_CYSMICON);
216         cx_large = GetSystemMetrics(SM_CXICON);
217         cy_large = GetSystemMetrics(SM_CYICON);
218
219         TRACE("\n");
220
221         if (sic_hdpa)   /* already initialized?*/
222           return TRUE;
223
224         sic_hdpa = DPA_Create(16);
225
226         if (!sic_hdpa)
227         {
228           return(FALSE);
229         }
230
231        ShellSmallIconList = ImageList_Create(16,16,ILC_COLOR32|ILC_MASK,0,0x20);
232        ShellBigIconList = ImageList_Create(32,32,ILC_COLOR32|ILC_MASK,0,0x20);
233
234        ImageList_SetBkColor(ShellSmallIconList, CLR_NONE);
235        ImageList_SetBkColor(ShellBigIconList, CLR_NONE);
236
237         for (index=1; index<39; index++)
238         {
239           hSm = (HICON)LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, cx_small, cy_small, LR_SHARED);
240           hLg = (HICON)LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, cx_large, cy_large, LR_SHARED);
241
242           if(!hSm)
243           {
244             hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(1), IMAGE_ICON, cx_small, cy_small, LR_SHARED);
245             hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(1), IMAGE_ICON, cx_large, cy_large, LR_SHARED);
246           }
247          SIC_IconAppend (swShell32Name, index, hSm, hLg);
248         }
249
250         TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
251
252         return TRUE;
253 }
254 /*************************************************************************
255  * SIC_Destroy
256  *
257  * frees the cache
258  */
259 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
260 {
261         HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
262         SHFree(ptr);
263         return TRUE;
264 }
265
266 void SIC_Destroy(void)
267 {
268         TRACE("\n");
269
270         EnterCriticalSection(&SHELL32_SicCS);
271
272         if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
273
274         sic_hdpa = NULL;
275         ImageList_Destroy(ShellSmallIconList);
276         ShellSmallIconList = 0;
277         ImageList_Destroy(ShellBigIconList);
278         ShellBigIconList = 0;
279
280         LeaveCriticalSection(&SHELL32_SicCS);
281         DeleteCriticalSection(&SHELL32_SicCS);
282 }
283
284 /*************************************************************************
285  * Shell_GetImageList                   [SHELL32.71]
286  *
287  * PARAMETERS
288  *  imglist[1|2] [OUT] pointer which receives imagelist handles
289  *
290  */
291 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
292 {       TRACE("(%p,%p)\n",lpBigList,lpSmallList);
293         if (lpBigList)
294         { *lpBigList = ShellBigIconList;
295         }
296         if (lpSmallList)
297         { *lpSmallList = ShellSmallIconList;
298         }
299
300         return TRUE;
301 }
302 /*************************************************************************
303  * PidlToSicIndex                       [INTERNAL]
304  *
305  * PARAMETERS
306  *      sh      [IN]    IShellFolder
307  *      pidl    [IN]
308  *      bBigIcon [IN]
309  *      uFlags  [IN]    GIL_*
310  *      pIndex  [OUT]   index within the SIC
311  *
312  */
313 BOOL PidlToSicIndex (
314         IShellFolder * sh,
315         LPCITEMIDLIST pidl,
316         BOOL bBigIcon,
317         UINT uFlags,
318         int * pIndex)
319 {
320         IExtractIconW   *ei;
321         WCHAR           szIconFile[MAX_PATH];   /* file containing the icon */
322         INT             iSourceIndex;           /* index or resID(negated) in this file */
323         BOOL            ret = FALSE;
324         UINT            dwFlags = 0;
325
326         TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
327
328         if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconW, 0, (void **)&ei)))
329         {
330           if (SUCCEEDED(IExtractIconW_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
331           {
332             *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
333             ret = TRUE;
334           }
335           IExtractIconW_Release(ei);
336         }
337
338         if (INVALID_INDEX == *pIndex)   /* default icon when failed */
339           *pIndex = 0;
340
341         return ret;
342
343 }
344
345 /*************************************************************************
346  * SHMapPIDLToSystemImageListIndex      [SHELL32.77]
347  *
348  * PARAMETERS
349  *      sh      [IN]            pointer to an instance of IShellFolder
350  *      pidl    [IN]
351  *      pIndex  [OUT][OPTIONAL] SIC index for big icon
352  *
353  */
354 int WINAPI SHMapPIDLToSystemImageListIndex(
355         IShellFolder *sh,
356         LPCITEMIDLIST pidl,
357         int *pIndex)
358 {
359         int Index;
360
361         TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
362         pdump(pidl);
363
364         if (pIndex)
365           PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
366         PidlToSicIndex ( sh, pidl, 0, 0, &Index);
367         return Index;
368 }
369
370 /*************************************************************************
371  * Shell_GetCachedImageIndex            [SHELL32.72]
372  *
373  */
374 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
375 {
376         INT ret, len;
377         LPWSTR szTemp;
378
379         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
380
381         len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
382         szTemp = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
383         MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
384
385         ret = SIC_GetIconIndex( szTemp, nIndex );
386
387         HeapFree( GetProcessHeap(), 0, szTemp );
388
389         return ret;
390 }
391
392 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
393 {
394         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
395
396         return SIC_GetIconIndex(szPath, nIndex);
397 }
398
399 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
400 {       if( SHELL_OsIsUnicode())
401           return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
402         return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
403 }
404
405 /*************************************************************************
406  * ExtractIconEx                        [SHELL32.@]
407  */
408 UINT WINAPI ExtractIconExAW(LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
409 {       if (SHELL_OsIsUnicode())
410           return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
411         return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
412 }
413
414 /*************************************************************************
415  * ExtractIconExW                       [SHELL32.@]
416  * RETURNS
417  *  0 no icon found
418  *  -1 file is not valid
419  *  or number of icons extracted
420  */
421 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
422 {
423         TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
424
425         return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
426 }
427
428 /*************************************************************************
429  * ExtractIconExA                       [SHELL32.@]
430  */
431 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
432 {
433     UINT ret;
434     INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
435     LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
436
437     TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
438
439     MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
440     ret = ExtractIconExW (lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
441     HeapFree(GetProcessHeap(), 0, lpwstrFile);
442     return ret;
443 }
444
445 /*************************************************************************
446  *                              ExtractAssociatedIconA (SHELL32.@)
447  *
448  * Return icon for given file (either from file itself or from associated
449  * executable) and patch parameters if needed.
450  */
451 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
452 {       
453         HICON hIcon;
454         WORD wDummyIcon = 0;
455         
456         TRACE("\n");
457
458         if(lpiIcon == NULL)
459             lpiIcon = &wDummyIcon;
460
461         hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
462
463         if( hIcon < (HICON)2 )
464         { if( hIcon == (HICON)1 ) /* no icons found in given file */
465           { char  tempPath[0x80];
466             HINSTANCE uRet = FindExecutableA(lpIconPath,NULL,tempPath);
467
468             if( uRet > (HINSTANCE)32 && tempPath[0] )
469             { strcpy(lpIconPath,tempPath);
470               hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
471               if( hIcon > (HICON)2 )
472                 return hIcon;
473             }
474             else hIcon = 0;
475           }
476
477           if( hIcon == (HICON)1 )
478             *lpiIcon = 2;   /* MSDOS icon - we found .exe but no icons in it */
479           else
480             *lpiIcon = 6;   /* generic icon - found nothing */
481
482           if (GetModuleFileNameA(hInst, lpIconPath, 0x80))
483           {
484               /* terminate string (GetModuleFileName doesn't do if buffer is too small) */
485               lpIconPath[0x80 - 1] = '\0';
486               hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
487           }
488         }
489         return hIcon;
490 }
491
492 /*************************************************************************
493  *                              ExtractAssociatedIconExW (SHELL32.@)
494  *
495  * Return icon for given file (either from file itself or from associated
496  * executable) and patch parameters if needed.
497  */
498 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
499 {
500   FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
501   return 0;
502 }
503
504 /*************************************************************************
505  *                              ExtractAssociatedIconExA (SHELL32.@)
506  *
507  * Return icon for given file (either from file itself or from associated
508  * executable) and patch parameters if needed.
509  */
510 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
511 {
512   HICON ret;
513   INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
514   LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
515
516   TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
517
518   MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
519   ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
520   HeapFree(GetProcessHeap(), 0, lpwstrFile);
521   return ret;
522 }
523
524
525 /****************************************************************************
526  * SHDefExtractIconW            [SHELL32.@]
527  */
528 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
529                                  HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
530 {
531         UINT ret;
532         HICON hIcons[2];
533         WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
534
535         ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
536         /* FIXME: deal with uFlags parameter which contains GIL_ flags */
537         if (ret == 0xFFFFFFFF)
538           return E_FAIL;
539         if (ret > 0) {
540           *phiconLarge = hIcons[0];
541           *phiconSmall = hIcons[1];
542           return S_OK;
543         }
544         return S_FALSE;
545 }
546
547 /****************************************************************************
548  * SHDefExtractIconA            [SHELL32.@]
549  */
550 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
551                                  HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
552 {
553   HRESULT ret;
554   INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
555   LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
556
557   TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
558
559   MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
560   ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
561   HeapFree(GetProcessHeap(), 0, lpwstrFile);
562   return ret;
563 }