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