ddraw: Do not set the rendertarget flag just because of the backbuffer flag.
[wine] / dlls / shell32 / shfldr_desktop.c
1
2 /*
3  *    Virtual Desktop Folder
4  *
5  *    Copyright 1997            Marcus Meissner
6  *    Copyright 1998, 1999, 2002    Juergen Schmied
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30
31 #define COBJMACROS
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34
35 #include "winerror.h"
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winreg.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41
42 #include "ole2.h"
43 #include "shlguid.h"
44
45 #include "enumidlist.h"
46 #include "pidl.h"
47 #include "undocshell.h"
48 #include "shell32_main.h"
49 #include "shresdef.h"
50 #include "shlwapi.h"
51 #include "shellfolder.h"
52 #include "wine/debug.h"
53 #include "debughlp.h"
54 #include "shfldr.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL (shell);
57
58 /***********************************************************************
59 *     Desktopfolder implementation
60 */
61
62 typedef struct {
63     const IShellFolder2Vtbl *lpVtbl;
64     LONG ref;
65
66     /* both paths are parsible from the desktop */
67     LPWSTR sPathTarget;     /* complete path to target used for enumeration and ChangeNotify */
68     LPITEMIDLIST pidlRoot;  /* absolute pidl */
69
70     UINT cfShellIDList;        /* clipboardformat for IDropTarget */
71     BOOL fAcceptFmt;        /* flag for pending Drop */
72 } IGenericSFImpl;
73
74 #define _IUnknown_(This)    (IShellFolder*)&(This->lpVtbl)
75 #define _IShellFolder_(This)    (IShellFolder*)&(This->lpVtbl)
76
77 static const shvheader DesktopSFHeader[] = {
78     {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
79     {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
80     {IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
81     {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
82     {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 5}
83 };
84
85 #define DESKTOPSHELLVIEWCOLUMNS 5
86
87 /**************************************************************************
88  *    ISF_Desktop_fnQueryInterface
89  *
90  * NOTES supports not IPersist/IPersistFolder
91  */
92 static HRESULT WINAPI ISF_Desktop_fnQueryInterface(
93                 IShellFolder2 * iface, REFIID riid, LPVOID * ppvObj)
94 {
95     IGenericSFImpl *This = (IGenericSFImpl *)iface;
96
97     TRACE ("(%p)->(%s,%p)\n", This, shdebugstr_guid (riid), ppvObj);
98
99     *ppvObj = NULL;
100
101     if (IsEqualIID (riid, &IID_IUnknown) ||
102         IsEqualIID (riid, &IID_IShellFolder) ||
103         IsEqualIID (riid, &IID_IShellFolder2))
104     {
105         *ppvObj = This;
106     }
107
108     if (*ppvObj)
109     {
110         IUnknown_AddRef ((IUnknown *) (*ppvObj));
111         TRACE ("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
112         return S_OK;
113     }
114     TRACE ("-- Interface: E_NOINTERFACE\n");
115     return E_NOINTERFACE;
116 }
117
118 static ULONG WINAPI ISF_Desktop_fnAddRef (IShellFolder2 * iface)
119 {
120     return 2; /* non-heap based object */
121 }
122
123 static ULONG WINAPI ISF_Desktop_fnRelease (IShellFolder2 * iface)
124 {
125     return 1; /* non-heap based object */
126 }
127
128 /**************************************************************************
129  *    ISF_Desktop_fnParseDisplayName
130  *
131  * NOTES
132  *    "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" and "" binds
133  *    to MyComputer
134  */
135 static HRESULT WINAPI ISF_Desktop_fnParseDisplayName (IShellFolder2 * iface,
136                 HWND hwndOwner, LPBC pbc, LPOLESTR lpszDisplayName,
137                 DWORD * pchEaten, LPITEMIDLIST * ppidl, DWORD * pdwAttributes)
138 {
139     IGenericSFImpl *This = (IGenericSFImpl *)iface;
140     WCHAR szElement[MAX_PATH];
141     LPCWSTR szNext = NULL;
142     LPITEMIDLIST pidlTemp = NULL;
143     HRESULT hr = S_OK;
144     CLSID clsid;
145
146     TRACE ("(%p)->(HWND=%p,%p,%p=%s,%p,pidl=%p,%p)\n",
147            This, hwndOwner, pbc, lpszDisplayName, debugstr_w(lpszDisplayName),
148            pchEaten, ppidl, pdwAttributes);
149
150     if (!lpszDisplayName || !ppidl)
151         return E_INVALIDARG;
152
153     *ppidl = 0;
154
155     if (pchEaten)
156         *pchEaten = 0;        /* strange but like the original */
157
158     if (lpszDisplayName[0] == ':' && lpszDisplayName[1] == ':')
159     {
160         szNext = GetNextElementW (lpszDisplayName, szElement, MAX_PATH);
161         TRACE ("-- element: %s\n", debugstr_w (szElement));
162         SHCLSIDFromStringW (szElement + 2, &clsid);
163         pidlTemp = _ILCreateGuid (PT_GUID, &clsid);
164     }
165     else if (PathGetDriveNumberW (lpszDisplayName) >= 0)
166     {
167         /* it's a filesystem path with a drive. Let MyComputer/UnixDosFolder parse it */
168         if (UNIXFS_is_rooted_at_desktop()) 
169             pidlTemp = _ILCreateGuid(PT_GUID, &CLSID_UnixDosFolder);
170         else
171             pidlTemp = _ILCreateMyComputer ();
172         szNext = lpszDisplayName;
173     }
174     else if (PathIsUNCW(lpszDisplayName))
175     {
176         pidlTemp = _ILCreateNetwork();
177         szNext = lpszDisplayName;
178     }
179     else if( (pidlTemp = SHELL32_CreatePidlFromBindCtx(pbc, lpszDisplayName)) )
180     {
181         *ppidl = pidlTemp;
182         return S_OK;
183     }
184     else
185     {
186         /* it's a filesystem path on the desktop. Let a FSFolder parse it */
187
188         if (*lpszDisplayName)
189         {
190             WCHAR szPath[MAX_PATH];
191             LPWSTR pathPtr;
192
193             /* build a complete path to create a simple pidl */
194             lstrcpynW(szPath, This->sPathTarget, MAX_PATH);
195             pathPtr = PathAddBackslashW(szPath);
196             if (pathPtr)
197             {
198                 lstrcpynW(pathPtr, lpszDisplayName, MAX_PATH - (pathPtr - szPath));
199                 hr = _ILCreateFromPathW(szPath, &pidlTemp);
200             }
201             else
202             {
203                 /* should never reach here, but for completeness */
204                 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
205             }
206         }
207         else
208             pidlTemp = _ILCreateMyComputer();
209
210         szNext = NULL;
211     }
212
213     if (SUCCEEDED(hr) && pidlTemp)
214     {
215         if (szNext && *szNext)
216         {
217             hr = SHELL32_ParseNextElement(iface, hwndOwner, pbc,
218                     &pidlTemp, (LPOLESTR) szNext, pchEaten, pdwAttributes);
219         }
220         else
221         {
222             if (pdwAttributes && *pdwAttributes)
223                 hr = SHELL32_GetItemAttributes(_IShellFolder_ (This),
224                                                pidlTemp, pdwAttributes);
225         }
226     }
227
228     *ppidl = pidlTemp;
229
230     TRACE ("(%p)->(-- ret=0x%08x)\n", This, hr);
231
232     return hr;
233 }
234
235 /**************************************************************************
236  *  CreateDesktopEnumList()
237  */
238 static const WCHAR Desktop_NameSpaceW[] = { 'S','O','F','T','W','A','R','E',
239  '\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
240  'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l',
241  'o','r','e','r','\\','D','e','s','k','t','o','p','\\','N','a','m','e','s','p',
242  'a','c','e','\0' };
243
244 static BOOL CreateDesktopEnumList(IEnumIDList *list, DWORD dwFlags)
245 {
246     BOOL ret = TRUE;
247     WCHAR szPath[MAX_PATH];
248
249     TRACE("(%p)->(flags=0x%08x)\n", list, dwFlags);
250
251     /* enumerate the root folders */
252     if (dwFlags & SHCONTF_FOLDERS)
253     {
254         HKEY hkey;
255         UINT i;
256
257         /* create the pidl for This item */
258         ret = AddToEnumList(list, _ILCreateMyComputer());
259
260         for (i=0; i<2; i++) {
261             if (ret && !RegOpenKeyExW(i == 0 ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
262                                       Desktop_NameSpaceW, 0, KEY_READ, &hkey))
263             {
264                 WCHAR iid[50];
265                 int i=0;
266
267                 while (ret)
268                 {
269                     DWORD size;
270                     LONG r;
271
272                     size = sizeof (iid) / sizeof (iid[0]);
273                     r = RegEnumKeyExW(hkey, i, iid, &size, 0, NULL, NULL, NULL);
274                     if (ERROR_SUCCESS == r)
275                     {
276                         ret = AddToEnumList(list, _ILCreateGuidFromStrW(iid));
277                         i++;
278                     }
279                     else if (ERROR_NO_MORE_ITEMS == r)
280                         break;
281                     else
282                         ret = FALSE;
283                 }
284                 RegCloseKey(hkey);
285             }
286         }
287     }
288
289     /* enumerate the elements in %windir%\desktop */
290     SHGetSpecialFolderPathW(0, szPath, CSIDL_DESKTOPDIRECTORY, FALSE);
291     ret = ret && CreateFolderEnumList(list, szPath, dwFlags);
292
293     return ret;
294 }
295
296 /**************************************************************************
297  *        ISF_Desktop_fnEnumObjects
298  */
299 static HRESULT WINAPI ISF_Desktop_fnEnumObjects (IShellFolder2 * iface,
300                 HWND hwndOwner, DWORD dwFlags, LPENUMIDLIST * ppEnumIDList)
301 {
302     IGenericSFImpl *This = (IGenericSFImpl *)iface;
303
304     TRACE ("(%p)->(HWND=%p flags=0x%08x pplist=%p)\n",
305            This, hwndOwner, dwFlags, ppEnumIDList);
306
307     *ppEnumIDList = IEnumIDList_Constructor();
308     if (*ppEnumIDList)
309         CreateDesktopEnumList(*ppEnumIDList, dwFlags);
310
311     TRACE ("-- (%p)->(new ID List: %p)\n", This, *ppEnumIDList);
312
313     return *ppEnumIDList ? S_OK : E_OUTOFMEMORY;
314 }
315
316 /**************************************************************************
317  *        ISF_Desktop_fnBindToObject
318  */
319 static HRESULT WINAPI ISF_Desktop_fnBindToObject (IShellFolder2 * iface,
320                 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
321 {
322     IGenericSFImpl *This = (IGenericSFImpl *)iface;
323
324     TRACE ("(%p)->(pidl=%p,%p,%s,%p)\n",
325            This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
326
327     return SHELL32_BindToChild( This->pidlRoot, This->sPathTarget, pidl, riid, ppvOut );
328 }
329
330 /**************************************************************************
331  *    ISF_Desktop_fnBindToStorage
332  */
333 static HRESULT WINAPI ISF_Desktop_fnBindToStorage (IShellFolder2 * iface,
334                 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
335 {
336     IGenericSFImpl *This = (IGenericSFImpl *)iface;
337
338     FIXME ("(%p)->(pidl=%p,%p,%s,%p) stub\n",
339            This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
340
341     *ppvOut = NULL;
342     return E_NOTIMPL;
343 }
344
345 /**************************************************************************
346  *     ISF_Desktop_fnCompareIDs
347  */
348 static HRESULT WINAPI ISF_Desktop_fnCompareIDs (IShellFolder2 * iface,
349                         LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
350 {
351     IGenericSFImpl *This = (IGenericSFImpl *)iface;
352     int nReturn;
353
354     TRACE ("(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n", This, lParam, pidl1, pidl2);
355     nReturn = SHELL32_CompareIDs (_IShellFolder_ (This), lParam, pidl1, pidl2);
356     TRACE ("-- %i\n", nReturn);
357     return nReturn;
358 }
359
360 /**************************************************************************
361  *    ISF_Desktop_fnCreateViewObject
362  */
363 static HRESULT WINAPI ISF_Desktop_fnCreateViewObject (IShellFolder2 * iface,
364                               HWND hwndOwner, REFIID riid, LPVOID * ppvOut)
365 {
366     IGenericSFImpl *This = (IGenericSFImpl *)iface;
367     LPSHELLVIEW pShellView;
368     HRESULT hr = E_INVALIDARG;
369
370     TRACE ("(%p)->(hwnd=%p,%s,%p)\n",
371            This, hwndOwner, shdebugstr_guid (riid), ppvOut);
372
373     if (!ppvOut)
374         return hr;
375
376     *ppvOut = NULL;
377
378     if (IsEqualIID (riid, &IID_IDropTarget))
379     {
380         WARN ("IDropTarget not implemented\n");
381         hr = E_NOTIMPL;
382     }
383     else if (IsEqualIID (riid, &IID_IContextMenu))
384     {
385         WARN ("IContextMenu not implemented\n");
386         hr = E_NOTIMPL;
387     }
388     else if (IsEqualIID (riid, &IID_IShellView))
389     {
390         pShellView = IShellView_Constructor ((IShellFolder *) iface);
391         if (pShellView)
392         {
393             hr = IShellView_QueryInterface (pShellView, riid, ppvOut);
394             IShellView_Release (pShellView);
395         }
396     }
397     TRACE ("-- (%p)->(interface=%p)\n", This, ppvOut);
398     return hr;
399 }
400
401 /**************************************************************************
402  *  ISF_Desktop_fnGetAttributesOf
403  */
404 static HRESULT WINAPI ISF_Desktop_fnGetAttributesOf (IShellFolder2 * iface,
405                 UINT cidl, LPCITEMIDLIST * apidl, DWORD * rgfInOut)
406 {
407     IGenericSFImpl *This = (IGenericSFImpl *)iface;
408     HRESULT hr = S_OK;
409     static const DWORD dwDesktopAttributes = 
410         SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
411         SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
412     static const DWORD dwMyComputerAttributes = 
413         SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
414         SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
415
416     TRACE ("(%p)->(cidl=%d apidl=%p mask=%p (0x%08x))\n",
417            This, cidl, apidl, rgfInOut, rgfInOut ? *rgfInOut : 0);
418
419     if (!rgfInOut)
420         return E_INVALIDARG;
421     if (cidl && !apidl)
422         return E_INVALIDARG;
423
424     if (*rgfInOut == 0)
425         *rgfInOut = ~0;
426     
427     if(cidl == 0) {
428         *rgfInOut &= dwDesktopAttributes; 
429     } else {
430         while (cidl > 0 && *apidl) {
431             pdump (*apidl);
432             if (_ILIsDesktop(*apidl)) { 
433                 *rgfInOut &= dwDesktopAttributes;
434             } else if (_ILIsMyComputer(*apidl)) {
435                 *rgfInOut &= dwMyComputerAttributes;
436             } else {
437                 SHELL32_GetItemAttributes (_IShellFolder_ (This), *apidl, rgfInOut);
438             }
439             apidl++;
440             cidl--;
441         }
442     }
443     /* make sure SFGAO_VALIDATE is cleared, some apps depend on that */
444     *rgfInOut &= ~SFGAO_VALIDATE;
445
446     TRACE ("-- result=0x%08x\n", *rgfInOut);
447
448     return hr;
449 }
450
451 /**************************************************************************
452  *    ISF_Desktop_fnGetUIObjectOf
453  *
454  * PARAMETERS
455  *  HWND           hwndOwner, //[in ] Parent window for any output
456  *  UINT           cidl,      //[in ] array size
457  *  LPCITEMIDLIST* apidl,     //[in ] simple pidl array
458  *  REFIID         riid,      //[in ] Requested Interface
459  *  UINT*          prgfInOut, //[   ] reserved
460  *  LPVOID*        ppvObject) //[out] Resulting Interface
461  *
462  */
463 static HRESULT WINAPI ISF_Desktop_fnGetUIObjectOf (IShellFolder2 * iface,
464                 HWND hwndOwner, UINT cidl, LPCITEMIDLIST * apidl,
465                 REFIID riid, UINT * prgfInOut, LPVOID * ppvOut)
466 {
467     IGenericSFImpl *This = (IGenericSFImpl *)iface;
468
469     LPITEMIDLIST pidl;
470     IUnknown *pObj = NULL;
471     HRESULT hr = E_INVALIDARG;
472
473     TRACE ("(%p)->(%p,%u,apidl=%p,%s,%p,%p)\n",
474        This, hwndOwner, cidl, apidl, shdebugstr_guid (riid), prgfInOut, ppvOut);
475
476     if (!ppvOut)
477         return hr;
478
479     *ppvOut = NULL;
480
481     if (IsEqualIID (riid, &IID_IContextMenu))
482     {
483         if (cidl > 0)
484             pObj = (LPUNKNOWN) ISvItemCm_Constructor( (IShellFolder *) iface, This->pidlRoot, apidl, cidl);
485         else
486             pObj = (LPUNKNOWN) ISvBgCm_Constructor( (IShellFolder *) iface, TRUE);
487         hr = S_OK;
488     }
489     else if (IsEqualIID (riid, &IID_IDataObject) && (cidl >= 1))
490     {
491         pObj = (LPUNKNOWN) IDataObject_Constructor( hwndOwner,
492                                                   This->pidlRoot, apidl, cidl);
493         hr = S_OK;
494     }
495     else if (IsEqualIID (riid, &IID_IExtractIconA) && (cidl == 1))
496     {
497         pidl = ILCombine (This->pidlRoot, apidl[0]);
498         pObj = (LPUNKNOWN) IExtractIconA_Constructor (pidl);
499         SHFree (pidl);
500         hr = S_OK;
501     }
502     else if (IsEqualIID (riid, &IID_IExtractIconW) && (cidl == 1))
503     {
504         pidl = ILCombine (This->pidlRoot, apidl[0]);
505         pObj = (LPUNKNOWN) IExtractIconW_Constructor (pidl);
506         SHFree (pidl);
507         hr = S_OK;
508     }
509     else if (IsEqualIID (riid, &IID_IDropTarget) && (cidl >= 1))
510     {
511         hr = IShellFolder_QueryInterface (iface,
512                                           &IID_IDropTarget, (LPVOID *) & pObj);
513     }
514     else if ((IsEqualIID(riid,&IID_IShellLinkW) ||
515               IsEqualIID(riid,&IID_IShellLinkA)) && (cidl == 1))
516     {
517         pidl = ILCombine (This->pidlRoot, apidl[0]);
518         hr = IShellLink_ConstructFromFile(NULL, riid, pidl, (LPVOID*)&pObj);
519         SHFree (pidl);
520     }
521     else
522         hr = E_NOINTERFACE;
523
524     if (SUCCEEDED(hr) && !pObj)
525         hr = E_OUTOFMEMORY;
526
527     *ppvOut = pObj;
528     TRACE ("(%p)->hr=0x%08x\n", This, hr);
529     return hr;
530 }
531
532 /**************************************************************************
533  *    ISF_Desktop_fnGetDisplayNameOf
534  *
535  * NOTES
536  *    special case: pidl = null gives desktop-name back
537  */
538 static HRESULT WINAPI ISF_Desktop_fnGetDisplayNameOf (IShellFolder2 * iface,
539                 LPCITEMIDLIST pidl, DWORD dwFlags, LPSTRRET strRet)
540 {
541     IGenericSFImpl *This = (IGenericSFImpl *)iface;
542     HRESULT hr = S_OK;
543     LPWSTR pszPath;
544
545     TRACE ("(%p)->(pidl=%p,0x%08x,%p)\n", This, pidl, dwFlags, strRet);
546     pdump (pidl);
547
548     if (!strRet)
549         return E_INVALIDARG;
550
551     pszPath = CoTaskMemAlloc((MAX_PATH +1) * sizeof(WCHAR));
552     if (!pszPath)
553         return E_OUTOFMEMORY;
554
555     if (_ILIsDesktop (pidl))
556     {
557         if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
558             (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING))
559             strcpyW(pszPath, This->sPathTarget);
560         else
561             HCR_GetClassNameW(&CLSID_ShellDesktop, pszPath, MAX_PATH);
562     }
563     else if (_ILIsPidlSimple (pidl))
564     {
565         GUID const *clsid;
566
567         if ((clsid = _ILGetGUIDPointer (pidl)))
568         {
569             if (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING)
570             {
571                 int bWantsForParsing;
572
573                 /*
574                  * We can only get a filesystem path from a shellfolder if the
575                  *  value WantsFORPARSING in CLSID\\{...}\\shellfolder exists.
576                  *
577                  * Exception: The MyComputer folder doesn't have this key,
578                  *   but any other filesystem backed folder it needs it.
579                  */
580                 if (IsEqualIID (clsid, &CLSID_MyComputer))
581                 {
582                     bWantsForParsing = TRUE;
583                 }
584                 else
585                 {
586                     /* get the "WantsFORPARSING" flag from the registry */
587                     static const WCHAR clsidW[] =
588                      { 'C','L','S','I','D','\\',0 };
589                     static const WCHAR shellfolderW[] =
590                      { '\\','s','h','e','l','l','f','o','l','d','e','r',0 };
591                     static const WCHAR wantsForParsingW[] =
592                      { 'W','a','n','t','s','F','o','r','P','a','r','s','i','n',
593                      'g',0 };
594                     WCHAR szRegPath[100];
595                     LONG r;
596
597                     lstrcpyW (szRegPath, clsidW);
598                     SHELL32_GUIDToStringW (clsid, &szRegPath[6]);
599                     lstrcatW (szRegPath, shellfolderW);
600                     r = SHGetValueW(HKEY_CLASSES_ROOT, szRegPath,
601                                     wantsForParsingW, NULL, NULL, NULL);
602                     if (r == ERROR_SUCCESS)
603                         bWantsForParsing = TRUE;
604                     else
605                         bWantsForParsing = FALSE;
606                 }
607
608                 if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
609                      bWantsForParsing)
610                 {
611                     /*
612                      * we need the filesystem path to the destination folder.
613                      * Only the folder itself can know it
614                      */
615                     hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
616                                                         pszPath,
617                                                         MAX_PATH);
618                 }
619                 else
620                 {
621                     /* parsing name like ::{...} */
622                     pszPath[0] = ':';
623                     pszPath[1] = ':';
624                     SHELL32_GUIDToStringW (clsid, &pszPath[2]);
625                 }
626             }
627             else
628             {
629                 /* user friendly name */
630                 HCR_GetClassNameW (clsid, pszPath, MAX_PATH);
631             }
632         }
633         else
634         {
635             int cLen = 0;
636
637             /* file system folder or file rooted at the desktop */
638             if ((GET_SHGDN_FOR(dwFlags) == SHGDN_FORPARSING) &&
639                 (GET_SHGDN_RELATION(dwFlags) != SHGDN_INFOLDER))
640             {
641                 lstrcpynW(pszPath, This->sPathTarget, MAX_PATH - 1);
642                 PathAddBackslashW(pszPath);
643                 cLen = lstrlenW(pszPath);
644             }
645
646             _ILSimpleGetTextW(pidl, pszPath + cLen, MAX_PATH - cLen);
647
648             if (!_ILIsFolder(pidl))
649                 SHELL_FS_ProcessDisplayFilename(pszPath, dwFlags);
650         }
651     }
652     else
653     {
654         /* a complex pidl, let the subfolder do the work */
655         hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
656                                             pszPath, MAX_PATH);
657     }
658
659     if (SUCCEEDED(hr))
660     {
661         /* Win9x always returns ANSI strings, NT always returns Unicode strings */
662         if (GetVersion() & 0x80000000)
663         {
664             strRet->uType = STRRET_CSTR;
665             if (!WideCharToMultiByte(CP_ACP, 0, pszPath, -1, strRet->u.cStr, MAX_PATH,
666                                      NULL, NULL))
667                 strRet->u.cStr[0] = '\0';
668             CoTaskMemFree(pszPath);
669         }
670         else
671         {
672             strRet->uType = STRRET_WSTR;
673             strRet->u.pOleStr = pszPath;
674         }
675     }
676     else
677         CoTaskMemFree(pszPath);
678
679     TRACE ("-- (%p)->(%s,0x%08x)\n", This,
680      strRet->uType == STRRET_CSTR ? strRet->u.cStr :
681      debugstr_w(strRet->u.pOleStr), hr);
682     return hr;
683 }
684
685 /**************************************************************************
686  *  ISF_Desktop_fnSetNameOf
687  *  Changes the name of a file object or subfolder, possibly changing its item
688  *  identifier in the process.
689  *
690  * PARAMETERS
691  *  HWND          hwndOwner,  //[in ] Owner window for output
692  *  LPCITEMIDLIST pidl,       //[in ] simple pidl of item to change
693  *  LPCOLESTR     lpszName,   //[in ] the items new display name
694  *  DWORD         dwFlags,    //[in ] SHGNO formatting flags
695  *  LPITEMIDLIST* ppidlOut)   //[out] simple pidl returned
696  */
697 static HRESULT WINAPI ISF_Desktop_fnSetNameOf (IShellFolder2 * iface,
698                 HWND hwndOwner, LPCITEMIDLIST pidl,    /* simple pidl */
699                 LPCOLESTR lpName, DWORD dwFlags, LPITEMIDLIST * pPidlOut)
700 {
701     IGenericSFImpl *This = (IGenericSFImpl *)iface;
702
703     FIXME ("(%p)->(%p,pidl=%p,%s,%u,%p)\n", This, hwndOwner, pidl,
704            debugstr_w (lpName), dwFlags, pPidlOut);
705
706     return E_FAIL;
707 }
708
709 static HRESULT WINAPI ISF_Desktop_fnGetDefaultSearchGUID(IShellFolder2 *iface,
710                 GUID * pguid)
711 {
712     IGenericSFImpl *This = (IGenericSFImpl *)iface;
713
714     FIXME ("(%p)\n", This);
715     return E_NOTIMPL;
716 }
717
718 static HRESULT WINAPI ISF_Desktop_fnEnumSearches (IShellFolder2 *iface,
719                 IEnumExtraSearch ** ppenum)
720 {
721     IGenericSFImpl *This = (IGenericSFImpl *)iface;
722     FIXME ("(%p)\n", This);
723     return E_NOTIMPL;
724 }
725
726 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumn (IShellFolder2 * iface,
727                 DWORD dwRes, ULONG * pSort, ULONG * pDisplay)
728 {
729     IGenericSFImpl *This = (IGenericSFImpl *)iface;
730
731     TRACE ("(%p)\n", This);
732
733     if (pSort)
734         *pSort = 0;
735     if (pDisplay)
736         *pDisplay = 0;
737
738     return S_OK;
739 }
740 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumnState (
741                 IShellFolder2 * iface, UINT iColumn, DWORD * pcsFlags)
742 {
743     IGenericSFImpl *This = (IGenericSFImpl *)iface;
744
745     TRACE ("(%p)\n", This);
746
747     if (!pcsFlags || iColumn >= DESKTOPSHELLVIEWCOLUMNS)
748     return E_INVALIDARG;
749
750     *pcsFlags = DesktopSFHeader[iColumn].pcsFlags;
751
752     return S_OK;
753 }
754
755 static HRESULT WINAPI ISF_Desktop_fnGetDetailsEx (IShellFolder2 * iface,
756                 LPCITEMIDLIST pidl, const SHCOLUMNID * pscid, VARIANT * pv)
757 {
758     IGenericSFImpl *This = (IGenericSFImpl *)iface;
759     FIXME ("(%p)\n", This);
760
761     return E_NOTIMPL;
762 }
763
764 static HRESULT WINAPI ISF_Desktop_fnGetDetailsOf (IShellFolder2 * iface,
765                 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS * psd)
766 {
767     IGenericSFImpl *This = (IGenericSFImpl *)iface;
768
769     HRESULT hr = S_OK;
770
771     TRACE ("(%p)->(%p %i %p)\n", This, pidl, iColumn, psd);
772
773     if (!psd || iColumn >= DESKTOPSHELLVIEWCOLUMNS)
774         return E_INVALIDARG;
775
776     if (!pidl)
777     {
778         psd->fmt = DesktopSFHeader[iColumn].fmt;
779         psd->cxChar = DesktopSFHeader[iColumn].cxChar;
780         psd->str.uType = STRRET_CSTR;
781         LoadStringA (shell32_hInstance, DesktopSFHeader[iColumn].colnameid,
782                      psd->str.u.cStr, MAX_PATH);
783         return S_OK;
784     }
785
786     /* the data from the pidl */
787     psd->str.uType = STRRET_CSTR;
788     switch (iColumn)
789     {
790     case 0:        /* name */
791         hr = IShellFolder_GetDisplayNameOf(iface, pidl,
792                    SHGDN_NORMAL | SHGDN_INFOLDER, &psd->str);
793         break;
794     case 1:        /* size */
795         _ILGetFileSize (pidl, psd->str.u.cStr, MAX_PATH);
796         break;
797     case 2:        /* type */
798         _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
799         break;
800     case 3:        /* date */
801         _ILGetFileDate (pidl, psd->str.u.cStr, MAX_PATH);
802         break;
803     case 4:        /* attributes */
804         _ILGetFileAttributes (pidl, psd->str.u.cStr, MAX_PATH);
805         break;
806     }
807
808     return hr;
809 }
810
811 static HRESULT WINAPI ISF_Desktop_fnMapColumnToSCID (
812                 IShellFolder2 * iface, UINT column, SHCOLUMNID * pscid)
813 {
814     IGenericSFImpl *This = (IGenericSFImpl *)iface;
815     FIXME ("(%p)\n", This);
816     return E_NOTIMPL;
817 }
818
819 static const IShellFolder2Vtbl vt_MCFldr_ShellFolder2 =
820 {
821     ISF_Desktop_fnQueryInterface,
822     ISF_Desktop_fnAddRef,
823     ISF_Desktop_fnRelease,
824     ISF_Desktop_fnParseDisplayName,
825     ISF_Desktop_fnEnumObjects,
826     ISF_Desktop_fnBindToObject,
827     ISF_Desktop_fnBindToStorage,
828     ISF_Desktop_fnCompareIDs,
829     ISF_Desktop_fnCreateViewObject,
830     ISF_Desktop_fnGetAttributesOf,
831     ISF_Desktop_fnGetUIObjectOf,
832     ISF_Desktop_fnGetDisplayNameOf,
833     ISF_Desktop_fnSetNameOf,
834     /* ShellFolder2 */
835     ISF_Desktop_fnGetDefaultSearchGUID,
836     ISF_Desktop_fnEnumSearches,
837     ISF_Desktop_fnGetDefaultColumn,
838     ISF_Desktop_fnGetDefaultColumnState,
839     ISF_Desktop_fnGetDetailsEx,
840     ISF_Desktop_fnGetDetailsOf,
841     ISF_Desktop_fnMapColumnToSCID
842 };
843
844 /**************************************************************************
845  *    ISF_Desktop_Constructor
846  */
847 HRESULT WINAPI ISF_Desktop_Constructor (
848                 IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
849 {
850     static IGenericSFImpl *cached_sf;
851     WCHAR szMyPath[MAX_PATH];
852
853     TRACE ("unkOut=%p %s\n", pUnkOuter, shdebugstr_guid (riid));
854
855     if (!ppv)
856         return E_POINTER;
857     if (pUnkOuter)
858         return CLASS_E_NOAGGREGATION;
859
860     if (!cached_sf)
861     {
862         IGenericSFImpl *sf;
863
864         if (!SHGetSpecialFolderPathW( 0, szMyPath, CSIDL_DESKTOPDIRECTORY, TRUE ))
865             return E_UNEXPECTED;
866
867         sf = LocalAlloc( LMEM_ZEROINIT, sizeof (IGenericSFImpl) );
868         if (!sf)
869             return E_OUTOFMEMORY;
870
871         sf->ref = 1;
872         sf->lpVtbl = &vt_MCFldr_ShellFolder2;
873         sf->pidlRoot = _ILCreateDesktop();    /* my qualified pidl */
874         sf->sPathTarget = SHAlloc( (lstrlenW(szMyPath) + 1)*sizeof(WCHAR) );
875         lstrcpyW( sf->sPathTarget, szMyPath );
876
877         if (InterlockedCompareExchangePointer((void *)&cached_sf, sf, NULL) != NULL)
878         {
879             /* some other thread already been here */
880             SHFree( sf->pidlRoot );
881             SHFree( sf->sPathTarget );
882             LocalFree( sf );
883         }
884     }
885
886     return IUnknown_QueryInterface( _IUnknown_(cached_sf), riid, ppv );
887 }