shell32: Use S_OK as successful return code name.
[wine/multimedia.git] / dlls / shell32 / shv_item_cmenu.c
blob4c05460db6a794e19b2ae5dcef918fef4616e611
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"
35 #include "winreg.h"
36 #include "prsht.h"
38 #include "shell32_main.h"
39 #include "shellfolder.h"
41 #include "shresdef.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(shell);
46 typedef struct
48 IContextMenu2 IContextMenu2_iface;
49 LONG ref;
50 IShellFolder* parent;
51 UINT verb_offset;
52 } ContextMenu;
54 /**************************************************************************
55 * IContextMenu Implementation
57 typedef struct
59 ContextMenu menu;
61 LPITEMIDLIST pidl; /* root pidl */
62 LPITEMIDLIST *apidl; /* array of child pidls */
63 UINT cidl;
64 BOOL bAllValues;
65 } ItemCmImpl;
67 static inline ItemCmImpl *impl_from_IContextMenu2(IContextMenu2 *iface)
69 return CONTAINING_RECORD(iface, ItemCmImpl, menu.IContextMenu2_iface);
72 /**************************************************************************
73 * ISvItemCm_CanRenameItems()
75 static BOOL ISvItemCm_CanRenameItems(ItemCmImpl *This)
77 DWORD attr;
79 TRACE("(%p)\n", This);
81 /* can't rename more than one item at a time*/
82 if (!This->apidl || This->cidl > 1) return FALSE;
84 attr = SFGAO_CANRENAME;
85 IShellFolder_GetAttributesOf(This->menu.parent, 1, (LPCITEMIDLIST*)This->apidl, &attr);
86 return attr & SFGAO_CANRENAME;
89 /**************************************************************************
90 * ISvItemCm_fnQueryInterface
92 static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu2 *iface, REFIID riid, LPVOID *ppvObj)
94 ItemCmImpl *This = impl_from_IContextMenu2(iface);
96 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
98 *ppvObj = NULL;
100 if(IsEqualIID(riid, &IID_IUnknown) ||
101 IsEqualIID(riid, &IID_IContextMenu) ||
102 IsEqualIID(riid, &IID_IContextMenu2))
104 *ppvObj = This;
106 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
108 FIXME("-- LPSHELLEXTINIT pointer requested\n");
111 if(*ppvObj)
113 IUnknown_AddRef((IUnknown*)*ppvObj);
114 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
115 return S_OK;
117 TRACE("-- Interface: E_NOINTERFACE\n");
118 return E_NOINTERFACE;
121 /**************************************************************************
122 * ISvItemCm_fnAddRef
124 static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu2 *iface)
126 ItemCmImpl *This = impl_from_IContextMenu2(iface);
127 ULONG refCount = InterlockedIncrement(&This->menu.ref);
129 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
131 return refCount;
134 /**************************************************************************
135 * ISvItemCm_fnRelease
137 static ULONG WINAPI ISvItemCm_fnRelease(IContextMenu2 *iface)
139 ItemCmImpl *This = impl_from_IContextMenu2(iface);
140 ULONG refCount = InterlockedDecrement(&This->menu.ref);
142 TRACE("(%p)->(count=%i)\n", This, refCount + 1);
144 if (!refCount)
146 TRACE(" destroying IContextMenu(%p)\n",This);
148 if(This->menu.parent)
149 IShellFolder_Release(This->menu.parent);
151 SHFree(This->pidl);
153 /*make sure the pidl is freed*/
154 _ILFreeaPidl(This->apidl, This->cidl);
156 HeapFree(GetProcessHeap(),0,This);
158 return refCount;
161 static void _InsertMenuItemW (
162 HMENU hmenu,
163 UINT indexMenu,
164 BOOL fByPosition,
165 UINT wID,
166 UINT fType,
167 LPWSTR dwTypeData,
168 UINT fState)
170 MENUITEMINFOW mii;
172 mii.cbSize = sizeof(mii);
173 if (fType == MFT_SEPARATOR)
175 mii.fMask = MIIM_ID | MIIM_TYPE;
177 else
179 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
180 mii.dwTypeData = dwTypeData;
181 mii.fState = fState;
183 mii.wID = wID;
184 mii.fType = fType;
185 InsertMenuItemW( hmenu, indexMenu, fByPosition, &mii);
188 /**************************************************************************
189 * ISvItemCm_fnQueryContextMenu()
191 static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
192 IContextMenu2 *iface,
193 HMENU hmenu,
194 UINT indexMenu,
195 UINT idCmdFirst,
196 UINT idCmdLast,
197 UINT uFlags)
199 ItemCmImpl *This = impl_from_IContextMenu2(iface);
200 INT uIDMax;
202 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
204 This->menu.verb_offset=idCmdFirst;
206 if(!(CMF_DEFAULTONLY & uFlags) && This->cidl>0)
208 HMENU hmenures = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_SHV_FILE));
210 if(uFlags & CMF_EXPLORE)
211 RemoveMenu(hmenures, FCIDM_SHVIEW_OPEN, MF_BYCOMMAND);
213 uIDMax = Shell_MergeMenus(hmenu, GetSubMenu(hmenures, 0), indexMenu, idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);
215 DestroyMenu(hmenures);
217 if(This->bAllValues)
219 MENUITEMINFOW mi;
220 WCHAR str[255];
221 mi.cbSize = sizeof(mi);
222 mi.fMask = MIIM_ID | MIIM_STRING | MIIM_FTYPE;
223 mi.dwTypeData = str;
224 mi.cch = 255;
225 GetMenuItemInfoW(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND, &mi);
226 RemoveMenu(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND);
227 _InsertMenuItemW(hmenu, (uFlags & CMF_EXPLORE) ? 1 : 2, MF_BYPOSITION, FCIDM_SHVIEW_EXPLORE, MFT_STRING, str, MFS_ENABLED);
230 SetMenuDefaultItem(hmenu, 0, MF_BYPOSITION);
232 if(uFlags & ~CMF_CANRENAME)
233 RemoveMenu(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND);
234 else
235 EnableMenuItem(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND | ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
237 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, uIDMax-idCmdFirst);
239 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
242 /**************************************************************************
243 * DoOpenExplore
245 * for folders only
248 static void DoOpenExplore(ItemCmImpl *This, HWND hwnd, LPCSTR verb)
250 UINT i, bFolderFound = FALSE;
251 LPITEMIDLIST pidlFQ;
252 SHELLEXECUTEINFOA sei;
254 /* Find the first item in the list that is not a value. These commands
255 should never be invoked if there isn't at least one folder item in the list.*/
257 for(i = 0; i<This->cidl; i++)
259 if(!_ILIsValue(This->apidl[i]))
261 bFolderFound = TRUE;
262 break;
266 if (!bFolderFound) return;
268 pidlFQ = ILCombine(This->pidl, This->apidl[i]);
270 ZeroMemory(&sei, sizeof(sei));
271 sei.cbSize = sizeof(sei);
272 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
273 sei.lpIDList = pidlFQ;
274 sei.lpClass = "Folder";
275 sei.hwnd = hwnd;
276 sei.nShow = SW_SHOWNORMAL;
277 sei.lpVerb = verb;
278 ShellExecuteExA(&sei);
279 SHFree(pidlFQ);
282 /**************************************************************************
283 * DoRename
285 static void DoRename(ItemCmImpl *This, HWND hwnd)
287 LPSHELLBROWSER lpSB;
288 LPSHELLVIEW lpSV;
290 TRACE("(%p)->(wnd=%p)\n",This, hwnd);
292 /* get the active IShellView */
293 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
295 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
297 TRACE("(sv=%p)\n",lpSV);
298 IShellView_SelectItem(lpSV, This->apidl[0],
299 SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
300 IShellView_Release(lpSV);
305 /**************************************************************************
306 * DoDelete
308 * deletes the currently selected items
310 static void DoDelete(ItemCmImpl *This)
312 ISFHelper * psfhlp;
314 IShellFolder_QueryInterface(This->menu.parent, &IID_ISFHelper, (LPVOID*)&psfhlp);
315 if (psfhlp)
317 ISFHelper_DeleteItems(psfhlp, This->cidl, (LPCITEMIDLIST *)This->apidl);
318 ISFHelper_Release(psfhlp);
322 /**************************************************************************
323 * DoCopyOrCut
325 * copies the currently selected items into the clipboard
327 static BOOL DoCopyOrCut(ItemCmImpl *This, HWND hwnd, BOOL bCut)
329 LPSHELLBROWSER lpSB;
330 LPSHELLVIEW lpSV;
331 LPDATAOBJECT lpDo;
333 TRACE("(%p)->(wnd=%p,bCut=0x%08x)\n",This, hwnd, bCut);
335 /* get the active IShellView */
336 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
338 if (SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
340 if (SUCCEEDED(IShellView_GetItemObject(lpSV, SVGIO_SELECTION, &IID_IDataObject, (LPVOID*)&lpDo)))
342 OleSetClipboard(lpDo);
343 IDataObject_Release(lpDo);
345 IShellView_Release(lpSV);
348 return TRUE;
351 /**************************************************************************
352 * Properties_AddPropSheetCallback
354 * Used by DoOpenProperties through SHCreatePropSheetExtArrayEx to add
355 * propertysheet pages from shell extensions.
357 static BOOL CALLBACK Properties_AddPropSheetCallback(HPROPSHEETPAGE hpage, LPARAM lparam)
359 LPPROPSHEETHEADERW psh = (LPPROPSHEETHEADERW) lparam;
360 psh->u3.phpage[psh->nPages++] = hpage;
362 return TRUE;
365 /**************************************************************************
366 * DoOpenProperties
368 static void DoOpenProperties(ItemCmImpl *This, HWND hwnd)
370 static const UINT MAX_PROP_PAGES = 99;
371 static const WCHAR wszFolder[] = {'F','o','l','d','e','r', 0};
372 static const WCHAR wszFiletypeAll[] = {'*',0};
373 LPSHELLFOLDER lpDesktopSF;
374 LPSHELLFOLDER lpSF;
375 LPDATAOBJECT lpDo;
376 WCHAR wszFiletype[MAX_PATH];
377 WCHAR wszFilename[MAX_PATH];
378 PROPSHEETHEADERW psh;
379 HPROPSHEETPAGE hpages[MAX_PROP_PAGES];
380 HPSXA hpsxa;
381 UINT ret;
383 TRACE("(%p)->(wnd=%p)\n", This, hwnd);
385 ZeroMemory(&psh, sizeof(PROPSHEETHEADERW));
386 psh.dwSize = sizeof (PROPSHEETHEADERW);
387 psh.hwndParent = hwnd;
388 psh.dwFlags = PSH_PROPTITLE;
389 psh.nPages = 0;
390 psh.u3.phpage = hpages;
391 psh.u2.nStartPage = 0;
393 _ILSimpleGetTextW(This->apidl[0], (LPVOID)wszFilename, MAX_PATH);
394 psh.pszCaption = (LPCWSTR)wszFilename;
396 /* Find out where to look for the shell extensions */
397 if (_ILIsValue(This->apidl[0]))
399 char sTemp[64];
400 sTemp[0] = 0;
401 if (_ILGetExtension(This->apidl[0], sTemp, 64))
403 HCR_MapTypeToValueA(sTemp, sTemp, 64, TRUE);
404 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, wszFiletype, MAX_PATH);
406 else
408 wszFiletype[0] = 0;
411 else if (_ILIsFolder(This->apidl[0]))
413 lstrcpynW(wszFiletype, wszFolder, 64);
415 else if (_ILIsSpecialFolder(This->apidl[0]))
417 LPGUID folderGUID;
418 static const WCHAR wszclsid[] = {'C','L','S','I','D','\\', 0};
419 folderGUID = _ILGetGUIDPointer(This->apidl[0]);
420 lstrcpyW(wszFiletype, wszclsid);
421 StringFromGUID2(folderGUID, &wszFiletype[6], MAX_PATH - 6);
423 else
425 FIXME("Requested properties for unknown type.\n");
426 return;
429 /* Get a suitable DataObject for accessing the files */
430 SHGetDesktopFolder(&lpDesktopSF);
431 if (_ILIsPidlSimple(This->pidl))
433 ret = IShellFolder_GetUIObjectOf(lpDesktopSF, hwnd, This->cidl, (LPCITEMIDLIST*)This->apidl,
434 &IID_IDataObject, NULL, (LPVOID *)&lpDo);
435 IShellFolder_Release(lpDesktopSF);
437 else
439 IShellFolder_BindToObject(lpDesktopSF, This->pidl, NULL, &IID_IShellFolder, (LPVOID*) &lpSF);
440 ret = IShellFolder_GetUIObjectOf(lpSF, hwnd, This->cidl, (LPCITEMIDLIST*)This->apidl,
441 &IID_IDataObject, NULL, (LPVOID *)&lpDo);
442 IShellFolder_Release(lpSF);
443 IShellFolder_Release(lpDesktopSF);
446 if (SUCCEEDED(ret))
448 hpsxa = SHCreatePropSheetExtArrayEx(HKEY_CLASSES_ROOT, wszFiletype, MAX_PROP_PAGES - psh.nPages, lpDo);
449 if (hpsxa != NULL)
451 SHAddFromPropSheetExtArray(hpsxa, Properties_AddPropSheetCallback, (LPARAM)&psh);
452 SHDestroyPropSheetExtArray(hpsxa);
454 hpsxa = SHCreatePropSheetExtArrayEx(HKEY_CLASSES_ROOT, wszFiletypeAll, MAX_PROP_PAGES - psh.nPages, lpDo);
455 if (hpsxa != NULL)
457 SHAddFromPropSheetExtArray(hpsxa, Properties_AddPropSheetCallback, (LPARAM)&psh);
458 SHDestroyPropSheetExtArray(hpsxa);
460 IDataObject_Release(lpDo);
463 if (psh.nPages)
464 PropertySheetW(&psh);
465 else
466 FIXME("No property pages found.\n");
469 /**************************************************************************
470 * ISvItemCm_fnInvokeCommand()
472 static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
473 IContextMenu2 *iface,
474 LPCMINVOKECOMMANDINFO lpcmi)
476 ItemCmImpl *This = impl_from_IContextMenu2(iface);
478 if (lpcmi->cbSize != sizeof(CMINVOKECOMMANDINFO))
479 FIXME("Is an EX structure\n");
481 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
483 if( HIWORD(lpcmi->lpVerb)==0 && LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST)
485 TRACE("Invalid Verb %x\n",LOWORD(lpcmi->lpVerb));
486 return E_INVALIDARG;
489 if (HIWORD(lpcmi->lpVerb) == 0)
491 switch(LOWORD(lpcmi->lpVerb-This->menu.verb_offset))
493 case FCIDM_SHVIEW_EXPLORE:
494 TRACE("Verb FCIDM_SHVIEW_EXPLORE\n");
495 DoOpenExplore(This, lpcmi->hwnd, "explore");
496 break;
497 case FCIDM_SHVIEW_OPEN:
498 TRACE("Verb FCIDM_SHVIEW_OPEN\n");
499 DoOpenExplore(This, lpcmi->hwnd, "open");
500 break;
501 case FCIDM_SHVIEW_RENAME:
502 TRACE("Verb FCIDM_SHVIEW_RENAME\n");
503 DoRename(This, lpcmi->hwnd);
504 break;
505 case FCIDM_SHVIEW_DELETE:
506 TRACE("Verb FCIDM_SHVIEW_DELETE\n");
507 DoDelete(This);
508 break;
509 case FCIDM_SHVIEW_COPY:
510 TRACE("Verb FCIDM_SHVIEW_COPY\n");
511 DoCopyOrCut(This, lpcmi->hwnd, FALSE);
512 break;
513 case FCIDM_SHVIEW_CUT:
514 TRACE("Verb FCIDM_SHVIEW_CUT\n");
515 DoCopyOrCut(This, lpcmi->hwnd, TRUE);
516 break;
517 case FCIDM_SHVIEW_PROPERTIES:
518 TRACE("Verb FCIDM_SHVIEW_PROPERTIES\n");
519 DoOpenProperties(This, lpcmi->hwnd);
520 break;
521 default:
522 FIXME("Unhandled Verb %xl\n",LOWORD(lpcmi->lpVerb)-This->menu.verb_offset);
523 return E_INVALIDARG;
526 else
528 TRACE("Verb is %s\n",debugstr_a(lpcmi->lpVerb));
529 if (strcmp(lpcmi->lpVerb,"delete")==0)
530 DoDelete(This);
531 else if (strcmp(lpcmi->lpVerb,"properties")==0)
532 DoOpenProperties(This, lpcmi->hwnd);
533 else {
534 FIXME("Unhandled string verb %s\n",debugstr_a(lpcmi->lpVerb));
535 return E_FAIL;
538 return S_OK;
541 /**************************************************************************
542 * ISvItemCm_fnGetCommandString()
544 static HRESULT WINAPI ISvItemCm_fnGetCommandString(
545 IContextMenu2 *iface,
546 UINT_PTR idCommand,
547 UINT uFlags,
548 UINT* lpReserved,
549 LPSTR lpszName,
550 UINT uMaxNameLen)
552 ItemCmImpl *This = impl_from_IContextMenu2(iface);
554 HRESULT hr = E_INVALIDARG;
556 TRACE("(%p)->(idcom=%lx flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
558 switch(uFlags)
560 case GCS_HELPTEXTA:
561 case GCS_HELPTEXTW:
562 hr = E_NOTIMPL;
563 break;
565 case GCS_VERBA:
566 switch(idCommand-This->menu.verb_offset)
568 case FCIDM_SHVIEW_RENAME:
569 strcpy(lpszName, "rename");
570 hr = S_OK;
571 break;
573 break;
575 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
576 case, you need to do the lstrcpyW to the pointer passed.*/
577 case GCS_VERBW:
578 switch(idCommand-This->menu.verb_offset)
580 case FCIDM_SHVIEW_RENAME:
581 MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
582 hr = S_OK;
583 break;
585 break;
587 case GCS_VALIDATEA:
588 case GCS_VALIDATEW:
589 hr = S_OK;
590 break;
592 TRACE("-- (%p)->(name=%s)\n",This, lpszName);
593 return hr;
596 /**************************************************************************
597 * ISvItemCm_fnHandleMenuMsg()
598 * NOTES
599 * should be only in IContextMenu2 and IContextMenu3
600 * is nevertheless called from word95
602 static HRESULT WINAPI ISvItemCm_fnHandleMenuMsg(
603 IContextMenu2 *iface,
604 UINT uMsg,
605 WPARAM wParam,
606 LPARAM lParam)
608 ItemCmImpl *This = impl_from_IContextMenu2(iface);
610 TRACE("(%p)->(msg=%x wp=%lx lp=%lx)\n",This, uMsg, wParam, lParam);
612 return E_NOTIMPL;
615 static const IContextMenu2Vtbl ItemContextMenuVtbl =
617 ISvItemCm_fnQueryInterface,
618 ISvItemCm_fnAddRef,
619 ISvItemCm_fnRelease,
620 ISvItemCm_fnQueryContextMenu,
621 ISvItemCm_fnInvokeCommand,
622 ISvItemCm_fnGetCommandString,
623 ISvItemCm_fnHandleMenuMsg
626 IContextMenu2 *ISvItemCm_Constructor(IShellFolder *parent, LPCITEMIDLIST pidl, const LPCITEMIDLIST *apidl, UINT cidl)
627 { ItemCmImpl* cm;
628 UINT u;
630 cm = HeapAlloc(GetProcessHeap(), 0, sizeof(ItemCmImpl));
631 cm->menu.IContextMenu2_iface.lpVtbl = &ItemContextMenuVtbl;
632 cm->menu.ref = 1;
633 cm->menu.verb_offset = 0;
634 cm->menu.parent = parent;
636 cm->pidl = ILClone(pidl);
638 if (parent) IShellFolder_AddRef(parent);
640 cm->apidl = _ILCopyaPidl(apidl, cidl);
641 cm->cidl = cidl;
643 cm->bAllValues = 1;
644 for(u = 0; u < cidl; u++)
645 cm->bAllValues &= (_ILIsValue(apidl[u]) ? 1 : 0);
647 TRACE("(%p)\n",cm);
649 return &cm->menu.IContextMenu2_iface;