dinput: Introduce new dinput_device_internal_unacquire helper.
[wine.git] / dlls / shell32 / folders.c
blob35c5414a287a4629b369001a9e3c048ae2f9257a
1 /*
2 * Copyright 1997 Marcus Meissner
3 * Copyright 1998 Juergen Schmied
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.
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.
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
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
25 #define COBJMACROS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "objbase.h"
31 #include "undocshell.h"
32 #include "shlguid.h"
34 #include "wine/debug.h"
36 #include "pidl.h"
37 #include "shell32_main.h"
38 #include "shfldr.h"
39 #include "shresdef.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(shell);
43 /***********************************************************************
44 * IExtractIconW implementation
46 typedef struct
48 IExtractIconW IExtractIconW_iface;
49 IExtractIconA IExtractIconA_iface;
50 IPersistFile IPersistFile_iface;
51 LONG ref;
52 LPITEMIDLIST pidl;
53 } IExtractIconWImpl;
55 static inline IExtractIconWImpl *impl_from_IExtractIconW(IExtractIconW *iface)
57 return CONTAINING_RECORD(iface, IExtractIconWImpl, IExtractIconW_iface);
60 static inline IExtractIconWImpl *impl_from_IExtractIconA(IExtractIconA *iface)
62 return CONTAINING_RECORD(iface, IExtractIconWImpl, IExtractIconA_iface);
65 static inline IExtractIconWImpl *impl_from_IPersistFile(IPersistFile *iface)
67 return CONTAINING_RECORD(iface, IExtractIconWImpl, IPersistFile_iface);
71 /**************************************************************************
72 * IExtractIconW::QueryInterface
74 static HRESULT WINAPI IExtractIconW_fnQueryInterface(IExtractIconW *iface, REFIID riid,
75 void **ppv)
77 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
79 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, debugstr_guid(riid), ppv);
81 *ppv = NULL;
82 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IExtractIconW))
83 *ppv = iface;
84 else if (IsEqualIID(riid, &IID_IPersistFile))
85 *ppv = &This->IPersistFile_iface;
86 else if (IsEqualIID(riid, &IID_IExtractIconA))
87 *ppv = &This->IExtractIconA_iface;
89 if(*ppv)
91 IUnknown_AddRef((IUnknown*)*ppv);
92 TRACE("-- Interface: (%p)->(%p)\n", ppv, *ppv);
93 return S_OK;
95 TRACE("-- Interface: E_NOINTERFACE\n");
96 return E_NOINTERFACE;
99 /**************************************************************************
100 * IExtractIconW::AddRef
102 static ULONG WINAPI IExtractIconW_fnAddRef(IExtractIconW * iface)
104 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
105 ULONG refCount = InterlockedIncrement(&This->ref);
107 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
109 return refCount;
111 /**************************************************************************
112 * IExtractIconW::Release
114 static ULONG WINAPI IExtractIconW_fnRelease(IExtractIconW * iface)
116 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
117 ULONG refCount = InterlockedDecrement(&This->ref);
119 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
121 if (!refCount)
123 TRACE(" destroying IExtractIcon(%p)\n",This);
124 SHFree(This->pidl);
125 heap_free(This);
127 return refCount;
130 static HRESULT getIconLocationForFolder(IExtractIconWImpl *This, UINT uFlags, LPWSTR szIconFile,
131 UINT cchMax, int *piIndex, UINT *pwFlags)
133 int icon_idx;
134 WCHAR wszPath[MAX_PATH];
135 WCHAR wszCLSIDValue[CHARS_IN_GUID];
137 if (SHELL32_GetCustomFolderAttribute(This->pidl, L".ShellClassInfo", L"IconFile",
138 wszPath, MAX_PATH))
140 WCHAR wszIconIndex[10];
141 SHELL32_GetCustomFolderAttribute(This->pidl, L".ShellClassInfo", L"IconIndex",
142 wszIconIndex, 10);
143 *piIndex = wcstol(wszIconIndex, NULL, 10);
145 else if (SHELL32_GetCustomFolderAttribute(This->pidl, L".ShellClassInfo", L"CLSID",
146 wszCLSIDValue, CHARS_IN_GUID) &&
147 HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &icon_idx))
149 *piIndex = icon_idx;
151 else if (SHELL32_GetCustomFolderAttribute(This->pidl, L".ShellClassInfo", L"CLSID2",
152 wszCLSIDValue, CHARS_IN_GUID) &&
153 HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &icon_idx))
155 *piIndex = icon_idx;
157 else
159 if (!HCR_GetDefaultIconW(L"Folder", szIconFile, cchMax, &icon_idx))
161 lstrcpynW(szIconFile, swShell32Name, cchMax);
162 icon_idx = -IDI_SHELL_FOLDER;
165 if (uFlags & GIL_OPENICON)
166 *piIndex = icon_idx<0? icon_idx-1: icon_idx+1;
167 else
168 *piIndex = icon_idx;
171 return S_OK;
174 WCHAR swShell32Name[MAX_PATH];
176 /**************************************************************************
177 * IExtractIconW::GetIconLocation
179 * mapping filetype to icon
181 static HRESULT WINAPI IExtractIconW_fnGetIconLocation(IExtractIconW * iface, UINT uFlags,
182 LPWSTR szIconFile, UINT cchMax, int * piIndex, UINT * pwFlags)
184 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
185 char sTemp[MAX_PATH];
186 int icon_idx;
187 GUID const * riid;
188 LPITEMIDLIST pSimplePidl = ILFindLastID(This->pidl);
190 TRACE("(%p) (flags=%u, %p, %u, %p, %p)\n", This, uFlags, szIconFile,
191 cchMax, piIndex, pwFlags);
193 if (pwFlags)
194 *pwFlags = 0;
196 if (_ILIsDesktop(pSimplePidl))
198 lstrcpynW(szIconFile, swShell32Name, cchMax);
199 *piIndex = -IDI_SHELL_DESKTOP;
202 /* my computer and other shell extensions */
203 else if ((riid = _ILGetGUIDPointer(pSimplePidl)))
205 WCHAR xriid[50];
207 swprintf(xriid, ARRAY_SIZE(xriid), L"CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
208 riid->Data1, riid->Data2, riid->Data3,
209 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
210 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]);
212 if (HCR_GetDefaultIconW(xriid, szIconFile, cchMax, &icon_idx))
214 *piIndex = icon_idx;
216 else
218 lstrcpynW(szIconFile, swShell32Name, cchMax);
219 if(IsEqualGUID(riid, &CLSID_MyComputer))
220 *piIndex = -IDI_SHELL_MY_COMPUTER;
221 else if(IsEqualGUID(riid, &CLSID_MyDocuments))
222 *piIndex = -IDI_SHELL_MY_DOCUMENTS;
223 else if(IsEqualGUID(riid, &CLSID_NetworkPlaces))
224 *piIndex = -IDI_SHELL_MY_NETWORK_PLACES;
225 else if(IsEqualGUID(riid, &CLSID_UnixFolder) ||
226 IsEqualGUID(riid, &CLSID_UnixDosFolder))
227 *piIndex = -IDI_SHELL_DRIVE;
228 else
229 *piIndex = -IDI_SHELL_FOLDER;
233 else if (_ILIsDrive (pSimplePidl))
235 icon_idx = -1;
237 if (_ILGetDrive(pSimplePidl, sTemp, MAX_PATH))
239 switch(GetDriveTypeA(sTemp))
241 case DRIVE_REMOVABLE: icon_idx = IDI_SHELL_FLOPPY; break;
242 case DRIVE_CDROM: icon_idx = IDI_SHELL_OPTICAL_DRIVE; break;
243 case DRIVE_REMOTE: icon_idx = IDI_SHELL_NETDRIVE; break;
244 case DRIVE_RAMDISK: icon_idx = IDI_SHELL_RAMDISK; break;
248 if (icon_idx != -1)
250 lstrcpynW(szIconFile, swShell32Name, cchMax);
251 *piIndex = -icon_idx;
253 else
255 if (HCR_GetDefaultIconW(L"Drive", szIconFile, cchMax, &icon_idx))
257 *piIndex = icon_idx;
259 else
261 lstrcpynW(szIconFile, swShell32Name, cchMax);
262 *piIndex = -IDI_SHELL_DRIVE;
266 else if (_ILIsFolder (pSimplePidl))
268 getIconLocationForFolder(This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
270 else
272 BOOL found = FALSE;
274 if (_ILIsCPanelStruct(pSimplePidl))
276 if (SUCCEEDED(CPanel_GetIconLocationW(pSimplePidl, szIconFile, cchMax, piIndex)))
277 found = TRUE;
279 else if (_ILGetExtension(pSimplePidl, sTemp, MAX_PATH))
281 if (HCR_MapTypeToValueA(sTemp, sTemp, MAX_PATH, TRUE)
282 && HCR_GetDefaultIconA(sTemp, sTemp, MAX_PATH, &icon_idx))
284 if (!lstrcmpA("%1", sTemp)) /* icon is in the file */
286 SHGetPathFromIDListW(This->pidl, szIconFile);
287 *piIndex = 0;
289 else
291 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, szIconFile, cchMax);
292 *piIndex = icon_idx;
295 found = TRUE;
297 else if (!lstrcmpiA(sTemp, "lnkfile"))
299 /* extract icon from shell shortcut */
300 IShellFolder* dsf;
301 IShellLinkW* psl;
303 if (SUCCEEDED(SHGetDesktopFolder(&dsf)))
305 HRESULT hr = IShellFolder_GetUIObjectOf(dsf, NULL, 1, (LPCITEMIDLIST*)&This->pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
307 if (SUCCEEDED(hr))
309 hr = IShellLinkW_GetIconLocation(psl, szIconFile, cchMax, piIndex);
311 if (SUCCEEDED(hr) && *szIconFile)
312 found = TRUE;
314 IShellLinkW_Release(psl);
317 IShellFolder_Release(dsf);
322 if (!found) /* default icon */
324 lstrcpynW(szIconFile, swShell32Name, cchMax);
325 *piIndex = 0;
329 TRACE("-- %s %x\n", debugstr_w(szIconFile), *piIndex);
330 return S_OK;
333 /**************************************************************************
334 * IExtractIconW::Extract
336 static HRESULT WINAPI IExtractIconW_fnExtract(IExtractIconW * iface, LPCWSTR pszFile,
337 UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
339 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
340 int index;
341 HIMAGELIST big_icons, small_icons;
343 FIXME("(%p) (file=%s index=%d %p %p size=%08x) semi-stub\n", This, debugstr_w(pszFile),
344 (signed)nIconIndex, phiconLarge, phiconSmall, nIconSize);
346 index = SIC_GetIconIndex(pszFile, nIconIndex, 0);
347 Shell_GetImageLists( &big_icons, &small_icons );
348 if (phiconLarge)
349 *phiconLarge = ImageList_GetIcon(big_icons, index, ILD_TRANSPARENT);
351 if (phiconSmall)
352 *phiconSmall = ImageList_GetIcon(small_icons, index, ILD_TRANSPARENT);
354 return S_OK;
357 static const IExtractIconWVtbl eivt =
359 IExtractIconW_fnQueryInterface,
360 IExtractIconW_fnAddRef,
361 IExtractIconW_fnRelease,
362 IExtractIconW_fnGetIconLocation,
363 IExtractIconW_fnExtract
366 /**************************************************************************
367 * IExtractIconA::QueryInterface
369 static HRESULT WINAPI IExtractIconA_fnQueryInterface(IExtractIconA * iface, REFIID riid,
370 void **ppv)
372 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
374 return IExtractIconW_QueryInterface(&This->IExtractIconW_iface, riid, ppv);
377 /**************************************************************************
378 * IExtractIconA::AddRef
380 static ULONG WINAPI IExtractIconA_fnAddRef(IExtractIconA * iface)
382 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
384 return IExtractIconW_AddRef(&This->IExtractIconW_iface);
386 /**************************************************************************
387 * IExtractIconA::Release
389 static ULONG WINAPI IExtractIconA_fnRelease(IExtractIconA * iface)
391 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
393 return IExtractIconW_Release(&This->IExtractIconW_iface);
395 /**************************************************************************
396 * IExtractIconA::GetIconLocation
398 * mapping filetype to icon
400 static HRESULT WINAPI IExtractIconA_fnGetIconLocation(IExtractIconA * iface, UINT uFlags,
401 LPSTR szIconFile, UINT cchMax, int * piIndex, UINT * pwFlags)
403 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
404 HRESULT ret;
405 LPWSTR lpwstrFile = heap_alloc(cchMax * sizeof(WCHAR));
407 TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
409 ret = IExtractIconW_GetIconLocation(&This->IExtractIconW_iface, uFlags, lpwstrFile, cchMax,
410 piIndex, pwFlags);
411 WideCharToMultiByte(CP_ACP, 0, lpwstrFile, -1, szIconFile, cchMax, NULL, NULL);
412 heap_free(lpwstrFile);
414 TRACE("-- %s %x\n", szIconFile, *piIndex);
415 return ret;
417 /**************************************************************************
418 * IExtractIconA::Extract
420 static HRESULT WINAPI IExtractIconA_fnExtract(IExtractIconA * iface, LPCSTR pszFile,
421 UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
423 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
424 HRESULT ret;
425 INT len = MultiByteToWideChar(CP_ACP, 0, pszFile, -1, NULL, 0);
426 LPWSTR lpwstrFile = heap_alloc(len * sizeof(WCHAR));
428 TRACE("(%p) (file=%p index=%u %p %p size=%u)\n", This, pszFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
430 MultiByteToWideChar(CP_ACP, 0, pszFile, -1, lpwstrFile, len);
431 ret = IExtractIconW_Extract(&This->IExtractIconW_iface, lpwstrFile, nIconIndex, phiconLarge,
432 phiconSmall, nIconSize);
433 heap_free(lpwstrFile);
434 return ret;
437 static const IExtractIconAVtbl eiavt =
439 IExtractIconA_fnQueryInterface,
440 IExtractIconA_fnAddRef,
441 IExtractIconA_fnRelease,
442 IExtractIconA_fnGetIconLocation,
443 IExtractIconA_fnExtract
446 /************************************************************************
447 * IEIPersistFile::QueryInterface
449 static HRESULT WINAPI IEIPersistFile_fnQueryInterface(IPersistFile *iface, REFIID iid,
450 void **ppv)
452 IExtractIconWImpl *This = impl_from_IPersistFile(iface);
454 return IExtractIconW_QueryInterface(&This->IExtractIconW_iface, iid, ppv);
457 /************************************************************************
458 * IEIPersistFile::AddRef
460 static ULONG WINAPI IEIPersistFile_fnAddRef(IPersistFile *iface)
462 IExtractIconWImpl *This = impl_from_IPersistFile(iface);
464 return IExtractIconW_AddRef(&This->IExtractIconW_iface);
467 /************************************************************************
468 * IEIPersistFile::Release
470 static ULONG WINAPI IEIPersistFile_fnRelease(IPersistFile *iface)
472 IExtractIconWImpl *This = impl_from_IPersistFile(iface);
474 return IExtractIconW_Release(&This->IExtractIconW_iface);
477 /************************************************************************
478 * IEIPersistFile::GetClassID
480 static HRESULT WINAPI IEIPersistFile_fnGetClassID(IPersistFile *iface, LPCLSID lpClassId)
482 CLSID StdFolderID = { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
484 if (lpClassId==NULL)
485 return E_POINTER;
487 *lpClassId = StdFolderID;
489 return S_OK;
492 /************************************************************************
493 * IEIPersistFile_Load
495 static HRESULT WINAPI IEIPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName,
496 DWORD dwMode)
498 IExtractIconWImpl *This = impl_from_IPersistFile(iface);
500 FIXME("%p\n", This);
501 return E_NOTIMPL;
504 static const IPersistFileVtbl pfvt =
506 IEIPersistFile_fnQueryInterface,
507 IEIPersistFile_fnAddRef,
508 IEIPersistFile_fnRelease,
509 IEIPersistFile_fnGetClassID,
510 (void *) 0xdeadbeef /* IEIPersistFile_fnIsDirty */,
511 IEIPersistFile_fnLoad,
512 (void *) 0xdeadbeef /* IEIPersistFile_fnSave */,
513 (void *) 0xdeadbeef /* IEIPersistFile_fnSaveCompleted */,
514 (void *) 0xdeadbeef /* IEIPersistFile_fnGetCurFile */
517 static IExtractIconWImpl *extracticon_create(LPCITEMIDLIST pidl)
519 IExtractIconWImpl *ei;
521 TRACE("%p\n", pidl);
523 ei = heap_alloc(sizeof(*ei));
524 ei->ref=1;
525 ei->IExtractIconW_iface.lpVtbl = &eivt;
526 ei->IExtractIconA_iface.lpVtbl = &eiavt;
527 ei->IPersistFile_iface.lpVtbl = &pfvt;
528 ei->pidl=ILClone(pidl);
530 pdump(pidl);
532 TRACE("(%p)\n", ei);
533 return ei;
536 IExtractIconW *IExtractIconW_Constructor(LPCITEMIDLIST pidl)
538 IExtractIconWImpl *ei = extracticon_create(pidl);
540 return &ei->IExtractIconW_iface;
543 IExtractIconA *IExtractIconA_Constructor(LPCITEMIDLIST pidl)
545 IExtractIconWImpl *ei = extracticon_create(pidl);
547 return &ei->IExtractIconA_iface;