Converted to the new debug interface, using script written by Patrik
[wine] / dlls / shell32 / shlfolder.c
1 /*
2  *      Shell Folder stuff
3  *
4  *      Copyright 1997  Marcus Meissner
5  *      Copyright 1998  Juergen Schmied
6  *
7  */
8
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "debugtools.h"
13 #include "winerror.h"
14
15 #include "oleidl.h"
16 #include "shlguid.h"
17
18 #include "pidl.h"
19 #include "wine/obj_base.h"
20 #include "wine/obj_dragdrop.h"
21 #include "wine/obj_shellfolder.h"
22 #include "shell32_main.h"
23
24 DEFAULT_DEBUG_CHANNEL(shell)
25
26 /***************************************************************************
27  * IDropTarget interface definition for the ShellFolder
28  */
29
30 typedef struct
31 {       ICOM_VTABLE(IDropTarget)* lpvtbl;
32         ULONG ref;
33 } ISFDropTarget;
34
35 static struct ICOM_VTABLE(IDropTarget) dtvt;
36
37
38 /****************************************************************************
39  * ISFDropTarget implementation
40  */
41
42 static IDropTarget * WINAPI ISFDropTarget_Constructor(void)
43 {
44         ISFDropTarget* sf;
45
46         sf = HeapAlloc(GetProcessHeap(), 0, sizeof(ISFDropTarget));
47
48         if (sf)
49         { sf->lpvtbl = &dtvt;
50           sf->ref    = 1;
51         }
52
53         return (IDropTarget *)sf;
54 }
55
56 static HRESULT WINAPI ISFDropTarget_QueryInterface(
57         IDropTarget *iface,
58         REFIID riid,
59         LPVOID *ppvObj)
60 {
61         ICOM_THIS(ISFDropTarget,iface);
62
63         char    xriid[50];
64         WINE_StringFromCLSID((LPCLSID)riid,xriid);
65
66         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,xriid,ppvObj);
67
68         if ( !This || !ppvObj)
69           return E_INVALIDARG;
70
71         *ppvObj = NULL;
72
73         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
74         { *ppvObj = This; 
75         }
76         else if(IsEqualIID(riid, &IID_IDropTarget))  /*IShellFolder*/
77         {    *ppvObj = (ISFDropTarget*)This;
78         }   
79
80         if(*ppvObj)
81         { IDropTarget_AddRef((IDropTarget*)*ppvObj);
82           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
83           return S_OK;
84         }
85
86         TRACE("-- Interface: E_NOINTERFACE\n");
87
88         return E_NOINTERFACE;
89 }
90
91 static ULONG WINAPI ISFDropTarget_AddRef( IDropTarget *iface)
92 {
93         ICOM_THIS(ISFDropTarget,iface);
94
95         TRACE("(%p)->(count=%lu)\n",This,This->ref);
96
97         shell32_ObjCount++;
98
99         return ++(This->ref);
100 }
101
102 static ULONG WINAPI ISFDropTarget_Release( IDropTarget *iface)
103 {
104         ICOM_THIS(ISFDropTarget,iface);
105
106         shell32_ObjCount--;
107
108         if (!--(This->ref)) 
109         { TRACE("-- destroying ISFDropTarget (%p)\n",This);
110           HeapFree(GetProcessHeap(),0,This);
111           return 0;
112         }
113         return This->ref;
114 }
115
116 static HRESULT WINAPI ISFDropTarget_DragEnter(
117         IDropTarget     *iface,
118         IDataObject     *pDataObject,
119         DWORD           grfKeyState,
120         POINTL          pt,
121         DWORD           *pdwEffect)
122 {       
123
124         ICOM_THIS(ISFDropTarget,iface);
125
126         FIXME("Stub: This=%p, DataObject=%p\n",This,pDataObject);
127
128         return E_NOTIMPL;
129 }
130
131 static HRESULT WINAPI ISFDropTarget_DragOver(
132         IDropTarget     *iface,
133         DWORD           grfKeyState,
134         POINTL          pt,
135         DWORD           *pdwEffect)
136 {
137         ICOM_THIS(ISFDropTarget,iface);
138
139         FIXME("Stub: This=%p\n",This);
140
141         return E_NOTIMPL;
142 }
143
144 static HRESULT WINAPI ISFDropTarget_DragLeave(
145         IDropTarget     *iface)
146 {
147         ICOM_THIS(ISFDropTarget,iface);
148
149         FIXME("Stub: This=%p\n",This);
150
151         return E_NOTIMPL;
152 }
153
154 static HRESULT WINAPI ISFDropTarget_Drop(
155         IDropTarget     *iface,
156         IDataObject*    pDataObject,
157         DWORD           grfKeyState,
158         POINTL          pt,
159         DWORD           *pdwEffect)
160 {
161         ICOM_THIS(ISFDropTarget,iface);
162
163         FIXME("Stub: This=%p\n",This);
164
165         return E_NOTIMPL;
166 }
167
168 static struct ICOM_VTABLE(IDropTarget) dtvt = 
169 {
170         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
171         ISFDropTarget_QueryInterface,
172         ISFDropTarget_AddRef,
173         ISFDropTarget_Release,
174         ISFDropTarget_DragEnter,
175         ISFDropTarget_DragOver,
176         ISFDropTarget_DragLeave,
177         ISFDropTarget_Drop
178 };
179
180 /***************************************************************************
181  *  GetNextElement (internal function)
182  *
183  * gets a part of a string till the first backslash
184  *
185  * PARAMETERS
186  *  pszNext [IN] string to get the element from
187  *  pszOut  [IN] pointer to buffer whitch receives string
188  *  dwOut   [IN] length of pszOut
189  *
190  *  RETURNS
191  *    LPSTR pointer to first, not yet parsed char
192  */
193 LPSTR GetNextElement(LPSTR pszNext,LPSTR pszOut,DWORD dwOut)
194 {       LPSTR   pszTail = pszNext;
195         DWORD dwCopy;
196         TRACE("(%s %p 0x%08lx)\n",debugstr_a(pszNext),pszOut,dwOut);
197
198         if(!pszNext || !*pszNext)
199           return NULL;
200
201         while(*pszTail && (*pszTail != '\\'))
202         { pszTail++;
203         }
204         dwCopy=((LPBYTE)pszTail-(LPBYTE)pszNext)/sizeof(CHAR)+1;
205         lstrcpynA(pszOut, pszNext, (dwOut<dwCopy)? dwOut : dwCopy);
206
207         if(*pszTail)
208         {  pszTail++;
209         }
210
211         TRACE("--(%s %s 0x%08lx)\n",debugstr_a(pszNext),debugstr_a(pszOut),dwOut);
212         return pszTail;
213 }
214
215 /***********************************************************************
216 *   IShellFolder implementation
217 */
218
219 static struct ICOM_VTABLE(IShellFolder) sfvt;
220 static struct ICOM_VTABLE(IPersistFolder) psfvt;
221
222 #define _IPersistFolder_Offset ((int)(&(((IGenericSFImpl*)0)->lpvtblPersistFolder))) 
223 #define _ICOM_THIS_From_IPersistFolder(class, name) class* This = (class*)(((char*)name)-_IPersistFolder_Offset); 
224
225 /**************************************************************************
226 *         IShellFolder_Constructor
227 */
228
229 IShellFolder * IShellFolder_Constructor(
230         IGenericSFImpl * pParent,
231         LPITEMIDLIST pidl) 
232 {
233         IGenericSFImpl *        sf;
234         DWORD                   dwSize=0;
235
236         sf=(IGenericSFImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IGenericSFImpl));
237         sf->ref=1;
238         sf->lpvtbl=&sfvt;
239         sf->lpvtblPersistFolder=&psfvt;
240         sf->sMyPath=NULL;       /* path of the folder */
241         sf->pMyPidl=NULL;       /* my qualified pidl */
242
243         TRACE("(%p)->(parent=%p, pidl=%p)\n",sf,pParent, pidl);
244         pdump(pidl);
245                 
246         /* keep a copy of the pidl in the instance*/
247         sf->mpidl = ILClone(pidl);              /* my short pidl */
248         
249         if(sf->mpidl)                           /* do we have a pidl? */
250         { dwSize = 0;
251           if(pParent->sMyPath)                  /* get the size of the parents path */
252           { dwSize += strlen(pParent->sMyPath) ;
253             TRACE("-- (%p)->(parent's path=%s)\n",sf, debugstr_a(pParent->sMyPath));
254           }   
255           dwSize += _ILGetFolderText(sf->mpidl,NULL,0); /* add the size of the foldername*/
256           sf->sMyPath = SHAlloc(dwSize+2);              /* '\0' and backslash */
257           if(sf->sMyPath)
258           { int len;
259             *(sf->sMyPath)=0x00;
260             if(pParent->sMyPath)                        /* if the parent has a path, get it*/
261             {  strcpy(sf->sMyPath, pParent->sMyPath);
262                PathAddBackslashA (sf->sMyPath);
263             }
264             sf->pMyPidl = ILCombine(pParent->pMyPidl, pidl);
265             len = strlen(sf->sMyPath);
266             _ILGetFolderText(sf->mpidl, sf->sMyPath+len, dwSize-len);
267             TRACE("-- (%p)->(my pidl=%p, my path=%s)\n",sf, sf->pMyPidl,debugstr_a(sf->sMyPath));
268             pdump (sf->pMyPidl);
269           }
270         }
271         shell32_ObjCount++;
272         return (IShellFolder *)sf;
273 }
274 /**************************************************************************
275  *  IShellFolder_fnQueryInterface
276  *
277  * PARAMETERS
278  *  REFIID riid         [in ] Requested InterfaceID
279  *  LPVOID* ppvObject   [out] Interface* to hold the result
280  */
281 static HRESULT WINAPI IShellFolder_fnQueryInterface(
282         IShellFolder * iface,
283         REFIID riid,
284         LPVOID *ppvObj)
285 {
286         ICOM_THIS(IGenericSFImpl, iface);
287
288         char    xriid[50];      
289         WINE_StringFromCLSID((LPCLSID)riid,xriid);
290         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,xriid,ppvObj);
291
292         *ppvObj = NULL;
293
294         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
295         { *ppvObj = This; 
296         }
297         else if(IsEqualIID(riid, &IID_IShellFolder))  /*IShellFolder*/
298         {    *ppvObj = (IShellFolder*)This;
299         }   
300         else if(IsEqualIID(riid, &IID_IPersistFolder))  /*IPersistFolder*/
301         {    *ppvObj = (IPersistFolder*)&(This->lpvtblPersistFolder);
302         }   
303
304         if(*ppvObj)
305         { IShellFolder_AddRef((IShellFolder*)*ppvObj);
306           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
307           return S_OK;
308         }
309         TRACE("-- Interface: E_NOINTERFACE\n");
310         return E_NOINTERFACE;
311 }
312
313 /**************************************************************************
314 *  IShellFolder::AddRef
315 */
316
317 static ULONG WINAPI IShellFolder_fnAddRef(IShellFolder * iface)
318 {
319         ICOM_THIS(IGenericSFImpl, iface);
320
321         TRACE("(%p)->(count=%lu)\n",This,This->ref);
322
323         shell32_ObjCount++;
324         return ++(This->ref);
325 }
326
327 /**************************************************************************
328  *  IShellFolder_fnRelease
329  */
330 static ULONG WINAPI IShellFolder_fnRelease(IShellFolder * iface) 
331 {
332         ICOM_THIS(IGenericSFImpl, iface);
333
334         TRACE("(%p)->(count=%lu)\n",This,This->ref);
335
336         shell32_ObjCount--;
337         if (!--(This->ref)) 
338         { TRACE("-- destroying IShellFolder(%p)\n",This);
339
340           if (pdesktopfolder == iface)
341           { pdesktopfolder=NULL;
342             TRACE("-- destroyed IShellFolder(%p) was Desktopfolder\n",This);
343           }
344           if(This->pMyPidl)
345           { SHFree(This->pMyPidl);
346           }
347           if(This->mpidl)
348           { SHFree(This->mpidl);
349           }
350           if(This->sMyPath)
351           { SHFree(This->sMyPath);
352           }
353
354           HeapFree(GetProcessHeap(),0,This);
355
356           return 0;
357         }
358         return This->ref;
359 }
360 /**************************************************************************
361 *               IShellFolder_fnParseDisplayName
362 * PARAMETERS
363 *  HWND          hwndOwner,      //[in ] Parent window for any message's
364 *  LPBC          pbc,            //[in ] reserved
365 *  LPOLESTR      lpszDisplayName,//[in ] "Unicode" displayname.
366 *  ULONG*        pchEaten,       //[out] (unicode) characters processed
367 *  LPITEMIDLIST* ppidl,          //[out] complex pidl to item
368 *  ULONG*        pdwAttributes   //[out] items attributes
369 *
370 * FIXME: 
371 *    pdwAttributes: not used
372 */
373 static HRESULT WINAPI IShellFolder_fnParseDisplayName(
374         IShellFolder * iface,
375         HWND hwndOwner,
376         LPBC pbcReserved,
377         LPOLESTR lpszDisplayName,
378         DWORD *pchEaten,
379         LPITEMIDLIST *ppidl,
380         DWORD *pdwAttributes)
381 {
382         ICOM_THIS(IGenericSFImpl, iface);
383
384         HRESULT         hr=E_OUTOFMEMORY;
385         LPITEMIDLIST    pidlFull=NULL, pidlTemp = NULL, pidlOld = NULL;
386         LPSTR           pszNext=NULL;
387         CHAR            szTemp[MAX_PATH],szElement[MAX_PATH];
388         BOOL            bIsFile;
389
390         TRACE("(%p)->(HWND=0x%08x,%p,%p=%s,%p,pidl=%p,%p)\n",
391         This,hwndOwner,pbcReserved,lpszDisplayName,
392         debugstr_w(lpszDisplayName),pchEaten,ppidl,pdwAttributes);
393
394         { hr = E_FAIL;
395           WideCharToLocal(szTemp, lpszDisplayName, lstrlenW(lpszDisplayName) + 1);
396           if(szTemp[0])
397           { if (strcmp(szTemp,"Desktop")==0)
398             { pidlFull = _ILCreateDesktop();
399             }
400             else if (strcmp(szTemp,"My Computer")==0)
401             { pidlFull = _ILCreateMyComputer();
402             }
403             else
404             { if (!PathIsRootA(szTemp))
405               { if (This->sMyPath && strlen (This->sMyPath))
406                 { if (strcmp(This->sMyPath,"My Computer"))
407                   { strcpy (szElement,This->sMyPath);
408                     PathAddBackslashA (szElement);
409                     strcat (szElement, szTemp);
410                     strcpy (szTemp, szElement);
411                   }
412                 }
413               }
414               
415               /* check if the lpszDisplayName is Folder or File*/
416               bIsFile = ! (GetFileAttributesA(szTemp) & FILE_ATTRIBUTE_DIRECTORY);
417               pszNext = GetNextElement(szTemp, szElement, MAX_PATH);
418
419               pidlFull = _ILCreateMyComputer();
420               pidlTemp = _ILCreateDrive(szElement);                     
421               pidlOld = pidlFull;
422               pidlFull = ILCombine(pidlFull,pidlTemp);
423               SHFree(pidlOld);
424
425               if(pidlFull)
426               { while((pszNext=GetNextElement(pszNext, szElement, MAX_PATH)))
427                 { if(!*pszNext && bIsFile)
428                   { pidlTemp = _ILCreateValue(NULL, szElement);         /* FIXME: shortname */
429                   }
430                   else                          
431                   { pidlTemp = _ILCreateFolder(NULL, szElement);        /* FIXME: shortname */
432                   }
433                   pidlOld = pidlFull;
434                   pidlFull = ILCombine(pidlFull,pidlTemp);
435                   SHFree(pidlOld);
436                 }
437                 hr = S_OK;
438               }
439             }
440           }
441         }
442         *ppidl = pidlFull;
443         return hr;
444 }
445
446 /**************************************************************************
447 *               IShellFolder_fnEnumObjects
448 * PARAMETERS
449 *  HWND          hwndOwner,    //[in ] Parent Window
450 *  DWORD         grfFlags,     //[in ] SHCONTF enumeration mask
451 *  LPENUMIDLIST* ppenumIDList  //[out] IEnumIDList interface
452 */
453 static HRESULT WINAPI IShellFolder_fnEnumObjects(
454         IShellFolder * iface,
455         HWND hwndOwner,
456         DWORD dwFlags,
457         LPENUMIDLIST* ppEnumIDList)
458 {
459         ICOM_THIS(IGenericSFImpl, iface);
460
461         TRACE("(%p)->(HWND=0x%08x flags=0x%08lx pplist=%p)\n",This,hwndOwner,dwFlags,ppEnumIDList);
462
463         *ppEnumIDList = NULL;
464         *ppEnumIDList = IEnumIDList_Constructor (This->sMyPath, dwFlags);
465         TRACE("-- (%p)->(new ID List: %p)\n",This,*ppEnumIDList);
466         if(!*ppEnumIDList)
467         { return E_OUTOFMEMORY;
468         }
469         return S_OK;            
470 }
471
472 /**************************************************************************
473 *               IShellFolder_fnBindToObject
474 * PARAMETERS
475 *  LPCITEMIDLIST pidl,       //[in ] complex pidl to open
476 *  LPBC          pbc,        //[in ] reserved
477 *  REFIID        riid,       //[in ] Initial Interface
478 *  LPVOID*       ppvObject   //[out] Interface*
479 */
480 static HRESULT WINAPI IShellFolder_fnBindToObject( IShellFolder * iface, LPCITEMIDLIST pidl,
481                         LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
482 {
483         ICOM_THIS(IGenericSFImpl, iface);
484
485         char            xriid[50];
486         HRESULT         hr;
487         LPSHELLFOLDER   pShellFolder;
488         
489         WINE_StringFromCLSID(riid,xriid);
490
491         TRACE("(%p)->(pidl=%p,%p,\n\tIID:%s,%p)\n",This,pidl,pbcReserved,xriid,ppvOut);
492
493         *ppvOut = NULL;
494
495         pShellFolder = IShellFolder_Constructor(This, pidl);
496
497         if(!pShellFolder)
498           return E_OUTOFMEMORY;
499
500         hr = pShellFolder->lpvtbl->fnQueryInterface(pShellFolder, riid, ppvOut);
501         pShellFolder->lpvtbl->fnRelease(pShellFolder);
502         TRACE("-- (%p)->(interface=%p)\n",This, ppvOut);
503         return hr;
504 }
505
506 /**************************************************************************
507 *  IShellFolder_fnBindToStorage
508 * PARAMETERS
509 *  LPCITEMIDLIST pidl,       //[in ] complex pidl to store
510 *  LPBC          pbc,        //[in ] reserved
511 *  REFIID        riid,       //[in ] Initial storage interface 
512 *  LPVOID*       ppvObject   //[out] Interface* returned
513 */
514 static HRESULT WINAPI IShellFolder_fnBindToStorage(
515         IShellFolder * iface,
516         LPCITEMIDLIST pidl,
517         LPBC pbcReserved,
518         REFIID riid,
519         LPVOID *ppvOut)
520 {
521         ICOM_THIS(IGenericSFImpl, iface);
522
523         char xriid[50];
524         WINE_StringFromCLSID(riid,xriid);
525
526         FIXME("(%p)->(pidl=%p,%p,\n\tIID:%s,%p) stub\n",This,pidl,pbcReserved,xriid,ppvOut);
527
528         *ppvOut = NULL;
529         return E_NOTIMPL;
530 }
531
532 /**************************************************************************
533 *  IShellFolder_fnCompareIDs
534 *
535 * PARMETERS
536 *  LPARAM        lParam, //[in ] Column?
537 *  LPCITEMIDLIST pidl1,  //[in ] simple pidl
538 *  LPCITEMIDLIST pidl2)  //[in ] simple pidl
539 *
540 * NOTES
541 *   Special case - If one of the items is a Path and the other is a File,
542 *   always make the Path come before the File.
543 *
544 * FIXME
545 *  we have to handle simple pidl's only (?)
546 */
547 static HRESULT WINAPI  IShellFolder_fnCompareIDs(
548         IShellFolder * iface,
549         LPARAM lParam,
550         LPCITEMIDLIST pidl1,
551         LPCITEMIDLIST pidl2)
552 {
553         ICOM_THIS(IGenericSFImpl, iface);
554
555         CHAR szString1[MAX_PATH] = "";
556         CHAR szString2[MAX_PATH] = "";
557         int   nReturn;
558         LPCITEMIDLIST  pidlTemp1 = pidl1, pidlTemp2 = pidl2;
559
560         TRACE("(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n",This,lParam,pidl1,pidl2);
561         pdump (pidl1);
562         pdump (pidl2);
563
564         if (!pidl1 && !pidl2)
565           return 0;
566         if (!pidl1)     /* Desktop < anything */
567           return -1;
568         if (!pidl2)
569           return 1;
570
571         /* get the last item in each list */
572         pidlTemp1 = ILFindLastID(pidlTemp1);
573         pidlTemp2 = ILFindLastID(pidlTemp2);
574
575         /* at This point, both pidlTemp1 and pidlTemp2 point to the last item in the list */
576         if(_ILIsValue(pidlTemp1) != _ILIsValue(pidlTemp2))
577         { if(_ILIsValue(pidlTemp1))
578             return 1;
579           return -1;
580         }
581
582         _ILGetDrive( pidl1,szString1,sizeof(szString1));
583         _ILGetDrive( pidl2,szString2,sizeof(szString2));
584         nReturn = strcasecmp(szString1, szString2);
585
586         if(nReturn)
587           return nReturn;
588
589         _ILGetFolderText( pidl1,szString1,sizeof(szString1));
590         _ILGetFolderText( pidl2,szString2,sizeof(szString2));
591         nReturn = strcasecmp(szString1, szString2);
592
593         if(nReturn)
594           return nReturn;
595
596         _ILGetValueText(pidl1,szString1,sizeof(szString1));
597         _ILGetValueText(pidl2,szString2,sizeof(szString2));
598         return strcasecmp(szString1, szString2);
599 }
600
601 /**************************************************************************
602 *         IShellFolder_fnCreateViewObject
603 * Creates an View Object representing the ShellFolder
604 *  IShellView / IShellBrowser / IContextMenu
605 *
606 * PARAMETERS
607 *  HWND    hwndOwner,  // Handle of owner window
608 *  REFIID  riid,       // Requested initial interface
609 *  LPVOID* ppvObject)  // Resultant interface*
610 *
611 * NOTES
612 *  the same as SHCreateShellFolderViewEx ???
613 */
614 static HRESULT WINAPI IShellFolder_fnCreateViewObject( IShellFolder * iface,
615                  HWND hwndOwner, REFIID riid, LPVOID *ppvOut)
616 {
617         ICOM_THIS(IGenericSFImpl, iface);
618
619         LPSHELLVIEW pShellView;
620         char    xriid[50];
621         HRESULT       hr;
622
623         WINE_StringFromCLSID(riid,xriid);
624         TRACE("(%p)->(hwnd=0x%x,\n\tIID:\t%s,%p)\n",This,hwndOwner,xriid,ppvOut);
625         
626         *ppvOut = NULL;
627
628         pShellView = IShellView_Constructor((IShellFolder *) This, This->mpidl);
629
630         if(!pShellView)
631           return E_OUTOFMEMORY;
632           
633         hr = pShellView->lpvtbl->fnQueryInterface(pShellView, riid, ppvOut);
634         pShellView->lpvtbl->fnRelease(pShellView);
635         TRACE("-- (%p)->(interface=%p)\n",This, ppvOut);
636         return hr; 
637 }
638
639 /**************************************************************************
640 *  IShellFolder_fnGetAttributesOf
641 *
642 * PARAMETERS
643 *  UINT            cidl,     //[in ] num elements in pidl array
644 +  LPCITEMIDLIST*  apidl,    //[in ] simple pidl array 
645 *  ULONG*          rgfInOut) //[out] result array  
646 *
647 * FIXME: quick hack
648 *  Note: rgfInOut is documented as being an array of ULONGS.
649 *  This does not seem to be the case. Testing This function using the shell to 
650 *  call it with cidl > 1 (by deleting multiple items) reveals that the shell
651 *  passes ONE element in the array and writing to further elements will
652 *  cause the shell to fail later.
653 */
654 static HRESULT WINAPI IShellFolder_fnGetAttributesOf(IShellFolder * iface,UINT cidl,LPCITEMIDLIST *apidl,DWORD *rgfInOut)
655 {
656         ICOM_THIS(IGenericSFImpl, iface);
657
658         LPCITEMIDLIST * pidltemp;
659         DWORD i;
660
661         TRACE("(%p)->(%d,%p,%p)\n",This,cidl,apidl,rgfInOut);
662
663         if ( (!cidl) || (!apidl) || (!rgfInOut))
664           return E_INVALIDARG;
665
666         pidltemp=apidl;
667         *rgfInOut = 0x00;
668         i=cidl;
669
670         TRACE("-- mask=0x%08lx\n",*rgfInOut);
671
672         do
673         { if (*pidltemp)
674           { pdump (*pidltemp);
675             if (_ILIsDesktop( *pidltemp))
676             { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FOLDER | SFGAO_DROPTARGET | SFGAO_HASPROPSHEET | SFGAO_CANLINK );
677             }
678             else if (_ILIsMyComputer( *pidltemp))
679             { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR |
680                              SFGAO_DROPTARGET | SFGAO_HASPROPSHEET | SFGAO_CANRENAME | SFGAO_CANLINK );
681             }
682             else if (_ILIsDrive( *pidltemp))
683             { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FILESYSTEM  | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR  | 
684                              SFGAO_DROPTARGET | SFGAO_HASPROPSHEET | SFGAO_CANLINK );
685             }
686             else if (_ILIsFolder( *pidltemp))
687             { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_CAPABILITYMASK );
688             }
689             else if (_ILIsValue( *pidltemp))
690             { *rgfInOut |= (SFGAO_FILESYSTEM | SFGAO_CAPABILITYMASK );
691             }
692           }
693           pidltemp++;
694           cidl--;
695         } while (cidl > 0 && *pidltemp);
696
697         return S_OK;
698 }
699 /**************************************************************************
700 *  IShellFolder_fnGetUIObjectOf
701 *
702 * PARAMETERS
703 *  HWND           hwndOwner, //[in ] Parent window for any output
704 *  UINT           cidl,      //[in ] array size
705 *  LPCITEMIDLIST* apidl,     //[in ] simple pidl array
706 *  REFIID         riid,      //[in ] Requested Interface
707 *  UINT*          prgfInOut, //[   ] reserved 
708 *  LPVOID*        ppvObject) //[out] Resulting Interface
709 *
710 * NOTES
711 *  This function gets asked to return "view objects" for one or more (multiple select)
712 *  items:
713 *  The viewobject typically is an COM object with one of the following interfaces:
714 *  IExtractIcon,IDataObject,IContextMenu
715 *  In order to support icon positions in the default Listview your DataObject
716 *  must implement the SetData method (in addition to GetData :) - the shell passes
717 *  a barely documented "Icon positions" structure to SetData when the drag starts,
718 *  and GetData's it if the drop is in another explorer window that needs the positions.
719 */
720 static HRESULT WINAPI IShellFolder_fnGetUIObjectOf( 
721         IShellFolder *  iface,
722         HWND            hwndOwner,
723         UINT            cidl,
724         LPCITEMIDLIST * apidl, 
725         REFIID          riid, 
726         UINT *          prgfInOut,
727         LPVOID *        ppvOut)
728 {       
729         ICOM_THIS(IGenericSFImpl, iface);
730
731         char            xclsid[50];
732         LPITEMIDLIST    pidl;
733         LPUNKNOWN       pObj = NULL; 
734
735         WINE_StringFromCLSID(riid,xclsid);
736
737         TRACE("(%p)->(%u,%u,apidl=%p,\n\tIID:%s,%p,%p)\n",
738           This,hwndOwner,cidl,apidl,xclsid,prgfInOut,ppvOut);
739
740         *ppvOut = NULL;
741
742         if(IsEqualIID(riid, &IID_IContextMenu))
743         { 
744           if(cidl < 1)
745             return E_INVALIDARG;
746
747           pObj  = (LPUNKNOWN)IContextMenu_Constructor((IShellFolder *)This, apidl, cidl);
748         }
749         else if (IsEqualIID(riid, &IID_IDataObject))
750         { 
751           if (cidl < 1)
752             return(E_INVALIDARG);
753
754           pObj = (LPUNKNOWN)IDataObject_Constructor (hwndOwner, (IShellFolder *)This, apidl, cidl);
755         }
756         else if(IsEqualIID(riid, &IID_IExtractIconA))
757         { 
758           if (cidl != 1)
759             return(E_INVALIDARG);
760
761           pidl = ILCombine(This->pMyPidl,apidl[0]);
762           pObj = (LPUNKNOWN)IExtractIconA_Constructor( pidl );
763           SHFree(pidl);
764         } 
765         else if (IsEqualIID(riid, &IID_IDropTarget))
766         { 
767           if (cidl < 1)
768             return(E_INVALIDARG);
769
770           pObj = (LPUNKNOWN)ISFDropTarget_Constructor();
771         }
772         else
773         { 
774           ERR("(%p)->E_NOINTERFACE\n",This);
775           return E_NOINTERFACE;
776         }
777
778         if(!pObj)
779           return E_OUTOFMEMORY;
780
781         *ppvOut = pObj;
782         return S_OK;
783 }
784 /**************************************************************************
785 *  IShellFolder_fnGetDisplayNameOf
786 *  Retrieves the display name for the specified file object or subfolder
787 *
788 * PARAMETERS
789 *  LPCITEMIDLIST pidl,    //[in ] complex pidl to item
790 *  DWORD         dwFlags, //[in ] SHGNO formatting flags
791 *  LPSTRRET      lpName)  //[out] Returned display name
792 *
793 * FIXME
794 *  if the name is in the pidl the ret value should be a STRRET_OFFSET
795 */
796 #define GET_SHGDN_FOR(dwFlags)         ((DWORD)dwFlags & (DWORD)0x0000FF00)
797 #define GET_SHGDN_RELATION(dwFlags)    ((DWORD)dwFlags & (DWORD)0x000000FF)
798
799 static HRESULT WINAPI IShellFolder_fnGetDisplayNameOf(
800         IShellFolder * iface,
801         LPCITEMIDLIST pidl,
802         DWORD dwFlags,
803         LPSTRRET lpName)
804 {
805         ICOM_THIS(IGenericSFImpl, iface);
806
807         CHAR    szText[MAX_PATH];
808         CHAR    szTemp[MAX_PATH];
809         CHAR    szSpecial[MAX_PATH];
810         CHAR    szDrive[MAX_PATH];
811         DWORD   dwVolumeSerialNumber,dwMaximumComponetLength,dwFileSystemFlags;
812         LPITEMIDLIST    pidlTemp=NULL;
813         BOOL    bSimplePidl=FALSE;
814                 
815         TRACE("(%p)->(pidl=%p,0x%08lx,%p)\n",This,pidl,dwFlags,lpName);
816         pdump(pidl);
817         
818         szSpecial[0]=0x00; 
819         szDrive[0]=0x00;
820         szText[0]=0x00;
821         szTemp[0]=0x00;
822         
823         /* test if simple(relative) or complex(absolute) pidl */
824         pidlTemp = ILGetNext(pidl);
825         if (pidlTemp && pidlTemp->mkid.cb==0x00)
826         { bSimplePidl = TRUE;
827           TRACE("-- simple pidl\n");
828         }
829
830         if (_ILIsDesktop( pidl))
831         { strcpy (szText,"Desktop");
832         }       
833         else
834         { if (_ILIsMyComputer(pidl))
835           { _ILGetItemText(pidl, szSpecial, MAX_PATH);
836             pidl = ILGetNext(pidl);
837           }
838
839           if (_ILIsDrive(pidl))
840           { _ILGetDrive( pidl, szTemp, MAX_PATH);
841
842             if ( dwFlags==SHGDN_NORMAL || dwFlags==SHGDN_INFOLDER)      /* like "A1-dos (C:)" */
843             { GetVolumeInformationA(szTemp,szDrive,MAX_PATH,&dwVolumeSerialNumber,&dwMaximumComponetLength,&dwFileSystemFlags,NULL,0);
844               szTemp[2]=0x00;                                           /* overwrite '\' */
845               strcat (szDrive," (");
846               strcat (szDrive,szTemp);
847               strcat (szDrive,")"); 
848             }
849             else                                                        /* like "C:\" */
850             {  PathAddBackslashA (szTemp);
851                strcpy(szDrive,szTemp);
852             }
853           }
854
855                 
856           switch(dwFlags)
857           { case SHGDN_NORMAL:                          /* 0x0000 */
858               _ILGetPidlPath( pidl, szText, MAX_PATH);
859               break;
860
861             case SHGDN_INFOLDER | SHGDN_FORPARSING:     /* 0x8001 */
862             case SHGDN_INFOLDER:                        /* 0x0001 */
863               pidlTemp = ILFindLastID(pidl);
864               if (pidlTemp)
865               { _ILGetItemText( pidlTemp, szText, MAX_PATH);
866               }
867               break;                            
868
869             case SHGDN_FORPARSING:                      /* 0x8000 */
870               if (bSimplePidl)
871               { /* if the IShellFolder has parents, get the path from the
872                 parent and add the ItemName*/
873                 szText[0]=0x00;
874                 if (This->sMyPath && strlen (This->sMyPath))
875                 { if (strcmp(This->sMyPath,"My Computer"))
876                   { strcpy (szText,This->sMyPath);
877                     PathAddBackslashA (szText);
878                   }
879                 }
880                 pidlTemp = ILFindLastID(pidl);
881                 if (pidlTemp)
882                 { _ILGetItemText( pidlTemp, szTemp, MAX_PATH );
883                 } 
884                 strcat(szText,szTemp);
885               }
886               else      /* if the pidl is absolute, get everything from the pidl*/                                      
887               { _ILGetPidlPath( pidl, szText, MAX_PATH);
888               }
889               break;
890             default:
891               TRACE("--- wrong flags=%lx\n", dwFlags);
892               return E_INVALIDARG;
893           }
894           if ((szText[0]==0x00 && szDrive[0]!=0x00)|| (bSimplePidl && szDrive[0]!=0x00))
895           { strcpy(szText,szDrive);
896           }
897           if (szText[0]==0x00 && szSpecial[0]!=0x00)
898           { strcpy(szText,szSpecial);
899           }
900         }
901
902         TRACE("-- (%p)->(%s)\n",This,szText);
903
904         if(!(lpName))
905         {  return E_OUTOFMEMORY;
906         }
907         lpName->uType = STRRET_CSTRA;   
908         strcpy(lpName->u.cStr,szText);
909         return S_OK;
910 }
911
912 /**************************************************************************
913 *  IShellFolder_fnSetNameOf
914 *  Changes the name of a file object or subfolder, possibly changing its item
915 *  identifier in the process.
916 *
917 * PARAMETERS
918 *  HWND          hwndOwner,  //[in ] Owner window for output
919 *  LPCITEMIDLIST pidl,       //[in ] simple pidl of item to change
920 *  LPCOLESTR     lpszName,   //[in ] the items new display name
921 *  DWORD         dwFlags,    //[in ] SHGNO formatting flags
922 *  LPITEMIDLIST* ppidlOut)   //[out] simple pidl returned
923 */
924 static HRESULT WINAPI IShellFolder_fnSetNameOf(
925         IShellFolder * iface,
926         HWND hwndOwner, 
927         LPCITEMIDLIST pidl, /*simple pidl*/
928         LPCOLESTR lpName, 
929         DWORD dw, 
930         LPITEMIDLIST *pPidlOut)
931 {
932         ICOM_THIS(IGenericSFImpl, iface);
933
934         FIXME("(%p)->(%u,pidl=%p,%s,%lu,%p),stub!\n",
935         This,hwndOwner,pidl,debugstr_w(lpName),dw,pPidlOut);
936
937         return E_NOTIMPL;
938 }
939
940 /**************************************************************************
941 *  IShellFolder_fnGetFolderPath
942 *  FIXME: drive not included
943 */
944 static HRESULT WINAPI IShellFolder_fnGetFolderPath(IShellFolder * iface, LPSTR lpszOut, DWORD dwOutSize)
945 {
946         ICOM_THIS(IGenericSFImpl, iface);
947         DWORD   dwSize;
948         
949         TRACE("(%p)->(%p %lu)\n",This, lpszOut, dwOutSize);
950         if (!lpszOut)
951         { return FALSE;
952         }
953
954         *lpszOut=0;
955
956         if (! This->sMyPath)
957           return FALSE;
958           
959         dwSize = strlen (This->sMyPath) +1;
960         if ( dwSize > dwOutSize)
961           return FALSE;
962         strcpy(lpszOut, This->sMyPath);
963
964         TRACE("-- (%p)->(return=%s)\n",This, lpszOut);
965         return TRUE;
966 }
967
968 static ICOM_VTABLE(IShellFolder) sfvt = 
969 {       
970         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
971         IShellFolder_fnQueryInterface,
972         IShellFolder_fnAddRef,
973         IShellFolder_fnRelease,
974         IShellFolder_fnParseDisplayName,
975         IShellFolder_fnEnumObjects,
976         IShellFolder_fnBindToObject,
977         IShellFolder_fnBindToStorage,
978         IShellFolder_fnCompareIDs,
979         IShellFolder_fnCreateViewObject,
980         IShellFolder_fnGetAttributesOf,
981         IShellFolder_fnGetUIObjectOf,
982         IShellFolder_fnGetDisplayNameOf,
983         IShellFolder_fnSetNameOf,
984         IShellFolder_fnGetFolderPath
985 };
986
987 /************************************************************************
988  * ISFPersistFolder_QueryInterface (IUnknown)
989  *
990  * See Windows documentation for more details on IUnknown methods.
991  */
992 static HRESULT WINAPI ISFPersistFolder_QueryInterface(                                        
993         IPersistFolder *        iface,
994         REFIID                  iid,
995         LPVOID*                 ppvObj)
996 {
997         _ICOM_THIS_From_IPersistFolder(IGenericSFImpl, iface);
998
999         return IShellFolder_QueryInterface((IShellFolder*)This, iid, ppvObj);
1000 }
1001
1002 /************************************************************************
1003  * ISFPersistFolder_AddRef (IUnknown)
1004  *
1005  * See Windows documentation for more details on IUnknown methods.
1006  */
1007 static ULONG WINAPI ISFPersistFolder_AddRef(
1008         IPersistFolder *        iface)
1009 {
1010         _ICOM_THIS_From_IPersistFolder(IShellFolder, iface);
1011
1012         return IShellFolder_AddRef((IShellFolder*)This);
1013 }
1014
1015 /************************************************************************
1016  * ISFPersistFolder_Release (IUnknown)
1017  *
1018  * See Windows documentation for more details on IUnknown methods.
1019  */
1020 static ULONG WINAPI ISFPersistFolder_Release(
1021         IPersistFolder *        iface)
1022 {
1023         _ICOM_THIS_From_IPersistFolder(IGenericSFImpl, iface);
1024
1025         return IShellFolder_Release((IShellFolder*)This);
1026 }
1027
1028 /************************************************************************
1029  * ISFPersistFolder_GetClassID (IPersist)
1030  *
1031  * See Windows documentation for more details on IPersist methods.
1032  */
1033 static HRESULT WINAPI ISFPersistFolder_GetClassID(
1034         const IPersistFolder *  iface,
1035         LPCLSID               lpClassId)
1036 {
1037         /* This ID is not documented anywhere but some tests in Windows tell 
1038          * me that This is the ID for the "standard" implementation of the 
1039          * IFolder interface. 
1040          */
1041
1042         CLSID StdFolderID = { 0xF3364BA0, 0x65B9, 0x11CE, {0xA9, 0xBA, 0x00, 0xAA, 0x00, 0x4A, 0xE8, 0x37} };
1043
1044         if (lpClassId==NULL)
1045           return E_POINTER;
1046
1047         memcpy(lpClassId, &StdFolderID, sizeof(StdFolderID));
1048
1049         return S_OK;
1050 }
1051
1052 /************************************************************************
1053  * ISFPersistFolder_Initialize (IPersistFolder)
1054  *
1055  * See Windows documentation for more details on IPersistFolder methods.
1056  */
1057 static HRESULT WINAPI ISFPersistFolder_Initialize(
1058         IPersistFolder *        iface,
1059         LPCITEMIDLIST           pidl)
1060 {
1061         _ICOM_THIS_From_IPersistFolder(IGenericSFImpl, iface);
1062
1063         if(This->pMyPidl)
1064         { SHFree(This->pMyPidl);
1065           This->pMyPidl = NULL;
1066         }
1067         This->pMyPidl = ILClone(pidl);
1068         return S_OK;
1069 }
1070
1071 static ICOM_VTABLE(IPersistFolder) psfvt = 
1072 {
1073         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1074         ISFPersistFolder_QueryInterface,
1075         ISFPersistFolder_AddRef,
1076         ISFPersistFolder_Release,
1077         ISFPersistFolder_GetClassID,
1078         ISFPersistFolder_Initialize
1079 };