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