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 "wine/obj_base.h"
29 #include "wine/obj_contextmenu.h"
30 #include "wine/obj_shellbrowser.h"
31 #include "wine/obj_shellextinit.h"
32 #include "undocshell.h"
34 #include "shell32_main.h"
35 #include "shellfolder.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(shell);
39 /**************************************************************************
40 * IContextMenu Implementation
43 { ICOM_VFIELD(IContextMenu);
45 IShellFolder* pSFParent;
46 LPITEMIDLIST pidl; /* root pidl */
47 LPITEMIDLIST *apidl; /* array of child pidls */
53 static struct ICOM_VTABLE(IContextMenu) cmvt;
55 /**************************************************************************
56 * ISvItemCm_CanRenameItems()
58 static BOOL ISvItemCm_CanRenameItems(ItemCmImpl *This)
62 TRACE("(%p)->()\n",This);
66 for(i = 0; i < This->cidl; i++){}
67 if(i > 1) return FALSE; /* can't rename more than one item at a time*/
68 dwAttributes = SFGAO_CANRENAME;
69 IShellFolder_GetAttributesOf(This->pSFParent, 1, This->apidl, &dwAttributes);
70 return dwAttributes & SFGAO_CANRENAME;
75 /**************************************************************************
76 * ISvItemCm_Constructor()
78 IContextMenu *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, LPCITEMIDLIST *apidl, UINT cidl)
82 cm = (ItemCmImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ItemCmImpl));
85 cm->pidl = ILClone(pidl);
86 cm->pSFParent = pSFParent;
88 if(pSFParent) IShellFolder_AddRef(pSFParent);
90 cm->apidl = _ILCopyaPidl(apidl, cidl);
94 for(u = 0; u < cidl; u++)
96 cm->bAllValues &= (_ILIsValue(apidl[u]) ? 1 : 0);
99 TRACE("(%p)->()\n",cm);
101 return (IContextMenu*)cm;
104 /**************************************************************************
105 * ISvItemCm_fnQueryInterface
107 static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu *iface, REFIID riid, LPVOID *ppvObj)
109 ICOM_THIS(ItemCmImpl, iface);
111 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
115 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
119 else if(IsEqualIID(riid, &IID_IContextMenu)) /*IContextMenu*/
123 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
125 FIXME("-- LPSHELLEXTINIT pointer requested\n");
130 IUnknown_AddRef((IUnknown*)*ppvObj);
131 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
134 TRACE("-- Interface: E_NOINTERFACE\n");
135 return E_NOINTERFACE;
138 /**************************************************************************
141 static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu *iface)
143 ICOM_THIS(ItemCmImpl, iface);
145 TRACE("(%p)->(count=%lu)\n",This, This->ref);
147 return ++(This->ref);
150 /**************************************************************************
151 * ISvItemCm_fnRelease
153 static ULONG WINAPI ISvItemCm_fnRelease(IContextMenu *iface)
155 ICOM_THIS(ItemCmImpl, iface);
157 TRACE("(%p)->()\n",This);
161 TRACE(" destroying IContextMenu(%p)\n",This);
164 IShellFolder_Release(This->pSFParent);
169 /*make sure the pidl is freed*/
170 _ILFreeaPidl(This->apidl, This->cidl);
172 HeapFree(GetProcessHeap(),0,This);
178 /**************************************************************************
181 void WINAPI _InsertMenuItem (
192 ZeroMemory(&mii, sizeof(mii));
193 mii.cbSize = sizeof(mii);
194 if (fType == MFT_SEPARATOR)
196 mii.fMask = MIIM_ID | MIIM_TYPE;
200 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
201 mii.dwTypeData = dwTypeData;
206 InsertMenuItemA( hmenu, indexMenu, fByPosition, &mii);
208 /**************************************************************************
209 * ISvItemCm_fnQueryContextMenu()
212 static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
220 ICOM_THIS(ItemCmImpl, iface);
222 TRACE("(%p)->(hmenu=%x indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
224 if(!(CMF_DEFAULTONLY & uFlags))
226 if(uFlags & CMF_EXPLORE)
230 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
231 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED|MFS_DEFAULT);
235 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED|MFS_DEFAULT);
236 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
241 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Select", MFS_ENABLED|MFS_DEFAULT);
243 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
244 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_COPY, MFT_STRING, "&Copy", MFS_ENABLED);
245 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_CUT, MFT_STRING, "&Cut", MFS_ENABLED);
247 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
248 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_DELETE, MFT_STRING, "&Delete", MFS_ENABLED);
250 if(uFlags & CMF_CANRENAME)
251 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_RENAME, MFT_STRING, "&Rename", ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
253 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (FCIDM_SHVIEWLAST));
255 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
258 /**************************************************************************
264 static void DoOpenExplore(
269 ICOM_THIS(ItemCmImpl, iface);
271 int i, bFolderFound = FALSE;
273 SHELLEXECUTEINFOA sei;
275 /* Find the first item in the list that is not a value. These commands
276 should never be invoked if there isn't at least one folder item in the list.*/
278 for(i = 0; i<This->cidl; i++)
280 if(!_ILIsValue(This->apidl[i]))
287 if (!bFolderFound) return;
289 pidlFQ = ILCombine(This->pidl, This->apidl[i]);
291 ZeroMemory(&sei, sizeof(sei));
292 sei.cbSize = sizeof(sei);
293 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
294 sei.lpIDList = pidlFQ;
295 sei.lpClass = "folder";
297 sei.nShow = SW_SHOWNORMAL;
299 ShellExecuteExA(&sei);
303 /**************************************************************************
306 static void DoRename(
310 ICOM_THIS(ItemCmImpl, iface);
315 TRACE("(%p)->(wnd=%x)\n",This, hwnd);
317 /* get the active IShellView */
318 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
320 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
322 TRACE("(sv=%p)\n",lpSV);
323 IShellView_SelectItem(lpSV, This->apidl[0],
324 SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
325 IShellView_Release(lpSV);
330 /**************************************************************************
333 * deletes the currently selected items
335 static void DoDelete(IContextMenu *iface)
337 ICOM_THIS(ItemCmImpl, iface);
340 IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
343 ISFHelper_DeleteItems(psfhlp, This->cidl, This->apidl);
344 ISFHelper_Release(psfhlp);
348 /**************************************************************************
351 * copies the currently selected items into the clipboard
353 static BOOL DoCopyOrCut(
358 ICOM_THIS(ItemCmImpl, iface);
364 TRACE("(%p)->(wnd=0x%04x,bCut=0x%08x)\n",This, hwnd, bCut);
368 /* get the active IShellView */
369 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
371 if (SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
373 if (SUCCEEDED(IShellView_GetItemObject(lpSV, SVGIO_SELECTION, &IID_IDataObject, (LPVOID*)&lpDo)))
375 pOleSetClipboard(lpDo);
376 IDataObject_Release(lpDo);
378 IShellView_Release(lpSV);
385 the following code does the copy operation witout ole32.dll
386 we might need this possibility too (js)
388 BOOL bSuccess = FALSE;
390 TRACE("(%p)\n", iface);
392 if(OpenClipboard(NULL))
396 IPersistFolder2 * ppf2;
397 IShellFolder_QueryInterface(This->pSFParent, &IID_IPersistFolder2, (LPVOID*)&ppf2);
401 IPersistFolder2_GetCurFolder(ppf2, &pidl);
406 hMem = RenderHDROP(pidl, This->apidl, This->cidl);
408 if(SetClipboardData(CF_HDROP, hMem))
414 IPersistFolder2_Release(ppf2);
423 /**************************************************************************
424 * ISvItemCm_fnInvokeCommand()
426 static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
428 LPCMINVOKECOMMANDINFO lpcmi)
430 ICOM_THIS(ItemCmImpl, iface);
432 TRACE("(%p)->(invcom=%p verb=%p wnd=%x)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
434 if(LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST) return E_INVALIDARG;
436 switch(LOWORD(lpcmi->lpVerb))
438 case FCIDM_SHVIEW_EXPLORE:
439 DoOpenExplore(iface, lpcmi->hwnd, "explore");
441 case FCIDM_SHVIEW_OPEN:
442 DoOpenExplore(iface, lpcmi->hwnd, "open");
444 case FCIDM_SHVIEW_RENAME:
445 DoRename(iface, lpcmi->hwnd);
447 case FCIDM_SHVIEW_DELETE:
450 case FCIDM_SHVIEW_COPY:
451 DoCopyOrCut(iface, lpcmi->hwnd, FALSE);
453 case FCIDM_SHVIEW_CUT:
454 DoCopyOrCut(iface, lpcmi->hwnd, TRUE);
460 /**************************************************************************
461 * ISvItemCm_fnGetCommandString()
463 static HRESULT WINAPI ISvItemCm_fnGetCommandString(
471 ICOM_THIS(ItemCmImpl, iface);
473 HRESULT hr = E_INVALIDARG;
475 TRACE("(%p)->(idcom=%x flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
486 case FCIDM_SHVIEW_RENAME:
487 strcpy((LPSTR)lpszName, "rename");
493 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
494 case, you need to do the lstrcpyW to the pointer passed.*/
497 { case FCIDM_SHVIEW_RENAME:
498 MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
508 TRACE("-- (%p)->(name=%s)\n",This, lpszName);
512 /**************************************************************************
513 * ISvItemCm_fnHandleMenuMsg()
515 * should be only in IContextMenu2 and IContextMenu3
516 * is nevertheless called from word95
518 static HRESULT WINAPI ISvItemCm_fnHandleMenuMsg(
524 ICOM_THIS(ItemCmImpl, iface);
526 TRACE("(%p)->(msg=%x wp=%x lp=%lx)\n",This, uMsg, wParam, lParam);
531 static struct ICOM_VTABLE(IContextMenu) cmvt =
533 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
534 ISvItemCm_fnQueryInterface,
537 ISvItemCm_fnQueryContextMenu,
538 ISvItemCm_fnInvokeCommand,
539 ISvItemCm_fnGetCommandString,
540 ISvItemCm_fnHandleMenuMsg,
541 (void *) 0xdeadbabe /* just paranoia */