shell32: Avoid casts when creating item menu.
[wine] / dlls / shell32 / folders.c
1 /*
2  *      Copyright 1997  Marcus Meissner
3  *      Copyright 1998  Juergen Schmied
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #define COBJMACROS
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "objbase.h"
34 #include "undocshell.h"
35 #include "shlguid.h"
36
37 #include "wine/debug.h"
38
39 #include "pidl.h"
40 #include "shell32_main.h"
41 #include "shfldr.h"
42 #include "shresdef.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(shell);
45
46 /***********************************************************************
47 *   IExtractIconW implementation
48 */
49 typedef struct
50 {
51         const IExtractIconWVtbl *lpVtbl;
52         LONG               ref;
53         const IPersistFileVtbl  *lpvtblPersistFile;
54         const IExtractIconAVtbl *lpvtblExtractIconA;
55         LPITEMIDLIST       pidl;
56 } IExtractIconWImpl;
57
58 static const IExtractIconAVtbl eiavt;
59 static const IExtractIconWVtbl eivt;
60 static const IPersistFileVtbl pfvt;
61
62 static inline IExtractIconW *impl_from_IPersistFile( IPersistFile *iface )
63 {
64     return (IExtractIconW *)((char*)iface - FIELD_OFFSET(IExtractIconWImpl, lpvtblPersistFile));
65 }
66
67 static inline IExtractIconW *impl_from_IExtractIconA( IExtractIconA *iface )
68 {
69     return (IExtractIconW *)((char*)iface - FIELD_OFFSET(IExtractIconWImpl, lpvtblExtractIconA));
70 }
71
72
73 /**************************************************************************
74 *  IExtractIconW_Constructor
75 */
76 IExtractIconW* IExtractIconW_Constructor(LPCITEMIDLIST pidl)
77 {
78         IExtractIconWImpl* ei;
79         
80         TRACE("%p\n", pidl);
81
82         ei = HeapAlloc(GetProcessHeap(),0,sizeof(IExtractIconWImpl));
83         ei->ref=1;
84         ei->lpVtbl = &eivt;
85         ei->lpvtblPersistFile = &pfvt;
86         ei->lpvtblExtractIconA = &eiavt;
87         ei->pidl=ILClone(pidl);
88
89         pdump(pidl);
90
91         TRACE("(%p)\n", ei);
92         return (IExtractIconW *)ei;
93 }
94 /**************************************************************************
95  *  IExtractIconW_QueryInterface
96  */
97 static HRESULT WINAPI IExtractIconW_fnQueryInterface(IExtractIconW *iface, REFIID riid, LPVOID *ppvObj)
98 {
99         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
100
101         TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, debugstr_guid(riid), ppvObj);
102
103         *ppvObj = NULL;
104
105         if (IsEqualIID(riid, &IID_IUnknown))                            /*IUnknown*/
106         {
107           *ppvObj = This;
108         }
109         else if (IsEqualIID(riid, &IID_IPersistFile))   /*IExtractIcon*/
110         {
111           *ppvObj = &This->lpvtblPersistFile;
112         }
113         else if (IsEqualIID(riid, &IID_IExtractIconA))  /*IExtractIcon*/
114         {
115           *ppvObj = &This->lpvtblExtractIconA;
116         }
117         else if (IsEqualIID(riid, &IID_IExtractIconW))  /*IExtractIcon*/
118         {
119           *ppvObj = This;
120         }
121
122         if(*ppvObj)
123         {
124           IExtractIconW_AddRef((IExtractIconW*) *ppvObj);
125           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
126           return S_OK;
127         }
128         TRACE("-- Interface: E_NOINTERFACE\n");
129         return E_NOINTERFACE;
130 }
131
132 /**************************************************************************
133 *  IExtractIconW_AddRef
134 */
135 static ULONG WINAPI IExtractIconW_fnAddRef(IExtractIconW * iface)
136 {
137         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
138         ULONG refCount = InterlockedIncrement(&This->ref);
139
140         TRACE("(%p)->(count=%u)\n", This, refCount - 1);
141
142         return refCount;
143 }
144 /**************************************************************************
145 *  IExtractIconW_Release
146 */
147 static ULONG WINAPI IExtractIconW_fnRelease(IExtractIconW * iface)
148 {
149         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
150         ULONG refCount = InterlockedDecrement(&This->ref);
151
152         TRACE("(%p)->(count=%u)\n", This, refCount + 1);
153
154         if (!refCount)
155         {
156           TRACE(" destroying IExtractIcon(%p)\n",This);
157           SHFree(This->pidl);
158           HeapFree(GetProcessHeap(),0,This);
159           return 0;
160         }
161         return refCount;
162 }
163
164 static HRESULT getIconLocationForFolder(IExtractIconW *iface, UINT uFlags,
165  LPWSTR szIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
166 {
167     IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
168     int icon_idx;
169     WCHAR wszPath[MAX_PATH];
170     WCHAR wszCLSIDValue[CHARS_IN_GUID];
171     static const WCHAR shellClassInfo[] = { '.','S','h','e','l','l','C','l','a','s','s','I','n','f','o',0 };
172     static const WCHAR iconFile[] = { 'I','c','o','n','F','i','l','e',0 };
173     static const WCHAR clsid[] = { 'C','L','S','I','D',0 };
174     static const WCHAR clsid2[] = { 'C','L','S','I','D','2',0 };
175     static const WCHAR iconIndex[] = { 'I','c','o','n','I','n','d','e','x',0 };
176
177     if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconFile,
178         wszPath, MAX_PATH))
179     {
180         WCHAR wszIconIndex[10];
181         SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconIndex,
182             wszIconIndex, 10);
183         *piIndex = atoiW(wszIconIndex);
184     }
185     else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid,
186         wszCLSIDValue, CHARS_IN_GUID) &&
187         HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &icon_idx))
188     {
189        *piIndex = icon_idx;
190     }
191     else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid2,
192         wszCLSIDValue, CHARS_IN_GUID) &&
193         HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &icon_idx))
194     {
195        *piIndex = icon_idx;
196     }
197     else
198     {
199         static const WCHAR folder[] = { 'F','o','l','d','e','r',0 };
200
201         if (!HCR_GetDefaultIconW(folder, szIconFile, cchMax, &icon_idx))
202         {
203             lstrcpynW(szIconFile, swShell32Name, cchMax);
204             icon_idx = -IDI_SHELL_FOLDER;
205         }
206
207         if (uFlags & GIL_OPENICON)
208             *piIndex = icon_idx<0? icon_idx-1: icon_idx+1;
209         else
210             *piIndex = icon_idx;
211     }
212
213     return S_OK;
214 }
215
216 WCHAR swShell32Name[MAX_PATH];
217
218 /**************************************************************************
219 *  IExtractIconW_GetIconLocation
220 *
221 * mapping filetype to icon
222 */
223 static HRESULT WINAPI IExtractIconW_fnGetIconLocation(
224         IExtractIconW * iface,
225         UINT uFlags,            /* GIL_ flags */
226         LPWSTR szIconFile,
227         UINT cchMax,
228         int * piIndex,
229         UINT * pwFlags)         /* returned GIL_ flags */
230 {
231         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
232
233         char    sTemp[MAX_PATH];
234         int             icon_idx;
235         GUID const * riid;
236         LPITEMIDLIST    pSimplePidl = ILFindLastID(This->pidl);
237
238         TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
239
240         if (pwFlags)
241           *pwFlags = 0;
242
243         if (_ILIsDesktop(pSimplePidl))
244         {
245           lstrcpynW(szIconFile, swShell32Name, cchMax);
246           *piIndex = -IDI_SHELL_DESKTOP;
247         }
248
249         /* my computer and other shell extensions */
250         else if ((riid = _ILGetGUIDPointer(pSimplePidl)))
251         {
252           static const WCHAR fmt[] = { 'C','L','S','I','D','\\',
253        '{','%','0','8','l','x','-','%','0','4','x','-','%','0','4','x','-',
254        '%','0','2','x','%','0','2','x','-','%','0','2','x', '%','0','2','x',
255        '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0 };
256           WCHAR xriid[50];
257
258           sprintfW(xriid, fmt,
259                   riid->Data1, riid->Data2, riid->Data3,
260                   riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
261                   riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]);
262
263           if (HCR_GetDefaultIconW(xriid, szIconFile, cchMax, &icon_idx))
264           {
265             *piIndex = icon_idx;
266           }
267           else
268           {
269             lstrcpynW(szIconFile, swShell32Name, cchMax);
270             if(IsEqualGUID(riid, &CLSID_MyComputer))
271                 *piIndex = -IDI_SHELL_MY_COMPUTER;
272             else if(IsEqualGUID(riid, &CLSID_MyDocuments))
273                 *piIndex = -IDI_SHELL_MY_DOCUMENTS;
274             else if(IsEqualGUID(riid, &CLSID_NetworkPlaces))
275                 *piIndex = -IDI_SHELL_MY_NETWORK_PLACES;
276             else if(IsEqualGUID(riid, &CLSID_UnixFolder) ||
277                     IsEqualGUID(riid, &CLSID_UnixDosFolder))
278                 *piIndex = -IDI_SHELL_DRIVE;
279             else
280                 *piIndex = -IDI_SHELL_FOLDER;
281           }
282         }
283
284         else if (_ILIsDrive (pSimplePidl))
285         {
286           static const WCHAR drive[] = { 'D','r','i','v','e',0 };
287
288           int icon_idx = -1;
289
290           if (_ILGetDrive(pSimplePidl, sTemp, MAX_PATH))
291           {
292                 switch(GetDriveTypeA(sTemp))
293                 {
294                   case DRIVE_REMOVABLE:   icon_idx = IDI_SHELL_FLOPPY;        break;
295                   case DRIVE_CDROM:       icon_idx = IDI_SHELL_CDROM;         break;
296                   case DRIVE_REMOTE:      icon_idx = IDI_SHELL_NETDRIVE;      break;
297                   case DRIVE_RAMDISK:     icon_idx = IDI_SHELL_RAMDISK;       break;
298                 }
299           }
300
301           if (icon_idx != -1)
302           {
303                 lstrcpynW(szIconFile, swShell32Name, cchMax);
304                 *piIndex = -icon_idx;
305           }
306           else
307           {
308                 if (HCR_GetDefaultIconW(drive, szIconFile, cchMax, &icon_idx))
309                 {
310                   *piIndex = icon_idx;
311                 }
312                 else
313                 {
314                   lstrcpynW(szIconFile, swShell32Name, cchMax);
315                   *piIndex = -IDI_SHELL_DRIVE;
316                 }
317           }
318         }
319         else if (_ILIsFolder (pSimplePidl))
320         {
321             getIconLocationForFolder(iface, uFlags, szIconFile, cchMax, piIndex,
322                                      pwFlags);
323         }
324         else
325         {
326           BOOL found = FALSE;
327
328           if (_ILIsCPanelStruct(pSimplePidl))
329           {
330             if (SUCCEEDED(CPanel_GetIconLocationW(pSimplePidl, szIconFile, cchMax, piIndex)))
331                 found = TRUE;
332           }
333           else if (_ILGetExtension(pSimplePidl, sTemp, MAX_PATH))
334           {
335             if (HCR_MapTypeToValueA(sTemp, sTemp, MAX_PATH, TRUE)
336                 && HCR_GetDefaultIconA(sTemp, sTemp, MAX_PATH, &icon_idx))
337             {
338               if (!lstrcmpA("%1", sTemp))               /* icon is in the file */
339               {
340                 SHGetPathFromIDListW(This->pidl, szIconFile);
341                 *piIndex = 0;
342               }
343               else
344               {
345                 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, szIconFile, cchMax);
346                 *piIndex = icon_idx;
347               }
348
349               found = TRUE;
350             }
351             else if (!lstrcmpiA(sTemp, "lnkfile"))
352             {
353               /* extract icon from shell shortcut */
354               IShellFolder* dsf;
355               IShellLinkW* psl;
356
357               if (SUCCEEDED(SHGetDesktopFolder(&dsf)))
358               {
359                 HRESULT hr = IShellFolder_GetUIObjectOf(dsf, NULL, 1, (LPCITEMIDLIST*)&This->pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
360
361                 if (SUCCEEDED(hr))
362                 {
363                   hr = IShellLinkW_GetIconLocation(psl, szIconFile, MAX_PATH, piIndex);
364
365                   if (SUCCEEDED(hr) && *szIconFile)
366                     found = TRUE;
367
368                   IShellLinkW_Release(psl);
369                 }
370
371                 IShellFolder_Release(dsf);
372               }
373             }
374           }
375
376           if (!found)                                   /* default icon */
377           {
378             lstrcpynW(szIconFile, swShell32Name, cchMax);
379             *piIndex = 0;
380           }
381         }
382
383         TRACE("-- %s %x\n", debugstr_w(szIconFile), *piIndex);
384         return S_OK;
385 }
386
387 /**************************************************************************
388 *  IExtractIconW_Extract
389 */
390 static HRESULT WINAPI IExtractIconW_fnExtract(IExtractIconW * iface, LPCWSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
391 {
392         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
393         int index;
394
395         FIXME("(%p) (file=%p index=%d %p %p size=%08x) semi-stub\n", This, debugstr_w(pszFile), (signed)nIconIndex,
396               phiconLarge, phiconSmall, nIconSize);
397
398         index = SIC_GetIconIndex(pszFile, nIconIndex, 0);
399
400         if (phiconLarge)
401           *phiconLarge = ImageList_GetIcon(ShellBigIconList, index, ILD_TRANSPARENT);
402
403         if (phiconSmall)
404           *phiconSmall = ImageList_GetIcon(ShellSmallIconList, index, ILD_TRANSPARENT);
405
406         return S_OK;
407 }
408
409 static const IExtractIconWVtbl eivt =
410 {
411         IExtractIconW_fnQueryInterface,
412         IExtractIconW_fnAddRef,
413         IExtractIconW_fnRelease,
414         IExtractIconW_fnGetIconLocation,
415         IExtractIconW_fnExtract
416 };
417
418 /**************************************************************************
419 *  IExtractIconA_Constructor
420 */
421 IExtractIconA* IExtractIconA_Constructor(LPCITEMIDLIST pidl)
422 {
423         IExtractIconWImpl *This = (IExtractIconWImpl *)IExtractIconW_Constructor(pidl);
424         IExtractIconA *eia = (IExtractIconA *)&This->lpvtblExtractIconA;
425         
426         TRACE("(%p)->(%p)\n", This, eia);
427         return eia;
428 }
429 /**************************************************************************
430  *  IExtractIconA_QueryInterface
431  */
432 static HRESULT WINAPI IExtractIconA_fnQueryInterface(IExtractIconA * iface, REFIID riid, LPVOID *ppvObj)
433 {
434         IExtractIconW *This = impl_from_IExtractIconA(iface);
435
436         return IExtractIconW_QueryInterface(This, riid, ppvObj);
437 }
438
439 /**************************************************************************
440 *  IExtractIconA_AddRef
441 */
442 static ULONG WINAPI IExtractIconA_fnAddRef(IExtractIconA * iface)
443 {
444         IExtractIconW *This = impl_from_IExtractIconA(iface);
445
446         return IExtractIconW_AddRef(This);
447 }
448 /**************************************************************************
449 *  IExtractIconA_Release
450 */
451 static ULONG WINAPI IExtractIconA_fnRelease(IExtractIconA * iface)
452 {
453         IExtractIconW *This = impl_from_IExtractIconA(iface);
454
455         return IExtractIconW_AddRef(This);
456 }
457 /**************************************************************************
458 *  IExtractIconA_GetIconLocation
459 *
460 * mapping filetype to icon
461 */
462 static HRESULT WINAPI IExtractIconA_fnGetIconLocation(
463         IExtractIconA * iface,
464         UINT uFlags,
465         LPSTR szIconFile,
466         UINT cchMax,
467         int * piIndex,
468         UINT * pwFlags)
469 {
470         HRESULT ret;
471         LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, cchMax * sizeof(WCHAR));
472         IExtractIconW *This = impl_from_IExtractIconA(iface);
473         
474         TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
475
476         ret = IExtractIconW_GetIconLocation(This, uFlags, lpwstrFile, cchMax, piIndex, pwFlags);
477         WideCharToMultiByte(CP_ACP, 0, lpwstrFile, -1, szIconFile, cchMax, NULL, NULL);
478         HeapFree(GetProcessHeap(), 0, lpwstrFile);
479
480         TRACE("-- %s %x\n", szIconFile, *piIndex);
481         return ret;
482 }
483 /**************************************************************************
484 *  IExtractIconA_Extract
485 */
486 static HRESULT WINAPI IExtractIconA_fnExtract(IExtractIconA * iface, LPCSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
487 {
488         HRESULT ret;
489         INT len = MultiByteToWideChar(CP_ACP, 0, pszFile, -1, NULL, 0);
490         LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
491         IExtractIconW *This = impl_from_IExtractIconA(iface);
492
493         TRACE("(%p) (file=%p index=%u %p %p size=%u)\n", This, pszFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
494
495         MultiByteToWideChar(CP_ACP, 0, pszFile, -1, lpwstrFile, len);
496         ret = IExtractIconW_Extract(This, lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
497         HeapFree(GetProcessHeap(), 0, lpwstrFile);
498         return ret;
499 }
500
501 static const IExtractIconAVtbl eiavt =
502 {
503         IExtractIconA_fnQueryInterface,
504         IExtractIconA_fnAddRef,
505         IExtractIconA_fnRelease,
506         IExtractIconA_fnGetIconLocation,
507         IExtractIconA_fnExtract
508 };
509
510 /************************************************************************
511  * IEIPersistFile_QueryInterface (IUnknown)
512  */
513 static HRESULT WINAPI IEIPersistFile_fnQueryInterface(
514         IPersistFile    *iface,
515         REFIID          iid,
516         LPVOID          *ppvObj)
517 {
518         IExtractIconW *This = impl_from_IPersistFile(iface);
519
520         return IExtractIconW_QueryInterface(This, iid, ppvObj);
521 }
522
523 /************************************************************************
524  * IEIPersistFile_AddRef (IUnknown)
525  */
526 static ULONG WINAPI IEIPersistFile_fnAddRef(
527         IPersistFile    *iface)
528 {
529         IExtractIconW *This = impl_from_IPersistFile(iface);
530
531         return IExtractIconW_AddRef(This);
532 }
533
534 /************************************************************************
535  * IEIPersistFile_Release (IUnknown)
536  */
537 static ULONG WINAPI IEIPersistFile_fnRelease(
538         IPersistFile    *iface)
539 {
540         IExtractIconW *This = impl_from_IPersistFile(iface);
541
542         return IExtractIconW_Release(This);
543 }
544
545 /************************************************************************
546  * IEIPersistFile_GetClassID (IPersist)
547  */
548 static HRESULT WINAPI IEIPersistFile_fnGetClassID(
549         IPersistFile    *iface,
550         LPCLSID         lpClassId)
551 {
552         CLSID StdFolderID = { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
553
554         if (lpClassId==NULL)
555           return E_POINTER;
556
557         *lpClassId = StdFolderID;
558
559         return S_OK;
560 }
561
562 /************************************************************************
563  * IEIPersistFile_Load (IPersistFile)
564  */
565 static HRESULT WINAPI IEIPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
566 {
567         IExtractIconW *This = impl_from_IPersistFile(iface);
568         FIXME("%p\n", This);
569         return E_NOTIMPL;
570
571 }
572
573 static const IPersistFileVtbl pfvt =
574 {
575         IEIPersistFile_fnQueryInterface,
576         IEIPersistFile_fnAddRef,
577         IEIPersistFile_fnRelease,
578         IEIPersistFile_fnGetClassID,
579         (void *) 0xdeadbeef /* IEIPersistFile_fnIsDirty */,
580         IEIPersistFile_fnLoad,
581         (void *) 0xdeadbeef /* IEIPersistFile_fnSave */,
582         (void *) 0xdeadbeef /* IEIPersistFile_fnSaveCompleted */,
583         (void *) 0xdeadbeef /* IEIPersistFile_fnGetCurFile */
584 };