wer: Add new stubbed wer.dll.
[wine/hramrach.git] / dlls / shell32 / shv_item_cmenu.c
blobe5d5e5b5e49c56c67693f2d10f9206a8661042f5
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);
45 /**************************************************************************
46 * IContextMenu Implementation
48 typedef struct
49 { const IContextMenu2Vtbl *lpVtbl;
50 LONG ref;
51 IShellFolder* pSFParent;
52 LPITEMIDLIST pidl; /* root pidl */
53 LPITEMIDLIST *apidl; /* array of child pidls */
54 UINT cidl;
55 BOOL bAllValues;
56 } ItemCmImpl;
59 static const IContextMenu2Vtbl cmvt;
61 /**************************************************************************
62 * ISvItemCm_CanRenameItems()
64 static BOOL ISvItemCm_CanRenameItems(ItemCmImpl *This)
65 { UINT i;
66 DWORD dwAttributes;
68 TRACE("(%p)->()\n",This);
70 if(This->apidl)
72 for(i = 0; i < This->cidl; i++){}
73 if(i > 1) return FALSE; /* can't rename more than one item at a time*/
74 dwAttributes = SFGAO_CANRENAME;
75 IShellFolder_GetAttributesOf(This->pSFParent, 1, (LPCITEMIDLIST*)This->apidl, &dwAttributes);
76 return dwAttributes & SFGAO_CANRENAME;
78 return FALSE;
81 /**************************************************************************
82 * ISvItemCm_Constructor()
84 IContextMenu2 *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, const LPCITEMIDLIST *apidl, UINT cidl)
85 { ItemCmImpl* cm;
86 UINT u;
88 cm = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ItemCmImpl));
89 cm->lpVtbl = &cmvt;
90 cm->ref = 1;
91 cm->pidl = ILClone(pidl);
92 cm->pSFParent = pSFParent;
94 if(pSFParent) IShellFolder_AddRef(pSFParent);
96 cm->apidl = _ILCopyaPidl(apidl, cidl);
97 cm->cidl = cidl;
99 cm->bAllValues = 1;
100 for(u = 0; u < cidl; u++)
102 cm->bAllValues &= (_ILIsValue(apidl[u]) ? 1 : 0);
105 TRACE("(%p)->()\n",cm);
107 return (IContextMenu2*)cm;
110 /**************************************************************************
111 * ISvItemCm_fnQueryInterface
113 static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu2 *iface, REFIID riid, LPVOID *ppvObj)
115 ItemCmImpl *This = (ItemCmImpl *)iface;
117 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
119 *ppvObj = NULL;
121 if(IsEqualIID(riid, &IID_IUnknown) ||
122 IsEqualIID(riid, &IID_IContextMenu) ||
123 IsEqualIID(riid, &IID_IContextMenu2))
125 *ppvObj = This;
127 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
129 FIXME("-- LPSHELLEXTINIT pointer requested\n");
132 if(*ppvObj)
134 IUnknown_AddRef((IUnknown*)*ppvObj);
135 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
136 return S_OK;
138 TRACE("-- Interface: E_NOINTERFACE\n");
139 return E_NOINTERFACE;
142 /**************************************************************************
143 * ISvItemCm_fnAddRef
145 static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu2 *iface)
147 ItemCmImpl *This = (ItemCmImpl *)iface;
148 ULONG refCount = InterlockedIncrement(&This->ref);
150 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
152 return refCount;
155 /**************************************************************************
156 * ISvItemCm_fnRelease
158 static ULONG WINAPI ISvItemCm_fnRelease(IContextMenu2 *iface)
160 ItemCmImpl *This = (ItemCmImpl *)iface;
161 ULONG refCount = InterlockedDecrement(&This->ref);
163 TRACE("(%p)->(count=%i)\n", This, refCount + 1);
165 if (!refCount)
167 TRACE(" destroying IContextMenu(%p)\n",This);
169 if(This->pSFParent)
170 IShellFolder_Release(This->pSFParent);
172 SHFree(This->pidl);
174 /*make sure the pidl is freed*/
175 _ILFreeaPidl(This->apidl, This->cidl);
177 HeapFree(GetProcessHeap(),0,This);
179 return refCount;
182 static void _InsertMenuItemW (
183 HMENU hmenu,
184 UINT indexMenu,
185 BOOL fByPosition,
186 UINT wID,
187 UINT fType,
188 LPWSTR dwTypeData,
189 UINT fState)
191 MENUITEMINFOW 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 = dwTypeData;
202 mii.fState = fState;
204 mii.wID = wID;
205 mii.fType = fType;
206 InsertMenuItemW( hmenu, indexMenu, fByPosition, &mii);
209 /**************************************************************************
210 * ISvItemCm_fnQueryContextMenu()
212 static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
213 IContextMenu2 *iface,
214 HMENU hmenu,
215 UINT indexMenu,
216 UINT idCmdFirst,
217 UINT idCmdLast,
218 UINT uFlags)
220 ItemCmImpl *This = (ItemCmImpl *)iface;
221 INT uIDMax;
223 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
225 if (idCmdFirst != 0)
226 FIXME("We should use idCmdFirst=%d and idCmdLast=%d for command ids\n", idCmdFirst, idCmdLast);
228 if(!(CMF_DEFAULTONLY & uFlags) && This->cidl>0)
230 HMENU hmenures = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_SHV_FILE));
232 if(uFlags & CMF_EXPLORE)
233 RemoveMenu(hmenures, FCIDM_SHVIEW_OPEN, MF_BYCOMMAND);
235 uIDMax = Shell_MergeMenus(hmenu, GetSubMenu(hmenures, 0), indexMenu, idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);
237 DestroyMenu(hmenures);
239 if(This->bAllValues)
241 MENUITEMINFOW mi;
242 WCHAR str[255];
243 mi.cbSize = sizeof(mi);
244 mi.fMask = MIIM_ID | MIIM_STRING | MIIM_FTYPE;
245 mi.dwTypeData = str;
246 mi.cch = 255;
247 GetMenuItemInfoW(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND, &mi);
248 RemoveMenu(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND);
249 _InsertMenuItemW(hmenu, (uFlags & CMF_EXPLORE) ? 1 : 2, MF_BYPOSITION, FCIDM_SHVIEW_EXPLORE, MFT_STRING, str, MFS_ENABLED);
252 SetMenuDefaultItem(hmenu, 0, MF_BYPOSITION);
254 if(uFlags & ~CMF_CANRENAME)
255 RemoveMenu(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND);
256 else
257 EnableMenuItem(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND | ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
259 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, uIDMax);
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;
388 /**************************************************************************
389 * Properties_AddPropSheetCallback
391 * Used by DoOpenProperties through SHCreatePropSheetExtArrayEx to add
392 * propertysheet pages from shell extensions.
394 static BOOL Properties_AddPropSheetCallback(HPROPSHEETPAGE hpage, LPARAM lparam)
396 LPPROPSHEETHEADERW psh = (LPPROPSHEETHEADERW) lparam;
397 psh->u3.phpage[psh->nPages++] = hpage;
399 return TRUE;
402 /**************************************************************************
403 * DoOpenProperties
405 static void DoOpenProperties(IContextMenu2 *iface, HWND hwnd)
407 ItemCmImpl *This = (ItemCmImpl *)iface;
408 static const UINT MAX_PROP_PAGES = 99;
409 static const WCHAR wszFolder[] = {'F','o','l','d','e','r', 0};
410 static const WCHAR wszFiletypeAll[] = {'*',0};
411 LPSHELLFOLDER lpDesktopSF;
412 LPSHELLFOLDER lpSF;
413 LPDATAOBJECT lpDo;
414 WCHAR wszFiletype[MAX_PATH];
415 WCHAR wszFilename[MAX_PATH];
416 PROPSHEETHEADERW psh;
417 HPROPSHEETPAGE hpages[MAX_PROP_PAGES];
418 HPSXA hpsxa;
419 UINT ret;
421 TRACE("(%p)->(wnd=%p)\n", This, hwnd);
423 ZeroMemory(&psh, sizeof(PROPSHEETHEADERW));
424 psh.dwSize = sizeof (PROPSHEETHEADERW);
425 psh.hwndParent = hwnd;
426 psh.dwFlags = PSH_PROPTITLE;
427 psh.nPages = 0;
428 psh.u3.phpage = hpages;
429 psh.u2.nStartPage = 0;
431 _ILSimpleGetTextW(This->apidl[0], (LPVOID)&wszFilename, MAX_PATH);
432 psh.pszCaption = (LPCWSTR)&wszFilename;
434 /* Find out where to look for the shell extensions */
435 if (_ILIsValue(This->apidl[0]))
437 char sTemp[64];
438 sTemp[0] = 0;
439 if (_ILGetExtension(This->apidl[0], sTemp, 64))
441 HCR_MapTypeToValueA(sTemp, sTemp, 64, TRUE);
442 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, wszFiletype, MAX_PATH);
444 else
446 wszFiletype[0] = 0;
449 else if (_ILIsFolder(This->apidl[0]))
451 lstrcpynW(wszFiletype, wszFolder, 64);
453 else if (_ILIsSpecialFolder(This->apidl[0]))
455 LPGUID folderGUID;
456 static const WCHAR wszclsid[] = {'C','L','S','I','D','\\', 0};
457 folderGUID = _ILGetGUIDPointer(This->apidl[0]);
458 lstrcpyW(wszFiletype, wszclsid);
459 StringFromGUID2(folderGUID, &wszFiletype[6], MAX_PATH - 6);
461 else
463 FIXME("Requested properties for unknown type.\n");
464 return;
467 /* Get a suitable DataObject for accessing the files */
468 SHGetDesktopFolder(&lpDesktopSF);
469 if (_ILIsPidlSimple(This->pidl))
471 ret = IShellFolder_GetUIObjectOf(lpDesktopSF, hwnd, This->cidl, (LPCITEMIDLIST*)This->apidl,
472 &IID_IDataObject, NULL, (LPVOID *)&lpDo);
473 IShellFolder_Release(lpDesktopSF);
475 else
477 IShellFolder_BindToObject(lpDesktopSF, This->pidl, NULL, &IID_IShellFolder, (LPVOID*) &lpSF);
478 ret = IShellFolder_GetUIObjectOf(lpSF, hwnd, This->cidl, (LPCITEMIDLIST*)This->apidl,
479 &IID_IDataObject, NULL, (LPVOID *)&lpDo);
480 IShellFolder_Release(lpSF);
481 IShellFolder_Release(lpDesktopSF);
484 if (SUCCEEDED(ret))
486 hpsxa = SHCreatePropSheetExtArrayEx(HKEY_CLASSES_ROOT, wszFiletype, MAX_PROP_PAGES - psh.nPages, lpDo);
487 if (hpsxa != NULL)
489 SHAddFromPropSheetExtArray((HPSXA)hpsxa,
490 (LPFNADDPROPSHEETPAGE)&Properties_AddPropSheetCallback,
491 (LPARAM)&psh);
492 SHDestroyPropSheetExtArray(hpsxa);
494 hpsxa = SHCreatePropSheetExtArrayEx(HKEY_CLASSES_ROOT, wszFiletypeAll, MAX_PROP_PAGES - psh.nPages, lpDo);
495 if (hpsxa != NULL)
497 SHAddFromPropSheetExtArray((HPSXA)hpsxa,
498 (LPFNADDPROPSHEETPAGE)&Properties_AddPropSheetCallback,
499 (LPARAM)&psh);
500 SHDestroyPropSheetExtArray(hpsxa);
502 IDataObject_Release(lpDo);
505 if (psh.nPages)
506 PropertySheetW(&psh);
507 else
508 FIXME("No property pages found.\n");
511 /**************************************************************************
512 * ISvItemCm_fnInvokeCommand()
514 static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
515 IContextMenu2 *iface,
516 LPCMINVOKECOMMANDINFO lpcmi)
518 ItemCmImpl *This = (ItemCmImpl *)iface;
520 if (lpcmi->cbSize != sizeof(CMINVOKECOMMANDINFO))
521 FIXME("Is an EX structure\n");
523 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
525 if( HIWORD(lpcmi->lpVerb)==0 && LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST)
527 TRACE("Invalid Verb %x\n",LOWORD(lpcmi->lpVerb));
528 return E_INVALIDARG;
531 if (HIWORD(lpcmi->lpVerb) == 0)
533 switch(LOWORD(lpcmi->lpVerb))
535 case FCIDM_SHVIEW_EXPLORE:
536 TRACE("Verb FCIDM_SHVIEW_EXPLORE\n");
537 DoOpenExplore(iface, lpcmi->hwnd, "explore");
538 break;
539 case FCIDM_SHVIEW_OPEN:
540 TRACE("Verb FCIDM_SHVIEW_OPEN\n");
541 DoOpenExplore(iface, lpcmi->hwnd, "open");
542 break;
543 case FCIDM_SHVIEW_RENAME:
544 TRACE("Verb FCIDM_SHVIEW_RENAME\n");
545 DoRename(iface, lpcmi->hwnd);
546 break;
547 case FCIDM_SHVIEW_DELETE:
548 TRACE("Verb FCIDM_SHVIEW_DELETE\n");
549 DoDelete(iface);
550 break;
551 case FCIDM_SHVIEW_COPY:
552 TRACE("Verb FCIDM_SHVIEW_COPY\n");
553 DoCopyOrCut(iface, lpcmi->hwnd, FALSE);
554 break;
555 case FCIDM_SHVIEW_CUT:
556 TRACE("Verb FCIDM_SHVIEW_CUT\n");
557 DoCopyOrCut(iface, lpcmi->hwnd, TRUE);
558 break;
559 case FCIDM_SHVIEW_PROPERTIES:
560 TRACE("Verb FCIDM_SHVIEW_PROPERTIES\n");
561 DoOpenProperties(iface, lpcmi->hwnd);
562 break;
563 default:
564 FIXME("Unhandled Verb %xl\n",LOWORD(lpcmi->lpVerb));
565 return E_INVALIDARG;
568 else
570 TRACE("Verb is %s\n",debugstr_a(lpcmi->lpVerb));
571 if (strcmp(lpcmi->lpVerb,"delete")==0)
572 DoDelete(iface);
573 else if (strcmp(lpcmi->lpVerb,"properties")==0)
574 DoOpenProperties(iface, lpcmi->hwnd);
575 else {
576 FIXME("Unhandled string verb %s\n",debugstr_a(lpcmi->lpVerb));
577 return E_FAIL;
580 return NOERROR;
583 /**************************************************************************
584 * ISvItemCm_fnGetCommandString()
586 static HRESULT WINAPI ISvItemCm_fnGetCommandString(
587 IContextMenu2 *iface,
588 UINT_PTR idCommand,
589 UINT uFlags,
590 UINT* lpReserved,
591 LPSTR lpszName,
592 UINT uMaxNameLen)
594 ItemCmImpl *This = (ItemCmImpl *)iface;
596 HRESULT hr = E_INVALIDARG;
598 TRACE("(%p)->(idcom=%lx flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
600 switch(uFlags)
602 case GCS_HELPTEXTA:
603 case GCS_HELPTEXTW:
604 hr = E_NOTIMPL;
605 break;
607 case GCS_VERBA:
608 switch(idCommand)
610 case FCIDM_SHVIEW_RENAME:
611 strcpy(lpszName, "rename");
612 hr = NOERROR;
613 break;
615 break;
617 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
618 case, you need to do the lstrcpyW to the pointer passed.*/
619 case GCS_VERBW:
620 switch(idCommand)
621 { case FCIDM_SHVIEW_RENAME:
622 MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
623 hr = NOERROR;
624 break;
626 break;
628 case GCS_VALIDATEA:
629 case GCS_VALIDATEW:
630 hr = NOERROR;
631 break;
633 TRACE("-- (%p)->(name=%s)\n",This, lpszName);
634 return hr;
637 /**************************************************************************
638 * ISvItemCm_fnHandleMenuMsg()
639 * NOTES
640 * should be only in IContextMenu2 and IContextMenu3
641 * is nevertheless called from word95
643 static HRESULT WINAPI ISvItemCm_fnHandleMenuMsg(
644 IContextMenu2 *iface,
645 UINT uMsg,
646 WPARAM wParam,
647 LPARAM lParam)
649 ItemCmImpl *This = (ItemCmImpl *)iface;
651 TRACE("(%p)->(msg=%x wp=%lx lp=%lx)\n",This, uMsg, wParam, lParam);
653 return E_NOTIMPL;
656 static const IContextMenu2Vtbl cmvt =
658 ISvItemCm_fnQueryInterface,
659 ISvItemCm_fnAddRef,
660 ISvItemCm_fnRelease,
661 ISvItemCm_fnQueryContextMenu,
662 ISvItemCm_fnInvokeCommand,
663 ISvItemCm_fnGetCommandString,
664 ISvItemCm_fnHandleMenuMsg