ISvItemCm_fnInvokeCommand: added support for "delete" command.
[wine/multimedia.git] / dlls / shell32 / shv_item_cmenu.c
blobc7ccb27aabd86a03abc45f7e9c95f6bf93c8a1b7
1 /*
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
21 #include <string.h>
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
25 #include "winerror.h"
26 #include "wine/debug.h"
28 #include "pidl.h"
29 #include "shlguid.h"
30 #include "undocshell.h"
31 #include "shlobj.h"
33 #include "shell32_main.h"
34 #include "shellfolder.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shell);
38 /**************************************************************************
39 * IContextMenu Implementation
41 typedef struct
42 { ICOM_VFIELD(IContextMenu);
43 DWORD ref;
44 IShellFolder* pSFParent;
45 LPITEMIDLIST pidl; /* root pidl */
46 LPITEMIDLIST *apidl; /* array of child pidls */
47 UINT cidl;
48 BOOL bAllValues;
49 } ItemCmImpl;
52 static struct ICOM_VTABLE(IContextMenu) cmvt;
54 /**************************************************************************
55 * ISvItemCm_CanRenameItems()
57 static BOOL ISvItemCm_CanRenameItems(ItemCmImpl *This)
58 { UINT i;
59 DWORD dwAttributes;
61 TRACE("(%p)->()\n",This);
63 if(This->apidl)
65 for(i = 0; i < This->cidl; i++){}
66 if(i > 1) return FALSE; /* can't rename more than one item at a time*/
67 dwAttributes = SFGAO_CANRENAME;
68 IShellFolder_GetAttributesOf(This->pSFParent, 1, This->apidl, &dwAttributes);
69 return dwAttributes & SFGAO_CANRENAME;
71 return FALSE;
74 /**************************************************************************
75 * ISvItemCm_Constructor()
77 IContextMenu *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, LPCITEMIDLIST *apidl, UINT cidl)
78 { ItemCmImpl* cm;
79 UINT u;
81 cm = (ItemCmImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ItemCmImpl));
82 cm->lpVtbl = &cmvt;
83 cm->ref = 1;
84 cm->pidl = ILClone(pidl);
85 cm->pSFParent = pSFParent;
87 if(pSFParent) IShellFolder_AddRef(pSFParent);
89 cm->apidl = _ILCopyaPidl(apidl, cidl);
90 cm->cidl = cidl;
92 cm->bAllValues = 1;
93 for(u = 0; u < cidl; u++)
95 cm->bAllValues &= (_ILIsValue(apidl[u]) ? 1 : 0);
98 TRACE("(%p)->()\n",cm);
100 return (IContextMenu*)cm;
103 /**************************************************************************
104 * ISvItemCm_fnQueryInterface
106 static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu *iface, REFIID riid, LPVOID *ppvObj)
108 ICOM_THIS(ItemCmImpl, iface);
110 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
112 *ppvObj = NULL;
114 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
116 *ppvObj = This;
118 else if(IsEqualIID(riid, &IID_IContextMenu)) /*IContextMenu*/
120 *ppvObj = This;
122 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
124 FIXME("-- LPSHELLEXTINIT pointer requested\n");
127 if(*ppvObj)
129 IUnknown_AddRef((IUnknown*)*ppvObj);
130 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
131 return S_OK;
133 TRACE("-- Interface: E_NOINTERFACE\n");
134 return E_NOINTERFACE;
137 /**************************************************************************
138 * ISvItemCm_fnAddRef
140 static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu *iface)
142 ICOM_THIS(ItemCmImpl, iface);
144 TRACE("(%p)->(count=%lu)\n",This, This->ref);
146 return ++(This->ref);
149 /**************************************************************************
150 * ISvItemCm_fnRelease
152 static ULONG WINAPI ISvItemCm_fnRelease(IContextMenu *iface)
154 ICOM_THIS(ItemCmImpl, iface);
156 TRACE("(%p)->()\n",This);
158 if (!--(This->ref))
160 TRACE(" destroying IContextMenu(%p)\n",This);
162 if(This->pSFParent)
163 IShellFolder_Release(This->pSFParent);
165 if(This->pidl)
166 SHFree(This->pidl);
168 /*make sure the pidl is freed*/
169 _ILFreeaPidl(This->apidl, This->cidl);
171 HeapFree(GetProcessHeap(),0,This);
172 return 0;
174 return This->ref;
177 /**************************************************************************
178 * ICM_InsertItem()
180 void WINAPI _InsertMenuItem (
181 HMENU hmenu,
182 UINT indexMenu,
183 BOOL fByPosition,
184 UINT wID,
185 UINT fType,
186 LPSTR dwTypeData,
187 UINT fState)
189 MENUITEMINFOA mii;
191 ZeroMemory(&mii, sizeof(mii));
192 mii.cbSize = sizeof(mii);
193 if (fType == MFT_SEPARATOR)
195 mii.fMask = MIIM_ID | MIIM_TYPE;
197 else
199 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
200 mii.dwTypeData = dwTypeData;
201 mii.fState = fState;
203 mii.wID = wID;
204 mii.fType = fType;
205 InsertMenuItemA( hmenu, indexMenu, fByPosition, &mii);
207 /**************************************************************************
208 * ISvItemCm_fnQueryContextMenu()
211 static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
212 IContextMenu *iface,
213 HMENU hmenu,
214 UINT indexMenu,
215 UINT idCmdFirst,
216 UINT idCmdLast,
217 UINT uFlags)
219 ICOM_THIS(ItemCmImpl, iface);
221 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
223 if(!(CMF_DEFAULTONLY & uFlags))
225 if(uFlags & CMF_EXPLORE)
227 if(This->bAllValues)
229 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
230 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED|MFS_DEFAULT);
232 else
234 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED|MFS_DEFAULT);
235 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
238 else
240 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Select", MFS_ENABLED|MFS_DEFAULT);
242 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
243 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_COPY, MFT_STRING, "&Copy", MFS_ENABLED);
244 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_CUT, MFT_STRING, "&Cut", MFS_ENABLED);
246 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
247 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_DELETE, MFT_STRING, "&Delete", MFS_ENABLED);
249 if(uFlags & CMF_CANRENAME)
250 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_RENAME, MFT_STRING, "&Rename", ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
252 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (FCIDM_SHVIEWLAST));
254 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
257 /**************************************************************************
258 * DoOpenExplore
260 * for folders only
263 static void DoOpenExplore(
264 IContextMenu *iface,
265 HWND hwnd,
266 LPCSTR verb)
268 ICOM_THIS(ItemCmImpl, iface);
270 UINT i, bFolderFound = FALSE;
271 LPITEMIDLIST pidlFQ;
272 SHELLEXECUTEINFOA sei;
274 /* Find the first item in the list that is not a value. These commands
275 should never be invoked if there isn't at least one folder item in the list.*/
277 for(i = 0; i<This->cidl; i++)
279 if(!_ILIsValue(This->apidl[i]))
281 bFolderFound = TRUE;
282 break;
286 if (!bFolderFound) return;
288 pidlFQ = ILCombine(This->pidl, This->apidl[i]);
290 ZeroMemory(&sei, sizeof(sei));
291 sei.cbSize = sizeof(sei);
292 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
293 sei.lpIDList = pidlFQ;
294 sei.lpClass = "folder";
295 sei.hwnd = hwnd;
296 sei.nShow = SW_SHOWNORMAL;
297 sei.lpVerb = verb;
298 ShellExecuteExA(&sei);
299 SHFree(pidlFQ);
302 /**************************************************************************
303 * DoRename
305 static void DoRename(
306 IContextMenu *iface,
307 HWND hwnd)
309 ICOM_THIS(ItemCmImpl, iface);
311 LPSHELLBROWSER lpSB;
312 LPSHELLVIEW lpSV;
314 TRACE("(%p)->(wnd=%p)\n",This, hwnd);
316 /* get the active IShellView */
317 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
319 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
321 TRACE("(sv=%p)\n",lpSV);
322 IShellView_SelectItem(lpSV, This->apidl[0],
323 SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
324 IShellView_Release(lpSV);
329 /**************************************************************************
330 * DoDelete
332 * deletes the currently selected items
334 static void DoDelete(IContextMenu *iface)
336 ICOM_THIS(ItemCmImpl, iface);
337 ISFHelper * psfhlp;
339 IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
340 if (psfhlp)
342 ISFHelper_DeleteItems(psfhlp, This->cidl, This->apidl);
343 ISFHelper_Release(psfhlp);
347 /**************************************************************************
348 * DoCopyOrCut
350 * copies the currently selected items into the clipboard
352 static BOOL DoCopyOrCut(
353 IContextMenu *iface,
354 HWND hwnd,
355 BOOL bCut)
357 ICOM_THIS(ItemCmImpl, iface);
359 LPSHELLBROWSER lpSB;
360 LPSHELLVIEW lpSV;
361 LPDATAOBJECT lpDo;
363 TRACE("(%p)->(wnd=%p,bCut=0x%08x)\n",This, hwnd, bCut);
365 if(GetShellOle())
367 /* get the active IShellView */
368 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
370 if (SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
372 if (SUCCEEDED(IShellView_GetItemObject(lpSV, SVGIO_SELECTION, &IID_IDataObject, (LPVOID*)&lpDo)))
374 pOleSetClipboard(lpDo);
375 IDataObject_Release(lpDo);
377 IShellView_Release(lpSV);
381 return TRUE;
382 #if 0
384 the following code does the copy operation witout ole32.dll
385 we might need this possibility too (js)
387 BOOL bSuccess = FALSE;
389 TRACE("(%p)\n", iface);
391 if(OpenClipboard(NULL))
393 if(EmptyClipboard())
395 IPersistFolder2 * ppf2;
396 IShellFolder_QueryInterface(This->pSFParent, &IID_IPersistFolder2, (LPVOID*)&ppf2);
397 if (ppf2)
399 LPITEMIDLIST pidl;
400 IPersistFolder2_GetCurFolder(ppf2, &pidl);
401 if(pidl)
403 HGLOBAL hMem;
405 hMem = RenderHDROP(pidl, This->apidl, This->cidl);
407 if(SetClipboardData(CF_HDROP, hMem))
409 bSuccess = TRUE;
411 SHFree(pidl);
413 IPersistFolder2_Release(ppf2);
417 CloseClipboard();
419 return bSuccess;
420 #endif
422 /**************************************************************************
423 * ISvItemCm_fnInvokeCommand()
425 static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
426 IContextMenu *iface,
427 LPCMINVOKECOMMANDINFO lpcmi)
429 ICOM_THIS(ItemCmImpl, iface);
431 if (lpcmi->cbSize != sizeof(CMINVOKECOMMANDINFO))
432 FIXME("Is an EX structure\n");
434 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
436 if( HIWORD(lpcmi->lpVerb)==0 && LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST)
438 TRACE("Invalid Verb %x\n",LOWORD(lpcmi->lpVerb));
439 return E_INVALIDARG;
442 if (HIWORD(lpcmi->lpVerb) == 0)
444 switch(LOWORD(lpcmi->lpVerb))
446 case FCIDM_SHVIEW_EXPLORE:
447 TRACE("Verb FCIDM_SHVIEW_EXPLORE\n");
448 DoOpenExplore(iface, lpcmi->hwnd, "explore");
449 break;
450 case FCIDM_SHVIEW_OPEN:
451 TRACE("Verb FCIDM_SHVIEW_OPEN\n");
452 DoOpenExplore(iface, lpcmi->hwnd, "open");
453 break;
454 case FCIDM_SHVIEW_RENAME:
455 TRACE("Verb FCIDM_SHVIEW_RENAME\n");
456 DoRename(iface, lpcmi->hwnd);
457 break;
458 case FCIDM_SHVIEW_DELETE:
459 TRACE("Verb FCIDM_SHVIEW_DELETE\n");
460 DoDelete(iface);
461 break;
462 case FCIDM_SHVIEW_COPY:
463 TRACE("Verb FCIDM_SHVIEW_COPY\n");
464 DoCopyOrCut(iface, lpcmi->hwnd, FALSE);
465 break;
466 case FCIDM_SHVIEW_CUT:
467 TRACE("Verb FCIDM_SHVIEW_CUT\n");
468 DoCopyOrCut(iface, lpcmi->hwnd, TRUE);
469 break;
470 default:
471 FIXME("Unhandled Verb %xl\n",LOWORD(lpcmi->lpVerb));
474 else
476 TRACE("Verb is %s\n",debugstr_a(lpcmi->lpVerb));
477 if (strcmp(lpcmi->lpVerb,"delete")==0)
478 DoDelete(iface);
479 else
480 FIXME("Unhandled string verb %s\n",debugstr_a(lpcmi->lpVerb));
482 return NOERROR;
485 /**************************************************************************
486 * ISvItemCm_fnGetCommandString()
488 static HRESULT WINAPI ISvItemCm_fnGetCommandString(
489 IContextMenu *iface,
490 UINT idCommand,
491 UINT uFlags,
492 UINT* lpReserved,
493 LPSTR lpszName,
494 UINT uMaxNameLen)
496 ICOM_THIS(ItemCmImpl, iface);
498 HRESULT hr = E_INVALIDARG;
500 TRACE("(%p)->(idcom=%x flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
502 switch(uFlags)
504 case GCS_HELPTEXT:
505 hr = E_NOTIMPL;
506 break;
508 case GCS_VERBA:
509 switch(idCommand)
511 case FCIDM_SHVIEW_RENAME:
512 strcpy((LPSTR)lpszName, "rename");
513 hr = NOERROR;
514 break;
516 break;
518 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
519 case, you need to do the lstrcpyW to the pointer passed.*/
520 case GCS_VERBW:
521 switch(idCommand)
522 { case FCIDM_SHVIEW_RENAME:
523 MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
524 hr = NOERROR;
525 break;
527 break;
529 case GCS_VALIDATE:
530 hr = NOERROR;
531 break;
533 TRACE("-- (%p)->(name=%s)\n",This, lpszName);
534 return hr;
537 /**************************************************************************
538 * ISvItemCm_fnHandleMenuMsg()
539 * NOTES
540 * should be only in IContextMenu2 and IContextMenu3
541 * is nevertheless called from word95
543 static HRESULT WINAPI ISvItemCm_fnHandleMenuMsg(
544 IContextMenu *iface,
545 UINT uMsg,
546 WPARAM wParam,
547 LPARAM lParam)
549 ICOM_THIS(ItemCmImpl, iface);
551 TRACE("(%p)->(msg=%x wp=%x lp=%lx)\n",This, uMsg, wParam, lParam);
553 return E_NOTIMPL;
556 static struct ICOM_VTABLE(IContextMenu) cmvt =
558 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
559 ISvItemCm_fnQueryInterface,
560 ISvItemCm_fnAddRef,
561 ISvItemCm_fnRelease,
562 ISvItemCm_fnQueryContextMenu,
563 ISvItemCm_fnInvokeCommand,
564 ISvItemCm_fnGetCommandString,
565 ISvItemCm_fnHandleMenuMsg,
566 (void *) 0xdeadbabe /* just paranoia */