Remove two ancient FAQ entries that did more harm than good.
[wine.git] / dlls / shell32 / shv_item_cmenu.c
blob9aedddd735bc0af5196bea77f57746a2a2c26e36
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 COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
27 #include "winerror.h"
28 #include "wine/debug.h"
30 #include "windef.h"
31 #include "wingdi.h"
32 #include "pidl.h"
33 #include "shlguid.h"
34 #include "undocshell.h"
35 #include "shlobj.h"
37 #include "shell32_main.h"
38 #include "shellfolder.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(shell);
42 /**************************************************************************
43 * IContextMenu Implementation
45 typedef struct
46 { IContextMenu2Vtbl *lpVtbl;
47 DWORD ref;
48 IShellFolder* pSFParent;
49 LPITEMIDLIST pidl; /* root pidl */
50 LPITEMIDLIST *apidl; /* array of child pidls */
51 UINT cidl;
52 BOOL bAllValues;
53 } ItemCmImpl;
56 static struct IContextMenu2Vtbl cmvt;
58 /**************************************************************************
59 * ISvItemCm_CanRenameItems()
61 static BOOL ISvItemCm_CanRenameItems(ItemCmImpl *This)
62 { UINT i;
63 DWORD dwAttributes;
65 TRACE("(%p)->()\n",This);
67 if(This->apidl)
69 for(i = 0; i < This->cidl; i++){}
70 if(i > 1) return FALSE; /* can't rename more than one item at a time*/
71 dwAttributes = SFGAO_CANRENAME;
72 IShellFolder_GetAttributesOf(This->pSFParent, 1, (LPCITEMIDLIST*)This->apidl, &dwAttributes);
73 return dwAttributes & SFGAO_CANRENAME;
75 return FALSE;
78 /**************************************************************************
79 * ISvItemCm_Constructor()
81 IContextMenu2 *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, LPCITEMIDLIST *apidl, UINT cidl)
82 { ItemCmImpl* cm;
83 UINT u;
85 cm = (ItemCmImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ItemCmImpl));
86 cm->lpVtbl = &cmvt;
87 cm->ref = 1;
88 cm->pidl = ILClone(pidl);
89 cm->pSFParent = pSFParent;
91 if(pSFParent) IShellFolder_AddRef(pSFParent);
93 cm->apidl = _ILCopyaPidl(apidl, cidl);
94 cm->cidl = cidl;
96 cm->bAllValues = 1;
97 for(u = 0; u < cidl; u++)
99 cm->bAllValues &= (_ILIsValue(apidl[u]) ? 1 : 0);
102 TRACE("(%p)->()\n",cm);
104 return (IContextMenu2*)cm;
107 /**************************************************************************
108 * ISvItemCm_fnQueryInterface
110 static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu2 *iface, REFIID riid, LPVOID *ppvObj)
112 ItemCmImpl *This = (ItemCmImpl *)iface;
114 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
116 *ppvObj = NULL;
118 if(IsEqualIID(riid, &IID_IUnknown) ||
119 IsEqualIID(riid, &IID_IContextMenu) ||
120 IsEqualIID(riid, &IID_IContextMenu2))
122 *ppvObj = This;
124 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
126 FIXME("-- LPSHELLEXTINIT pointer requested\n");
129 if(*ppvObj)
131 IUnknown_AddRef((IUnknown*)*ppvObj);
132 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
133 return S_OK;
135 TRACE("-- Interface: E_NOINTERFACE\n");
136 return E_NOINTERFACE;
139 /**************************************************************************
140 * ISvItemCm_fnAddRef
142 static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu2 *iface)
144 ItemCmImpl *This = (ItemCmImpl *)iface;
145 ULONG refCount = InterlockedIncrement(&This->ref);
147 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
149 return refCount;
152 /**************************************************************************
153 * ISvItemCm_fnRelease
155 static ULONG WINAPI ISvItemCm_fnRelease(IContextMenu2 *iface)
157 ItemCmImpl *This = (ItemCmImpl *)iface;
158 ULONG refCount = InterlockedDecrement(&This->ref);
160 TRACE("(%p)->(count=%li)\n", This, refCount + 1);
162 if (!refCount)
164 TRACE(" destroying IContextMenu(%p)\n",This);
166 if(This->pSFParent)
167 IShellFolder_Release(This->pSFParent);
169 if(This->pidl)
170 SHFree(This->pidl);
172 /*make sure the pidl is freed*/
173 _ILFreeaPidl(This->apidl, This->cidl);
175 HeapFree(GetProcessHeap(),0,This);
177 return refCount;
180 /**************************************************************************
181 * ICM_InsertItem()
183 void WINAPI _InsertMenuItem (
184 HMENU hmenu,
185 UINT indexMenu,
186 BOOL fByPosition,
187 UINT wID,
188 UINT fType,
189 LPSTR dwTypeData,
190 UINT fState)
192 MENUITEMINFOA mii;
194 ZeroMemory(&mii, sizeof(mii));
195 mii.cbSize = sizeof(mii);
196 if (fType == MFT_SEPARATOR)
198 mii.fMask = MIIM_ID | MIIM_TYPE;
200 else
202 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
203 mii.dwTypeData = dwTypeData;
204 mii.fState = fState;
206 mii.wID = wID;
207 mii.fType = fType;
208 InsertMenuItemA( hmenu, indexMenu, fByPosition, &mii);
211 /**************************************************************************
212 * ISvItemCm_fnQueryContextMenu()
213 * FIXME: load menu MENU_SHV_FILE out of resources instead if creating
214 * each menu item by calling _InsertMenuItem()
216 static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
217 IContextMenu2 *iface,
218 HMENU hmenu,
219 UINT indexMenu,
220 UINT idCmdFirst,
221 UINT idCmdLast,
222 UINT uFlags)
224 ItemCmImpl *This = (ItemCmImpl *)iface;
226 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
228 if (idCmdFirst != 0)
229 FIXME("We should use idCmdFirst=%d and idCmdLast=%d for command ids\n", idCmdFirst, idCmdLast);
231 if(!(CMF_DEFAULTONLY & uFlags) && This->cidl>0)
233 if(!(uFlags & CMF_EXPLORE))
234 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Select", MFS_ENABLED);
236 if(This->bAllValues)
238 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
239 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED);
241 else
243 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED);
244 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
247 SetMenuDefaultItem(hmenu, 0, MF_BYPOSITION);
249 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
250 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_COPY, MFT_STRING, "&Copy", MFS_ENABLED);
251 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_CUT, MFT_STRING, "&Cut", MFS_ENABLED);
253 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
254 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_DELETE, MFT_STRING, "&Delete", MFS_ENABLED);
256 if(uFlags & CMF_CANRENAME)
257 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_RENAME, MFT_STRING, "&Rename", ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
259 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (FCIDM_SHVIEWLAST));
261 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
264 /**************************************************************************
265 * DoOpenExplore
267 * for folders only
270 static void DoOpenExplore(
271 IContextMenu2 *iface,
272 HWND hwnd,
273 LPCSTR verb)
275 ItemCmImpl *This = (ItemCmImpl *)iface;
277 UINT i, bFolderFound = FALSE;
278 LPITEMIDLIST pidlFQ;
279 SHELLEXECUTEINFOA sei;
281 /* Find the first item in the list that is not a value. These commands
282 should never be invoked if there isn't at least one folder item in the list.*/
284 for(i = 0; i<This->cidl; i++)
286 if(!_ILIsValue(This->apidl[i]))
288 bFolderFound = TRUE;
289 break;
293 if (!bFolderFound) return;
295 pidlFQ = ILCombine(This->pidl, This->apidl[i]);
297 ZeroMemory(&sei, sizeof(sei));
298 sei.cbSize = sizeof(sei);
299 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
300 sei.lpIDList = pidlFQ;
301 sei.lpClass = "Folder";
302 sei.hwnd = hwnd;
303 sei.nShow = SW_SHOWNORMAL;
304 sei.lpVerb = verb;
305 ShellExecuteExA(&sei);
306 SHFree(pidlFQ);
309 /**************************************************************************
310 * DoRename
312 static void DoRename(
313 IContextMenu2 *iface,
314 HWND hwnd)
316 ItemCmImpl *This = (ItemCmImpl *)iface;
318 LPSHELLBROWSER lpSB;
319 LPSHELLVIEW lpSV;
321 TRACE("(%p)->(wnd=%p)\n",This, hwnd);
323 /* get the active IShellView */
324 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
326 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
328 TRACE("(sv=%p)\n",lpSV);
329 IShellView_SelectItem(lpSV, This->apidl[0],
330 SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
331 IShellView_Release(lpSV);
336 /**************************************************************************
337 * DoDelete
339 * deletes the currently selected items
341 static void DoDelete(IContextMenu2 *iface)
343 ItemCmImpl *This = (ItemCmImpl *)iface;
344 ISFHelper * psfhlp;
346 IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
347 if (psfhlp)
349 ISFHelper_DeleteItems(psfhlp, This->cidl, (LPCITEMIDLIST *)This->apidl);
350 ISFHelper_Release(psfhlp);
354 /**************************************************************************
355 * DoCopyOrCut
357 * copies the currently selected items into the clipboard
359 static BOOL DoCopyOrCut(
360 IContextMenu2 *iface,
361 HWND hwnd,
362 BOOL bCut)
364 ItemCmImpl *This = (ItemCmImpl *)iface;
366 LPSHELLBROWSER lpSB;
367 LPSHELLVIEW lpSV;
368 LPDATAOBJECT lpDo;
370 TRACE("(%p)->(wnd=%p,bCut=0x%08x)\n",This, hwnd, bCut);
372 /* get the active IShellView */
373 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
375 if (SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
377 if (SUCCEEDED(IShellView_GetItemObject(lpSV, SVGIO_SELECTION, &IID_IDataObject, (LPVOID*)&lpDo)))
379 OleSetClipboard(lpDo);
380 IDataObject_Release(lpDo);
382 IShellView_Release(lpSV);
385 return TRUE;
387 /**************************************************************************
388 * ISvItemCm_fnInvokeCommand()
390 static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
391 IContextMenu2 *iface,
392 LPCMINVOKECOMMANDINFO lpcmi)
394 ItemCmImpl *This = (ItemCmImpl *)iface;
396 if (lpcmi->cbSize != sizeof(CMINVOKECOMMANDINFO))
397 FIXME("Is an EX structure\n");
399 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
401 if( HIWORD(lpcmi->lpVerb)==0 && LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST)
403 TRACE("Invalid Verb %x\n",LOWORD(lpcmi->lpVerb));
404 return E_INVALIDARG;
407 if (HIWORD(lpcmi->lpVerb) == 0)
409 switch(LOWORD(lpcmi->lpVerb))
411 case FCIDM_SHVIEW_EXPLORE:
412 TRACE("Verb FCIDM_SHVIEW_EXPLORE\n");
413 DoOpenExplore(iface, lpcmi->hwnd, "explore");
414 break;
415 case FCIDM_SHVIEW_OPEN:
416 TRACE("Verb FCIDM_SHVIEW_OPEN\n");
417 DoOpenExplore(iface, lpcmi->hwnd, "open");
418 break;
419 case FCIDM_SHVIEW_RENAME:
420 TRACE("Verb FCIDM_SHVIEW_RENAME\n");
421 DoRename(iface, lpcmi->hwnd);
422 break;
423 case FCIDM_SHVIEW_DELETE:
424 TRACE("Verb FCIDM_SHVIEW_DELETE\n");
425 DoDelete(iface);
426 break;
427 case FCIDM_SHVIEW_COPY:
428 TRACE("Verb FCIDM_SHVIEW_COPY\n");
429 DoCopyOrCut(iface, lpcmi->hwnd, FALSE);
430 break;
431 case FCIDM_SHVIEW_CUT:
432 TRACE("Verb FCIDM_SHVIEW_CUT\n");
433 DoCopyOrCut(iface, lpcmi->hwnd, TRUE);
434 break;
435 default:
436 FIXME("Unhandled Verb %xl\n",LOWORD(lpcmi->lpVerb));
439 else
441 TRACE("Verb is %s\n",debugstr_a(lpcmi->lpVerb));
442 if (strcmp(lpcmi->lpVerb,"delete")==0)
443 DoDelete(iface);
444 else
445 FIXME("Unhandled string verb %s\n",debugstr_a(lpcmi->lpVerb));
447 return NOERROR;
450 /**************************************************************************
451 * ISvItemCm_fnGetCommandString()
453 static HRESULT WINAPI ISvItemCm_fnGetCommandString(
454 IContextMenu2 *iface,
455 UINT idCommand,
456 UINT uFlags,
457 UINT* lpReserved,
458 LPSTR lpszName,
459 UINT uMaxNameLen)
461 ItemCmImpl *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);
467 switch(uFlags)
469 case GCS_HELPTEXTA:
470 case GCS_HELPTEXTW:
471 hr = E_NOTIMPL;
472 break;
474 case GCS_VERBA:
475 switch(idCommand)
477 case FCIDM_SHVIEW_RENAME:
478 strcpy((LPSTR)lpszName, "rename");
479 hr = NOERROR;
480 break;
482 break;
484 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
485 case, you need to do the lstrcpyW to the pointer passed.*/
486 case GCS_VERBW:
487 switch(idCommand)
488 { case FCIDM_SHVIEW_RENAME:
489 MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
490 hr = NOERROR;
491 break;
493 break;
495 case GCS_VALIDATEA:
496 case GCS_VALIDATEW:
497 hr = NOERROR;
498 break;
500 TRACE("-- (%p)->(name=%s)\n",This, lpszName);
501 return hr;
504 /**************************************************************************
505 * ISvItemCm_fnHandleMenuMsg()
506 * NOTES
507 * should be only in IContextMenu2 and IContextMenu3
508 * is nevertheless called from word95
510 static HRESULT WINAPI ISvItemCm_fnHandleMenuMsg(
511 IContextMenu2 *iface,
512 UINT uMsg,
513 WPARAM wParam,
514 LPARAM lParam)
516 ItemCmImpl *This = (ItemCmImpl *)iface;
518 TRACE("(%p)->(msg=%x wp=%x lp=%lx)\n",This, uMsg, wParam, lParam);
520 return E_NOTIMPL;
523 static struct IContextMenu2Vtbl cmvt =
525 ISvItemCm_fnQueryInterface,
526 ISvItemCm_fnAddRef,
527 ISvItemCm_fnRelease,
528 ISvItemCm_fnQueryContextMenu,
529 ISvItemCm_fnInvokeCommand,
530 ISvItemCm_fnGetCommandString,
531 ISvItemCm_fnHandleMenuMsg