Small fixes.
[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 "debug.h"
13 #include "winerror.h"
14
15 #include "wine/obj_base.h"
16 #include "shlguid.h"
17
18 #include "pidl.h"
19 #include "objbase.h"
20 #include "shlobj.h"
21 #include "shell32_main.h"
22
23 static HRESULT WINAPI IShellFolder_QueryInterface(LPSHELLFOLDER,REFIID,LPVOID*);
24 static ULONG WINAPI IShellFolder_AddRef(LPSHELLFOLDER);
25 static ULONG WINAPI IShellFolder_Release(LPSHELLFOLDER);
26 static HRESULT WINAPI IShellFolder_Initialize(LPSHELLFOLDER,LPCITEMIDLIST);
27 static HRESULT WINAPI IShellFolder_ParseDisplayName(LPSHELLFOLDER,HWND32,LPBC,LPOLESTR32,DWORD*,LPITEMIDLIST*,DWORD*);
28 static HRESULT WINAPI IShellFolder_EnumObjects(LPSHELLFOLDER,HWND32,DWORD,LPENUMIDLIST*);
29 static HRESULT WINAPI IShellFolder_BindToObject(LPSHELLFOLDER,LPCITEMIDLIST,LPBC,REFIID,LPVOID*);
30 static HRESULT WINAPI IShellFolder_BindToStorage(LPSHELLFOLDER,LPCITEMIDLIST,LPBC,REFIID,LPVOID*);
31 static HRESULT WINAPI IShellFolder_CompareIDs(LPSHELLFOLDER,LPARAM,LPCITEMIDLIST,LPCITEMIDLIST);
32 static HRESULT WINAPI IShellFolder_CreateViewObject(LPSHELLFOLDER,HWND32,REFIID,LPVOID*);
33 static HRESULT WINAPI IShellFolder_GetAttributesOf(LPSHELLFOLDER,UINT32,LPCITEMIDLIST*,DWORD*);
34 static HRESULT WINAPI IShellFolder_GetUIObjectOf(LPSHELLFOLDER,HWND32,UINT32,LPCITEMIDLIST*,REFIID,UINT32*,LPVOID*);
35 static HRESULT WINAPI IShellFolder_GetDisplayNameOf(LPSHELLFOLDER,LPCITEMIDLIST,DWORD,LPSTRRET);
36 static HRESULT WINAPI IShellFolder_SetNameOf(LPSHELLFOLDER,HWND32,LPCITEMIDLIST,LPCOLESTR32,DWORD,LPITEMIDLIST*);
37 static BOOL32 WINAPI IShellFolder_GetFolderPath(LPSHELLFOLDER,LPSTR,DWORD);
38
39 /***************************************************************************
40  *  GetNextElement (internal function)
41  *
42  * gets a part of a string till the first backslash
43  *
44  * PARAMETERS
45  *  pszNext [IN] string to get the element from
46  *  pszOut  [IN] pointer to buffer whitch receives string
47  *  dwOut   [IN] length of pszOut
48  *
49  *  RETURNS
50  *    LPSTR pointer to first, not yet parsed char
51  */
52 LPSTR GetNextElement(LPSTR pszNext,LPSTR pszOut,DWORD dwOut)
53 {       LPSTR   pszTail = pszNext;
54         DWORD dwCopy;
55         TRACE(shell,"(%s %p 0x%08lx)\n",debugstr_a(pszNext),pszOut,dwOut);
56
57         if(!pszNext || !*pszNext)
58           return NULL;
59
60         while(*pszTail && (*pszTail != '\\'))
61         { pszTail++;
62         }
63         dwCopy=((LPBYTE)pszTail-(LPBYTE)pszNext)/sizeof(CHAR)+1;
64         lstrcpyn32A(pszOut, pszNext, (dwOut<dwCopy)? dwOut : dwCopy);
65
66         if(*pszTail)
67         {  pszTail++;
68         }
69
70         TRACE(shell,"--(%s %s 0x%08lx)\n",debugstr_a(pszNext),debugstr_a(pszOut),dwOut);
71         return pszTail;
72 }
73
74 /***********************************************************************
75 *   IShellFolder implementation
76 */
77 static struct IShellFolder_VTable sfvt = 
78 { IShellFolder_QueryInterface,
79   IShellFolder_AddRef,
80   IShellFolder_Release,
81   IShellFolder_ParseDisplayName,
82   IShellFolder_EnumObjects,
83   IShellFolder_BindToObject,
84   IShellFolder_BindToStorage,
85   IShellFolder_CompareIDs,
86   IShellFolder_CreateViewObject,
87   IShellFolder_GetAttributesOf,
88   IShellFolder_GetUIObjectOf,
89   IShellFolder_GetDisplayNameOf,
90   IShellFolder_SetNameOf,
91   IShellFolder_GetFolderPath
92 };
93 /**************************************************************************
94 *         IShellFolder_Constructor
95 */
96
97 LPSHELLFOLDER IShellFolder_Constructor(LPSHELLFOLDER pParent,LPITEMIDLIST pidl) 
98 {       LPSHELLFOLDER    sf;
99         DWORD dwSize=0;
100         sf=(LPSHELLFOLDER)HeapAlloc(GetProcessHeap(),0,sizeof(IShellFolder));
101         sf->ref=1;
102         sf->lpvtbl=&sfvt;
103         sf->sMyPath=NULL;       /* path of the folder */
104         sf->pMyPidl=NULL;       /* my qualified pidl */
105
106         TRACE(shell,"(%p)->(parent=%p, pidl=%p)\n",sf,pParent, pidl);
107         pdump(pidl);
108                 
109         /* keep a copy of the pidl in the instance*/
110         sf->mpidl = ILClone(pidl);              /* my short pidl */
111         
112         if(sf->mpidl)                           /* do we have a pidl? */
113         { dwSize = 0;
114           if(pParent->sMyPath)                  /* get the size of the parents path */
115           { dwSize += strlen(pParent->sMyPath) ;
116             TRACE(shell,"-- (%p)->(parent's path=%s)\n",sf, debugstr_a(pParent->sMyPath));
117           }   
118           dwSize += _ILGetFolderText(sf->mpidl,NULL,0); /* add the size of the foldername*/
119           sf->sMyPath = SHAlloc(dwSize+2);              /* '\0' and backslash */
120           if(sf->sMyPath)
121           { int len;
122             *(sf->sMyPath)=0x00;
123             if(pParent->sMyPath)                        /* if the parent has a path, get it*/
124             {  strcpy(sf->sMyPath, pParent->sMyPath);
125                PathAddBackslash32A (sf->sMyPath);
126             }
127             sf->pMyPidl = ILCombine(pParent->pMyPidl, pidl);
128             len = strlen(sf->sMyPath);
129             _ILGetFolderText(sf->mpidl, sf->sMyPath+len, dwSize-len);
130             TRACE(shell,"-- (%p)->(my pidl=%p, my path=%s)\n",sf, sf->pMyPidl,debugstr_a(sf->sMyPath));
131             pdump (sf->pMyPidl);
132           }
133         }
134         shell32_ObjCount++;
135         return sf;
136 }
137 /**************************************************************************
138 *  IShellFolder::QueryInterface
139 * PARAMETERS
140 *  REFIID riid,        //[in ] Requested InterfaceID
141 *  LPVOID* ppvObject)  //[out] Interface* to hold the result
142 */
143 static HRESULT WINAPI IShellFolder_QueryInterface(
144   LPSHELLFOLDER this, REFIID riid, LPVOID *ppvObj)
145 {       char    xriid[50];
146         WINE_StringFromCLSID((LPCLSID)riid,xriid);
147         TRACE(shell,"(%p)->(\n\tIID:\t%s,%p)\n",this,xriid,ppvObj);
148
149         *ppvObj = NULL;
150
151         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
152         { *ppvObj = this; 
153         }
154         else if(IsEqualIID(riid, &IID_IShellFolder))  /*IShellFolder*/
155         {    *ppvObj = (IShellFolder*)this;
156         }   
157
158         if(*ppvObj)
159         { (*(LPSHELLFOLDER*)ppvObj)->lpvtbl->fnAddRef(this);    
160           TRACE(shell,"-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
161           return S_OK;
162         }
163         TRACE(shell,"-- Interface: E_NOINTERFACE\n");
164         return E_NOINTERFACE;
165 }   
166
167 /**************************************************************************
168 *  IShellFolder::AddRef
169 */
170
171 static ULONG WINAPI IShellFolder_AddRef(LPSHELLFOLDER this)
172 {       TRACE(shell,"(%p)->(count=%lu)\n",this,this->ref);
173         shell32_ObjCount++;
174         return ++(this->ref);
175 }
176
177 /**************************************************************************
178  *  IShellFolder_Release
179  */
180 static ULONG WINAPI IShellFolder_Release(LPSHELLFOLDER this) 
181 {       TRACE(shell,"(%p)->(count=%lu)\n",this,this->ref);
182
183         shell32_ObjCount--;
184         if (!--(this->ref)) 
185         { TRACE(shell,"-- destroying IShellFolder(%p)\n",this);
186
187           if (pdesktopfolder==this)
188           { pdesktopfolder=NULL;
189             TRACE(shell,"-- destroyed IShellFolder(%p) was Desktopfolder\n",this);
190           }
191           if(this->pMyPidl)
192           { SHFree(this->pMyPidl);
193           }
194           if(this->mpidl)
195           { SHFree(this->mpidl);
196           }
197           if(this->sMyPath)
198           { SHFree(this->sMyPath);
199           }
200
201           HeapFree(GetProcessHeap(),0,this);
202
203           return 0;
204         }
205         return this->ref;
206 }
207 /**************************************************************************
208 *               IShellFolder_ParseDisplayName
209 * PARAMETERS
210 *  HWND          hwndOwner,      //[in ] Parent window for any message's
211 *  LPBC          pbc,            //[in ] reserved
212 *  LPOLESTR      lpszDisplayName,//[in ] "Unicode" displayname.
213 *  ULONG*        pchEaten,       //[out] (unicode) characters processed
214 *  LPITEMIDLIST* ppidl,          //[out] complex pidl to item
215 *  ULONG*        pdwAttributes   //[out] items attributes
216 *
217 * FIXME: 
218 *    pdwAttributes: not used
219 */
220 static HRESULT WINAPI IShellFolder_ParseDisplayName(
221         LPSHELLFOLDER this,
222         HWND32 hwndOwner,
223         LPBC pbcReserved,
224         LPOLESTR32 lpszDisplayName,
225         DWORD *pchEaten,
226         LPITEMIDLIST *ppidl,
227         DWORD *pdwAttributes)
228 {       HRESULT        hr=E_OUTOFMEMORY;
229         LPITEMIDLIST   pidlFull=NULL, pidlTemp = NULL, pidlOld = NULL;
230         LPSTR          pszNext=NULL;
231         CHAR           szTemp[MAX_PATH],szElement[MAX_PATH];
232         BOOL32         bIsFile;
233        
234         TRACE(shell,"(%p)->(HWND=0x%08x,%p,%p=%s,%p,pidl=%p,%p)\n",
235                 this,hwndOwner,pbcReserved,lpszDisplayName,
236                 debugstr_w(lpszDisplayName),pchEaten,ppidl,pdwAttributes);
237
238         { hr = E_FAIL;
239           WideCharToLocal32(szTemp, lpszDisplayName, lstrlen32W(lpszDisplayName) + 1);
240           if(szTemp[0])
241           { if (strcmp(szTemp,"Desktop")==0)
242             { pidlFull = _ILCreateDesktop();
243             }
244             else if (strcmp(szTemp,"My Computer")==0)
245             { pidlFull = _ILCreateMyComputer();
246             }
247             else
248             { if (!PathIsRoot32A(szTemp))
249               { if (this->sMyPath && strlen (this->sMyPath))
250                 { if (strcmp(this->sMyPath,"My Computer"))
251                   { strcpy (szElement,this->sMyPath);
252                     PathAddBackslash32A (szElement);
253                     strcat (szElement, szTemp);
254                     strcpy (szTemp, szElement);
255                   }
256                 }
257               }
258               
259               /* check if the lpszDisplayName is Folder or File*/
260               bIsFile = ! (GetFileAttributes32A(szTemp) & FILE_ATTRIBUTE_DIRECTORY);
261               pszNext = GetNextElement(szTemp, szElement, MAX_PATH);
262
263               pidlFull = _ILCreateMyComputer();
264               pidlTemp = _ILCreateDrive(szElement);                     
265               pidlOld = pidlFull;
266               pidlFull = ILCombine(pidlFull,pidlTemp);
267               SHFree(pidlOld);
268   
269               if(pidlFull)
270               { while((pszNext=GetNextElement(pszNext, szElement, MAX_PATH)))
271                 { if(!*pszNext && bIsFile)
272                   { pidlTemp = _ILCreateValue(szElement);
273                   }
274                   else                          
275                   { pidlTemp = _ILCreateFolder(szElement);
276                   }
277                   pidlOld = pidlFull;
278                   pidlFull = ILCombine(pidlFull,pidlTemp);
279                   SHFree(pidlOld);
280                 }
281                 hr = S_OK;
282               }
283             }
284           }
285         }
286         *ppidl = pidlFull;
287         return hr;
288 }
289
290 /**************************************************************************
291 *               IShellFolder_EnumObjects
292 * PARAMETERS
293 *  HWND          hwndOwner,    //[in ] Parent Window
294 *  DWORD         grfFlags,     //[in ] SHCONTF enumeration mask
295 *  LPENUMIDLIST* ppenumIDList  //[out] IEnumIDList interface
296 */
297 static HRESULT WINAPI IShellFolder_EnumObjects(
298         LPSHELLFOLDER this,
299         HWND32 hwndOwner,
300         DWORD dwFlags,
301         LPENUMIDLIST* ppEnumIDList)
302 {       TRACE(shell,"(%p)->(HWND=0x%08x flags=0x%08lx pplist=%p)\n",this,hwndOwner,dwFlags,ppEnumIDList);
303
304         *ppEnumIDList = NULL;
305         *ppEnumIDList = IEnumIDList_Constructor (this->sMyPath, dwFlags);
306         TRACE(shell,"-- (%p)->(new ID List: %p)\n",this,*ppEnumIDList);
307         if(!*ppEnumIDList)
308         { return E_OUTOFMEMORY;
309         }
310         return S_OK;            
311 }
312 /**************************************************************************
313  *  IShellFolder_Initialize()
314  *  IPersistFolder Method
315  */
316 static HRESULT WINAPI IShellFolder_Initialize( LPSHELLFOLDER this,LPCITEMIDLIST pidl)
317 {       TRACE(shell,"(%p)->(pidl=%p)\n",this,pidl);
318
319         if(this->pMyPidl)
320         { SHFree(this->pMyPidl);
321           this->pMyPidl = NULL;
322         }
323         this->pMyPidl = ILClone(pidl);
324         return S_OK;
325 }
326
327 /**************************************************************************
328 *               IShellFolder_BindToObject
329 * PARAMETERS
330 *  LPCITEMIDLIST pidl,       //[in ] complex pidl to open
331 *  LPBC          pbc,        //[in ] reserved
332 *  REFIID        riid,       //[in ] Initial Interface
333 *  LPVOID*       ppvObject   //[out] Interface*
334 */
335 static HRESULT WINAPI IShellFolder_BindToObject( LPSHELLFOLDER this, LPCITEMIDLIST pidl,
336                         LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
337 {       char            xriid[50];
338         HRESULT       hr;
339         LPSHELLFOLDER pShellFolder;
340         
341         WINE_StringFromCLSID(riid,xriid);
342
343         TRACE(shell,"(%p)->(pidl=%p,%p,\n\tIID:%s,%p)\n",this,pidl,pbcReserved,xriid,ppvOut);
344
345         *ppvOut = NULL;
346
347         pShellFolder = IShellFolder_Constructor(this, pidl);
348
349         if(!pShellFolder)
350           return E_OUTOFMEMORY;
351
352         hr = pShellFolder->lpvtbl->fnQueryInterface(pShellFolder, riid, ppvOut);
353         pShellFolder->lpvtbl->fnRelease(pShellFolder);
354         TRACE(shell,"-- (%p)->(interface=%p)\n",this, ppvOut);
355         return hr;
356 }
357
358 /**************************************************************************
359 *  IShellFolder_BindToStorage
360 * PARAMETERS
361 *  LPCITEMIDLIST pidl,       //[in ] complex pidl to store
362 *  LPBC          pbc,        //[in ] reserved
363 *  REFIID        riid,       //[in ] Initial storage interface 
364 *  LPVOID*       ppvObject   //[out] Interface* returned
365 */
366 static HRESULT WINAPI IShellFolder_BindToStorage(LPSHELLFOLDER this,
367             LPCITEMIDLIST pidl,LPBC pbcReserved, REFIID riid, LPVOID *ppvOut)
368 {       char xriid[50];
369         WINE_StringFromCLSID(riid,xriid);
370
371         FIXME(shell,"(%p)->(pidl=%p,%p,\n\tIID:%s,%p) stub\n",this,pidl,pbcReserved,xriid,ppvOut);
372
373         *ppvOut = NULL;
374         return E_NOTIMPL;
375 }
376
377 /**************************************************************************
378 *  IShellFolder_CompareIDs
379 *
380 * PARMETERS
381 *  LPARAM        lParam, //[in ] Column?
382 *  LPCITEMIDLIST pidl1,  //[in ] simple pidl
383 *  LPCITEMIDLIST pidl2)  //[in ] simple pidl
384 *
385 * NOTES
386 *   Special case - If one of the items is a Path and the other is a File,
387 *   always make the Path come before the File.
388 *
389 * FIXME
390 *  we have to handle simple pidl's only (?)
391 */
392 static HRESULT WINAPI  IShellFolder_CompareIDs(LPSHELLFOLDER this,
393                  LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2) 
394 { CHAR szString1[MAX_PATH] = "";
395   CHAR szString2[MAX_PATH] = "";
396   int   nReturn;
397   LPCITEMIDLIST  pidlTemp1 = pidl1, pidlTemp2 = pidl2;
398
399   TRACE(shell,"(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n",this,lParam,pidl1,pidl2);
400   pdump (pidl1);
401   pdump (pidl2);
402
403   if (!pidl1 && !pidl2)
404     return 0;
405   if (!pidl1)   /* Desktop < anything */
406     return -1;
407   if (!pidl2)
408     return 1;
409   
410   /* get the last item in each list */
411   while((ILGetNext(pidlTemp1))->mkid.cb)
412     pidlTemp1 = ILGetNext(pidlTemp1);
413   while((ILGetNext(pidlTemp2))->mkid.cb)
414     pidlTemp2 = ILGetNext(pidlTemp2);
415
416   /* at this point, both pidlTemp1 and pidlTemp2 point to the last item in the list */
417   if(_ILIsValue(pidlTemp1) != _ILIsValue(pidlTemp2))
418   { if(_ILIsValue(pidlTemp1))
419       return 1;
420    return -1;
421   }
422
423   _ILGetDrive( pidl1,szString1,sizeof(szString1));
424   _ILGetDrive( pidl2,szString1,sizeof(szString2));
425   nReturn = strcasecmp(szString1, szString2);
426
427   if(nReturn)
428     return nReturn;
429
430   _ILGetFolderText( pidl1,szString1,sizeof(szString1));
431   _ILGetFolderText( pidl2,szString2,sizeof(szString2));
432   nReturn = strcasecmp(szString1, szString2);
433
434   if(nReturn)
435     return nReturn;
436
437   _ILGetValueText(pidl1,szString1,sizeof(szString1));
438   _ILGetValueText(pidl2,szString2,sizeof(szString2));
439   return strcasecmp(szString1, szString2);
440 }
441
442 /**************************************************************************
443 *         IShellFolder_CreateViewObject
444 * Creates an View Object representing the ShellFolder
445 *  IShellView / IShellBrowser / IContextMenu
446 *
447 * PARAMETERS
448 *  HWND    hwndOwner,  // Handle of owner window
449 *  REFIID  riid,       // Requested initial interface
450 *  LPVOID* ppvObject)  // Resultant interface*
451 *
452 * NOTES
453 *  the same as SHCreateShellFolderViewEx ???
454 */
455 static HRESULT WINAPI IShellFolder_CreateViewObject( LPSHELLFOLDER this,
456                  HWND32 hwndOwner, REFIID riid, LPVOID *ppvOut)
457 {       LPSHELLVIEW pShellView;
458         char    xriid[50];
459         HRESULT       hr;
460
461         WINE_StringFromCLSID(riid,xriid);
462         TRACE(shell,"(%p)->(hwnd=0x%x,\n\tIID:\t%s,%p)\n",this,hwndOwner,xriid,ppvOut);
463         
464         *ppvOut = NULL;
465
466         pShellView = IShellView_Constructor(this, this->mpidl);
467
468         if(!pShellView)
469           return E_OUTOFMEMORY;
470           
471         hr = pShellView->lpvtbl->fnQueryInterface(pShellView, riid, ppvOut);
472         pShellView->lpvtbl->fnRelease(pShellView);
473         TRACE(shell,"-- (%p)->(interface=%p)\n",this, ppvOut);
474         return hr; 
475 }
476
477 /**************************************************************************
478 *  IShellFolder_GetAttributesOf
479 *
480 * PARAMETERS
481 *  UINT            cidl,     //[in ] num elements in pidl array
482 +  LPCITEMIDLIST*  apidl,    //[in ] simple pidl array 
483 *  ULONG*          rgfInOut) //[out] result array  
484 *
485 * FIXME: quick hack
486 *  Note: rgfInOut is documented as being an array of ULONGS.
487 *  This does not seem to be the case. Testing this function using the shell to 
488 *  call it with cidl > 1 (by deleting multiple items) reveals that the shell
489 *  passes ONE element in the array and writing to further elements will
490 *  cause the shell to fail later.
491 */
492 static HRESULT WINAPI IShellFolder_GetAttributesOf(LPSHELLFOLDER this,UINT32 cidl,LPCITEMIDLIST *apidl,DWORD *rgfInOut)
493 { LPCITEMIDLIST * pidltemp;
494   DWORD i;
495
496   TRACE(shell,"(%p)->(%d,%p,%p)\n",this,cidl,apidl,rgfInOut);
497
498   if ((! cidl )| (!apidl) | (!rgfInOut))
499     return E_INVALIDARG;
500
501   pidltemp=apidl;
502   *rgfInOut = 0x00;
503   i=cidl;
504
505   TRACE(shell,"-- mask=0x%08lx\n",*rgfInOut);
506   
507   do
508   { if (*pidltemp)
509     { pdump (*pidltemp);
510       if (_ILIsDesktop( *pidltemp))
511       { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FOLDER | SFGAO_DROPTARGET | SFGAO_HASPROPSHEET | SFGAO_CANLINK );
512       }
513       else if (_ILIsMyComputer( *pidltemp))
514       { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR
515                         | SFGAO_DROPTARGET | SFGAO_HASPROPSHEET | SFGAO_CANRENAME | SFGAO_CANLINK );
516       }
517       else if (_ILIsDrive( *pidltemp))
518       { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FILESYSTEM  | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR  | 
519                         SFGAO_DROPTARGET | SFGAO_HASPROPSHEET | SFGAO_CANLINK );
520       }
521       else if (_ILIsFolder( *pidltemp))
522       { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_CAPABILITYMASK );
523       }
524       else if (_ILIsValue( *pidltemp))
525       { *rgfInOut |= (SFGAO_FILESYSTEM | SFGAO_CAPABILITYMASK );
526       }
527     }
528     pidltemp++;
529     cidl--;
530   } while (cidl > 0 && *pidltemp);
531
532   return S_OK;
533 }
534 /**************************************************************************
535 *  IShellFolder_GetUIObjectOf
536 *
537 * PARAMETERS
538 *  HWND           hwndOwner, //[in ] Parent window for any output
539 *  UINT           cidl,      //[in ] array size
540 *  LPCITEMIDLIST* apidl,     //[in ] simple pidl array
541 *  REFIID         riid,      //[in ] Requested Interface
542 *  UINT*          prgfInOut, //[   ] reserved 
543 *  LPVOID*        ppvObject) //[out] Resulting Interface
544 *
545 * NOTES
546 *  This function gets asked to return "view objects" for one or more (multiple select)
547 *  items:
548 *  The viewobject typically is an COM object with one of the following interfaces:
549 *  IExtractIcon,IDataObject,IContextMenu
550 *  In order to support icon positions in the default Listview your DataObject
551 *  must implement the SetData method (in addition to GetData :) - the shell passes
552 *  a barely documented "Icon positions" structure to SetData when the drag starts,
553 *  and GetData's it if the drop is in another explorer window that needs the positions.
554 */
555 static HRESULT WINAPI IShellFolder_GetUIObjectOf( LPSHELLFOLDER this,HWND32 hwndOwner,UINT32 cidl,
556  LPCITEMIDLIST * apidl, REFIID riid, UINT32 * prgfInOut,LPVOID * ppvOut)
557 {       char            xclsid[50];
558         LPITEMIDLIST    pidl;
559         LPUNKNOWN       pObj = NULL; 
560    
561         WINE_StringFromCLSID(riid,xclsid);
562
563         TRACE(shell,"(%p)->(%u,%u,apidl=%p,\n\tIID:%s,%p,%p)\n",
564           this,hwndOwner,cidl,apidl,xclsid,prgfInOut,ppvOut);
565
566         *ppvOut = NULL;
567
568         if(IsEqualIID(riid, &IID_IContextMenu))
569         { if(cidl < 1)
570             return E_INVALIDARG;
571           pObj  = (LPUNKNOWN)IContextMenu_Constructor(this, apidl, cidl);
572         }
573         else if (IsEqualIID(riid, &IID_IDataObject))
574         { if (cidl < 1)
575             return(E_INVALIDARG);
576           pObj = (LPUNKNOWN)IDataObject_Constructor (hwndOwner, this, apidl, cidl);
577         }
578         else if(IsEqualIID(riid, &IID_IExtractIcon))
579         { if (cidl != 1)
580             return(E_INVALIDARG);
581           pidl = ILCombine(this->pMyPidl,apidl[0]);
582           pObj = (LPUNKNOWN)IExtractIcon_Constructor( pidl );
583           SHFree(pidl);
584         } 
585         else
586         { ERR(shell,"(%p)->E_NOINTERFACE\n",this);
587           return E_NOINTERFACE;
588         }
589         if(!pObj)
590           return E_OUTOFMEMORY;
591
592         *ppvOut = pObj;
593         return S_OK;
594 }
595 /**************************************************************************
596 *  IShellFolder_GetDisplayNameOf
597 *  Retrieves the display name for the specified file object or subfolder
598 *
599 * PARAMETERS
600 *  LPCITEMIDLIST pidl,    //[in ] complex pidl to item
601 *  DWORD         dwFlags, //[in ] SHGNO formatting flags
602 *  LPSTRRET      lpName)  //[out] Returned display name
603 *
604 * FIXME
605 *  if the name is in the pidl the ret value should be a STRRET_OFFSET
606 */
607 #define GET_SHGDN_FOR(dwFlags)         ((DWORD)dwFlags & (DWORD)0x0000FF00)
608 #define GET_SHGDN_RELATION(dwFlags)    ((DWORD)dwFlags & (DWORD)0x000000FF)
609
610 static HRESULT WINAPI IShellFolder_GetDisplayNameOf( LPSHELLFOLDER this, LPCITEMIDLIST pidl, DWORD dwFlags, LPSTRRET lpName)
611 {       CHAR    szText[MAX_PATH];
612         CHAR    szTemp[MAX_PATH];
613         CHAR    szSpecial[MAX_PATH];
614         CHAR    szDrive[MAX_PATH];
615         DWORD   dwVolumeSerialNumber,dwMaximumComponetLength,dwFileSystemFlags;
616         LPITEMIDLIST    pidlTemp=NULL;
617         BOOL32  bSimplePidl=FALSE;
618                 
619         TRACE(shell,"(%p)->(pidl=%p,0x%08lx,%p)\n",this,pidl,dwFlags,lpName);
620         pdump(pidl);
621         
622         szSpecial[0]=0x00; 
623         szDrive[0]=0x00;
624         szText[0]=0x00;
625         szTemp[0]=0x00;
626         
627         /* test if simple(relative) or complex(absolute) pidl */
628         pidlTemp = ILGetNext(pidl);
629         if (pidlTemp && pidlTemp->mkid.cb==0x00)
630         { bSimplePidl = TRUE;
631           TRACE(shell,"-- simple pidl\n");
632         }
633
634         if (_ILIsDesktop( pidl))
635         { strcpy (szText,"Desktop");
636         }       
637         else
638         { if (_ILIsMyComputer(pidl))
639           { _ILGetItemText(pidl, szSpecial, MAX_PATH);
640             pidl = ILGetNext(pidl);
641           }
642
643           if (_ILIsDrive(pidl))
644           { _ILGetDrive( pidl, szTemp, MAX_PATH);
645
646             if ( dwFlags==SHGDN_NORMAL || dwFlags==SHGDN_INFOLDER)      /* like "A1-dos (C:)" */
647             { GetVolumeInformation32A(szTemp,szDrive,MAX_PATH,&dwVolumeSerialNumber,&dwMaximumComponetLength,&dwFileSystemFlags,NULL,0);
648               szTemp[2]=0x00;                                           /* overwrite '\' */
649               strcat (szDrive," (");
650               strcat (szDrive,szTemp);
651               strcat (szDrive,")"); 
652             }
653             else                                                        /* like "C:\" */
654             {  PathAddBackslash32A (szTemp);
655                strcpy(szDrive,szTemp);
656             }
657           }
658
659                 
660           switch(dwFlags)
661           { case SHGDN_NORMAL:                          /* 0x0000 */
662               _ILGetPidlPath( pidl, szText, MAX_PATH);
663               break;
664
665             case SHGDN_INFOLDER | SHGDN_FORPARSING:     /* 0x8001 */
666             case SHGDN_INFOLDER:                        /* 0x0001 */
667               pidlTemp = ILFindLastID(pidl);
668               if (pidlTemp)
669               { _ILGetItemText( pidlTemp, szText, MAX_PATH);
670               }
671               break;                            
672
673             case SHGDN_FORPARSING:                      /* 0x8000 */
674               if (bSimplePidl)
675               { /* if the IShellFolder has parents, get the path from the
676                 parent and add the ItemName*/
677                 szText[0]=0x00;
678                 if (this->sMyPath && strlen (this->sMyPath))
679                 { if (strcmp(this->sMyPath,"My Computer"))
680                   { strcpy (szText,this->sMyPath);
681                     PathAddBackslash32A (szText);
682                   }
683                 }
684                 pidlTemp = ILFindLastID(pidl);
685                 if (pidlTemp)
686                 { _ILGetItemText( pidlTemp, szTemp, MAX_PATH );
687                 } 
688                 strcat(szText,szTemp);
689               }
690               else      /* if the pidl is absolute, get everything from the pidl*/                                      
691               { _ILGetPidlPath( pidl, szText, MAX_PATH);
692               }
693               break;
694             default:
695               TRACE(shell,"--- wrong flags=%lx\n", dwFlags);
696               return E_INVALIDARG;
697           }
698           if ((szText[0]==0x00 && szDrive[0]!=0x00)|| (bSimplePidl && szDrive[0]!=0x00))
699           { strcpy(szText,szDrive);
700           }
701           if (szText[0]==0x00 && szSpecial[0]!=0x00)
702           { strcpy(szText,szSpecial);
703           }
704         }
705   
706         TRACE(shell,"-- (%p)->(%s)\n",this,szText);
707
708         if(!(lpName))
709         {  return E_OUTOFMEMORY;
710         }
711         lpName->uType = STRRET_CSTRA;   
712         strcpy(lpName->u.cStr,szText);
713         return S_OK;
714 }
715
716 /**************************************************************************
717 *  IShellFolder_SetNameOf
718 *  Changes the name of a file object or subfolder, possibly changing its item
719 *  identifier in the process.
720 *
721 * PARAMETERS
722 *  HWND          hwndOwner,  //[in ] Owner window for output
723 *  LPCITEMIDLIST pidl,       //[in ] simple pidl of item to change
724 *  LPCOLESTR     lpszName,   //[in ] the items new display name
725 *  DWORD         dwFlags,    //[in ] SHGNO formatting flags
726 *  LPITEMIDLIST* ppidlOut)   //[out] simple pidl returned
727 */
728 static HRESULT WINAPI IShellFolder_SetNameOf(
729         LPSHELLFOLDER this,
730                 HWND32 hwndOwner, 
731     LPCITEMIDLIST pidl, /*simple pidl*/
732     LPCOLESTR32 lpName, 
733     DWORD dw, 
734     LPITEMIDLIST *pPidlOut)
735 {  FIXME(shell,"(%p)->(%u,pidl=%p,%s,%lu,%p),stub!\n",
736           this,hwndOwner,pidl,debugstr_w(lpName),dw,pPidlOut);
737          return E_NOTIMPL;
738 }
739 /**************************************************************************
740 *  IShellFolder_GetFolderPath
741 *  FIXME: drive not included
742 */
743 static BOOL32 WINAPI IShellFolder_GetFolderPath(LPSHELLFOLDER this, LPSTR lpszOut, DWORD dwOutSize)
744 {       DWORD   dwSize;
745
746         TRACE(shell,"(%p)->(%p %lu)\n",this, lpszOut, dwOutSize);
747         if (!lpszOut)
748         { return FALSE;
749         }
750     
751         *lpszOut=0;
752
753         if (! this->sMyPath)
754           return FALSE;
755           
756         dwSize = strlen (this->sMyPath) +1;
757         if ( dwSize > dwOutSize)
758           return FALSE;
759         strcpy(lpszOut, this->sMyPath);
760
761         TRACE(shell,"-- (%p)->(return=%s)\n",this, lpszOut);
762         return TRUE;
763 }