msi: Avoid accessing a freed object.
[wine.git] / dlls / shell32 / shv_item_cmenu.c
blob8b9ff0fbed79a7fbc16a59f865333964d0ff065c
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "undocshell.h"
34 #include "shlobj.h"
36 #include "shell32_main.h"
37 #include "shellfolder.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(shell);
41 /**************************************************************************
42 * IContextMenu Implementation
44 typedef struct
45 { const IContextMenu2Vtbl *lpVtbl;
46 LONG ref;
47 IShellFolder* pSFParent;
48 LPITEMIDLIST pidl; /* root pidl */
49 LPITEMIDLIST *apidl; /* array of child pidls */
50 UINT cidl;
51 BOOL bAllValues;
52 } ItemCmImpl;
55 static const IContextMenu2Vtbl cmvt;
57 /**************************************************************************
58 * ISvItemCm_CanRenameItems()
60 static BOOL ISvItemCm_CanRenameItems(ItemCmImpl *This)
61 { UINT i;
62 DWORD dwAttributes;
64 TRACE("(%p)->()\n",This);
66 if(This->apidl)
68 for(i = 0; i < This->cidl; i++){}
69 if(i > 1) return FALSE; /* can't rename more than one item at a time*/
70 dwAttributes = SFGAO_CANRENAME;
71 IShellFolder_GetAttributesOf(This->pSFParent, 1, (LPCITEMIDLIST*)This->apidl, &dwAttributes);
72 return dwAttributes & SFGAO_CANRENAME;
74 return FALSE;
77 /**************************************************************************
78 * ISvItemCm_Constructor()
80 IContextMenu2 *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, LPCITEMIDLIST *apidl, UINT cidl)
81 { ItemCmImpl* cm;
82 UINT u;
84 cm = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ItemCmImpl));
85 cm->lpVtbl = &cmvt;
86 cm->ref = 1;
87 cm->pidl = ILClone(pidl);
88 cm->pSFParent = pSFParent;
90 if(pSFParent) IShellFolder_AddRef(pSFParent);
92 cm->apidl = _ILCopyaPidl(apidl, cidl);
93 cm->cidl = cidl;
95 cm->bAllValues = 1;
96 for(u = 0; u < cidl; u++)
98 cm->bAllValues &= (_ILIsValue(apidl[u]) ? 1 : 0);
101 TRACE("(%p)->()\n",cm);
103 return (IContextMenu2*)cm;
106 /**************************************************************************
107 * ISvItemCm_fnQueryInterface
109 static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu2 *iface, REFIID riid, LPVOID *ppvObj)
111 ItemCmImpl *This = (ItemCmImpl *)iface;
113 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
115 *ppvObj = NULL;
117 if(IsEqualIID(riid, &IID_IUnknown) ||
118 IsEqualIID(riid, &IID_IContextMenu) ||
119 IsEqualIID(riid, &IID_IContextMenu2))
121 *ppvObj = This;
123 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
125 FIXME("-- LPSHELLEXTINIT pointer requested\n");
128 if(*ppvObj)
130 IUnknown_AddRef((IUnknown*)*ppvObj);
131 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
132 return S_OK;
134 TRACE("-- Interface: E_NOINTERFACE\n");
135 return E_NOINTERFACE;
138 /**************************************************************************
139 * ISvItemCm_fnAddRef
141 static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu2 *iface)
143 ItemCmImpl *This = (ItemCmImpl *)iface;
144 ULONG refCount = InterlockedIncrement(&This->ref);
146 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
148 return refCount;
151 /**************************************************************************
152 * ISvItemCm_fnRelease
154 static ULONG WINAPI ISvItemCm_fnRelease(IContextMenu2 *iface)
156 ItemCmImpl *This = (ItemCmImpl *)iface;
157 ULONG refCount = InterlockedDecrement(&This->ref);
159 TRACE("(%p)->(count=%i)\n", This, refCount + 1);
161 if (!refCount)
163 TRACE(" destroying IContextMenu(%p)\n",This);
165 if(This->pSFParent)
166 IShellFolder_Release(This->pSFParent);
168 SHFree(This->pidl);
170 /*make sure the pidl is freed*/
171 _ILFreeaPidl(This->apidl, This->cidl);
173 HeapFree(GetProcessHeap(),0,This);
175 return refCount;
178 /**************************************************************************
179 * ICM_InsertItem()
181 void WINAPI _InsertMenuItem (
182 HMENU hmenu,
183 UINT indexMenu,
184 BOOL fByPosition,
185 UINT wID,
186 UINT fType,
187 LPCSTR dwTypeData,
188 UINT fState)
190 MENUITEMINFOA mii;
192 ZeroMemory(&mii, sizeof(mii));
193 mii.cbSize = sizeof(mii);
194 if (fType == MFT_SEPARATOR)
196 mii.fMask = MIIM_ID | MIIM_TYPE;
198 else
200 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
201 mii.dwTypeData = (LPSTR) dwTypeData;
202 mii.fState = fState;
204 mii.wID = wID;
205 mii.fType = fType;
206 InsertMenuItemA( hmenu, indexMenu, fByPosition, &mii);
209 /**************************************************************************
210 * ISvItemCm_fnQueryContextMenu()
211 * FIXME: load menu MENU_SHV_FILE out of resources instead if creating
212 * each menu item by calling _InsertMenuItem()
214 static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
215 IContextMenu2 *iface,
216 HMENU hmenu,
217 UINT indexMenu,
218 UINT idCmdFirst,
219 UINT idCmdLast,
220 UINT uFlags)
222 ItemCmImpl *This = (ItemCmImpl *)iface;
224 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
226 if (idCmdFirst != 0)
227 FIXME("We should use idCmdFirst=%d and idCmdLast=%d for command ids\n", idCmdFirst, idCmdLast);
229 if(!(CMF_DEFAULTONLY & uFlags) && This->cidl>0)
231 if(!(uFlags & CMF_EXPLORE))
232 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Select", MFS_ENABLED);
234 if(This->bAllValues)
236 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
237 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED);
239 else
241 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED);
242 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
245 SetMenuDefaultItem(hmenu, 0, MF_BYPOSITION);
247 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
248 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_COPY, MFT_STRING, "&Copy", MFS_ENABLED);
249 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_CUT, MFT_STRING, "&Cut", MFS_ENABLED);
251 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
252 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_DELETE, MFT_STRING, "&Delete", MFS_ENABLED);
254 if(uFlags & CMF_CANRENAME)
255 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_RENAME, MFT_STRING, "&Rename", ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
257 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (FCIDM_SHVIEWLAST));
259 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
262 /**************************************************************************
263 * DoOpenExplore
265 * for folders only
268 static void DoOpenExplore(
269 IContextMenu2 *iface,
270 HWND hwnd,
271 LPCSTR verb)
273 ItemCmImpl *This = (ItemCmImpl *)iface;
275 UINT i, bFolderFound = FALSE;
276 LPITEMIDLIST pidlFQ;
277 SHELLEXECUTEINFOA sei;
279 /* Find the first item in the list that is not a value. These commands
280 should never be invoked if there isn't at least one folder item in the list.*/
282 for(i = 0; i<This->cidl; i++)
284 if(!_ILIsValue(This->apidl[i]))
286 bFolderFound = TRUE;
287 break;
291 if (!bFolderFound) return;
293 pidlFQ = ILCombine(This->pidl, This->apidl[i]);
295 ZeroMemory(&sei, sizeof(sei));
296 sei.cbSize = sizeof(sei);
297 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
298 sei.lpIDList = pidlFQ;
299 sei.lpClass = "Folder";
300 sei.hwnd = hwnd;
301 sei.nShow = SW_SHOWNORMAL;
302 sei.lpVerb = verb;
303 ShellExecuteExA(&sei);
304 SHFree(pidlFQ);
307 /**************************************************************************
308 * DoRename
310 static void DoRename(
311 IContextMenu2 *iface,
312 HWND hwnd)
314 ItemCmImpl *This = (ItemCmImpl *)iface;
316 LPSHELLBROWSER lpSB;
317 LPSHELLVIEW lpSV;
319 TRACE("(%p)->(wnd=%p)\n",This, hwnd);
321 /* get the active IShellView */
322 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
324 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
326 TRACE("(sv=%p)\n",lpSV);
327 IShellView_SelectItem(lpSV, This->apidl[0],
328 SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
329 IShellView_Release(lpSV);
334 /**************************************************************************
335 * DoDelete
337 * deletes the currently selected items
339 static void DoDelete(IContextMenu2 *iface)
341 ItemCmImpl *This = (ItemCmImpl *)iface;
342 ISFHelper * psfhlp;
344 IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
345 if (psfhlp)
347 ISFHelper_DeleteItems(psfhlp, This->cidl, (LPCITEMIDLIST *)This->apidl);
348 ISFHelper_Release(psfhlp);
352 /**************************************************************************
353 * DoCopyOrCut
355 * copies the currently selected items into the clipboard
357 static BOOL DoCopyOrCut(
358 IContextMenu2 *iface,
359 HWND hwnd,
360 BOOL bCut)
362 ItemCmImpl *This = (ItemCmImpl *)iface;
364 LPSHELLBROWSER lpSB;
365 LPSHELLVIEW lpSV;
366 LPDATAOBJECT lpDo;
368 TRACE("(%p)->(wnd=%p,bCut=0x%08x)\n",This, hwnd, bCut);
370 /* get the active IShellView */
371 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
373 if (SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
375 if (SUCCEEDED(IShellView_GetItemObject(lpSV, SVGIO_SELECTION, &IID_IDataObject, (LPVOID*)&lpDo)))
377 OleSetClipboard(lpDo);
378 IDataObject_Release(lpDo);
380 IShellView_Release(lpSV);
383 return TRUE;
385 /**************************************************************************
386 * ISvItemCm_fnInvokeCommand()
388 static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
389 IContextMenu2 *iface,
390 LPCMINVOKECOMMANDINFO lpcmi)
392 ItemCmImpl *This = (ItemCmImpl *)iface;
394 if (lpcmi->cbSize != sizeof(CMINVOKECOMMANDINFO))
395 FIXME("Is an EX structure\n");
397 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
399 if( HIWORD(lpcmi->lpVerb)==0 && LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST)
401 TRACE("Invalid Verb %x\n",LOWORD(lpcmi->lpVerb));
402 return E_INVALIDARG;
405 if (HIWORD(lpcmi->lpVerb) == 0)
407 switch(LOWORD(lpcmi->lpVerb))
409 case FCIDM_SHVIEW_EXPLORE:
410 TRACE("Verb FCIDM_SHVIEW_EXPLORE\n");
411 DoOpenExplore(iface, lpcmi->hwnd, "explore");
412 break;
413 case FCIDM_SHVIEW_OPEN:
414 TRACE("Verb FCIDM_SHVIEW_OPEN\n");
415 DoOpenExplore(iface, lpcmi->hwnd, "open");
416 break;
417 case FCIDM_SHVIEW_RENAME:
418 TRACE("Verb FCIDM_SHVIEW_RENAME\n");
419 DoRename(iface, lpcmi->hwnd);
420 break;
421 case FCIDM_SHVIEW_DELETE:
422 TRACE("Verb FCIDM_SHVIEW_DELETE\n");
423 DoDelete(iface);
424 break;
425 case FCIDM_SHVIEW_COPY:
426 TRACE("Verb FCIDM_SHVIEW_COPY\n");
427 DoCopyOrCut(iface, lpcmi->hwnd, FALSE);
428 break;
429 case FCIDM_SHVIEW_CUT:
430 TRACE("Verb FCIDM_SHVIEW_CUT\n");
431 DoCopyOrCut(iface, lpcmi->hwnd, TRUE);
432 break;
433 default:
434 FIXME("Unhandled Verb %xl\n",LOWORD(lpcmi->lpVerb));
437 else
439 TRACE("Verb is %s\n",debugstr_a(lpcmi->lpVerb));
440 if (strcmp(lpcmi->lpVerb,"delete")==0)
441 DoDelete(iface);
442 else
443 FIXME("Unhandled string verb %s\n",debugstr_a(lpcmi->lpVerb));
445 return NOERROR;
448 /**************************************************************************
449 * ISvItemCm_fnGetCommandString()
451 static HRESULT WINAPI ISvItemCm_fnGetCommandString(
452 IContextMenu2 *iface,
453 UINT_PTR idCommand,
454 UINT uFlags,
455 UINT* lpReserved,
456 LPSTR lpszName,
457 UINT uMaxNameLen)
459 ItemCmImpl *This = (ItemCmImpl *)iface;
461 HRESULT hr = E_INVALIDARG;
463 TRACE("(%p)->(idcom=%lx flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
465 switch(uFlags)
467 case GCS_HELPTEXTA:
468 case GCS_HELPTEXTW:
469 hr = E_NOTIMPL;
470 break;
472 case GCS_VERBA:
473 switch(idCommand)
475 case FCIDM_SHVIEW_RENAME:
476 strcpy((LPSTR)lpszName, "rename");
477 hr = NOERROR;
478 break;
480 break;
482 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
483 case, you need to do the lstrcpyW to the pointer passed.*/
484 case GCS_VERBW:
485 switch(idCommand)
486 { case FCIDM_SHVIEW_RENAME:
487 MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
488 hr = NOERROR;
489 break;
491 break;
493 case GCS_VALIDATEA:
494 case GCS_VALIDATEW:
495 hr = NOERROR;
496 break;
498 TRACE("-- (%p)->(name=%s)\n",This, lpszName);
499 return hr;
502 /**************************************************************************
503 * ISvItemCm_fnHandleMenuMsg()
504 * NOTES
505 * should be only in IContextMenu2 and IContextMenu3
506 * is nevertheless called from word95
508 static HRESULT WINAPI ISvItemCm_fnHandleMenuMsg(
509 IContextMenu2 *iface,
510 UINT uMsg,
511 WPARAM wParam,
512 LPARAM lParam)
514 ItemCmImpl *This = (ItemCmImpl *)iface;
516 TRACE("(%p)->(msg=%x wp=%lx lp=%lx)\n",This, uMsg, wParam, lParam);
518 return E_NOTIMPL;
521 static const IContextMenu2Vtbl cmvt =
523 ISvItemCm_fnQueryInterface,
524 ISvItemCm_fnAddRef,
525 ISvItemCm_fnRelease,
526 ISvItemCm_fnQueryContextMenu,
527 ISvItemCm_fnInvokeCommand,
528 ISvItemCm_fnGetCommandString,
529 ISvItemCm_fnHandleMenuMsg