2 * IContextMenu for items in the shellview
4 * 1998, 2000 Juergen Schmied <juergen.schmied@debitel.net>
9 #include "debugtools.h"
13 #include "wine/winestring.h"
14 #include "wine/obj_base.h"
15 #include "wine/obj_contextmenu.h"
16 #include "wine/obj_shellbrowser.h"
17 #include "wine/obj_shellextinit.h"
18 #include "wine/undocshell.h"
20 #include "shell32_main.h"
21 #include "shellfolder.h"
23 DEFAULT_DEBUG_CHANNEL(shell);
25 /**************************************************************************
26 * IContextMenu Implementation
29 { ICOM_VFIELD(IContextMenu);
31 IShellFolder* pSFParent;
32 LPITEMIDLIST pidl; /* root pidl */
33 LPITEMIDLIST *apidl; /* array of child pidls */
39 static struct ICOM_VTABLE(IContextMenu) cmvt;
41 /**************************************************************************
42 * ISvItemCm_CanRenameItems()
44 static BOOL ISvItemCm_CanRenameItems(ItemCmImpl *This)
48 TRACE("(%p)->()\n",This);
52 for(i = 0; i < This->cidl; i++){}
53 if(i > 1) return FALSE; /* can't rename more than one item at a time*/
54 dwAttributes = SFGAO_CANRENAME;
55 IShellFolder_GetAttributesOf(This->pSFParent, 1, This->apidl, &dwAttributes);
56 return dwAttributes & SFGAO_CANRENAME;
61 /**************************************************************************
62 * ISvItemCm_Constructor()
64 IContextMenu *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, LPCITEMIDLIST *apidl, UINT cidl)
68 cm = (ItemCmImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ItemCmImpl));
71 cm->pidl = ILClone(pidl);
72 cm->pSFParent = pSFParent;
74 if(pSFParent) IShellFolder_AddRef(pSFParent);
76 cm->apidl = _ILCopyaPidl(apidl, cidl);
80 for(u = 0; u < cidl; u++)
82 cm->bAllValues &= (_ILIsValue(apidl[u]) ? 1 : 0);
85 TRACE("(%p)->()\n",cm);
88 return (IContextMenu*)cm;
91 /**************************************************************************
92 * ISvItemCm_fnQueryInterface
94 static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu *iface, REFIID riid, LPVOID *ppvObj)
96 ICOM_THIS(ItemCmImpl, iface);
98 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
102 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
106 else if(IsEqualIID(riid, &IID_IContextMenu)) /*IContextMenu*/
110 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
112 FIXME("-- LPSHELLEXTINIT pointer requested\n");
117 IUnknown_AddRef((IUnknown*)*ppvObj);
118 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
121 TRACE("-- Interface: E_NOINTERFACE\n");
122 return E_NOINTERFACE;
125 /**************************************************************************
128 static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu *iface)
130 ICOM_THIS(ItemCmImpl, iface);
132 TRACE("(%p)->(count=%lu)\n",This, This->ref);
135 return ++(This->ref);
138 /**************************************************************************
139 * ISvItemCm_fnRelease
141 static ULONG WINAPI ISvItemCm_fnRelease(IContextMenu *iface)
143 ICOM_THIS(ItemCmImpl, iface);
145 TRACE("(%p)->()\n",This);
151 TRACE(" destroying IContextMenu(%p)\n",This);
154 IShellFolder_Release(This->pSFParent);
159 /*make sure the pidl is freed*/
160 _ILFreeaPidl(This->apidl, This->cidl);
162 HeapFree(GetProcessHeap(),0,This);
168 /**************************************************************************
171 void WINAPI _InsertMenuItem (
182 ZeroMemory(&mii, sizeof(mii));
183 mii.cbSize = sizeof(mii);
184 if (fType == MFT_SEPARATOR)
186 mii.fMask = MIIM_ID | MIIM_TYPE;
190 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
191 mii.dwTypeData = dwTypeData;
196 InsertMenuItemA( hmenu, indexMenu, fByPosition, &mii);
198 /**************************************************************************
199 * ISvItemCm_fnQueryContextMenu()
202 static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
210 ICOM_THIS(ItemCmImpl, iface);
212 TRACE("(%p)->(hmenu=%x indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
214 if(!(CMF_DEFAULTONLY & uFlags))
216 if(uFlags & CMF_EXPLORE)
220 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
221 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED|MFS_DEFAULT);
225 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED|MFS_DEFAULT);
226 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
231 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Select", MFS_ENABLED|MFS_DEFAULT);
233 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
234 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_COPY, MFT_STRING, "&Copy", MFS_ENABLED);
235 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_CUT, MFT_STRING, "&Cut", MFS_ENABLED);
237 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
238 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_DELETE, MFT_STRING, "&Delete", MFS_ENABLED);
240 if(uFlags & CMF_CANRENAME)
241 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_RENAME, MFT_STRING, "&Rename", ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
243 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (FCIDM_SHVIEWLAST));
245 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
248 /**************************************************************************
254 static void DoOpenExplore(
259 ICOM_THIS(ItemCmImpl, iface);
261 int i, bFolderFound = FALSE;
263 SHELLEXECUTEINFOA sei;
265 /* Find the first item in the list that is not a value. These commands
266 should never be invoked if there isn't at least one folder item in the list.*/
268 for(i = 0; i<This->cidl; i++)
270 if(!_ILIsValue(This->apidl[i]))
277 if (!bFolderFound) return;
279 pidlFQ = ILCombine(This->pidl, This->apidl[i]);
281 ZeroMemory(&sei, sizeof(sei));
282 sei.cbSize = sizeof(sei);
283 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
284 sei.lpIDList = pidlFQ;
285 sei.lpClass = "folder";
287 sei.nShow = SW_SHOWNORMAL;
289 ShellExecuteExA(&sei);
293 /**************************************************************************
296 static void DoRename(
300 ICOM_THIS(ItemCmImpl, iface);
305 TRACE("(%p)->(wnd=%x)\n",This, hwnd);
307 /* get the active IShellView */
308 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
310 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
312 TRACE("(sv=%p)\n",lpSV);
313 IShellView_SelectItem(lpSV, This->apidl[0],
314 SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
315 IShellView_Release(lpSV);
320 /**************************************************************************
323 * deletes the currently selected items
325 static void DoDelete(IContextMenu *iface)
327 ICOM_THIS(ItemCmImpl, iface);
330 IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
333 ISFHelper_DeleteItems(psfhlp, This->cidl, This->apidl);
334 ISFHelper_Release(psfhlp);
338 /**************************************************************************
341 * copys the currently selected items into the clipboard
343 static BOOL DoCopyOrCut(
348 ICOM_THIS(ItemCmImpl, iface);
354 TRACE("(%p)->(wnd=0x%04x,bCut=0x%08x)\n",This, hwnd, bCut);
358 /* get the active IShellView */
359 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
361 if (SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
363 if (SUCCEEDED(IShellView_GetItemObject(lpSV, SVGIO_SELECTION, &IID_IDataObject, (LPVOID*)&lpDo)))
365 pOleSetClipboard(lpDo);
366 IDataObject_Release(lpDo);
368 IShellView_Release(lpSV);
375 the following code does the copy operation witout ole32.dll
376 we might need this possibility too (js)
378 BOOL bSuccess = FALSE;
380 TRACE("(%p)\n", iface);
382 if(OpenClipboard(NULL))
386 IPersistFolder2 * ppf2;
387 IShellFolder_QueryInterface(This->pSFParent, &IID_IPersistFolder2, (LPVOID*)&ppf2);
391 IPersistFolder2_GetCurFolder(ppf2, &pidl);
396 hMem = RenderHDROP(pidl, This->apidl, This->cidl);
398 if(SetClipboardData(CF_HDROP, hMem))
404 IPersistFolder2_Release(ppf2);
413 /**************************************************************************
414 * ISvItemCm_fnInvokeCommand()
416 static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
418 LPCMINVOKECOMMANDINFO lpcmi)
420 ICOM_THIS(ItemCmImpl, iface);
422 TRACE("(%p)->(invcom=%p verb=%p wnd=%x)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
424 if(LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST) return E_INVALIDARG;
426 switch(LOWORD(lpcmi->lpVerb))
428 case FCIDM_SHVIEW_EXPLORE:
429 DoOpenExplore(iface, lpcmi->hwnd, "explore");
431 case FCIDM_SHVIEW_OPEN:
432 DoOpenExplore(iface, lpcmi->hwnd, "open");
434 case FCIDM_SHVIEW_RENAME:
435 DoRename(iface, lpcmi->hwnd);
437 case FCIDM_SHVIEW_DELETE:
440 case FCIDM_SHVIEW_COPY:
441 DoCopyOrCut(iface, lpcmi->hwnd, FALSE);
443 case FCIDM_SHVIEW_CUT:
444 DoCopyOrCut(iface, lpcmi->hwnd, TRUE);
450 /**************************************************************************
451 * ISvItemCm_fnGetCommandString()
453 static HRESULT WINAPI ISvItemCm_fnGetCommandString(
461 ICOM_THIS(ItemCmImpl, iface);
463 HRESULT hr = E_INVALIDARG;
465 TRACE("(%p)->(idcom=%x flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
476 case FCIDM_SHVIEW_RENAME:
477 strcpy((LPSTR)lpszName, "rename");
483 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
484 case, you need to do the lstrcpyW to the pointer passed.*/
487 { case FCIDM_SHVIEW_RENAME:
488 lstrcpyAtoW((LPWSTR)lpszName, "rename");
498 TRACE("-- (%p)->(name=%s)\n",This, lpszName);
502 /**************************************************************************
503 * ISvItemCm_fnHandleMenuMsg()
505 * should be only in IContextMenu2 and IContextMenu3
506 * is nevertheless called from word95
508 static HRESULT WINAPI ISvItemCm_fnHandleMenuMsg(
514 ICOM_THIS(ItemCmImpl, iface);
516 TRACE("(%p)->(msg=%x wp=%x lp=%lx)\n",This, uMsg, wParam, lParam);
521 static struct ICOM_VTABLE(IContextMenu) cmvt =
523 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
524 ISvItemCm_fnQueryInterface,
527 ISvItemCm_fnQueryContextMenu,
528 ISvItemCm_fnInvokeCommand,
529 ISvItemCm_fnGetCommandString,
530 ISvItemCm_fnHandleMenuMsg,
531 (void *) 0xdeadbabe /* just paranoia */