2934304c3d3284a517aba40aba8f0c29c7ff3a12
[reactos.git] / reactos / dll / win32 / shell32 / iconcache.cpp
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "precomp.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(shell);
24
25 /********************** THE ICON CACHE ********************************/
26
27 #define INVALID_INDEX -1
28
29 typedef struct
30 {
31 LPWSTR sSourceFile; /* file (not path!) containing the icon */
32 DWORD dwSourceIndex; /* index within the file, if it is a resoure ID it will be negated */
33 DWORD dwListIndex; /* index within the iconlist */
34 DWORD dwFlags; /* GIL_* flags */
35 DWORD dwAccessTime;
36 } SIC_ENTRY, * LPSIC_ENTRY;
37
38 static HDPA sic_hdpa = 0;
39
40 static HIMAGELIST ShellSmallIconList;
41 static HIMAGELIST ShellBigIconList;
42
43 namespace
44 {
45 extern CRITICAL_SECTION SHELL32_SicCS;
46 CRITICAL_SECTION_DEBUG critsect_debug =
47 {
48 0, 0, &SHELL32_SicCS,
49 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
50 0, 0, { (DWORD_PTR)(__FILE__ ": SHELL32_SicCS") }
51 };
52 CRITICAL_SECTION SHELL32_SicCS = { &critsect_debug, -1, 0, 0, 0, 0 };
53 }
54
55 /*****************************************************************************
56 * SIC_CompareEntries
57 *
58 * NOTES
59 * Callback for DPA_Search
60 */
61 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
62 { LPSIC_ENTRY e1 = (LPSIC_ENTRY)p1, e2 = (LPSIC_ENTRY)p2;
63
64 TRACE("%p %p %8lx\n", p1, p2, lparam);
65
66 /* Icons in the cache are keyed by the name of the file they are
67 * loaded from, their resource index and the fact if they have a shortcut
68 * icon overlay or not.
69 */
70 /* first the faster one */
71 if (e1->dwSourceIndex != e2->dwSourceIndex)
72 return (e1->dwSourceIndex < e2->dwSourceIndex) ? -1 : 1;
73
74 if ((e1->dwFlags & GIL_FORSHORTCUT) != (e2->dwFlags & GIL_FORSHORTCUT))
75 return ((e1->dwFlags & GIL_FORSHORTCUT) < (e2->dwFlags & GIL_FORSHORTCUT)) ? -1 : 1;
76
77 return wcsicmp(e1->sSourceFile,e2->sSourceFile);
78 }
79
80 /* declare SIC_LoadOverlayIcon() */
81 static int SIC_LoadOverlayIcon(int icon_idx);
82
83 /*****************************************************************************
84 * SIC_OverlayShortcutImage [internal]
85 *
86 * NOTES
87 * Creates a new icon as a copy of the passed-in icon, overlayed with a
88 * shortcut image.
89 * FIXME: This should go to the ImageList implementation!
90 */
91 static HICON SIC_OverlayShortcutImage(HICON SourceIcon, BOOL large)
92 {
93 ICONINFO ShortcutIconInfo, TargetIconInfo;
94 HICON ShortcutIcon = NULL, TargetIcon;
95 BITMAP TargetBitmapInfo, ShortcutBitmapInfo;
96 HDC ShortcutDC = NULL,
97 TargetDC = NULL;
98 HBITMAP OldShortcutBitmap = NULL,
99 OldTargetBitmap = NULL;
100
101 static int s_imgListIdx = -1;
102 ZeroMemory(&ShortcutIconInfo, sizeof(ShortcutIconInfo));
103 ZeroMemory(&TargetIconInfo, sizeof(TargetIconInfo));
104
105 /* Get information about the source icon and shortcut overlay.
106 * We will write over the source bitmaps to get the final ones */
107 if (! GetIconInfo(SourceIcon, &TargetIconInfo))
108 return NULL;
109
110 /* Is it possible with the ImageList implementation? */
111 if(!TargetIconInfo.hbmColor)
112 {
113 /* Maybe we'll support this at some point */
114 FIXME("1bpp icon wants its overlay!\n");
115 goto fail;
116 }
117
118 if(!GetObjectW(TargetIconInfo.hbmColor, sizeof(BITMAP), &TargetBitmapInfo))
119 {
120 goto fail;
121 }
122
123 /* search for the shortcut icon only once */
124 if (s_imgListIdx == -1)
125 s_imgListIdx = SIC_LoadOverlayIcon(- IDI_SHELL_SHORTCUT);
126 /* FIXME should use icon index 29 instead of the
127 resource id, but not all icons are present yet
128 so we can't use icon indices */
129
130 if (s_imgListIdx != -1)
131 {
132 if (large)
133 ShortcutIcon = ImageList_GetIcon(ShellBigIconList, s_imgListIdx, ILD_TRANSPARENT);
134 else
135 ShortcutIcon = ImageList_GetIcon(ShellSmallIconList, s_imgListIdx, ILD_TRANSPARENT);
136 } else
137 ShortcutIcon = NULL;
138
139 if (!ShortcutIcon || !GetIconInfo(ShortcutIcon, &ShortcutIconInfo))
140 {
141 goto fail;
142 }
143
144 /* Is it possible with the ImageLists ? */
145 if(!ShortcutIconInfo.hbmColor)
146 {
147 /* Maybe we'll support this at some point */
148 FIXME("Should draw 1bpp overlay!\n");
149 goto fail;
150 }
151
152 if(!GetObjectW(ShortcutIconInfo.hbmColor, sizeof(BITMAP), &ShortcutBitmapInfo))
153 {
154 goto fail;
155 }
156
157 /* Setup the masks */
158 ShortcutDC = CreateCompatibleDC(NULL);
159 if (NULL == ShortcutDC) goto fail;
160 OldShortcutBitmap = (HBITMAP)SelectObject(ShortcutDC, ShortcutIconInfo.hbmMask);
161 if (NULL == OldShortcutBitmap) goto fail;
162
163 TargetDC = CreateCompatibleDC(NULL);
164 if (NULL == TargetDC) goto fail;
165 OldTargetBitmap = (HBITMAP)SelectObject(TargetDC, TargetIconInfo.hbmMask);
166 if (NULL == OldTargetBitmap) goto fail;
167
168 /* Create the complete mask by ANDing the source and shortcut masks.
169 * NOTE: in an ImageList, all icons have the same dimensions */
170 if (!BitBlt(TargetDC, 0, 0, ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
171 ShortcutDC, 0, 0, SRCAND))
172 {
173 goto fail;
174 }
175
176 /*
177 * We must remove or add the alpha component to the shortcut overlay:
178 * If we don't, SRCCOPY will copy it to our resulting icon, resulting in a
179 * partially transparent icons where it shouldn't be, and to an invisible icon
180 * if the underlying icon don't have any alpha channel information. (16bpp only icon for instance).
181 * But if the underlying icon has alpha channel information, then we must mark the overlay information
182 * as opaque.
183 * NOTE: This code sucks(tm) and should belong to the ImageList implementation.
184 * NOTE2: there are better ways to do this.
185 */
186 if(ShortcutBitmapInfo.bmBitsPixel == 32)
187 {
188 BOOL add_alpha;
189 BYTE buffer[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
190 BITMAPINFO* lpbmi = (BITMAPINFO*)buffer;
191 PVOID bits;
192 PULONG pixel;
193 INT i, j;
194
195 /* Find if the source bitmap has an alpha channel */
196 if(TargetBitmapInfo.bmBitsPixel != 32) add_alpha = FALSE;
197 else
198 {
199 ZeroMemory(buffer, sizeof(buffer));
200 lpbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
201 lpbmi->bmiHeader.biWidth = TargetBitmapInfo.bmWidth;
202 lpbmi->bmiHeader.biHeight = TargetBitmapInfo.bmHeight;
203 lpbmi->bmiHeader.biPlanes = 1;
204 lpbmi->bmiHeader.biBitCount = 32;
205
206 bits = HeapAlloc(GetProcessHeap(), 0, TargetBitmapInfo.bmHeight * TargetBitmapInfo.bmWidthBytes);
207
208 if(!bits) goto fail;
209
210 if(!GetDIBits(TargetDC, TargetIconInfo.hbmColor, 0, TargetBitmapInfo.bmHeight, bits, lpbmi, DIB_RGB_COLORS))
211 {
212 ERR("GetBIBits failed!\n");
213 HeapFree(GetProcessHeap(), 0, bits);
214 goto fail;
215 }
216
217 i = j = 0;
218 pixel = (PULONG)bits;
219
220 for(i=0; i<TargetBitmapInfo.bmHeight; i++)
221 {
222 for(j=0; j<TargetBitmapInfo.bmWidth; j++)
223 {
224 add_alpha = (*pixel++ & 0xFF000000) != 0;
225 if(add_alpha) break;
226 }
227 if(add_alpha) break;
228 }
229 HeapFree(GetProcessHeap(), 0, bits);
230 }
231
232 /* Allocate the bits */
233 bits = HeapAlloc(GetProcessHeap(), 0, ShortcutBitmapInfo.bmHeight*ShortcutBitmapInfo.bmWidthBytes);
234 if(!bits) goto fail;
235
236 ZeroMemory(buffer, sizeof(buffer));
237 lpbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
238 lpbmi->bmiHeader.biWidth = ShortcutBitmapInfo.bmWidth;
239 lpbmi->bmiHeader.biHeight = ShortcutBitmapInfo.bmHeight;
240 lpbmi->bmiHeader.biPlanes = 1;
241 lpbmi->bmiHeader.biBitCount = 32;
242
243 if(!GetDIBits(TargetDC, ShortcutIconInfo.hbmColor, 0, ShortcutBitmapInfo.bmHeight, bits, lpbmi, DIB_RGB_COLORS))
244 {
245 ERR("GetBIBits failed!\n");
246 HeapFree(GetProcessHeap(), 0, bits);
247 goto fail;
248 }
249
250 pixel = (PULONG)bits;
251 /* Remove alpha channel component or make it totally opaque */
252 for(i=0; i<ShortcutBitmapInfo.bmHeight; i++)
253 {
254 for(j=0; j<ShortcutBitmapInfo.bmWidth; j++)
255 {
256 if(add_alpha) *pixel++ |= 0xFF000000;
257 else *pixel++ &= 0x00FFFFFF;
258 }
259 }
260
261 /* GetDIBits return BI_BITFIELDS with masks set to 0, and SetDIBits fails when masks are 0. The irony... */
262 lpbmi->bmiHeader.biCompression = BI_RGB;
263
264 /* Set the bits again */
265 if(!SetDIBits(TargetDC, ShortcutIconInfo.hbmColor, 0, ShortcutBitmapInfo.bmHeight, bits, lpbmi, DIB_RGB_COLORS))
266 {
267 ERR("SetBIBits failed!, %lu\n", GetLastError());
268 HeapFree(GetProcessHeap(), 0, bits);
269 goto fail;
270 }
271 HeapFree(GetProcessHeap(), 0, bits);
272 }
273
274 /* Now do the copy. We overwrite the original icon data */
275 if (NULL == SelectObject(ShortcutDC, ShortcutIconInfo.hbmColor) ||
276 NULL == SelectObject(TargetDC, TargetIconInfo.hbmColor))
277 goto fail;
278 if (!MaskBlt(TargetDC, 0, 0, ShortcutBitmapInfo.bmWidth, ShortcutBitmapInfo.bmHeight,
279 ShortcutDC, 0, 0, ShortcutIconInfo.hbmMask, 0, 0,
280 MAKEROP4(0xAA0000, SRCCOPY)))
281 {
282 goto fail;
283 }
284
285 /* Clean up, we're not goto'ing to 'fail' after this so we can be lazy and not set
286 handles to NULL */
287 SelectObject(TargetDC, OldTargetBitmap);
288 DeleteDC(TargetDC);
289 SelectObject(ShortcutDC, OldShortcutBitmap);
290 DeleteDC(ShortcutDC);
291
292 /* Create the icon using the bitmaps prepared earlier */
293 TargetIcon = CreateIconIndirect(&TargetIconInfo);
294
295 /* CreateIconIndirect copies the bitmaps, so we can release our bitmaps now */
296 DeleteObject(TargetIconInfo.hbmColor);
297 DeleteObject(TargetIconInfo.hbmMask);
298 /* Delete what GetIconInfo gave us */
299 DeleteObject(ShortcutIconInfo.hbmColor);
300 DeleteObject(ShortcutIconInfo.hbmMask);
301 DestroyIcon(ShortcutIcon);
302
303 return TargetIcon;
304
305 fail:
306 /* Clean up scratch resources we created */
307 if (NULL != OldTargetBitmap) SelectObject(TargetDC, OldTargetBitmap);
308 if (NULL != TargetDC) DeleteDC(TargetDC);
309 if (NULL != OldShortcutBitmap) SelectObject(ShortcutDC, OldShortcutBitmap);
310 if (NULL != ShortcutDC) DeleteDC(ShortcutDC);
311 if (NULL != TargetIconInfo.hbmColor) DeleteObject(TargetIconInfo.hbmColor);
312 if (NULL != TargetIconInfo.hbmMask) DeleteObject(TargetIconInfo.hbmMask);
313 if (NULL != ShortcutIconInfo.hbmColor) DeleteObject(ShortcutIconInfo.hbmColor);
314 if (NULL != ShortcutIconInfo.hbmMask) DeleteObject(ShortcutIconInfo.hbmMask);
315 if (NULL != ShortcutIcon) DestroyIcon(ShortcutIcon);
316
317 return NULL;
318 }
319
320 /*****************************************************************************
321 * SIC_IconAppend [internal]
322 *
323 * NOTES
324 * appends an icon pair to the end of the cache
325 */
326 static INT SIC_IconAppend (LPCWSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon, DWORD dwFlags)
327 {
328 LPSIC_ENTRY lpsice;
329 INT ret, index, index1, indexDPA;
330 WCHAR path[MAX_PATH];
331 TRACE("%s %i %p %p\n", debugstr_w(sSourceFile), dwSourceIndex, hSmallIcon ,hBigIcon);
332
333 lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
334
335 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
336 lpsice->sSourceFile = (LPWSTR)HeapAlloc( GetProcessHeap(), 0, (wcslen(path)+1)*sizeof(WCHAR) );
337 wcscpy( lpsice->sSourceFile, path );
338
339 lpsice->dwSourceIndex = dwSourceIndex;
340 lpsice->dwFlags = dwFlags;
341
342 EnterCriticalSection(&SHELL32_SicCS);
343
344 indexDPA = DPA_Search (sic_hdpa, lpsice, 0, SIC_CompareEntries, 0, DPAS_SORTED|DPAS_INSERTAFTER);
345 indexDPA = DPA_InsertPtr(sic_hdpa, indexDPA, lpsice);
346 if ( -1 == indexDPA )
347 {
348 ret = INVALID_INDEX;
349 goto leave;
350 }
351
352 index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
353 index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
354
355 /* Something went wrong when allocating a new image in the list. Abort. */
356 if((index == -1) || (index1 == -1))
357 {
358 WARN("Something went wrong when adding the icon to the list: small - 0x%x, big - 0x%x.\n",
359 index, index1);
360 if(index != -1) ImageList_Remove(ShellSmallIconList, index);
361 if(index1 != -1) ImageList_Remove(ShellBigIconList, index1);
362 ret = INVALID_INDEX;
363 goto leave;
364 }
365
366 if (index!=index1)
367 {
368 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
369 /* What to do ???? */
370 }
371 lpsice->dwListIndex = index;
372 ret = lpsice->dwListIndex;
373
374 leave:
375 if(ret == INVALID_INDEX)
376 {
377 if(indexDPA != -1) DPA_DeletePtr(sic_hdpa, indexDPA);
378 HeapFree(GetProcessHeap(), 0, lpsice->sSourceFile);
379 SHFree(lpsice);
380 }
381 LeaveCriticalSection(&SHELL32_SicCS);
382 return ret;
383 }
384 /****************************************************************************
385 * SIC_LoadIcon [internal]
386 *
387 * NOTES
388 * gets small/big icon by number from a file
389 */
390 static INT SIC_LoadIcon (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags)
391 {
392 HICON hiconLarge=0;
393 HICON hiconSmall=0;
394 UINT ret;
395
396 PrivateExtractIconsW(sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, NULL, 1, LR_COPYFROMRESOURCE);
397 PrivateExtractIconsW(sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, NULL, 1, LR_COPYFROMRESOURCE);
398
399 if ( !hiconLarge || !hiconSmall)
400 {
401 WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, debugstr_w(sSourceFile), hiconLarge, hiconSmall);
402 if(hiconLarge) DestroyIcon(hiconLarge);
403 if(hiconSmall) DestroyIcon(hiconSmall);
404 return INVALID_INDEX;
405 }
406
407 if (0 != (dwFlags & GIL_FORSHORTCUT))
408 {
409 HICON hiconLargeShortcut = SIC_OverlayShortcutImage(hiconLarge, TRUE);
410 HICON hiconSmallShortcut = SIC_OverlayShortcutImage(hiconSmall, FALSE);
411 if (NULL != hiconLargeShortcut && NULL != hiconSmallShortcut)
412 {
413 DestroyIcon(hiconLarge);
414 DestroyIcon(hiconSmall);
415 hiconLarge = hiconLargeShortcut;
416 hiconSmall = hiconSmallShortcut;
417 }
418 else
419 {
420 WARN("Failed to create shortcut overlayed icons\n");
421 if (NULL != hiconLargeShortcut) DestroyIcon(hiconLargeShortcut);
422 if (NULL != hiconSmallShortcut) DestroyIcon(hiconSmallShortcut);
423 dwFlags &= ~ GIL_FORSHORTCUT;
424 }
425 }
426
427 ret = SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge, dwFlags);
428 DestroyIcon(hiconLarge);
429 DestroyIcon(hiconSmall);
430 return ret;
431 }
432 /*****************************************************************************
433 * SIC_GetIconIndex [internal]
434 *
435 * Parameters
436 * sSourceFile [IN] filename of file containing the icon
437 * index [IN] index/resID (negated) in this file
438 *
439 * NOTES
440 * look in the cache for a proper icon. if not available the icon is taken
441 * from the file and cached
442 */
443 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex, DWORD dwFlags )
444 {
445 SIC_ENTRY sice;
446 INT ret, index = INVALID_INDEX;
447 WCHAR path[MAX_PATH];
448
449 TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
450
451 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
452 sice.sSourceFile = path;
453 sice.dwSourceIndex = dwSourceIndex;
454 sice.dwFlags = dwFlags;
455
456 EnterCriticalSection(&SHELL32_SicCS);
457
458 if (NULL != DPA_GetPtr (sic_hdpa, 0))
459 {
460 /* search linear from position 0*/
461 index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, DPAS_SORTED);
462 }
463
464 if ( INVALID_INDEX == index )
465 {
466 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex, dwFlags);
467 }
468 else
469 {
470 TRACE("-- found\n");
471 ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
472 }
473
474 LeaveCriticalSection(&SHELL32_SicCS);
475 return ret;
476 }
477
478 /*****************************************************************************
479 * SIC_Initialize [internal]
480 */
481 BOOL SIC_Initialize(void)
482 {
483 HICON hSm = NULL, hLg = NULL;
484 INT cx_small, cy_small;
485 INT cx_large, cy_large;
486 HDC hDC;
487 INT bpp;
488 DWORD ilMask;
489 BOOL result = FALSE;
490
491 TRACE("Entered SIC_Initialize\n");
492
493 if (sic_hdpa)
494 {
495 TRACE("Icon cache already initialized\n");
496 return TRUE;
497 }
498
499 sic_hdpa = DPA_Create(16);
500 if (!sic_hdpa)
501 {
502 return FALSE;
503 }
504
505 hDC = CreateICW(L"DISPLAY", NULL, NULL, NULL);
506 if (!hDC)
507 {
508 ERR("Failed to create information context (error %d)\n", GetLastError());
509 goto end;
510 }
511
512 bpp = GetDeviceCaps(hDC, BITSPIXEL);
513 DeleteDC(hDC);
514
515 if (bpp <= 4)
516 ilMask = ILC_COLOR4;
517 else if (bpp <= 8)
518 ilMask = ILC_COLOR8;
519 else if (bpp <= 16)
520 ilMask = ILC_COLOR16;
521 else if (bpp <= 24)
522 ilMask = ILC_COLOR24;
523 else if (bpp <= 32)
524 ilMask = ILC_COLOR32;
525 else
526 ilMask = ILC_COLOR;
527
528 ilMask |= ILC_MASK;
529
530 cx_small = GetSystemMetrics(SM_CXSMICON);
531 cy_small = GetSystemMetrics(SM_CYSMICON);
532 cx_large = GetSystemMetrics(SM_CXICON);
533 cy_large = GetSystemMetrics(SM_CYICON);
534
535 ShellSmallIconList = ImageList_Create(cx_small,
536 cy_small,
537 ilMask,
538 100,
539 100);
540 if (!ShellSmallIconList)
541 {
542 ERR("Failed to create the small icon list.\n");
543 goto end;
544 }
545
546 ShellBigIconList = ImageList_Create(cx_large,
547 cy_large,
548 ilMask,
549 100,
550 100);
551 if (!ShellBigIconList)
552 {
553 ERR("Failed to create the big icon list.\n");
554 goto end;
555 }
556
557 /* Load the document icon, which is used as the default if an icon isn't found. */
558 hSm = (HICON)LoadImageW(shell32_hInstance,
559 MAKEINTRESOURCEW(IDI_SHELL_DOCUMENT),
560 IMAGE_ICON,
561 cx_small,
562 cy_small,
563 LR_SHARED | LR_DEFAULTCOLOR);
564 if (!hSm)
565 {
566 ERR("Failed to load small IDI_SHELL_DOCUMENT icon!\n");
567 goto end;
568 }
569
570 hLg = (HICON)LoadImageW(shell32_hInstance,
571 MAKEINTRESOURCEW(IDI_SHELL_DOCUMENT),
572 IMAGE_ICON,
573 cx_large,
574 cy_large,
575 LR_SHARED | LR_DEFAULTCOLOR);
576 if (!hLg)
577 {
578 ERR("Failed to load large IDI_SHELL_DOCUMENT icon!\n");
579 goto end;
580 }
581
582 if(SIC_IconAppend(swShell32Name, IDI_SHELL_DOCUMENT-1, hSm, hLg, 0) == INVALID_INDEX)
583 {
584 ERR("Failed to add IDI_SHELL_DOCUMENT icon to cache.\n");
585 goto end;
586 }
587 if(SIC_IconAppend(swShell32Name, -IDI_SHELL_DOCUMENT, hSm, hLg, 0) == INVALID_INDEX)
588 {
589 ERR("Failed to add IDI_SHELL_DOCUMENT icon to cache.\n");
590 goto end;
591 }
592
593 /* Everything went fine */
594 result = TRUE;
595
596 end:
597 /* The image list keeps a copy of the icons, we must destroy them */
598 if(hSm) DestroyIcon(hSm);
599 if(hLg) DestroyIcon(hLg);
600
601 /* Clean everything if something went wrong */
602 if(!result)
603 {
604 if(sic_hdpa) DPA_Destroy(sic_hdpa);
605 if(ShellSmallIconList) ImageList_Destroy(ShellSmallIconList);
606 if(ShellBigIconList) ImageList_Destroy(ShellSmallIconList);
607 sic_hdpa = NULL;
608 ShellSmallIconList = NULL;
609 ShellBigIconList = NULL;
610 }
611
612 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
613
614 return result;
615 }
616
617 /*************************************************************************
618 * SIC_Destroy
619 *
620 * frees the cache
621 */
622 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
623 {
624 HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
625 SHFree(ptr);
626 return TRUE;
627 }
628
629 void SIC_Destroy(void)
630 {
631 TRACE("\n");
632
633 EnterCriticalSection(&SHELL32_SicCS);
634
635 if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
636
637 sic_hdpa = NULL;
638 ImageList_Destroy(ShellSmallIconList);
639 ShellSmallIconList = 0;
640 ImageList_Destroy(ShellBigIconList);
641 ShellBigIconList = 0;
642
643 LeaveCriticalSection(&SHELL32_SicCS);
644 //DeleteCriticalSection(&SHELL32_SicCS); //static
645 }
646
647 /*****************************************************************************
648 * SIC_LoadOverlayIcon [internal]
649 *
650 * Load a shell overlay icon and return its icon cache index.
651 */
652 static int SIC_LoadOverlayIcon(int icon_idx)
653 {
654 WCHAR buffer[1024], wszIdx[8];
655 HKEY hKeyShellIcons;
656 LPCWSTR iconPath;
657 int iconIdx;
658
659 static const WCHAR wszShellIcons[] = {
660 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
661 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
662 'E','x','p','l','o','r','e','r','\\','S','h','e','l','l',' ','I','c','o','n','s',0
663 };
664 static const WCHAR wszNumFmt[] = {'%','d',0};
665
666 iconPath = swShell32Name; /* default: load icon from shell32.dll */
667 iconIdx = icon_idx;
668
669 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszShellIcons, 0, KEY_READ, &hKeyShellIcons) == ERROR_SUCCESS)
670 {
671 DWORD count = sizeof(buffer);
672
673 swprintf(wszIdx, wszNumFmt, icon_idx);
674
675 /* read icon path and index */
676 if (RegQueryValueExW(hKeyShellIcons, wszIdx, NULL, NULL, (LPBYTE)buffer, &count) == ERROR_SUCCESS)
677 {
678 LPWSTR p = wcschr(buffer, ',');
679
680 if (p)
681 *p++ = 0;
682
683 iconPath = buffer;
684 iconIdx = _wtoi(p);
685 }
686
687 RegCloseKey(hKeyShellIcons);
688 }
689
690 return SIC_LoadIcon(iconPath, iconIdx, 0);
691 }
692
693 /*************************************************************************
694 * Shell_GetImageLists [SHELL32.71]
695 *
696 * PARAMETERS
697 * imglist[1|2] [OUT] pointer which receives imagelist handles
698 *
699 */
700 BOOL WINAPI Shell_GetImageLists(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
701 { TRACE("(%p,%p)\n",lpBigList,lpSmallList);
702 if (lpBigList)
703 { *lpBigList = ShellBigIconList;
704 }
705 if (lpSmallList)
706 { *lpSmallList = ShellSmallIconList;
707 }
708
709 return TRUE;
710 }
711 /*************************************************************************
712 * PidlToSicIndex [INTERNAL]
713 *
714 * PARAMETERS
715 * sh [IN] IShellFolder
716 * pidl [IN]
717 * bBigIcon [IN]
718 * uFlags [IN] GIL_*
719 * pIndex [OUT] index within the SIC
720 *
721 */
722 BOOL PidlToSicIndex (
723 IShellFolder * sh,
724 LPCITEMIDLIST pidl,
725 BOOL bBigIcon,
726 UINT uFlags,
727 int * pIndex)
728 {
729 CComPtr<IExtractIconW> ei;
730 WCHAR szIconFile[MAX_PATH]; /* file containing the icon */
731 INT iSourceIndex; /* index or resID(negated) in this file */
732 BOOL ret = FALSE;
733 UINT dwFlags = 0;
734 int iShortcutDefaultIndex = INVALID_INDEX;
735
736 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
737
738 if (SUCCEEDED (sh->GetUIObjectOf(0, 1, &pidl, IID_NULL_PPV_ARG(IExtractIconW, &ei))))
739 {
740 if (SUCCEEDED(ei->GetIconLocation(uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
741 {
742 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex, uFlags);
743 ret = TRUE;
744 }
745 }
746
747 if (INVALID_INDEX == *pIndex) /* default icon when failed */
748 {
749 if (0 == (uFlags & GIL_FORSHORTCUT))
750 {
751 *pIndex = 0;
752 }
753 else
754 {
755 if (INVALID_INDEX == iShortcutDefaultIndex)
756 {
757 iShortcutDefaultIndex = SIC_LoadIcon(swShell32Name, 0, GIL_FORSHORTCUT);
758 }
759 *pIndex = (INVALID_INDEX != iShortcutDefaultIndex ? iShortcutDefaultIndex : 0);
760 }
761 }
762
763 return ret;
764
765 }
766
767 /*************************************************************************
768 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
769 *
770 * PARAMETERS
771 * sh [IN] pointer to an instance of IShellFolder
772 * pidl [IN]
773 * pIndex [OUT][OPTIONAL] SIC index for big icon
774 *
775 */
776 int WINAPI SHMapPIDLToSystemImageListIndex(
777 IShellFolder *sh,
778 LPCITEMIDLIST pidl,
779 int *pIndex)
780 {
781 int Index;
782 UINT uGilFlags = 0;
783
784 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
785 pdump(pidl);
786
787 if (SHELL_IsShortcut(pidl))
788 uGilFlags |= GIL_FORSHORTCUT;
789
790 if (pIndex)
791 if (!PidlToSicIndex ( sh, pidl, 1, uGilFlags, pIndex))
792 *pIndex = -1;
793
794 if (!PidlToSicIndex ( sh, pidl, 0, uGilFlags, &Index))
795 return -1;
796
797 return Index;
798 }
799
800 /*************************************************************************
801 * SHMapIDListToImageListIndexAsync [SHELL32.148]
802 */
803 EXTERN_C HRESULT WINAPI SHMapIDListToImageListIndexAsync(IShellTaskScheduler *pts, IShellFolder *psf,
804 LPCITEMIDLIST pidl, UINT flags,
805 PFNASYNCICONTASKBALLBACK pfn, void *pvData, void *pvHint,
806 int *piIndex, int *piIndexSel)
807 {
808 FIXME("(%p, %p, %p, 0x%08x, %p, %p, %p, %p, %p)\n",
809 pts, psf, pidl, flags, pfn, pvData, pvHint, piIndex, piIndexSel);
810 return E_FAIL;
811 }
812
813 /*************************************************************************
814 * Shell_GetCachedImageIndex [SHELL32.72]
815 *
816 */
817 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, UINT bSimulateDoc)
818 {
819 INT ret, len;
820 LPWSTR szTemp;
821
822 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
823
824 len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
825 szTemp = (LPWSTR)HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
826 MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
827
828 ret = SIC_GetIconIndex( szTemp, nIndex, 0 );
829
830 HeapFree( GetProcessHeap(), 0, szTemp );
831
832 return ret;
833 }
834
835 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, UINT bSimulateDoc)
836 {
837 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
838
839 return SIC_GetIconIndex(szPath, nIndex, 0);
840 }
841
842 EXTERN_C INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
843 { if( SHELL_OsIsUnicode())
844 return Shell_GetCachedImageIndexW((LPCWSTR)szPath, nIndex, bSimulateDoc);
845 return Shell_GetCachedImageIndexA((LPCSTR)szPath, nIndex, bSimulateDoc);
846 }
847
848 EXTERN_C INT WINAPI Shell_GetCachedImageIndex(LPCWSTR szPath, INT nIndex, UINT bSimulateDoc)
849 {
850 return Shell_GetCachedImageIndexAW(szPath, nIndex, bSimulateDoc);
851 }
852
853 /*************************************************************************
854 * ExtractIconExW [SHELL32.@]
855 * RETURNS
856 * 0 no icon found
857 * -1 file is not valid
858 * or number of icons extracted
859 */
860 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
861 {
862 /* get entry point of undocumented function PrivateExtractIconExW() in user32 */
863 #if defined(__CYGWIN__) || defined (__MINGW32__) || defined(_MSC_VER)
864 static UINT (WINAPI*PrivateExtractIconExW)(LPCWSTR,int,HICON*,HICON*,UINT) = NULL;
865
866 if (!PrivateExtractIconExW) {
867 HMODULE hUser32 = GetModuleHandleA("user32");
868 PrivateExtractIconExW = (UINT(WINAPI*)(LPCWSTR,int,HICON*,HICON*,UINT)) GetProcAddress(hUser32, "PrivateExtractIconExW");
869
870 if (!PrivateExtractIconExW)
871 return 0;
872 }
873 #endif
874
875 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
876
877 return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
878 }
879
880 /*************************************************************************
881 * ExtractIconExA [SHELL32.@]
882 */
883 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
884 {
885 UINT ret = 0;
886 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
887 LPWSTR lpwstrFile = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
888
889 TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
890
891 if (lpwstrFile)
892 {
893 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
894 ret = ExtractIconExW(lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
895 HeapFree(GetProcessHeap(), 0, lpwstrFile);
896 }
897 return ret;
898 }
899
900 /*************************************************************************
901 * ExtractAssociatedIconA (SHELL32.@)
902 *
903 * Return icon for given file (either from file itself or from associated
904 * executable) and patch parameters if needed.
905 */
906 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
907 {
908 HICON hIcon = NULL;
909 INT len = MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, NULL, 0);
910 /* Note that we need to allocate MAX_PATH, since we are supposed to fill
911 * the correct executable if there is no icon in lpIconPath directly.
912 * lpIconPath itself is supposed to be large enough, so make sure lpIconPathW
913 * is large enough too. Yes, I am puking too.
914 */
915 LPWSTR lpIconPathW = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
916
917 TRACE("%p %s %p\n", hInst, debugstr_a(lpIconPath), lpiIcon);
918
919 if (lpIconPathW)
920 {
921 MultiByteToWideChar(CP_ACP, 0, lpIconPath, -1, lpIconPathW, len);
922 hIcon = ExtractAssociatedIconW(hInst, lpIconPathW, lpiIcon);
923 WideCharToMultiByte(CP_ACP, 0, lpIconPathW, -1, lpIconPath, MAX_PATH , NULL, NULL);
924 HeapFree(GetProcessHeap(), 0, lpIconPathW);
925 }
926 return hIcon;
927 }
928
929 /*************************************************************************
930 * ExtractAssociatedIconW (SHELL32.@)
931 *
932 * Return icon for given file (either from file itself or from associated
933 * executable) and patch parameters if needed.
934 */
935 HICON WINAPI ExtractAssociatedIconW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIcon)
936 {
937 HICON hIcon = NULL;
938 WORD wDummyIcon = 0;
939
940 TRACE("%p %s %p\n", hInst, debugstr_w(lpIconPath), lpiIcon);
941
942 if(lpiIcon == NULL)
943 lpiIcon = &wDummyIcon;
944
945 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
946
947 if( hIcon < (HICON)2 )
948 { if( hIcon == (HICON)1 ) /* no icons found in given file */
949 { WCHAR tempPath[MAX_PATH];
950 HINSTANCE uRet = FindExecutableW(lpIconPath,NULL,tempPath);
951
952 if( uRet > (HINSTANCE)32 && tempPath[0] )
953 { wcscpy(lpIconPath,tempPath);
954 hIcon = ExtractIconW(hInst, lpIconPath, *lpiIcon);
955 if( hIcon > (HICON)2 )
956 return hIcon;
957 }
958 }
959
960 if( hIcon == (HICON)1 )
961 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
962 else
963 *lpiIcon = 6; /* generic icon - found nothing */
964
965 if (GetModuleFileNameW(hInst, lpIconPath, MAX_PATH))
966 hIcon = LoadIconW(hInst, MAKEINTRESOURCEW(*lpiIcon));
967 }
968 return hIcon;
969 }
970
971 /*************************************************************************
972 * ExtractAssociatedIconExW (SHELL32.@)
973 *
974 * Return icon for given file (either from file itself or from associated
975 * executable) and patch parameters if needed.
976 */
977 EXTERN_C HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
978 {
979 FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
980 return 0;
981 }
982
983 /*************************************************************************
984 * ExtractAssociatedIconExA (SHELL32.@)
985 *
986 * Return icon for given file (either from file itself or from associated
987 * executable) and patch parameters if needed.
988 */
989 EXTERN_C HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
990 {
991 HICON ret;
992 INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
993 LPWSTR lpwstrFile = (LPWSTR)HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
994
995 TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
996
997 MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
998 ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
999 HeapFree(GetProcessHeap(), 0, lpwstrFile);
1000 return ret;
1001 }
1002
1003
1004 /****************************************************************************
1005 * SHDefExtractIconW [SHELL32.@]
1006 */
1007 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
1008 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
1009 {
1010 UINT ret;
1011 HICON hIcons[2];
1012 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
1013
1014 ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
1015 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
1016 if (ret == 0xFFFFFFFF)
1017 return E_FAIL;
1018 if (ret > 0) {
1019 if (phiconLarge)
1020 *phiconLarge = hIcons[0];
1021 else
1022 DestroyIcon(hIcons[0]);
1023 if (phiconSmall)
1024 *phiconSmall = hIcons[1];
1025 else
1026 DestroyIcon(hIcons[1]);
1027 return S_OK;
1028 }
1029 return S_FALSE;
1030 }
1031
1032 /****************************************************************************
1033 * SHDefExtractIconA [SHELL32.@]
1034 */
1035 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
1036 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
1037 {
1038 HRESULT ret;
1039 INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
1040 LPWSTR lpwstrFile = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1041
1042 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
1043
1044 MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
1045 ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
1046 HeapFree(GetProcessHeap(), 0, lpwstrFile);
1047 return ret;
1048 }
1049
1050 /****************************************************************************
1051 * SHGetIconOverlayIndexA [SHELL32.@]
1052 *
1053 * Returns the index of the overlay icon in the system image list.
1054 */
1055 EXTERN_C INT WINAPI SHGetIconOverlayIndexA(LPCSTR pszIconPath, INT iIconIndex)
1056 {
1057 FIXME("%s, %d\n", debugstr_a(pszIconPath), iIconIndex);
1058
1059 return -1;
1060 }
1061
1062 /****************************************************************************
1063 * SHGetIconOverlayIndexW [SHELL32.@]
1064 *
1065 * Returns the index of the overlay icon in the system image list.
1066 */
1067 EXTERN_C INT WINAPI SHGetIconOverlayIndexW(LPCWSTR pszIconPath, INT iIconIndex)
1068 {
1069 FIXME("%s, %d\n", debugstr_w(pszIconPath), iIconIndex);
1070
1071 return -1;
1072 }