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