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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/debug.h"
28 #include "undocshell.h"
31 #include "shell32_main.h"
32 #include "shellfolder.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(shell);
36 /**************************************************************************
37 * IContextMenu Implementation
40 { ICOM_VFIELD(IContextMenu);
42 IShellFolder* pSFParent;
43 LPITEMIDLIST pidl; /* root pidl */
44 LPITEMIDLIST *apidl; /* array of child pidls */
50 static struct ICOM_VTABLE(IContextMenu) cmvt;
52 /**************************************************************************
53 * ISvItemCm_CanRenameItems()
55 static BOOL ISvItemCm_CanRenameItems(ItemCmImpl *This)
59 TRACE("(%p)->()\n",This);
63 for(i = 0; i < This->cidl; i++){}
64 if(i > 1) return FALSE; /* can't rename more than one item at a time*/
65 dwAttributes = SFGAO_CANRENAME;
66 IShellFolder_GetAttributesOf(This->pSFParent, 1, This->apidl, &dwAttributes);
67 return dwAttributes & SFGAO_CANRENAME;
72 /**************************************************************************
73 * ISvItemCm_Constructor()
75 IContextMenu *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, LPCITEMIDLIST *apidl, UINT cidl)
79 cm = (ItemCmImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ItemCmImpl));
82 cm->pidl = ILClone(pidl);
83 cm->pSFParent = pSFParent;
85 if(pSFParent) IShellFolder_AddRef(pSFParent);
87 cm->apidl = _ILCopyaPidl(apidl, cidl);
91 for(u = 0; u < cidl; u++)
93 cm->bAllValues &= (_ILIsValue(apidl[u]) ? 1 : 0);
96 TRACE("(%p)->()\n",cm);
98 return (IContextMenu*)cm;
101 /**************************************************************************
102 * ISvItemCm_fnQueryInterface
104 static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu *iface, REFIID riid, LPVOID *ppvObj)
106 ICOM_THIS(ItemCmImpl, iface);
108 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
112 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
116 else if(IsEqualIID(riid, &IID_IContextMenu)) /*IContextMenu*/
120 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
122 FIXME("-- LPSHELLEXTINIT pointer requested\n");
127 IUnknown_AddRef((IUnknown*)*ppvObj);
128 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
131 TRACE("-- Interface: E_NOINTERFACE\n");
132 return E_NOINTERFACE;
135 /**************************************************************************
138 static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu *iface)
140 ICOM_THIS(ItemCmImpl, iface);
142 TRACE("(%p)->(count=%lu)\n",This, This->ref);
144 return ++(This->ref);
147 /**************************************************************************
148 * ISvItemCm_fnRelease
150 static ULONG WINAPI ISvItemCm_fnRelease(IContextMenu *iface)
152 ICOM_THIS(ItemCmImpl, iface);
154 TRACE("(%p)->()\n",This);
158 TRACE(" destroying IContextMenu(%p)\n",This);
161 IShellFolder_Release(This->pSFParent);
166 /*make sure the pidl is freed*/
167 _ILFreeaPidl(This->apidl, This->cidl);
169 HeapFree(GetProcessHeap(),0,This);
175 /**************************************************************************
178 void WINAPI _InsertMenuItem (
189 ZeroMemory(&mii, sizeof(mii));
190 mii.cbSize = sizeof(mii);
191 if (fType == MFT_SEPARATOR)
193 mii.fMask = MIIM_ID | MIIM_TYPE;
197 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
198 mii.dwTypeData = dwTypeData;
203 InsertMenuItemA( hmenu, indexMenu, fByPosition, &mii);
205 /**************************************************************************
206 * ISvItemCm_fnQueryContextMenu()
209 static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
217 ICOM_THIS(ItemCmImpl, iface);
219 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
221 if(!(CMF_DEFAULTONLY & uFlags))
223 if(uFlags & CMF_EXPLORE)
227 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
228 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED|MFS_DEFAULT);
232 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED|MFS_DEFAULT);
233 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
238 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Select", MFS_ENABLED|MFS_DEFAULT);
240 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
241 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_COPY, MFT_STRING, "&Copy", MFS_ENABLED);
242 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_CUT, MFT_STRING, "&Cut", MFS_ENABLED);
244 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
245 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_DELETE, MFT_STRING, "&Delete", MFS_ENABLED);
247 if(uFlags & CMF_CANRENAME)
248 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_RENAME, MFT_STRING, "&Rename", ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
250 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (FCIDM_SHVIEWLAST));
252 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
255 /**************************************************************************
261 static void DoOpenExplore(
266 ICOM_THIS(ItemCmImpl, iface);
268 UINT i, bFolderFound = FALSE;
270 SHELLEXECUTEINFOA sei;
272 /* Find the first item in the list that is not a value. These commands
273 should never be invoked if there isn't at least one folder item in the list.*/
275 for(i = 0; i<This->cidl; i++)
277 if(!_ILIsValue(This->apidl[i]))
284 if (!bFolderFound) return;
286 pidlFQ = ILCombine(This->pidl, This->apidl[i]);
288 ZeroMemory(&sei, sizeof(sei));
289 sei.cbSize = sizeof(sei);
290 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
291 sei.lpIDList = pidlFQ;
292 sei.lpClass = "folder";
294 sei.nShow = SW_SHOWNORMAL;
296 ShellExecuteExA(&sei);
300 /**************************************************************************
303 static void DoRename(
307 ICOM_THIS(ItemCmImpl, iface);
312 TRACE("(%p)->(wnd=%p)\n",This, hwnd);
314 /* get the active IShellView */
315 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
317 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
319 TRACE("(sv=%p)\n",lpSV);
320 IShellView_SelectItem(lpSV, This->apidl[0],
321 SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
322 IShellView_Release(lpSV);
327 /**************************************************************************
330 * deletes the currently selected items
332 static void DoDelete(IContextMenu *iface)
334 ICOM_THIS(ItemCmImpl, iface);
337 IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
340 ISFHelper_DeleteItems(psfhlp, This->cidl, This->apidl);
341 ISFHelper_Release(psfhlp);
345 /**************************************************************************
348 * copies the currently selected items into the clipboard
350 static BOOL DoCopyOrCut(
355 ICOM_THIS(ItemCmImpl, iface);
361 TRACE("(%p)->(wnd=%p,bCut=0x%08x)\n",This, hwnd, bCut);
365 /* get the active IShellView */
366 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
368 if (SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
370 if (SUCCEEDED(IShellView_GetItemObject(lpSV, SVGIO_SELECTION, &IID_IDataObject, (LPVOID*)&lpDo)))
372 pOleSetClipboard(lpDo);
373 IDataObject_Release(lpDo);
375 IShellView_Release(lpSV);
382 the following code does the copy operation witout ole32.dll
383 we might need this possibility too (js)
385 BOOL bSuccess = FALSE;
387 TRACE("(%p)\n", iface);
389 if(OpenClipboard(NULL))
393 IPersistFolder2 * ppf2;
394 IShellFolder_QueryInterface(This->pSFParent, &IID_IPersistFolder2, (LPVOID*)&ppf2);
398 IPersistFolder2_GetCurFolder(ppf2, &pidl);
403 hMem = RenderHDROP(pidl, This->apidl, This->cidl);
405 if(SetClipboardData(CF_HDROP, hMem))
411 IPersistFolder2_Release(ppf2);
420 /**************************************************************************
421 * ISvItemCm_fnInvokeCommand()
423 static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
425 LPCMINVOKECOMMANDINFO lpcmi)
427 ICOM_THIS(ItemCmImpl, iface);
429 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
431 if(LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST) return E_INVALIDARG;
433 switch(LOWORD(lpcmi->lpVerb))
435 case FCIDM_SHVIEW_EXPLORE:
436 DoOpenExplore(iface, lpcmi->hwnd, "explore");
438 case FCIDM_SHVIEW_OPEN:
439 DoOpenExplore(iface, lpcmi->hwnd, "open");
441 case FCIDM_SHVIEW_RENAME:
442 DoRename(iface, lpcmi->hwnd);
444 case FCIDM_SHVIEW_DELETE:
447 case FCIDM_SHVIEW_COPY:
448 DoCopyOrCut(iface, lpcmi->hwnd, FALSE);
450 case FCIDM_SHVIEW_CUT:
451 DoCopyOrCut(iface, lpcmi->hwnd, TRUE);
457 /**************************************************************************
458 * ISvItemCm_fnGetCommandString()
460 static HRESULT WINAPI ISvItemCm_fnGetCommandString(
468 ICOM_THIS(ItemCmImpl, iface);
470 HRESULT hr = E_INVALIDARG;
472 TRACE("(%p)->(idcom=%x flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
483 case FCIDM_SHVIEW_RENAME:
484 strcpy((LPSTR)lpszName, "rename");
490 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
491 case, you need to do the lstrcpyW to the pointer passed.*/
494 { case FCIDM_SHVIEW_RENAME:
495 MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
505 TRACE("-- (%p)->(name=%s)\n",This, lpszName);
509 /**************************************************************************
510 * ISvItemCm_fnHandleMenuMsg()
512 * should be only in IContextMenu2 and IContextMenu3
513 * is nevertheless called from word95
515 static HRESULT WINAPI ISvItemCm_fnHandleMenuMsg(
521 ICOM_THIS(ItemCmImpl, iface);
523 TRACE("(%p)->(msg=%x wp=%x lp=%lx)\n",This, uMsg, wParam, lParam);
528 static struct ICOM_VTABLE(IContextMenu) cmvt =
530 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
531 ISvItemCm_fnQueryInterface,
534 ISvItemCm_fnQueryContextMenu,
535 ISvItemCm_fnInvokeCommand,
536 ISvItemCm_fnGetCommandString,
537 ISvItemCm_fnHandleMenuMsg,
538 (void *) 0xdeadbabe /* just paranoia */