62540ab7cb9249fd907001f099cc4421d7586e8c
[reactos.git] / reactos / dll / win32 / shell32 / folders / CControlPanelFolder.cpp
1 /*
2 * Control panel folder
3 *
4 * Copyright 2003 Martin Fuchs
5 * Copyright 2009 Andrew Hill
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include <precomp.h>
23
24 WINE_DEFAULT_DEBUG_CHANNEL(shell);
25
26 /***********************************************************************
27 * control panel implementation in shell namespace
28 */
29
30 class CControlPanelEnum :
31 public CEnumIDListBase
32 {
33 public:
34 CControlPanelEnum();
35 ~CControlPanelEnum();
36 HRESULT WINAPI Initialize(DWORD dwFlags);
37 BOOL RegisterCPanelApp(LPCWSTR path);
38 int RegisterRegistryCPanelApps(HKEY hkey_root, LPCWSTR szRepPath);
39 int RegisterCPanelFolders(HKEY hkey_root, LPCWSTR szRepPath);
40 BOOL CreateCPanelEnumList(DWORD dwFlags);
41
42 BEGIN_COM_MAP(CControlPanelEnum)
43 COM_INTERFACE_ENTRY_IID(IID_IEnumIDList, IEnumIDList)
44 END_COM_MAP()
45 };
46
47 /***********************************************************************
48 * IShellFolder [ControlPanel] implementation
49 */
50
51 static const shvheader ControlPanelSFHeader[] = {
52 {IDS_SHV_COLUMN_NAME, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 20},/*FIXME*/
53 {IDS_SHV_COLUMN_COMMENTS, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 80},/*FIXME*/
54 };
55
56 #define CONROLPANELSHELLVIEWCOLUMNS 2
57
58 CControlPanelEnum::CControlPanelEnum()
59 {
60 }
61
62 CControlPanelEnum::~CControlPanelEnum()
63 {
64 }
65
66 HRESULT WINAPI CControlPanelEnum::Initialize(DWORD dwFlags)
67 {
68 if (CreateCPanelEnumList(dwFlags) == FALSE)
69 return E_FAIL;
70 return S_OK;
71 }
72
73 static LPITEMIDLIST _ILCreateCPanelApplet(LPCWSTR pszName, LPCWSTR pszDisplayName, LPCWSTR pszComment, int iIconIdx)
74 {
75 PIDLCPanelStruct *pCP;
76 LPITEMIDLIST pidl;
77 LPPIDLDATA pData;
78 int cchName, cchDisplayName, cchComment, cbData;
79
80 /* Calculate lengths of given strings */
81 cchName = wcslen(pszName);
82 cchDisplayName = wcslen(pszDisplayName);
83 cchComment = wcslen(pszComment);
84
85 /* Allocate PIDL */
86 cbData = sizeof(pidl->mkid.cb) + sizeof(pData->type) + sizeof(pData->u.cpanel) - sizeof(pData->u.cpanel.szName)
87 + (cchName + cchDisplayName + cchComment + 3) * sizeof(WCHAR);
88 pidl = (LPITEMIDLIST)SHAlloc(cbData + sizeof(WORD));
89 if (!pidl)
90 return NULL;
91
92 /* Copy data to allocated memory */
93 pidl->mkid.cb = cbData;
94 pData = (PIDLDATA *)pidl->mkid.abID;
95 pData->type = PT_CPLAPPLET;
96
97 pCP = &pData->u.cpanel;
98 pCP->dummy = 0;
99 pCP->iconIdx = iIconIdx;
100 wcscpy(pCP->szName, pszName);
101 pCP->offsDispName = cchName + 1;
102 wcscpy(pCP->szName + pCP->offsDispName, pszDisplayName);
103 pCP->offsComment = pCP->offsDispName + cchDisplayName + 1;
104 wcscpy(pCP->szName + pCP->offsComment, pszComment);
105
106 /* Add PIDL NULL terminator */
107 *(WORD*)(pCP->szName + pCP->offsComment + cchComment + 1) = 0;
108
109 pcheck(pidl);
110
111 return pidl;
112 }
113
114 /**************************************************************************
115 * _ILGetCPanelPointer()
116 * gets a pointer to the control panel struct stored in the pidl
117 */
118 static PIDLCPanelStruct *_ILGetCPanelPointer(LPCITEMIDLIST pidl)
119 {
120 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
121
122 if (pdata && pdata->type == PT_CPLAPPLET)
123 return (PIDLCPanelStruct *) & (pdata->u.cpanel);
124
125 return NULL;
126 }
127
128 HRESULT CCPLExtractIcon_CreateInstance(IShellFolder * psf, LPCITEMIDLIST pidl, REFIID riid, LPVOID * ppvOut)
129 {
130 PIDLCPanelStruct *pData = _ILGetCPanelPointer(pidl);
131 if (!pData)
132 return E_FAIL;
133
134 CComPtr<IDefaultExtractIconInit> initIcon;
135 HRESULT hr = SHCreateDefaultExtractIcon(IID_PPV_ARG(IDefaultExtractIconInit, &initIcon));
136 if (FAILED_UNEXPECTEDLY(hr))
137 return hr;
138
139 initIcon->SetNormalIcon(pData->szName, (int)pData->iconIdx != -1 ? pData->iconIdx : 0);
140
141 return initIcon->QueryInterface(riid, ppvOut);
142 }
143
144 BOOL CControlPanelEnum::RegisterCPanelApp(LPCWSTR wpath)
145 {
146 CPlApplet* applet = Control_LoadApplet(0, wpath, NULL);
147 int iconIdx;
148
149 if (applet)
150 {
151 for (UINT i = 0; i < applet->count; ++i)
152 {
153 if (applet->info[i].idIcon > 0)
154 iconIdx = -applet->info[i].idIcon; /* negative icon index instead of icon number */
155 else
156 iconIdx = 0;
157
158 LPITEMIDLIST pidl = _ILCreateCPanelApplet(wpath,
159 applet->info[i].name,
160 applet->info[i].info,
161 iconIdx);
162
163 if (pidl)
164 AddToEnumList(pidl);
165 }
166 Control_UnloadApplet(applet);
167 }
168 return TRUE;
169 }
170
171 int CControlPanelEnum::RegisterRegistryCPanelApps(HKEY hkey_root, LPCWSTR szRepPath)
172 {
173 WCHAR name[MAX_PATH];
174 WCHAR value[MAX_PATH];
175 HKEY hkey;
176
177 int cnt = 0;
178
179 if (RegOpenKeyW(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
180 {
181 int idx = 0;
182
183 for(; ; idx++)
184 {
185 DWORD nameLen = MAX_PATH;
186 DWORD valueLen = MAX_PATH;
187 WCHAR buffer[MAX_PATH];
188
189 if (RegEnumValueW(hkey, idx, name, &nameLen, NULL, NULL, (LPBYTE)&value, &valueLen) != ERROR_SUCCESS)
190 break;
191
192 if (ExpandEnvironmentStringsW(value, buffer, MAX_PATH))
193 {
194 wcscpy(value, buffer);
195 }
196
197 if (RegisterCPanelApp(value))
198 ++cnt;
199 }
200 RegCloseKey(hkey);
201 }
202
203 return cnt;
204 }
205
206 int CControlPanelEnum::RegisterCPanelFolders(HKEY hkey_root, LPCWSTR szRepPath)
207 {
208 WCHAR name[MAX_PATH];
209 HKEY hkey;
210
211 int cnt = 0;
212
213 if (RegOpenKeyW(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
214 {
215 for (int idx = 0; ; idx++)
216 {
217 if (RegEnumKeyW(hkey, idx, name, MAX_PATH) != ERROR_SUCCESS)
218 break;
219
220 if (*name == '{')
221 {
222 LPITEMIDLIST pidl = _ILCreateGuidFromStrW(name);
223
224 if (pidl && AddToEnumList(pidl))
225 ++cnt;
226 }
227 }
228
229 RegCloseKey(hkey);
230 }
231
232 return cnt;
233 }
234
235 /**************************************************************************
236 * CControlPanelEnum::CreateCPanelEnumList()
237 */
238 BOOL CControlPanelEnum::CreateCPanelEnumList(DWORD dwFlags)
239 {
240 WCHAR szPath[MAX_PATH];
241 WIN32_FIND_DATAW wfd;
242 HANDLE hFile;
243
244 TRACE("(%p)->(flags=0x%08x)\n", this, dwFlags);
245
246 /* enumerate control panel folders */
247 if (dwFlags & SHCONTF_FOLDERS)
248 RegisterCPanelFolders(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ControlPanel\\NameSpace");
249
250 /* enumerate the control panel applets */
251 if (dwFlags & SHCONTF_NONFOLDERS)
252 {
253 LPWSTR p;
254
255 GetSystemDirectoryW(szPath, MAX_PATH);
256 p = PathAddBackslashW(szPath);
257 wcscpy(p, L"*.cpl");
258
259 hFile = FindFirstFileW(szPath, &wfd);
260
261 if (hFile != INVALID_HANDLE_VALUE)
262 {
263 do
264 {
265 if (!(dwFlags & SHCONTF_INCLUDEHIDDEN) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
266 continue;
267
268 if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
269 wcscpy(p, wfd.cFileName);
270 if (wcscmp(wfd.cFileName, L"ncpa.cpl"))
271 RegisterCPanelApp(szPath);
272 }
273 } while(FindNextFileW(hFile, &wfd));
274 FindClose(hFile);
275 }
276
277 RegisterRegistryCPanelApps(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Control Panel\\Cpls");
278 RegisterRegistryCPanelApps(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Control Panel\\Cpls");
279 }
280 return TRUE;
281 }
282
283 CControlPanelFolder::CControlPanelFolder()
284 {
285 pidlRoot = NULL; /* absolute pidl */
286 }
287
288 CControlPanelFolder::~CControlPanelFolder()
289 {
290 TRACE("-- destroying IShellFolder(%p)\n", this);
291 SHFree(pidlRoot);
292 }
293
294 /**************************************************************************
295 * CControlPanelFolder::ParseDisplayName
296 */
297 HRESULT WINAPI CControlPanelFolder::ParseDisplayName(
298 HWND hwndOwner,
299 LPBC pbc,
300 LPOLESTR lpszDisplayName,
301 DWORD *pchEaten,
302 PIDLIST_RELATIVE *ppidl,
303 DWORD *pdwAttributes)
304 {
305 /* We only support parsing guid names */
306 return m_regFolder->ParseDisplayName(hwndOwner, pbc, lpszDisplayName, pchEaten, ppidl, pdwAttributes);
307 }
308
309 /**************************************************************************
310 * CControlPanelFolder::EnumObjects
311 */
312 HRESULT WINAPI CControlPanelFolder::EnumObjects(HWND hwndOwner, DWORD dwFlags, LPENUMIDLIST *ppEnumIDList)
313 {
314 return ShellObjectCreatorInit<CControlPanelEnum>(dwFlags, IID_PPV_ARG(IEnumIDList, ppEnumIDList));
315 }
316
317 /**************************************************************************
318 * CControlPanelFolder::BindToObject
319 */
320 HRESULT WINAPI CControlPanelFolder::BindToObject(
321 PCUIDLIST_RELATIVE pidl,
322 LPBC pbcReserved,
323 REFIID riid,
324 LPVOID *ppvOut)
325 {
326 return m_regFolder->BindToObject(pidl, pbcReserved, riid, ppvOut);
327 }
328
329 /**************************************************************************
330 * CControlPanelFolder::BindToStorage
331 */
332 HRESULT WINAPI CControlPanelFolder::BindToStorage(
333 PCUIDLIST_RELATIVE pidl,
334 LPBC pbcReserved,
335 REFIID riid,
336 LPVOID *ppvOut)
337 {
338 FIXME("(%p)->(pidl=%p,%p,%s,%p) stub\n", this, pidl, pbcReserved, shdebugstr_guid(&riid), ppvOut);
339
340 *ppvOut = NULL;
341 return E_NOTIMPL;
342 }
343
344 /**************************************************************************
345 * CControlPanelFolder::CompareIDs
346 */
347 HRESULT WINAPI CControlPanelFolder::CompareIDs(LPARAM lParam, PCUIDLIST_RELATIVE pidl1, PCUIDLIST_RELATIVE pidl2)
348 {
349 /* Dont use SHELL32_CompareGuidItems because it would cause guid items to come first */
350 if (_ILIsSpecialFolder(pidl1) || _ILIsSpecialFolder(pidl2))
351 {
352 return SHELL32_CompareDetails(this, lParam, pidl1, pidl2);
353 }
354 PIDLCPanelStruct *pData1 = _ILGetCPanelPointer(pidl1);
355 PIDLCPanelStruct *pData2 = _ILGetCPanelPointer(pidl2);
356
357 if (!pData1 || !pData2 || LOWORD(lParam)>= CONROLPANELSHELLVIEWCOLUMNS)
358 return E_INVALIDARG;
359
360 int result;
361 switch(LOWORD(lParam))
362 {
363 case 0: /* name */
364 result = wcsicmp(pData1->szName + pData1->offsDispName, pData2->szName + pData2->offsDispName);
365 break;
366 case 1: /* comment */
367 result = wcsicmp(pData1->szName + pData1->offsComment, pData2->szName + pData2->offsComment);
368 break;
369 default:
370 ERR("Got wrong lParam!\n");
371 return E_INVALIDARG;
372 }
373
374 return MAKE_COMPARE_HRESULT(result);
375 }
376
377 /**************************************************************************
378 * CControlPanelFolder::CreateViewObject
379 */
380 HRESULT WINAPI CControlPanelFolder::CreateViewObject(HWND hwndOwner, REFIID riid, LPVOID * ppvOut)
381 {
382 CComPtr<IShellView> pShellView;
383 HRESULT hr = E_INVALIDARG;
384
385 TRACE("(%p)->(hwnd=%p,%s,%p)\n", this, hwndOwner, shdebugstr_guid(&riid), ppvOut);
386
387 if (ppvOut) {
388 *ppvOut = NULL;
389
390 if (IsEqualIID(riid, IID_IDropTarget)) {
391 WARN("IDropTarget not implemented\n");
392 hr = E_NOTIMPL;
393 } else if (IsEqualIID(riid, IID_IContextMenu)) {
394 WARN("IContextMenu not implemented\n");
395 hr = E_NOTIMPL;
396 } else if (IsEqualIID(riid, IID_IShellView)) {
397 SFV_CREATE sfvparams = {sizeof(SFV_CREATE), this};
398 hr = SHCreateShellFolderView(&sfvparams, (IShellView**)ppvOut);
399 }
400 }
401 TRACE("--(%p)->(interface=%p)\n", this, ppvOut);
402 return hr;
403 }
404
405 /**************************************************************************
406 * CControlPanelFolder::GetAttributesOf
407 */
408 HRESULT WINAPI CControlPanelFolder::GetAttributesOf(UINT cidl, PCUITEMID_CHILD_ARRAY apidl, DWORD * rgfInOut)
409 {
410 HRESULT hr = S_OK;
411 static const DWORD dwControlPanelAttributes =
412 SFGAO_HASSUBFOLDER | SFGAO_FOLDER | SFGAO_CANLINK;
413
414 TRACE("(%p)->(cidl=%d apidl=%p mask=%p (0x%08x))\n",
415 this, cidl, apidl, rgfInOut, rgfInOut ? *rgfInOut : 0);
416
417 if (!rgfInOut)
418 return E_INVALIDARG;
419 if (cidl && !apidl)
420 return E_INVALIDARG;
421
422 if (*rgfInOut == 0)
423 *rgfInOut = ~0;
424
425 if (!cidl)
426 {
427 *rgfInOut &= dwControlPanelAttributes;
428 }
429 else
430 {
431 while(cidl > 0 && *apidl)
432 {
433 pdump(*apidl);
434 if (_ILIsCPanelStruct(*apidl))
435 *rgfInOut &= SFGAO_CANLINK;
436 else if (_ILIsSpecialFolder(*apidl))
437 m_regFolder->GetAttributesOf(1, apidl, rgfInOut);
438 else
439 ERR("Got an unkown pidl here!\n");
440 apidl++;
441 cidl--;
442 }
443 }
444 /* make sure SFGAO_VALIDATE is cleared, some apps depend on that */
445 *rgfInOut &= ~SFGAO_VALIDATE;
446
447 TRACE("-- result=0x%08x\n", *rgfInOut);
448 return hr;
449 }
450
451 /**************************************************************************
452 * CControlPanelFolder::GetUIObjectOf
453 *
454 * PARAMETERS
455 * HWND hwndOwner, //[in ] Parent window for any output
456 * UINT cidl, //[in ] array size
457 * LPCITEMIDLIST* apidl, //[in ] simple pidl array
458 * REFIID riid, //[in ] Requested Interface
459 * UINT* prgfInOut, //[ ] reserved
460 * LPVOID* ppvObject) //[out] Resulting Interface
461 *
462 */
463 HRESULT WINAPI CControlPanelFolder::GetUIObjectOf(HWND hwndOwner,
464 UINT cidl, PCUITEMID_CHILD_ARRAY apidl, REFIID riid, UINT * prgfInOut, LPVOID * ppvOut)
465 {
466 LPVOID pObj = NULL;
467 HRESULT hr = E_INVALIDARG;
468
469 TRACE("(%p)->(%p,%u,apidl=%p,%s,%p,%p)\n",
470 this, hwndOwner, cidl, apidl, shdebugstr_guid(&riid), prgfInOut, ppvOut);
471
472 if (ppvOut) {
473 *ppvOut = NULL;
474
475 if (IsEqualIID(riid, IID_IContextMenu) && (cidl >= 1)) {
476
477 /* HACK: We should use callbacks from CDefaultContextMenu instead of creating one on our own */
478 BOOL bHasCpl = FALSE;
479 for (UINT i = 0; i < cidl; i++)
480 {
481 if(_ILIsCPanelStruct(apidl[i]))
482 {
483 bHasCpl = TRUE;
484 }
485 }
486
487 if (bHasCpl)
488 hr = ShellObjectCreatorInit<CCPLItemMenu>(cidl, apidl, riid, &pObj);
489 else
490 hr = m_regFolder->GetUIObjectOf(hwndOwner, cidl, apidl, riid, prgfInOut, &pObj);
491 } else if (IsEqualIID(riid, IID_IDataObject) && (cidl >= 1)) {
492 hr = IDataObject_Constructor(hwndOwner, pidlRoot, apidl, cidl, (IDataObject **)&pObj);
493 } else if ((IsEqualIID(riid, IID_IExtractIconA) || IsEqualIID(riid, IID_IExtractIconW)) && (cidl == 1)) {
494 if (_ILGetCPanelPointer(apidl[0]))
495 hr = CCPLExtractIcon_CreateInstance(this, apidl[0], riid, &pObj);
496 else
497 hr = m_regFolder->GetUIObjectOf(hwndOwner, cidl, apidl, riid, prgfInOut, &pObj);
498 } else {
499 hr = E_NOINTERFACE;
500 }
501
502 if (SUCCEEDED(hr) && !pObj)
503 hr = E_OUTOFMEMORY;
504
505 *ppvOut = pObj;
506 }
507 TRACE("(%p)->hr=0x%08x\n", this, hr);
508 return hr;
509 }
510
511 /**************************************************************************
512 * CControlPanelFolder::GetDisplayNameOf
513 */
514 HRESULT WINAPI CControlPanelFolder::GetDisplayNameOf(PCUITEMID_CHILD pidl, DWORD dwFlags, LPSTRRET strRet)
515 {
516 if (!pidl)
517 return S_FALSE;
518
519 PIDLCPanelStruct *pCPanel = _ILGetCPanelPointer(pidl);
520
521 if (pCPanel)
522 {
523 return SHSetStrRet(strRet, pCPanel->szName + pCPanel->offsDispName);
524 }
525 else if (_ILIsSpecialFolder(pidl))
526 {
527 return m_regFolder->GetDisplayNameOf(pidl, dwFlags, strRet);
528 }
529
530 return E_FAIL;
531 }
532
533 /**************************************************************************
534 * CControlPanelFolder::SetNameOf
535 * Changes the name of a file object or subfolder, possibly changing its item
536 * identifier in the process.
537 *
538 * PARAMETERS
539 * HWND hwndOwner, //[in ] Owner window for output
540 * LPCITEMIDLIST pidl, //[in ] simple pidl of item to change
541 * LPCOLESTR lpszName, //[in ] the items new display name
542 * DWORD dwFlags, //[in ] SHGNO formatting flags
543 * LPITEMIDLIST* ppidlOut) //[out] simple pidl returned
544 */
545 HRESULT WINAPI CControlPanelFolder::SetNameOf(HWND hwndOwner, PCUITEMID_CHILD pidl, /*simple pidl */
546 LPCOLESTR lpName, DWORD dwFlags, PITEMID_CHILD *pPidlOut)
547 {
548 FIXME("(%p)->(%p,pidl=%p,%s,%u,%p)\n", this, hwndOwner, pidl, debugstr_w(lpName), dwFlags, pPidlOut);
549 return E_FAIL;
550 }
551
552 HRESULT WINAPI CControlPanelFolder::GetDefaultSearchGUID(GUID *pguid)
553 {
554 FIXME("(%p)\n", this);
555 return E_NOTIMPL;
556 }
557
558 HRESULT WINAPI CControlPanelFolder::EnumSearches(IEnumExtraSearch **ppenum)
559 {
560 FIXME("(%p)\n", this);
561 return E_NOTIMPL;
562 }
563
564 HRESULT WINAPI CControlPanelFolder::GetDefaultColumn(DWORD dwRes, ULONG *pSort, ULONG *pDisplay)
565 {
566 TRACE("(%p)\n", this);
567
568 if (pSort) *pSort = 0;
569 if (pDisplay) *pDisplay = 0;
570 return S_OK;
571 }
572
573 HRESULT WINAPI CControlPanelFolder::GetDefaultColumnState(UINT iColumn, DWORD *pcsFlags)
574 {
575 TRACE("(%p)\n", this);
576
577 if (!pcsFlags || iColumn >= CONROLPANELSHELLVIEWCOLUMNS) return E_INVALIDARG;
578 *pcsFlags = ControlPanelSFHeader[iColumn].pcsFlags;
579 return S_OK;
580 }
581
582 HRESULT WINAPI CControlPanelFolder::GetDetailsEx(PCUITEMID_CHILD pidl, const SHCOLUMNID *pscid, VARIANT *pv)
583 {
584 FIXME("(%p)\n", this);
585 return E_NOTIMPL;
586 }
587
588 HRESULT WINAPI CControlPanelFolder::GetDetailsOf(PCUITEMID_CHILD pidl, UINT iColumn, SHELLDETAILS *psd)
589 {
590 if (!psd || iColumn >= CONROLPANELSHELLVIEWCOLUMNS)
591 return E_INVALIDARG;
592
593 if (!pidl)
594 {
595 psd->fmt = ControlPanelSFHeader[iColumn].fmt;
596 psd->cxChar = ControlPanelSFHeader[iColumn].cxChar;
597 return SHSetStrRet(&psd->str, shell32_hInstance, ControlPanelSFHeader[iColumn].colnameid);
598 }
599 else if (_ILIsSpecialFolder(pidl))
600 {
601 return m_regFolder->GetDetailsOf(pidl, iColumn, psd);
602 }
603 else
604 {
605 PIDLCPanelStruct *pCPanel = _ILGetCPanelPointer(pidl);
606
607 if (!pCPanel)
608 return E_FAIL;
609
610 switch(iColumn)
611 {
612 case 0: /* name */
613 return SHSetStrRet(&psd->str, pCPanel->szName + pCPanel->offsDispName);
614 case 1: /* comment */
615 return SHSetStrRet(&psd->str, pCPanel->szName + pCPanel->offsComment);
616 }
617 }
618
619 return S_OK;
620 }
621
622 HRESULT WINAPI CControlPanelFolder::MapColumnToSCID(UINT column, SHCOLUMNID *pscid)
623 {
624 FIXME("(%p)\n", this);
625 return E_NOTIMPL;
626 }
627
628 /************************************************************************
629 * CControlPanelFolder::GetClassID
630 */
631 HRESULT WINAPI CControlPanelFolder::GetClassID(CLSID *lpClassId)
632 {
633 TRACE("(%p)\n", this);
634
635 if (!lpClassId)
636 return E_POINTER;
637 *lpClassId = CLSID_ControlPanel;
638
639 return S_OK;
640 }
641
642 /************************************************************************
643 * CControlPanelFolder::Initialize
644 *
645 * NOTES: it makes no sense to change the pidl
646 */
647 HRESULT WINAPI CControlPanelFolder::Initialize(LPCITEMIDLIST pidl)
648 {
649 if (pidlRoot)
650 SHFree((LPVOID)pidlRoot);
651
652 pidlRoot = ILClone(pidl);
653
654 /* Create the inner reg folder */
655 HRESULT hr;
656 static const WCHAR* pszCPanelPath = L"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}";
657 hr = CRegFolder_CreateInstance(&CLSID_ControlPanel,
658 pidlRoot,
659 pszCPanelPath,
660 IID_PPV_ARG(IShellFolder2, &m_regFolder));
661 if (FAILED_UNEXPECTEDLY(hr))
662 return hr;
663
664 return S_OK;
665 }
666
667 /**************************************************************************
668 * CControlPanelFolder::GetCurFolder
669 */
670 HRESULT WINAPI CControlPanelFolder::GetCurFolder(LPITEMIDLIST * pidl)
671 {
672 TRACE("(%p)->(%p)\n", this, pidl);
673
674 if (!pidl)
675 return E_POINTER;
676 *pidl = ILClone(pidlRoot);
677 return S_OK;
678 }
679
680 CCPLItemMenu::CCPLItemMenu()
681 {
682 m_apidl = NULL;
683 m_cidl = 0;
684 }
685
686 HRESULT WINAPI CCPLItemMenu::Initialize(UINT cidl, PCUITEMID_CHILD_ARRAY apidl)
687 {
688 m_cidl = cidl;
689 m_apidl = _ILCopyaPidl(apidl, m_cidl);
690 if (m_cidl && !m_apidl)
691 return E_OUTOFMEMORY;
692
693 return S_OK;
694 }
695
696 CCPLItemMenu::~CCPLItemMenu()
697 {
698 _ILFreeaPidl(m_apidl, m_cidl);
699 }
700
701 HRESULT WINAPI CCPLItemMenu::QueryContextMenu(
702 HMENU hMenu,
703 UINT indexMenu,
704 UINT idCmdFirst,
705 UINT idCmdLast,
706 UINT uFlags)
707 {
708 _InsertMenuItemW(hMenu, indexMenu++, TRUE, IDS_OPEN, MFT_STRING, MAKEINTRESOURCEW(IDS_OPEN), MFS_DEFAULT);
709 _InsertMenuItemW(hMenu, indexMenu++, TRUE, idCmdFirst + 1, MFT_SEPARATOR, NULL, MFS_ENABLED);
710 _InsertMenuItemW(hMenu, indexMenu++, TRUE, IDS_CREATELINK, MFT_STRING, MAKEINTRESOURCEW(IDS_CREATELINK), MFS_ENABLED);
711
712 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 2);
713 }
714
715 EXTERN_C
716 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow);
717
718 /**************************************************************************
719 * ICPanel_IContextMenu_InvokeCommand()
720 */
721 HRESULT WINAPI CCPLItemMenu::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
722 {
723 HRESULT hResult;
724
725 PIDLCPanelStruct *pCPanel = _ILGetCPanelPointer(m_apidl[0]);
726 if(!pCPanel)
727 return E_FAIL;
728
729 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n", this, lpcmi, lpcmi->lpVerb, lpcmi->hwnd);
730
731 if (lpcmi->lpVerb == MAKEINTRESOURCEA(IDS_OPEN)) //FIXME
732 {
733 /* Hardcode the command here; Executing a cpl file would be fine but we also need to run things like console.dll */
734 WCHAR wszParams[MAX_PATH];
735 PCWSTR wszFile = L"rundll32.exe";
736 PCWSTR wszFormat = L"shell32.dll,Control_RunDLL %s,%s";
737
738 wsprintfW(wszParams, wszFormat, pCPanel->szName, pCPanel->szName + pCPanel->offsDispName);
739
740 /* Note: we pass the applet name to Control_RunDLL to distinguish between multiple applets in one .cpl file */
741 ShellExecuteW(NULL, NULL, wszFile, wszParams, NULL, 0);
742 }
743 else if (lpcmi->lpVerb == MAKEINTRESOURCEA(IDS_CREATELINK)) //FIXME
744 {
745 CComPtr<IDataObject> pDataObj;
746 LPITEMIDLIST pidl = _ILCreateControlPanel();
747
748 hResult = SHCreateDataObject(pidl, m_cidl, m_apidl, NULL, IID_PPV_ARG(IDataObject, &pDataObj));
749 if (FAILED(hResult))
750 return hResult;
751
752 SHFree(pidl);
753
754 //FIXME: Use SHCreateLinks
755 CComPtr<IShellFolder> psf;
756 CComPtr<IDropTarget> pDT;
757
758 hResult = SHGetDesktopFolder(&psf);
759 if (FAILED(hResult))
760 return hResult;
761
762 hResult = psf->CreateViewObject(NULL, IID_PPV_ARG(IDropTarget, &pDT));
763 if (FAILED(hResult))
764 return hResult;
765
766 SHSimulateDrop(pDT, pDataObj, MK_CONTROL|MK_SHIFT, NULL, NULL);
767 }
768 return S_OK;
769 }
770
771 /**************************************************************************
772 * ICPanel_IContextMenu_GetCommandString()
773 *
774 */
775 HRESULT WINAPI CCPLItemMenu::GetCommandString(
776 UINT_PTR idCommand,
777 UINT uFlags,
778 UINT* lpReserved,
779 LPSTR lpszName,
780 UINT uMaxNameLen)
781 {
782 TRACE("(%p)->(idcom=%lx flags=%x %p name=%p len=%x)\n", this, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
783
784 FIXME("unknown command string\n");
785 return E_FAIL;
786 }
787
788 /**************************************************************************
789 * ICPanel_IContextMenu_HandleMenuMsg()
790 */
791 HRESULT WINAPI CCPLItemMenu::HandleMenuMsg(
792 UINT uMsg,
793 WPARAM wParam,
794 LPARAM lParam)
795 {
796 TRACE("ICPanel_IContextMenu_HandleMenuMsg (%p)->(msg=%x wp=%lx lp=%lx)\n", this, uMsg, wParam, lParam);
797
798 return E_NOTIMPL;
799 }