Added more implementation of IDocumentView.
[wine] / dlls / shell32 / folders.c
1 /*
2  *      Copyright 1997  Marcus Meissner
3  *      Copyright 1998  Juergen Schmied
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #define COBJMACROS
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "objbase.h"
34 #include "undocshell.h"
35 #include "shlguid.h"
36 #include "winreg.h"
37
38 #include "wine/debug.h"
39
40 #include "pidl.h"
41 #include "shell32_main.h"
42 #include "shfldr.h"
43 #include "shresdef.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(shell);
46
47 /***********************************************************************
48 *   IExtractIconW implementation
49 */
50 typedef struct
51 {
52         IExtractIconWVtbl *lpVtbl;
53         DWORD              ref;
54         IPersistFileVtbl  *lpvtblPersistFile;
55         IExtractIconAVtbl *lpvtblExtractIconA;
56         LPITEMIDLIST       pidl;
57 } IExtractIconWImpl;
58
59 static struct IExtractIconAVtbl eiavt;
60 static struct IExtractIconWVtbl eivt;
61 static struct IPersistFileVtbl pfvt;
62
63 #define _IPersistFile_Offset ((int)(&(((IExtractIconWImpl*)0)->lpvtblPersistFile)))
64 #define _ICOM_THIS_From_IPersistFile(class, name) class* This = (class*)(((char*)name)-_IPersistFile_Offset);
65
66 #define _IExtractIconA_Offset ((int)(&(((IExtractIconWImpl*)0)->lpvtblExtractIconA)))
67 #define _ICOM_THIS_From_IExtractIconA(class, name) class* This = (class*)(((char*)name)-_IExtractIconA_Offset);
68
69 /**************************************************************************
70 *  IExtractIconW_Constructor
71 */
72 IExtractIconW* IExtractIconW_Constructor(LPCITEMIDLIST pidl)
73 {
74         IExtractIconWImpl* ei;
75         
76         TRACE("%p\n", pidl);
77
78         ei = HeapAlloc(GetProcessHeap(),0,sizeof(IExtractIconWImpl));
79         ei->ref=1;
80         ei->lpVtbl = &eivt;
81         ei->lpvtblPersistFile = &pfvt;
82         ei->lpvtblExtractIconA = &eiavt;
83         ei->pidl=ILClone(pidl);
84
85         pdump(pidl);
86
87         TRACE("(%p)\n", ei);
88         return (IExtractIconW *)ei;
89 }
90 /**************************************************************************
91  *  IExtractIconW_QueryInterface
92  */
93 static HRESULT WINAPI IExtractIconW_fnQueryInterface(IExtractIconW *iface, REFIID riid, LPVOID *ppvObj)
94 {
95         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
96
97         TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, debugstr_guid(riid), ppvObj);
98
99         *ppvObj = NULL;
100
101         if (IsEqualIID(riid, &IID_IUnknown))                            /*IUnknown*/
102         {
103           *ppvObj = This;
104         }
105         else if (IsEqualIID(riid, &IID_IPersistFile))   /*IExtractIcon*/
106         {
107           *ppvObj = (IPersistFile*)&(This->lpvtblPersistFile);
108         }
109         else if (IsEqualIID(riid, &IID_IExtractIconA))  /*IExtractIcon*/
110         {
111           *ppvObj = (IExtractIconA*)&(This->lpvtblExtractIconA);
112         }
113         else if (IsEqualIID(riid, &IID_IExtractIconW))  /*IExtractIcon*/
114         {
115           *ppvObj = (IExtractIconW*)This;
116         }
117
118         if(*ppvObj)
119         {
120           IExtractIconW_AddRef((IExtractIconW*) *ppvObj);
121           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
122           return S_OK;
123         }
124         TRACE("-- Interface: E_NOINTERFACE\n");
125         return E_NOINTERFACE;
126 }
127
128 /**************************************************************************
129 *  IExtractIconW_AddRef
130 */
131 static ULONG WINAPI IExtractIconW_fnAddRef(IExtractIconW * iface)
132 {
133         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
134         ULONG refCount = InterlockedIncrement(&This->ref);
135
136         TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
137
138         return refCount;
139 }
140 /**************************************************************************
141 *  IExtractIconW_Release
142 */
143 static ULONG WINAPI IExtractIconW_fnRelease(IExtractIconW * iface)
144 {
145         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
146         ULONG refCount = InterlockedDecrement(&This->ref);
147
148         TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
149
150         if (!refCount)
151         {
152           TRACE(" destroying IExtractIcon(%p)\n",This);
153           SHFree(This->pidl);
154           HeapFree(GetProcessHeap(),0,This);
155           return 0;
156         }
157         return refCount;
158 }
159
160 static HRESULT getIconLocationForFolder(IExtractIconW *iface, UINT uFlags,
161  LPWSTR szIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
162 {
163     IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
164     DWORD dwNr;
165     WCHAR wszPath[MAX_PATH];
166     WCHAR wszCLSIDValue[CHARS_IN_GUID];
167     static const WCHAR shellClassInfo[] = { '.','S','h','e','l','l','C','l','a','s','s','I','n','f','o',0 };
168     static const WCHAR iconFile[] = { 'I','c','o','n','F','i','l','e',0 };
169     static const WCHAR clsid[] = { 'C','L','S','I','D',0 };
170     static const WCHAR clsid2[] = { 'C','L','S','I','D','2',0 };
171     static const WCHAR iconIndex[] = { 'I','c','o','n','I','n','d','e','x',0 };
172
173     if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconFile,
174         wszPath, MAX_PATH))
175     {
176         WCHAR wszIconIndex[10];
177         SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconIndex,
178             wszIconIndex, 10);
179         *piIndex = atoiW(wszIconIndex);
180     }
181     else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid,
182         wszCLSIDValue, CHARS_IN_GUID) &&
183         HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &dwNr))
184     {
185        *piIndex = dwNr;
186     }
187     else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid2,
188         wszCLSIDValue, CHARS_IN_GUID) &&
189         HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &dwNr))
190     {
191        *piIndex = dwNr;
192     }
193     else
194     {
195         static const WCHAR folder[] = { 'F','o','l','d','e','r',0 };
196
197         if (!HCR_GetDefaultIconW(folder, szIconFile, cchMax, &dwNr))
198         {
199             lstrcpynW(szIconFile, swShell32Name, cchMax);
200             dwNr = IDI_SHELL_FOLDER;
201         }
202         *piIndex = -((uFlags & GIL_OPENICON) ? dwNr + 1 : dwNr);
203     }
204     return S_OK;
205 }
206
207 WCHAR swShell32Name[MAX_PATH];
208
209 /**************************************************************************
210 *  IExtractIconW_GetIconLocation
211 *
212 * mapping filetype to icon
213 */
214 static HRESULT WINAPI IExtractIconW_fnGetIconLocation(
215         IExtractIconW * iface,
216         UINT uFlags,            /* GIL_ flags */
217         LPWSTR szIconFile,
218         UINT cchMax,
219         int * piIndex,
220         UINT * pwFlags)         /* returned GIL_ flags */
221 {
222         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
223
224         char    sTemp[MAX_PATH];
225         DWORD   dwNr;
226         GUID const * riid;
227         LPITEMIDLIST    pSimplePidl = ILFindLastID(This->pidl);
228
229         TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
230
231         if (pwFlags)
232           *pwFlags = 0;
233
234         if (_ILIsDesktop(pSimplePidl))
235         {
236           lstrcpynW(szIconFile, swShell32Name, cchMax);
237           *piIndex = -IDI_SHELL_DESKTOP;
238         }
239
240         /* my computer and other shell extensions */
241         else if ((riid = _ILGetGUIDPointer(pSimplePidl)))
242         {
243           static const WCHAR fmt[] = { 'C','L','S','I','D','\\',
244        '{','%','0','8','l','x','-','%','0','4','x','-','%','0','4','x','-',
245        '%','0','2','x','%','0','2','x','-','%','0','2','x', '%','0','2','x',
246        '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0 };
247           WCHAR xriid[50];
248
249           sprintfW(xriid, fmt,
250                   riid->Data1, riid->Data2, riid->Data3,
251                   riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
252                   riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]);
253
254           if (HCR_GetDefaultIconW(xriid, szIconFile, cchMax, &dwNr))
255           {
256             *piIndex = dwNr;
257           }
258           else
259           {
260             lstrcpynW(szIconFile, swShell32Name, cchMax);
261             if(IsEqualGUID(riid, &CLSID_MyComputer))
262                 *piIndex = -IDI_SHELL_MY_COMPUTER;
263             else if(IsEqualGUID(riid, &CLSID_MyDocuments))
264                 *piIndex = -IDI_SHELL_FOLDER;
265             else if(IsEqualGUID(riid, &CLSID_NetworkPlaces))
266                 *piIndex = -IDI_SHELL_MY_NETWORK_PLACES;
267             else
268                 *piIndex = -IDI_SHELL_FOLDER;
269           }
270         }
271
272         else if (_ILIsDrive (pSimplePidl))
273         {
274           static const WCHAR drive[] = { 'D','r','i','v','e',0 };
275
276           int icon_idx = -1;
277
278           if (_ILGetDrive(pSimplePidl, sTemp, MAX_PATH))
279           {
280                 switch(GetDriveTypeA(sTemp))
281                 {
282                   case DRIVE_REMOVABLE:   icon_idx = IDI_SHELL_FLOPPY;        break;
283                   case DRIVE_CDROM:       icon_idx = IDI_SHELL_CDROM;         break;
284                   case DRIVE_REMOTE:      icon_idx = IDI_SHELL_NETDRIVE;      break;
285                   case DRIVE_RAMDISK:     icon_idx = IDI_SHELL_RAMDISK;       break;
286                 }
287           }
288
289           if (icon_idx != -1)
290           {
291                 lstrcpynW(szIconFile, swShell32Name, cchMax);
292                 *piIndex = -icon_idx;
293           }
294           else
295           {
296                 if (HCR_GetDefaultIconW(drive, szIconFile, cchMax, &dwNr))
297                 {
298                   *piIndex = dwNr;
299                 }
300                 else
301                 {
302                   lstrcpynW(szIconFile, swShell32Name, cchMax);
303                   *piIndex = -IDI_SHELL_DRIVE;
304                 }
305           }
306         }
307         else if (_ILIsFolder (pSimplePidl))
308         {
309             getIconLocationForFolder(iface, uFlags, szIconFile, cchMax, piIndex,
310                                      pwFlags);
311         }
312         else
313         {
314           BOOL found = FALSE;
315
316           if (_ILIsCPanelStruct(pSimplePidl))
317           {
318             if (SUCCEEDED(CPanel_GetIconLocationW(pSimplePidl, szIconFile, cchMax, piIndex)))
319                 found = TRUE;
320           }
321           else if (_ILGetExtension(pSimplePidl, sTemp, MAX_PATH))
322           {
323             if (HCR_MapTypeToValueA(sTemp, sTemp, MAX_PATH, TRUE)
324                 && HCR_GetDefaultIconA(sTemp, sTemp, MAX_PATH, &dwNr))
325             {
326               if (!lstrcmpA("%1", sTemp))               /* icon is in the file */
327               {
328                 SHGetPathFromIDListW(This->pidl, szIconFile);
329                 *piIndex = 0;
330               }
331               else
332               {
333                 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, szIconFile, cchMax);
334                 *piIndex = dwNr;
335               }
336
337               found = TRUE;
338             }
339             else if (!lstrcmpiA(sTemp, "lnkfile"))
340             {
341               /* extract icon from shell shortcut */
342               IShellFolder* dsf;
343               IShellLinkW* psl;
344
345               if (SUCCEEDED(SHGetDesktopFolder(&dsf)))
346               {
347                 HRESULT hr = IShellFolder_GetUIObjectOf(dsf, NULL, 1, (LPCITEMIDLIST*)&This->pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
348
349                 if (SUCCEEDED(hr))
350                 {
351                   hr = IShellLinkW_GetIconLocation(psl, szIconFile, MAX_PATH, piIndex);
352
353                   if (SUCCEEDED(hr) && *szIconFile)
354                     found = TRUE;
355
356                   IShellLinkW_Release(psl);
357                 }
358
359                 IShellFolder_Release(dsf);
360               }
361             }
362           }
363
364           if (!found)                                   /* default icon */
365           {
366             lstrcpynW(szIconFile, swShell32Name, cchMax);
367             *piIndex = 0;
368           }
369         }
370
371         TRACE("-- %s %x\n", debugstr_w(szIconFile), *piIndex);
372         return NOERROR;
373 }
374
375 /**************************************************************************
376 *  IExtractIconW_Extract
377 */
378 static HRESULT WINAPI IExtractIconW_fnExtract(IExtractIconW * iface, LPCWSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
379 {
380         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
381         int index;
382
383         FIXME("(%p) (file=%p index=%d %p %p size=%08x) semi-stub\n", This, debugstr_w(pszFile), (signed)nIconIndex,
384               phiconLarge, phiconSmall, nIconSize);
385
386         index = SIC_GetIconIndex(pszFile, nIconIndex);
387
388         if (phiconLarge)
389           *phiconLarge = ImageList_GetIcon(ShellBigIconList, index, ILD_TRANSPARENT);
390
391         if (phiconSmall)
392           *phiconSmall = ImageList_GetIcon(ShellSmallIconList, index, ILD_TRANSPARENT);
393
394         return S_OK;
395 }
396
397 static struct IExtractIconWVtbl eivt =
398 {
399         IExtractIconW_fnQueryInterface,
400         IExtractIconW_fnAddRef,
401         IExtractIconW_fnRelease,
402         IExtractIconW_fnGetIconLocation,
403         IExtractIconW_fnExtract
404 };
405
406 /**************************************************************************
407 *  IExtractIconA_Constructor
408 */
409 IExtractIconA* IExtractIconA_Constructor(LPCITEMIDLIST pidl)
410 {
411         IExtractIconWImpl *This = (IExtractIconWImpl *)IExtractIconW_Constructor(pidl);
412         IExtractIconA *eia = (IExtractIconA *)&This->lpvtblExtractIconA;
413         
414         TRACE("(%p)->(%p)\n", This, eia);
415         return eia;
416 }
417 /**************************************************************************
418  *  IExtractIconA_QueryInterface
419  */
420 static HRESULT WINAPI IExtractIconA_fnQueryInterface(IExtractIconA * iface, REFIID riid, LPVOID *ppvObj)
421 {
422         _ICOM_THIS_From_IExtractIconA(IExtractIconW, iface);
423
424         return IExtractIconW_QueryInterface(This, riid, ppvObj);
425 }
426
427 /**************************************************************************
428 *  IExtractIconA_AddRef
429 */
430 static ULONG WINAPI IExtractIconA_fnAddRef(IExtractIconA * iface)
431 {
432         _ICOM_THIS_From_IExtractIconA(IExtractIconW, iface);
433
434         return IExtractIconW_AddRef(This);
435 }
436 /**************************************************************************
437 *  IExtractIconA_Release
438 */
439 static ULONG WINAPI IExtractIconA_fnRelease(IExtractIconA * iface)
440 {
441         _ICOM_THIS_From_IExtractIconA(IExtractIconW, iface);
442
443         return IExtractIconW_AddRef(This);
444 }
445 /**************************************************************************
446 *  IExtractIconA_GetIconLocation
447 *
448 * mapping filetype to icon
449 */
450 static HRESULT WINAPI IExtractIconA_fnGetIconLocation(
451         IExtractIconA * iface,
452         UINT uFlags,
453         LPSTR szIconFile,
454         UINT cchMax,
455         int * piIndex,
456         UINT * pwFlags)
457 {
458         HRESULT ret;
459         LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, cchMax * sizeof(WCHAR));
460         _ICOM_THIS_From_IExtractIconA(IExtractIconW, iface);
461         
462         TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
463
464         ret = IExtractIconW_GetIconLocation(This, uFlags, lpwstrFile, cchMax, piIndex, pwFlags);
465         WideCharToMultiByte(CP_ACP, 0, lpwstrFile, -1, szIconFile, cchMax, NULL, NULL);
466         HeapFree(GetProcessHeap(), 0, lpwstrFile);
467
468         TRACE("-- %s %x\n", szIconFile, *piIndex);
469         return ret;
470 }
471 /**************************************************************************
472 *  IExtractIconA_Extract
473 */
474 static HRESULT WINAPI IExtractIconA_fnExtract(IExtractIconA * iface, LPCSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
475 {
476         HRESULT ret;
477         INT len = MultiByteToWideChar(CP_ACP, 0, pszFile, -1, NULL, 0);
478         LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
479         _ICOM_THIS_From_IExtractIconA(IExtractIconW, iface);
480
481         TRACE("(%p) (file=%p index=%u %p %p size=%u)\n", This, pszFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
482
483         MultiByteToWideChar(CP_ACP, 0, pszFile, -1, lpwstrFile, len);
484         ret = IExtractIconW_Extract(This, lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
485         HeapFree(GetProcessHeap(), 0, lpwstrFile);
486         return ret;
487 }
488
489 static struct IExtractIconAVtbl eiavt =
490 {
491         IExtractIconA_fnQueryInterface,
492         IExtractIconA_fnAddRef,
493         IExtractIconA_fnRelease,
494         IExtractIconA_fnGetIconLocation,
495         IExtractIconA_fnExtract
496 };
497
498 /************************************************************************
499  * IEIPersistFile_QueryInterface (IUnknown)
500  */
501 static HRESULT WINAPI IEIPersistFile_fnQueryInterface(
502         IPersistFile    *iface,
503         REFIID          iid,
504         LPVOID          *ppvObj)
505 {
506         _ICOM_THIS_From_IPersistFile(IExtractIconW, iface);
507
508         return IExtractIconW_QueryInterface(This, iid, ppvObj);
509 }
510
511 /************************************************************************
512  * IEIPersistFile_AddRef (IUnknown)
513  */
514 static ULONG WINAPI IEIPersistFile_fnAddRef(
515         IPersistFile    *iface)
516 {
517         _ICOM_THIS_From_IPersistFile(IExtractIconW, iface);
518
519         return IExtractIconW_AddRef(This);
520 }
521
522 /************************************************************************
523  * IEIPersistFile_Release (IUnknown)
524  */
525 static ULONG WINAPI IEIPersistFile_fnRelease(
526         IPersistFile    *iface)
527 {
528         _ICOM_THIS_From_IPersistFile(IExtractIconW, iface);
529
530         return IExtractIconW_Release(This);
531 }
532
533 /************************************************************************
534  * IEIPersistFile_GetClassID (IPersist)
535  */
536 static HRESULT WINAPI IEIPersistFile_fnGetClassID(
537         IPersistFile    *iface,
538         LPCLSID         lpClassId)
539 {
540         CLSID StdFolderID = { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
541
542         if (lpClassId==NULL)
543           return E_POINTER;
544
545         memcpy(lpClassId, &StdFolderID, sizeof(StdFolderID));
546
547         return S_OK;
548 }
549
550 /************************************************************************
551  * IEIPersistFile_Load (IPersistFile)
552  */
553 static HRESULT WINAPI IEIPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
554 {
555         _ICOM_THIS_From_IPersistFile(IExtractIconW, iface);
556         FIXME("%p\n", This);
557         return E_NOTIMPL;
558
559 }
560
561 static struct IPersistFileVtbl pfvt =
562 {
563         IEIPersistFile_fnQueryInterface,
564         IEIPersistFile_fnAddRef,
565         IEIPersistFile_fnRelease,
566         IEIPersistFile_fnGetClassID,
567         (void *) 0xdeadbeef /* IEIPersistFile_fnIsDirty */,
568         IEIPersistFile_fnLoad,
569         (void *) 0xdeadbeef /* IEIPersistFile_fnSave */,
570         (void *) 0xdeadbeef /* IEIPersistFile_fnSaveCompleted */,
571         (void *) 0xdeadbeef /* IEIPersistFile_fnGetCurFile */
572 };