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