2 * IContextMenu for items in the shellview
4 * Copyright 1998, 2000 Juergen Schmied <juergen.schmied@debitel.net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
28 #include "wine/debug.h"
33 #include "undocshell.h"
38 #include "shell32_main.h"
39 #include "shellfolder.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(shell);
45 /**************************************************************************
46 * IContextMenu Implementation
50 IContextMenu2 IContextMenu2_iface;
52 IShellFolder* pSFParent;
53 LPITEMIDLIST pidl; /* root pidl */
54 LPITEMIDLIST *apidl; /* array of child pidls */
60 static inline ItemCmImpl *impl_from_IContextMenu2(IContextMenu2 *iface)
62 return CONTAINING_RECORD(iface, ItemCmImpl, IContextMenu2_iface);
66 static const IContextMenu2Vtbl cmvt;
68 /**************************************************************************
69 * ISvItemCm_CanRenameItems()
71 static BOOL ISvItemCm_CanRenameItems(ItemCmImpl *This)
75 TRACE("(%p)->()\n",This);
79 for(i = 0; i < This->cidl; i++){}
80 if(i > 1) return FALSE; /* can't rename more than one item at a time*/
81 dwAttributes = SFGAO_CANRENAME;
82 IShellFolder_GetAttributesOf(This->pSFParent, 1, (LPCITEMIDLIST*)This->apidl, &dwAttributes);
83 return dwAttributes & SFGAO_CANRENAME;
88 /**************************************************************************
89 * ISvItemCm_Constructor()
91 IContextMenu2 *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, const LPCITEMIDLIST *apidl, UINT cidl)
95 cm = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ItemCmImpl));
96 cm->IContextMenu2_iface.lpVtbl = &cmvt;
98 cm->pidl = ILClone(pidl);
99 cm->pSFParent = pSFParent;
101 if(pSFParent) IShellFolder_AddRef(pSFParent);
103 cm->apidl = _ILCopyaPidl(apidl, cidl);
107 for(u = 0; u < cidl; u++)
109 cm->bAllValues &= (_ILIsValue(apidl[u]) ? 1 : 0);
112 TRACE("(%p)->()\n",cm);
114 return &cm->IContextMenu2_iface;
117 /**************************************************************************
118 * ISvItemCm_fnQueryInterface
120 static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu2 *iface, REFIID riid, LPVOID *ppvObj)
122 ItemCmImpl *This = impl_from_IContextMenu2(iface);
124 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
128 if(IsEqualIID(riid, &IID_IUnknown) ||
129 IsEqualIID(riid, &IID_IContextMenu) ||
130 IsEqualIID(riid, &IID_IContextMenu2))
134 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
136 FIXME("-- LPSHELLEXTINIT pointer requested\n");
141 IUnknown_AddRef((IUnknown*)*ppvObj);
142 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
145 TRACE("-- Interface: E_NOINTERFACE\n");
146 return E_NOINTERFACE;
149 /**************************************************************************
152 static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu2 *iface)
154 ItemCmImpl *This = impl_from_IContextMenu2(iface);
155 ULONG refCount = InterlockedIncrement(&This->ref);
157 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
162 /**************************************************************************
163 * ISvItemCm_fnRelease
165 static ULONG WINAPI ISvItemCm_fnRelease(IContextMenu2 *iface)
167 ItemCmImpl *This = impl_from_IContextMenu2(iface);
168 ULONG refCount = InterlockedDecrement(&This->ref);
170 TRACE("(%p)->(count=%i)\n", This, refCount + 1);
174 TRACE(" destroying IContextMenu(%p)\n",This);
177 IShellFolder_Release(This->pSFParent);
181 /*make sure the pidl is freed*/
182 _ILFreeaPidl(This->apidl, This->cidl);
184 HeapFree(GetProcessHeap(),0,This);
189 static void _InsertMenuItemW (
200 mii.cbSize = sizeof(mii);
201 if (fType == MFT_SEPARATOR)
203 mii.fMask = MIIM_ID | MIIM_TYPE;
207 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
208 mii.dwTypeData = dwTypeData;
213 InsertMenuItemW( hmenu, indexMenu, fByPosition, &mii);
216 /**************************************************************************
217 * ISvItemCm_fnQueryContextMenu()
219 static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
220 IContextMenu2 *iface,
227 ItemCmImpl *This = impl_from_IContextMenu2(iface);
230 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
232 This->verb_offset=idCmdFirst;
234 if(!(CMF_DEFAULTONLY & uFlags) && This->cidl>0)
236 HMENU hmenures = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_SHV_FILE));
238 if(uFlags & CMF_EXPLORE)
239 RemoveMenu(hmenures, FCIDM_SHVIEW_OPEN, MF_BYCOMMAND);
241 uIDMax = Shell_MergeMenus(hmenu, GetSubMenu(hmenures, 0), indexMenu, idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);
243 DestroyMenu(hmenures);
249 mi.cbSize = sizeof(mi);
250 mi.fMask = MIIM_ID | MIIM_STRING | MIIM_FTYPE;
253 GetMenuItemInfoW(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND, &mi);
254 RemoveMenu(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND);
255 _InsertMenuItemW(hmenu, (uFlags & CMF_EXPLORE) ? 1 : 2, MF_BYPOSITION, FCIDM_SHVIEW_EXPLORE, MFT_STRING, str, MFS_ENABLED);
258 SetMenuDefaultItem(hmenu, 0, MF_BYPOSITION);
260 if(uFlags & ~CMF_CANRENAME)
261 RemoveMenu(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND);
263 EnableMenuItem(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND | ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
265 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, uIDMax-idCmdFirst);
267 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
270 /**************************************************************************
276 static void DoOpenExplore(ItemCmImpl *This, HWND hwnd, LPCSTR verb)
278 UINT i, bFolderFound = FALSE;
280 SHELLEXECUTEINFOA sei;
282 /* Find the first item in the list that is not a value. These commands
283 should never be invoked if there isn't at least one folder item in the list.*/
285 for(i = 0; i<This->cidl; i++)
287 if(!_ILIsValue(This->apidl[i]))
294 if (!bFolderFound) return;
296 pidlFQ = ILCombine(This->pidl, This->apidl[i]);
298 ZeroMemory(&sei, sizeof(sei));
299 sei.cbSize = sizeof(sei);
300 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
301 sei.lpIDList = pidlFQ;
302 sei.lpClass = "Folder";
304 sei.nShow = SW_SHOWNORMAL;
306 ShellExecuteExA(&sei);
310 /**************************************************************************
313 static void DoRename(ItemCmImpl *This, HWND hwnd)
318 TRACE("(%p)->(wnd=%p)\n",This, hwnd);
320 /* get the active IShellView */
321 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
323 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
325 TRACE("(sv=%p)\n",lpSV);
326 IShellView_SelectItem(lpSV, This->apidl[0],
327 SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
328 IShellView_Release(lpSV);
333 /**************************************************************************
336 * deletes the currently selected items
338 static void DoDelete(ItemCmImpl *This)
342 IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
345 ISFHelper_DeleteItems(psfhlp, This->cidl, (LPCITEMIDLIST *)This->apidl);
346 ISFHelper_Release(psfhlp);
350 /**************************************************************************
353 * copies the currently selected items into the clipboard
355 static BOOL DoCopyOrCut(ItemCmImpl *This, HWND hwnd, BOOL bCut)
361 TRACE("(%p)->(wnd=%p,bCut=0x%08x)\n",This, hwnd, bCut);
363 /* get the active IShellView */
364 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
366 if (SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
368 if (SUCCEEDED(IShellView_GetItemObject(lpSV, SVGIO_SELECTION, &IID_IDataObject, (LPVOID*)&lpDo)))
370 OleSetClipboard(lpDo);
371 IDataObject_Release(lpDo);
373 IShellView_Release(lpSV);
379 /**************************************************************************
380 * Properties_AddPropSheetCallback
382 * Used by DoOpenProperties through SHCreatePropSheetExtArrayEx to add
383 * propertysheet pages from shell extensions.
385 static BOOL CALLBACK Properties_AddPropSheetCallback(HPROPSHEETPAGE hpage, LPARAM lparam)
387 LPPROPSHEETHEADERW psh = (LPPROPSHEETHEADERW) lparam;
388 psh->u3.phpage[psh->nPages++] = hpage;
393 /**************************************************************************
396 static void DoOpenProperties(ItemCmImpl *This, HWND hwnd)
398 static const UINT MAX_PROP_PAGES = 99;
399 static const WCHAR wszFolder[] = {'F','o','l','d','e','r', 0};
400 static const WCHAR wszFiletypeAll[] = {'*',0};
401 LPSHELLFOLDER lpDesktopSF;
404 WCHAR wszFiletype[MAX_PATH];
405 WCHAR wszFilename[MAX_PATH];
406 PROPSHEETHEADERW psh;
407 HPROPSHEETPAGE hpages[MAX_PROP_PAGES];
411 TRACE("(%p)->(wnd=%p)\n", This, hwnd);
413 ZeroMemory(&psh, sizeof(PROPSHEETHEADERW));
414 psh.dwSize = sizeof (PROPSHEETHEADERW);
415 psh.hwndParent = hwnd;
416 psh.dwFlags = PSH_PROPTITLE;
418 psh.u3.phpage = hpages;
419 psh.u2.nStartPage = 0;
421 _ILSimpleGetTextW(This->apidl[0], (LPVOID)&wszFilename, MAX_PATH);
422 psh.pszCaption = (LPCWSTR)&wszFilename;
424 /* Find out where to look for the shell extensions */
425 if (_ILIsValue(This->apidl[0]))
429 if (_ILGetExtension(This->apidl[0], sTemp, 64))
431 HCR_MapTypeToValueA(sTemp, sTemp, 64, TRUE);
432 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, wszFiletype, MAX_PATH);
439 else if (_ILIsFolder(This->apidl[0]))
441 lstrcpynW(wszFiletype, wszFolder, 64);
443 else if (_ILIsSpecialFolder(This->apidl[0]))
446 static const WCHAR wszclsid[] = {'C','L','S','I','D','\\', 0};
447 folderGUID = _ILGetGUIDPointer(This->apidl[0]);
448 lstrcpyW(wszFiletype, wszclsid);
449 StringFromGUID2(folderGUID, &wszFiletype[6], MAX_PATH - 6);
453 FIXME("Requested properties for unknown type.\n");
457 /* Get a suitable DataObject for accessing the files */
458 SHGetDesktopFolder(&lpDesktopSF);
459 if (_ILIsPidlSimple(This->pidl))
461 ret = IShellFolder_GetUIObjectOf(lpDesktopSF, hwnd, This->cidl, (LPCITEMIDLIST*)This->apidl,
462 &IID_IDataObject, NULL, (LPVOID *)&lpDo);
463 IShellFolder_Release(lpDesktopSF);
467 IShellFolder_BindToObject(lpDesktopSF, This->pidl, NULL, &IID_IShellFolder, (LPVOID*) &lpSF);
468 ret = IShellFolder_GetUIObjectOf(lpSF, hwnd, This->cidl, (LPCITEMIDLIST*)This->apidl,
469 &IID_IDataObject, NULL, (LPVOID *)&lpDo);
470 IShellFolder_Release(lpSF);
471 IShellFolder_Release(lpDesktopSF);
476 hpsxa = SHCreatePropSheetExtArrayEx(HKEY_CLASSES_ROOT, wszFiletype, MAX_PROP_PAGES - psh.nPages, lpDo);
479 SHAddFromPropSheetExtArray(hpsxa, Properties_AddPropSheetCallback, (LPARAM)&psh);
480 SHDestroyPropSheetExtArray(hpsxa);
482 hpsxa = SHCreatePropSheetExtArrayEx(HKEY_CLASSES_ROOT, wszFiletypeAll, MAX_PROP_PAGES - psh.nPages, lpDo);
485 SHAddFromPropSheetExtArray(hpsxa, Properties_AddPropSheetCallback, (LPARAM)&psh);
486 SHDestroyPropSheetExtArray(hpsxa);
488 IDataObject_Release(lpDo);
492 PropertySheetW(&psh);
494 FIXME("No property pages found.\n");
497 /**************************************************************************
498 * ISvItemCm_fnInvokeCommand()
500 static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
501 IContextMenu2 *iface,
502 LPCMINVOKECOMMANDINFO lpcmi)
504 ItemCmImpl *This = impl_from_IContextMenu2(iface);
506 if (lpcmi->cbSize != sizeof(CMINVOKECOMMANDINFO))
507 FIXME("Is an EX structure\n");
509 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
511 if( HIWORD(lpcmi->lpVerb)==0 && LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST)
513 TRACE("Invalid Verb %x\n",LOWORD(lpcmi->lpVerb));
517 if (HIWORD(lpcmi->lpVerb) == 0)
519 switch(LOWORD(lpcmi->lpVerb-This->verb_offset))
521 case FCIDM_SHVIEW_EXPLORE:
522 TRACE("Verb FCIDM_SHVIEW_EXPLORE\n");
523 DoOpenExplore(This, lpcmi->hwnd, "explore");
525 case FCIDM_SHVIEW_OPEN:
526 TRACE("Verb FCIDM_SHVIEW_OPEN\n");
527 DoOpenExplore(This, lpcmi->hwnd, "open");
529 case FCIDM_SHVIEW_RENAME:
530 TRACE("Verb FCIDM_SHVIEW_RENAME\n");
531 DoRename(This, lpcmi->hwnd);
533 case FCIDM_SHVIEW_DELETE:
534 TRACE("Verb FCIDM_SHVIEW_DELETE\n");
537 case FCIDM_SHVIEW_COPY:
538 TRACE("Verb FCIDM_SHVIEW_COPY\n");
539 DoCopyOrCut(This, lpcmi->hwnd, FALSE);
541 case FCIDM_SHVIEW_CUT:
542 TRACE("Verb FCIDM_SHVIEW_CUT\n");
543 DoCopyOrCut(This, lpcmi->hwnd, TRUE);
545 case FCIDM_SHVIEW_PROPERTIES:
546 TRACE("Verb FCIDM_SHVIEW_PROPERTIES\n");
547 DoOpenProperties(This, lpcmi->hwnd);
550 FIXME("Unhandled Verb %xl\n",LOWORD(lpcmi->lpVerb)-This->verb_offset);
556 TRACE("Verb is %s\n",debugstr_a(lpcmi->lpVerb));
557 if (strcmp(lpcmi->lpVerb,"delete")==0)
559 else if (strcmp(lpcmi->lpVerb,"properties")==0)
560 DoOpenProperties(This, lpcmi->hwnd);
562 FIXME("Unhandled string verb %s\n",debugstr_a(lpcmi->lpVerb));
569 /**************************************************************************
570 * ISvItemCm_fnGetCommandString()
572 static HRESULT WINAPI ISvItemCm_fnGetCommandString(
573 IContextMenu2 *iface,
580 ItemCmImpl *This = impl_from_IContextMenu2(iface);
582 HRESULT hr = E_INVALIDARG;
584 TRACE("(%p)->(idcom=%lx flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
594 switch(idCommand-This->verb_offset)
596 case FCIDM_SHVIEW_RENAME:
597 strcpy(lpszName, "rename");
603 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
604 case, you need to do the lstrcpyW to the pointer passed.*/
606 switch(idCommand-This->verb_offset)
608 case FCIDM_SHVIEW_RENAME:
609 MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
620 TRACE("-- (%p)->(name=%s)\n",This, lpszName);
624 /**************************************************************************
625 * ISvItemCm_fnHandleMenuMsg()
627 * should be only in IContextMenu2 and IContextMenu3
628 * is nevertheless called from word95
630 static HRESULT WINAPI ISvItemCm_fnHandleMenuMsg(
631 IContextMenu2 *iface,
636 ItemCmImpl *This = impl_from_IContextMenu2(iface);
638 TRACE("(%p)->(msg=%x wp=%lx lp=%lx)\n",This, uMsg, wParam, lParam);
643 static const IContextMenu2Vtbl cmvt =
645 ISvItemCm_fnQueryInterface,
648 ISvItemCm_fnQueryContextMenu,
649 ISvItemCm_fnInvokeCommand,
650 ISvItemCm_fnGetCommandString,
651 ISvItemCm_fnHandleMenuMsg